-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project_xO.java
260 lines (239 loc) · 9.78 KB
/
Project_xO.java
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package project_x.o;
import java.util.Scanner;
public class Project_xO {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[][] matrix;
matrix = new String[3][3];//the matrix of the play
int place = 1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = "[" + place + "]";
place++;
}
}
print_free(matrix);//print the usual shape of x o and when it is free
int x = 0, p = 0;
String s = new String("");
do {
//loop to make alot of chooses to win any one or to draw
menu();
System.out.print("Please enter a choose :");
x = sc.nextInt();
//function truee it makes if it input datatype wrong or intger out the limit of matrix input another number
switch (x) {
case 1:
int choice,
row,
colum;
System.out.print("Please enter the place:");
choice = truee();
row = given_row(choice);
colum = given_colum(choice);
if (Valid_place(matrix, row, colum) == false) {
while (Valid_place(matrix, row, colum) == false) {
System.out.println("Please enter correct place,this is not avalible place:");
choice = truee();
row = given_row(choice);
colum = given_colum(choice);
}
}
p++;//counter that makes join x or o if odd x if even o
if (p % 2 != 0) {
System.out.print("Please enter the cahracter x:");
s = sc.next();
while (!s.equals("x")) {
System.out.print("Please enter the cahracter x:");
s = sc.next();
}
} else {
System.out.print("Please enter the cahracter o:");
s = sc.next();
while (!s.equals("o")) {
System.out.print("Please enter the cahracter o:");
s = sc.next();
}
}
matrix[row][colum] = "[" + s + "]";
//enter the char if it free
print_free(matrix);//the print after join any char
if (!WIN(matrix).equals("false")) {//if x win or o win break and the program finish
System.out.println(WIN(matrix));
break;
}
break;
case 2:
int choice2,
row2,
colum2;
System.out.print("Please enter a place you want to cancel:");
choice2 = truee();
row2 = given_row(choice2);
colum2 = given_colum(choice2);
if (matrix[row2][colum2].equals("[" + choice2 + "]")) {
System.out.println("That is already free");//if the place was free before!
} else {
matrix[row2][colum2] = "[" + choice2 + "]";
--p;
}//that is make counter to enter the char has canceled again
print_free(matrix); //print the shape after cancel
break;
default:
System.out.print("Enter correct choose:");//if it is enter choose not 1 or 2 enter another
break;
}
if (!WIN(matrix).equals("false"))//if any player win break
{
break;
} else if (WIN(matrix).equals("false") && p == 9)//if it is no free places and no one win that mean draw
{
System.out.println("Draw");
break;
}
} while (WIN(matrix).equals("false") || fun2(matrix).equals("true"));
}/*this loop to enter chooses to continue if no one win
and if there were places free*/
public static String fun2(String[][] matrix) {//fun return true if there were places free and false if it all not free
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[i][j].equals("[ ]")) {
return "true";
}
}
}
return "false";
}
public static void menu()//this is menu to show the opinions
{
System.out.println("Please press 1 if you want to input ");
System.out.println("Please press 2 if you want to cancel");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
public static void print_free(String[][] matrix) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public static int truee() {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
if (input.hasNextInt()) {
int x = input.nextInt();
if (x >= 1 && x <= 9) {
return x;
}
} else {
System.out.println("Please enter a correct intger in range [1,3]");
}
input.next();
}
return 0;
}
public static String WIN(String[][] matrix) {//if any player win
String win = "";
if (matrix[0][0].equals(matrix[0][1]) && matrix[0][1].equals(matrix[0][2])
&& !matrix[0][0].equals("[ ]")) {
if (matrix[0][0].equals("[x]")) {
win = "Player X is win";
} else {
win = "Player O is win";
}
} else if (matrix[1][0].equals(matrix[1][1]) && matrix[1][1].equals(matrix[1][2])
&& !matrix[1][1].equals("[ ]")) {
if (matrix[1][1].equals("[x]")) {
win = "Player X is win";
} else {
win = "Player O is win";
}
} else if (matrix[2][0].equals(matrix[2][1]) && matrix[2][1].equals(matrix[2][2])
&& !matrix[2][2].equals("[ ]")) {
if (matrix[2][2].equals("[x]")) {
win = "Player X is win";
} else {
win = "Player O is win";
}
} else if (matrix[0][0].equals(matrix[1][0]) && matrix[1][0].equals(matrix[2][0])
&& !matrix[0][0].equals("[ ]")) {
if (matrix[0][0].equals("[x]")) {
win = "Player X is win";
} else {
win = "Player O is win";
}
} else if (matrix[0][1].equals(matrix[1][1]) && matrix[1][1].equals(matrix[2][1])
&& !matrix[1][1].equals("[ ]")) {
if (matrix[1][1].equals("[x]")) {
win = "Player X is win";
} else {
win = "Player O is win";
}
} else if (matrix[0][2].equals(matrix[1][2]) && matrix[1][2].equals(matrix[2][2])
&& !matrix[2][2].equals("[ ]")) {
if (matrix[2][2].equals("[x]")) {
win = "Player X is win";
} else {
win = "Player O is win";
}
} else if (matrix[0][0].equals(matrix[1][1]) && matrix[1][1].equals(matrix[2][2])
&& !matrix[2][2].equals("[ ]")) {
if (matrix[2][2].equals("[x]")) {
win = "Player X is win";
} else {
win = "Player O is win";
}
} else if (matrix[0][2].equals(matrix[1][1]) && matrix[1][1].equals(matrix[2][0])
&& !matrix[1][1].equals("[ ]")) {
if (matrix[1][1].equals("[x]")) {
win = "Player X is win";
} else {
win = "Player O is win";
}
} else {
win = "false";
}
return win;
}
public static boolean Valid_place(String[][] matrix, int row, int colum) {//check that is valid place
if (matrix[row][colum].equals("[1]")) {
return true;
} else if (matrix[row][colum].equals("[2]")) {
return true;
} else if (matrix[row][colum].equals("[3]")) {
return true;
} else if (matrix[row][colum].equals("[4]")) {
return true;
} else if (matrix[row][colum].equals("[5]")) {
return true;
} else if (matrix[row][colum].equals("[6]")) {
return true;
} else if (matrix[row][colum].equals("[7]")) {
return true;
} else if (matrix[row][colum].equals("[8]")) {
return true;
} else if (matrix[row][colum].equals("[9]")) {
return true;
} else {
return false;
}
}
public static int given_row(int x) {//take the place return the row of the place
if (x == 1 || x == 2 || x == 3) {
return 0;
} else if (x == 4 || x == 5 || x == 6) {
return 1;
} else {
return 2;
}
}
public static int given_colum(int x) {//take the place return the colum of the place
if (x == 1 || x == 4 || x == 7) {
return 0;
} else if (x == 2 || x == 5 || x == 8) {
return 1;
} else {
return 2;
}
}
}