Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.11 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.ArrayList;
  4. import java.io.*;
  5. import java.lang.Math;
  6.  
  7. public class WordSearchPuzzle {
  8.     private char[][] puzzle;
  9.     private List<String> puzzleWords;
  10.     private List<String> wordsForGrid;
  11.     private int gridDimension;
  12.     private char[] alphabet;
  13.     private int wordNum;
  14.  
  15.     public WordSearchPuzzle(List<String> userSpecifiedWords) {
  16.         this.gridDimension = sizeOfGrid(puzzleWords);
  17.         this.puzzle = new char[gridDimension][gridDimension];//grid is going to be a square
  18.         this.puzzleWords = userSpecifiedWords;
  19.     }
  20.  
  21.     public WordSearchPuzzle(String wordFile, int wordCount, int shortest, int longest) {
  22.         alphabet = new char[]{('a'), ('b'), ('c'), ('d'), ('e'), ('f'), ('g'), ('h'), ('i'), ('j'), ('k'), ('l'), ('m'), ('n'), ('o'), ('p'), ('q'), ('r'), ('s'), ('t'), ('u'), ('v'), ('w'), ('x'), ('y'), ('z')};
  23.         int temp;
  24.         wordsForGrid = new ArrayList<String>();
  25.         this.wordNum = wordCount;
  26.         if (shortest > longest) {
  27.             temp = longest;
  28.             longest = shortest;
  29.             shortest = temp;
  30.         }
  31.         puzzleWords = loadWordsFromFile(wordFile, shortest, longest);
  32.         for (int i = 0; i < wordCount; i++) {
  33.             int pos = (int) (Math.random() * puzzleWords.size());
  34.             wordsForGrid.add(puzzleWords.get(pos));
  35.         }
  36.         this.gridDimension = sizeOfGrid(puzzleWords) ;
  37.  
  38.         this.puzzle = new char[gridDimension+1][gridDimension+1];
  39.         puzzleWords = wordsForGrid;
  40.     }
  41.  
  42.     public int sizeOfGrid(List<String> puzzleWords) {
  43.         double temp = 0;
  44.         double temp2 = 0;
  45.         int temp3 = 0;
  46.         for (int i = 0; i < wordNum; i++) {
  47.             temp = temp + puzzleWords.get(i).length();
  48.         }
  49.         temp2 = Math.sqrt(temp);
  50.         temp3 = (int) ((temp2) * 1.75);
  51.         return temp3;
  52.     }
  53.  
  54.     public void fillGrid(List<String> puzzleWords) {
  55.         for (int i = 0; i < puzzleWords.size(); i++) {
  56.  
  57.         }
  58.     }
  59.  
  60.     private List<String> loadWordsFromFile(String wordFile, int shortest, int longest) {
  61.         try {
  62.             FileReader aFileReader = new FileReader(wordFile);
  63.             BufferedReader aBufferReader = new BufferedReader(aFileReader);
  64.             String lineFromFile;
  65.             List<String> words = new ArrayList<String>();//would not let me create an empty List, created an ArrayList insead.
  66.             lineFromFile = aBufferReader.readLine();
  67.             int wordLength;
  68.             while (lineFromFile != null) {
  69.                 wordLength = lineFromFile.length();
  70.                 if (wordLength >= shortest && wordLength <= longest) {
  71.                     words.add(lineFromFile);
  72.                 }
  73.                 lineFromFile = aBufferReader.readLine();
  74.             }
  75.             aBufferReader.close();
  76.             aFileReader.close();
  77.             return words;
  78.         } catch (IOException x) {
  79.             System.out.println((x));
  80.             return null;
  81.         }
  82.     }
  83.  
  84.     public List<String> getWordSearchList() {
  85.         return this.puzzleWords;
  86.     }
  87.  
  88.     public char[][] getPuzzleAsGrid() {
  89.         int i;
  90.         int j;
  91.         for (i = 0; i < puzzle.length; i++) {
  92.             for (j = 0; j < gridDimension; j++) {
  93.                 int randomChar = (int) (Math.random() * alphabet.length);
  94.                 this.puzzle[i][j] = alphabet[randomChar];
  95.                 System.out.print(puzzle[i][j] + "");
  96.             }
  97.             System.out.println();
  98.         }
  99.         return puzzle;
  100.     }
  101.  
  102.     public String getPuzzleAsString() {
  103.         int i;
  104.         int j;
  105.         String puzzleString = "";
  106.         for (i = 0; i < gridDimension; i++) {
  107.             for (j = 0; j < gridDimension; j++) {
  108.                 puzzleString += puzzle[i][j];
  109.             }
  110.             if (i == gridDimension) {
  111.                 puzzleString += "\n";
  112.             }
  113.         }
  114.         return puzzleString;
  115.     }
  116.  
  117.     public void showWordSearchPuzzle() {
  118.         generateWordSearchPuzzle();
  119.         for (int i = 0; i < puzzleWords.size(); i++) {
  120.             System.out.println(puzzleWords.get(i));
  121.         }
  122.     }
  123.  
  124.     public void generateWordSearchPuzzle() {
  125.         int j = 0;//to be used to check if the space where the current word is supposed to go is empty
  126.         int i;//to be used to go through the list of words
  127.         int k;//to be used to get the individual character from the current word
  128.         int randomDirection;//determine which direction to place the word
  129.         int randomRow;//starting position for word
  130.         int randomCol;//starting position for word
  131.         for (i = 0; i < puzzleWords.size(); ) {
  132.             randomDirection = (int) (Math.random() * 4);
  133.             randomRow = (int) (Math.random() * (gridDimension ));
  134.             randomCol = (int) (Math.random() * (gridDimension ));
  135.             while((randomRow + puzzleWords.get(i).length() > (gridDimension) ) || (randomRow -puzzleWords.get(i).length() < 0) ){
  136.                 randomRow = (int) (Math.random() * (gridDimension ));
  137.             }
  138.             while((randomCol + puzzleWords.get(i).length() > (gridDimension) ) || (randomCol -puzzleWords.get(i).length() < 0)){
  139.                 randomCol= (int) (Math.random() * (gridDimension ));
  140.             }
  141.             int randomColTemp = randomCol;
  142.             int randomRowTemp = randomRow;
  143.             if (randomDirection == 0)//word being placed to the right
  144.             {
  145.                 if (gridDimension - (randomCol + 1) >= puzzleWords.get(i).length()) {
  146.                     for (j = 0; j < puzzleWords.get(i).length(); j++) {
  147.                         if (puzzle[randomRow][randomColTemp] == 0) {
  148.                             randomColTemp++;
  149.                         } else
  150.                             j = puzzleWords.get(i).length() + 1;//space is not empty,want to break the loop
  151.                     }
  152.                 }
  153.                 if (j == puzzleWords.get(i).length()) {
  154.                     for (k = 0; k < puzzleWords.get(i).length(); ) {
  155.                         puzzle[randomRow][randomCol] = puzzleWords.get(i).charAt(k);
  156.                         randomCol++;
  157.                         k++;
  158.                     }
  159.                     i++;
  160.                 }
  161.             }
  162.             if (randomDirection == 1)//word being placed to the left)
  163.             {
  164.                 if (randomCol + 1 >= puzzleWords.get(i).length()) {
  165.                     for (j = 0; j < puzzleWords.get(i).length(); j++) {
  166.                         if (puzzle[randomRow][randomColTemp] == 0) {
  167.                             randomColTemp--;
  168.                         } else
  169.                             j = puzzleWords.get(i).length() + 1;
  170.                     }
  171.                 }
  172.                 if (j == puzzleWords.get(i).length()) {
  173.                     for (k = 0; k < puzzleWords.get(i).length(); ) {
  174.                         puzzle[randomRow][randomCol] = puzzleWords.get(i).charAt(k);
  175.                         randomCol--;
  176.                         k++;
  177.                     }
  178.                     i++;
  179.                 }
  180.             }
  181.             if (randomDirection == 2)//word being placed vertically down
  182.             {
  183.                 if (gridDimension - (randomRow + 1) >= puzzleWords.get(i).length()) {
  184.                     for (j = 0; j < puzzleWords.get(i).length(); j++) {
  185.                         if (puzzle[randomRowTemp][randomCol] == 0) {
  186.                             randomRowTemp++;
  187.                         } else
  188.                             j = puzzleWords.get(i).length() + 1;
  189.                     }
  190.                 }
  191.                 if (j == puzzleWords.get(i).length()) {
  192.                     for (k = 0; k < puzzleWords.get(i).length(); ) {
  193.                         puzzle[randomRow][randomCol] = puzzleWords.get(i).charAt(k);
  194.                         randomRow++;
  195.                         k++;
  196.                     }
  197.                     i++;
  198.                 }
  199.             }
  200.             if (randomDirection == 3)//word being placed vertically up
  201.             {
  202.                 if (randomRow + 1 >= puzzleWords.get(i).length()) {
  203.                     for (j = 0; j < puzzleWords.get(i).length(); j++) {
  204.                         if (puzzle[randomRowTemp][randomCol] == 0) {
  205.                             randomRowTemp--;
  206.                         } else
  207.                             j = puzzleWords.get(i).length() + 1;
  208.                     }
  209.                 }
  210.                 if (j == puzzleWords.get(i).length()) {
  211.                     for (k = 0; k < puzzleWords.get(i).length(); ) {
  212.                         puzzle[randomRow][randomCol] = puzzleWords.get(i).charAt(k);
  213.                         randomRow--;
  214.                         k++;
  215.                     }
  216.                     i++;
  217.                 }
  218.             }
  219.         }
  220.         for (int l = 0; i < gridDimension; i++) {
  221.             for (int m = 0; j < gridDimension; j++) {
  222.                 if (puzzle[l][m] == 0) {
  223.                     int randomAlphabeticChar = (int) (Math.random() * alphabet.length);
  224.                     puzzle[l][m] = alphabet[randomAlphabeticChar];
  225.                 }
  226.             }
  227.         }
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement