-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSearch.java
More file actions
206 lines (191 loc) · 5.67 KB
/
WordSearch.java
File metadata and controls
206 lines (191 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//Name: Rishi Saravanan
//Date: 1/22/21
//Program Name: WordSearch
//Program Goal: Use a 2d array to play a word search game with 2 people
import java.util.Scanner;
public class WordSearch {
public static void main(String[] args) {
WordSearch w1 = new WordSearch();
w1.runner();
}
public void runner() {
Scanner s1 = new Scanner(System.in);
char[][] array = new char[8][8];
String[] finalArray = new String[84];
String[] wordsUsed = new String[100];
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
array[x][y] = (char) (int) (Math.random() * 26 + 65);
}
}
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
System.out.print(array[x][y] + " ");
}
System.out.println();
}
for (int x = 0; x < finalArray.length; x++) {
finalArray[x] = "";
}
int count = 0;
for (int s = 0; s < 8; s++) {
for (int t = 0; t < 8; t++) {
finalArray[count] = finalArray[count] + array[s][t] + "";
}
count++;
}
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
finalArray[count] = finalArray[count] + array[y][x] + "";
}
count++;
}
for (int s = 0; s < 8; s++) {
for (int t = 0; t < 8; t++) {
finalArray[count] = array[s][t] + "" + finalArray[count];
}
count++;
}
for (int s = 0; s < 8; s++) {
for (int t = 0; t < 8; t++) {
finalArray[count] = array[t][s] + "" + finalArray[count];
}
count++;
}
int startCount = 2;
for(int a = 2; a < 9; a++) {
String str = firstDiag(array, startCount);
String newStr = secDiag(array, startCount);
String reverseStr = reverse(str);
String reverseNewStr = reverse(newStr);
finalArray[count] = str;
startCount++;
count++;
finalArray[count] = newStr;
count++;
finalArray[count] = reverseStr;
count++;
finalArray[count] = reverseNewStr;
}
int player1Score = 0;
int player2Score = 0;
String inputTwo = "";
System.out.println("\nPlayer 1,");
System.out.println("Please enter a word you found in the puzzle(at least 2 chars):");
String inputOne = s1.next();
if(!inputOne.equals("Dunzo!")) {
int count2 = 0;
if (!addWordUsed(inputOne, wordsUsed)) {
for (int x = 0; x < finalArray.length; x++) {
if (finalArray[x].contains(inputOne.toUpperCase())) {
count2++;
player1Score = player1Score + inputOne.length();
x = finalArray.length;
}
}
printScore(player1Score, player2Score, count2);
}
System.out.println("\nPlayer 2,");
System.out.println("Please enter a word you found in the puzzle(at least 2 chars):");
inputTwo = s1.next();
int count3 = 0;
if (!addWordUsed(inputTwo, wordsUsed)) {
for (int x = 0; x < finalArray.length; x++) {
if (finalArray[x].contains(inputTwo.toUpperCase())) {
count3++;
player2Score = player2Score + inputTwo.length();
x = finalArray.length;
}
}
printScore(player1Score, player2Score, count3);
}
}
while(!inputOne.equals("Dunzo!") && !inputTwo.equals("Dunzo!")) {
if(!inputOne.equals("Dunzo!") && !inputTwo.equals("Dunzo!")) {
System.out.println("\nPlayer 1,");
System.out.println("Please enter a word you found in the puzzle(at least 2 chars):");
inputOne = s1.next();
if(!inputOne.equals("Dunzo!")) {
int count4 = 0;
if (!addWordUsed(inputOne, wordsUsed)) {
for (int x = 0; x < finalArray.length; x++) {
if (finalArray[x].contains(inputOne.toUpperCase())) {
player1Score = player1Score + inputOne.length();
x = finalArray.length;
count4++;
}
}
printScore(player1Score, player2Score, count4);
}
System.out.println("\nPlayer 2,");
System.out.println("Please enter a word you found in the puzzle(at least 2 chars):");
inputTwo = s1.next();
int count5 = 0;
if (!addWordUsed(inputTwo, wordsUsed)) {
for (int x = 0; x < finalArray.length; x++) {
if (finalArray[x].contains(inputTwo.toUpperCase())) {
player2Score = player2Score + inputTwo.length();
x = finalArray.length;
count5++;
}
}
printScore(player1Score, player2Score, count5);
}
}
}
}
if (player1Score > player2Score)
System.out.println("\nPlayer 1 Wins!");
else if (player1Score < player2Score)
System.out.println("\nPlayer 2 Wins");
else
System.out.println("\nIts a Tie!");
System.out.println("Thanks for playing");
}
public String firstDiag(char[][] array, int start) {
String updatedStr = "";
int count = start;
for(int x = 0; x < start; x++) {
updatedStr = updatedStr + array[x][count-1];
count--;
}
return updatedStr;
}
public String secDiag(char[][] array, int count) {
String updatedStr = "";
for(int x = 7; x > -1; x--) {
updatedStr = updatedStr + array[x][count-1];
count--;
if(count == 0) x = -1;
}
return updatedStr;
}
public String reverse(String str) {
String newStr = "";
for(int x = str.length()-1; x > -1; x--) {
newStr+=str.charAt(x);
}
return newStr;
}
public void printScore(int player1Score, int player2Score, int count) {
if (count == 0) {
System.out.println("That word is not in the puzzle");
}
System.out.println("Player 1 Score: " + player1Score);
System.out.println("Player 2 Score: " + player2Score);
}
public boolean addWordUsed(String word, String[] wordsUsed) {
int indexToAdd = 0;
boolean wordFound = false;
for (int i=0; i < wordsUsed.length; i++) {
if (wordsUsed[i] != null && wordsUsed[i].equals(word)) {
System.out.println("That word has already been used. Please try again.");
wordFound = true;
indexToAdd++;
}
}
if (!wordFound)
wordsUsed[indexToAdd] = word;
return wordFound;
}
}