Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write a description of class BuildWS here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- import java.util.Random;
- import java.util.Scanner;
- public class BuildWS
- {
- private char[][] wordSearch;
- public void build() {
- Scanner rowCol = new Scanner(System.in);
- int randomRow, randomCol;
- String word = "";
- System.out.println("How many rows would you like? >");
- randomRow = rowCol.nextInt();
- System.out.println("How many columns would you like? >");
- randomCol = rowCol.nextInt();
- wordSearch = new char[randomRow][randomCol];
- while(!word.equals("end")) {
- System.out.print("Add a word to your search (end to stop) >");
- word = rowCol.next();
- Place(word);
- }
- generateRandomGrid(randomCol, randomRow);
- for(int i = 0; i < wordSearch.length; i++) {
- for(int j = 0; j < wordSearch[i].length; j++) {
- System.out.print(wordSearch[i][j] + " ");
- }
- System.out.println();
- }
- }
- private void generateRandomGrid(int columnCount, int rowCount) {
- Random ran = new Random();
- wordSearch = new char[columnCount][rowCount];
- for(int i = 0; i < columnCount; i++) {
- for(int j = 0; j < rowCount; j++) {
- wordSearch[i][j] = (char)(int)(ran.nextInt(26) + 'a');
- }
- }
- }
- private void Place(String word) {
- boolean place = false;
- int[][] placeIndex = new int[word.length()][2];;
- while(place == false) {
- placeIndex = new int[word.length()][2];
- int randomRow = (int) (Math.random() * wordSearch.length);
- int randomCol = (int) (Math.random() * wordSearch[0].length);
- int randomDir = (int) (Math.random() * 3);
- if(randomDir == 0) { //Horizontally
- for(int i = 0; randomRow + i < wordSearch.length; i++) {
- if(wordSearch[randomRow + i][randomCol] == (char)0 || wordSearch[randomRow + i][randomCol] == word.charAt(i)){
- placeIndex[i][0] = randomRow + i;
- placeIndex[i][1] = randomCol;
- if(word.length() - 1 == i) {
- place = true;
- break;
- }
- } else {
- break;
- }
- }
- }
- else if(randomDir == 1) { //Vertically
- for(int i = 0; randomCol + i < wordSearch[0].length; i++) {
- if(wordSearch[randomRow][randomCol + i] == (char)0 || wordSearch[randomRow][randomCol + i] == word.charAt(i)){
- placeIndex[i][0] = randomRow;
- placeIndex[i][1] = randomCol + i;
- if(word.length() - 1 == i) {
- place = true;
- break;
- }
- } else {
- break;
- }
- }
- }
- else if(randomDir == 2) { //Diagonally
- for(int i = 0; randomRow + i < wordSearch.length && randomCol + i < wordSearch[0].length; i++) {
- if(wordSearch[randomRow + i][randomCol + i] == (char)0 || wordSearch[randomRow + i][randomCol + i] == word.charAt(i)){
- placeIndex[i][0] = randomRow + i;
- placeIndex[i][1] = randomCol + i;
- if(word.length() - 1 == i) {
- place = true;
- break;
- }
- } else {
- break;
- }
- }
- }
- }
- for(int index = 0; index < word.length(); index++) {
- wordSearch[placeIndex[index][0]][placeIndex[index][1]] = word.charAt(index);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement