I've been working on a program that creates a word search based on user inputted words and parameters. Basically the idea is to create something similar to this:
Enter number of rows
8
Enter number of columns
7
Enter words
cat
dog
the program would then generate a word search with 8 rows, 7 columns, and would include the words cat and dog. The program would generate these words diagonally, horizontally and vertically within the given constraints.
I don't want an answer or for this to be done for me, but rather am interested in being pointed in the right direction.
Attached below is the most recent code that I have to give everyone an idea.
Again, all that I am asking for is for is a start in the right direction.
Thanks for any and all help!
public void fillArray() {Scanner sc = new Scanner(System.in);
System.out.println("How many rows would you like? >");
int row = sc.nextInt();
System.out.println("How many columns would you like? >");
int col = sc.nextInt();
String word = sc.next();
String testString = word;
char[] stringToCharArray = testString.toCharArray();
System.out.println(testString.toCharArray());
int[][] arrayRC = new int[row][col];
for (int i = 0; i < row + 1; i++) {
for (int j = 0; j < col + 1; j++) {
if (j < col && i < row) {
arrayRC[i][j] = (int) ((Math.random() * 26) + 1);
char alphabet;
switch (arrayRC[i][j]) {
case 1:
alphabet = 'a';
break;
case 2:
alphabet = 'b';
break;
case 3:
alphabet = 'c';
break;
case 4:
alphabet = 'd';
break;
case 5:
alphabet = 'e';
break;
case 6:
alphabet = 'f';
break;
case 7:
alphabet = 'g';
break;
case 8:
alphabet = 'h';
break;
case 9:
alphabet = 'i';
break;
case 10:
alphabet = 'j';
break;
case 11:
alphabet = 'k';
break;
case 12:
alphabet = 'l';
break;
case 13:
alphabet = 'm';
break;
case 14:
alphabet = 'n';
break;
case 15:
alphabet = 'o';
break;
case 16:
alphabet = 'p';
break;
case 17:
alphabet = 'q';
break;
case 18:
alphabet = 'r';
break;
case 19:
alphabet = 's';
break;
case 20:
alphabet = 't';
break;
case 21:
alphabet = 'u';
break;
case 22:
alphabet = 'v';
break;
case 23:
alphabet = 'w';
break;
case 24:
alphabet = 'x';
break;
case 25:
alphabet = 'y';
break;
case 26:
alphabet = 'z';
break;
default:
alphabet = '-';
break;
}
System.out.print(alphabet);
} else {
System.out.println();
}
}
}
}
I believe the following will work (as you can guess, I have not tried it out).
I would use a 2D array of char
, not int
. The initial value of each field will be '\0', we are going to use that. I would fit in the words first and the random fill letters afterwards. To fill in a word, first select a random direction or orientation (vertical, horizontal or diagonal). It’s not clear to me whether you want 3, 5 or 8 directions, but as long as you know… The direction sets some limits on where a word can begin, e.g., a horizontal word of length 3 may begin in the bottom row, but at least 3 positions from the right edge; a vertical word cannot begin at the bottom. Pick a random start square from within the possible ones. See if there is a conflict with letters already filled in. Remember: you may fill in 'c' in a field if either it already contains 'c' or '\0', not if it contains another letter. Only if no letter in the word conflicts, fill in all the letters. In case of a conflict, try another start square. If after, say, 100 attempts you still cannot fill in the word, give up and tell the user you cannot fill in all the words.
After you’ve filled in all the words, put a random letter in all fields that don’t already hold a letter.
An issue is that the final filling in of random letters may accidentally spell, say, cat in one more place so it ends being in there twice. Decide if you can live with that.
EDIT: To choose a letter at random, rather than you long switch statement I would recommend something like
private static final char[] LETTERS = "abcdefghijklmnopqrstuvwxyz".toCharArray();
private static final Random RAND = new Random();
private static char getRandomLetter() {
int randomIndex = RAND.nextInt(LETTERS.length);
return LETTERS[randomIndex];
}