Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.17 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.             System.out.println(randomDirection);
  134.             randomRow = (int) (Math.random() * (gridDimension ));
  135.             randomCol = (int) (Math.random() * (gridDimension ));
  136.             while((randomRow + puzzleWords.get(i).length() > (gridDimension) +1) || (randomRow -puzzleWords.get(i).length() < 0) ){
  137.                 randomRow = (int) (Math.random() * (gridDimension ));
  138.             }
  139.             while((randomCol + puzzleWords.get(i).length() > (gridDimension) +1) || (randomCol -puzzleWords.get(i).length() < 0)){
  140.                 randomCol= (int) (Math.random() * (gridDimension ));
  141.             }
  142.             int randomColTemp = randomCol;
  143.             int randomRowTemp = randomRow;
  144.             if (randomDirection == 0)//word being placed to the right
  145.             {
  146.                 if (gridDimension - (randomCol + 1) >= puzzleWords.get(i).length()) {
  147.                     for (j = 0; j < puzzleWords.get(i).length(); j++) {
  148.                         if (puzzle[randomRow][randomColTemp] == 0) {
  149.                             randomColTemp++;
  150.                         } else
  151.                             j = puzzleWords.get(i).length() + 1;//space is not empty,want to break the loop
  152.                     }
  153.                 }
  154.                 if (j == puzzleWords.get(i).length()) {
  155.                     for (k = 0; k < puzzleWords.get(i).length(); ) {
  156.                         puzzle[randomRow][randomCol] = puzzleWords.get(i).charAt(k);
  157.                         randomCol++;
  158.                         k++;
  159.                     }
  160.                     i++;
  161.                 }
  162.             }
  163.             if (randomDirection == 1)//word being placed to the left)
  164.             {
  165.                 if (randomCol + 1 >= puzzleWords.get(i).length()) {
  166.                     for (j = 0; j < puzzleWords.get(i).length(); j++) {
  167.                         if (puzzle[randomRow][randomColTemp] == 0) {
  168.                             randomColTemp--;
  169.                         } else
  170.                             j = puzzleWords.get(i).length() + 1;
  171.                     }
  172.                 }
  173.                 if (j == puzzleWords.get(i).length()) {
  174.                     for (k = 0; k < puzzleWords.get(i).length(); ) {
  175.                         puzzle[randomRow][randomCol] = puzzleWords.get(i).charAt(k);
  176.                         randomCol--;
  177.                         k++;
  178.                     }
  179.                     i++;
  180.                 }
  181.             }
  182.             if (randomDirection == 2)//word being placed vertically down
  183.             {
  184.                 if (gridDimension - (randomRow + 1) >= puzzleWords.get(i).length()) {
  185.                     for (j = 0; j < puzzleWords.get(i).length(); j++) {
  186.                         if (puzzle[randomRowTemp][randomCol] == 0) {
  187.                             randomRowTemp++;
  188.                         } else
  189.                             j = puzzleWords.get(i).length() + 1;
  190.                     }
  191.                 }
  192.                 if (j == puzzleWords.get(i).length()) {
  193.                     for (k = 0; k < puzzleWords.get(i).length(); ) {
  194.                         puzzle[randomRow][randomCol] = puzzleWords.get(i).charAt(k);
  195.                         randomRow++;
  196.                         k++;
  197.                     }
  198.                     i++;
  199.                 }
  200.             }
  201.             if (randomDirection == 3)//word being placed vertically up
  202.             {
  203.                 if (randomRow + 1 >= puzzleWords.get(i).length()) {
  204.                     for (j = 0; j < puzzleWords.get(i).length(); j++) {
  205.                         if (puzzle[randomRowTemp][randomCol] == 0) {
  206.                             randomRowTemp--;
  207.                         } else
  208.                             j = puzzleWords.get(i).length() + 1;
  209.                     }
  210.                 }
  211.                 if (j == puzzleWords.get(i).length()) {
  212.                     for (k = 0; k < puzzleWords.get(i).length(); ) {
  213.                         puzzle[randomRow][randomCol] = puzzleWords.get(i).charAt(k);
  214.                         randomRow--;
  215.                         k++;
  216.                     }
  217.                     i++;
  218.                 }
  219.             }
  220.         }
  221.         for (int l = 0; i < gridDimension; i++) {
  222.             for (int m = 0; j < gridDimension; j++) {
  223.                 if (puzzle[l][m] == 0) {
  224.                     int randomAlphabeticChar = (int) (Math.random() * alphabet.length);
  225.                     puzzle[l][m] = alphabet[randomAlphabeticChar];
  226.                 }
  227.             }
  228.         }
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement