Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class BuildWS here.
  4.  *
  5.  * @author (your name)
  6.  * @version (a version number or a date)
  7.  */
  8. import java.util.Random;
  9. import java.util.Scanner;
  10.  
  11. public class BuildWS
  12. {
  13.     private char[][] wordSearch;
  14.    
  15.     public void build() {
  16.         Scanner rowCol = new Scanner(System.in);
  17.         int randomRow, randomCol;
  18.         String word = "";
  19.        
  20.         System.out.println("How many rows would you like? >");
  21.         randomRow = rowCol.nextInt();
  22.        
  23.         System.out.println("How many columns would you like? >");
  24.         randomCol = rowCol.nextInt();
  25.        
  26.         wordSearch = new char[randomRow][randomCol];
  27.        
  28.        
  29.         while(!word.equals("end")) {
  30.             System.out.print("Add a word to your search (end to stop) >");
  31.             word = rowCol.next();
  32.             Place(word);
  33.         }
  34.        
  35.         generateRandomGrid(randomCol, randomRow);
  36.        
  37.         for(int i = 0; i < wordSearch.length; i++) {
  38.             for(int j = 0; j < wordSearch[i].length; j++) {
  39.                 System.out.print(wordSearch[i][j] + " ");
  40.             }
  41.             System.out.println();
  42.         }
  43.     }
  44.    
  45.     private void generateRandomGrid(int columnCount, int rowCount) {
  46.         Random ran = new Random();
  47.         wordSearch = new char[columnCount][rowCount];
  48.         for(int i = 0; i < columnCount; i++) {
  49.             for(int j = 0; j < rowCount; j++) {
  50.                 wordSearch[i][j] = (char)(int)(ran.nextInt(26) + 'a');
  51.             }
  52.         }
  53.     }
  54.    
  55.     private void Place(String word) {
  56.         boolean place = false;
  57.         int[][] placeIndex = new int[word.length()][2];;
  58.         while(place == false) {
  59.             placeIndex = new int[word.length()][2];
  60.             int randomRow = (int) (Math.random() * wordSearch.length);
  61.             int randomCol = (int) (Math.random() * wordSearch[0].length);
  62.             int randomDir = (int) (Math.random() * 3);
  63.             if(randomDir == 0) { //Horizontally
  64.                 for(int i = 0; randomRow + i < wordSearch.length; i++) {
  65.                     if(wordSearch[randomRow + i][randomCol] == (char)0 || wordSearch[randomRow + i][randomCol] == word.charAt(i)){
  66.                         placeIndex[i][0] = randomRow + i;
  67.                         placeIndex[i][1] = randomCol;
  68.                         if(word.length() - 1 == i) {
  69.                             place = true;
  70.                             break;
  71.                         }
  72.                     } else {
  73.                         break;
  74.                     }
  75.                 }
  76.             }
  77.             else if(randomDir == 1) { //Vertically
  78.                 for(int i = 0; randomCol + i < wordSearch[0].length; i++) {
  79.                     if(wordSearch[randomRow][randomCol + i] == (char)0 || wordSearch[randomRow][randomCol + i] == word.charAt(i)){
  80.                         placeIndex[i][0] = randomRow;
  81.                         placeIndex[i][1] = randomCol + i;
  82.                         if(word.length() - 1 == i) {
  83.                             place = true;
  84.                             break;
  85.                         }
  86.                     } else {
  87.                         break;
  88.                     }
  89.                 }
  90.             }
  91.             else if(randomDir == 2) { //Diagonally
  92.                 for(int i = 0; randomRow + i < wordSearch.length && randomCol + i < wordSearch[0].length; i++) {
  93.                     if(wordSearch[randomRow + i][randomCol + i] == (char)0 || wordSearch[randomRow + i][randomCol + i] == word.charAt(i)){
  94.                         placeIndex[i][0] = randomRow + i;
  95.                         placeIndex[i][1] = randomCol + i;
  96.                         if(word.length() - 1 == i) {
  97.                             place = true;
  98.                             break;
  99.                         }
  100.                     } else {
  101.                         break;
  102.                     }
  103.                 }
  104.             }
  105.         }
  106.         for(int index = 0; index < word.length(); index++) {
  107.             wordSearch[placeIndex[index][0]][placeIndex[index][1]] = word.charAt(index);
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement