-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic tac toe
216 lines (206 loc) · 5.46 KB
/
tic tac toe
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
package tictactoe;
import java.util.Scanner;
public class Main {
static String[] board;
public static void main(String[] args) {
// write your code here
int block=0;
int confirm = 0;
Scanner sc = new Scanner(System.in);
board = new String[9];
for(int i=0; i<9; i++ ) {
board[i] = " ";
}
showBoard();
for (int i=1; i<10; i++) {
do {
System.out.println("Enter the coordinates:");
try {
String cords = sc.nextLine();
String x= String.valueOf(cords.charAt(0));
String y= String.valueOf(cords.charAt(2));
block = Integer.parseInt(x+y);
}
catch (Exception e) {
System.out.println("You should enter numbers!");
continue;
}
if (i%2 == 0) {
confirm = enterMove(block,"O");
} else {
confirm = enterMove(block,"X");
}
} while (confirm != 1);
showBoard();
checkStatus();
}
System.out.println(checkGame());
}
static void showBoard() {
System.out.println("---------");
System.out.println("| " + board[0] + " " + board[1] + " " + board[2] + " |");
System.out.println("| " + board[3] + " " + board[4] + " " + board[5] + " |");
System.out.println("| " + board[6] + " " + board[7] + " " + board[8] + " |");
System.out.println("---------");
}
static int enterMove(int block,String move) {
int movValid = 0;
switch (block) {
case 11: if ( board[6].equals("_") || board[6].equals(" ") ) {
board[6] = move;
movValid = 1; }
else {
System.out.println("This cell is occupied! Choose another one!");
}
break;
case 12: if ( board[3].equals("_") || board[3].equals(" ") ) {
board[3] = move;
movValid = 1; }
else {
System.out.println("This cell is occupied! Choose another one!");
}
break;
case 13: if ( board[0].equals("_") || board[0].equals(" ") ) {
board[0] = move;
movValid = 1; }
else {
System.out.println("This cell is occupied! Choose another one!");
}
break;
case 21: if ( board[7].equals("_") || board[7].equals(" ") ) {
board[7] = move;
movValid = 1; }
else {
System.out.println("This cell is occupied! Choose another one!");
}
break;
case 22: if ( board[4].equals("_") || board[4].equals(" ") ) {
board[4] = move;
movValid = 1; }
else {
System.out.println("This cell is occupied! Choose another one!");
}
break;
case 23: if ( board[1].equals("_") || board[1].equals(" ") ) {
board[1] = move;
movValid = 1; }
else {
System.out.println("This cell is occupied! Choose another one!");
}
break;
case 31: if ( board[8].equals("_") || board[8].equals(" ") ) {
board[8] = move;
movValid = 1; }
else {
System.out.println("This cell is occupied! Choose another one!");
}
break;
case 32: if ( board[5].equals("_") || board[5].equals(" ") ) {
board[5] = move;
movValid = 1; }
else {
System.out.println("This cell is occupied! Choose another one!");
}
break;
case 33: if ( board[2].equals("_") || board[2].equals(" ") ) {
board[2] = move;
movValid = 1; }
else {
System.out.println("This cell is occupied! Choose another one!");
}
break;
default: System.out.println("Coordinates should be from 1 to 3!");
break;
}
return movValid;
}
static void checkStatus() {
String result = null;
result = checkImpossible();
if (result.equals("YES")) {
System.out.println("Impossible");
System.exit(0);
}
result = checkWinner("XXX");
if (result.equals("YES")) {
System.out.println("X wins");
System.exit(0);
}
result=checkWinner("OOO");
if (result.equals("YES")) {
System.out.println("O wins");
System.exit(0);
}
}
static String checkWinner(String checkfor) {
String line = null;
for (int a = 0; a < 8; a++) {
switch (a) {
case 0:
line = board[0] + board[1] + board[2];
break;
case 1:
line = board[3] + board[4] + board[5];
break;
case 2:
line = board[6] + board[7] + board[8];
break;
case 3:
line = board[0] + board[3] + board[6];
break;
case 4:
line = board[1] + board[4] + board[7];
break;
case 5:
line = board[2] + board[5] + board[8];
break;
case 6:
line = board[0] + board[4] + board[8];
break;
case 7:
line = board[2] + board[4] + board[6];
break;
}
if (line.equals(checkfor)) {
return "YES";
}
}
return "NO";
}
static String checkGame() {
int count = 0;
for(int i=0; i<9; i++) {
if ( board[i].equals("_") || board[i].equals(" ") ) {
count += 1;
}
}
if (count == 0) {
return "Draw";
} else {
return "Game not finished";
}
}
static String checkImpossible() {
String xwins,owins;
int countx,counto,diff;
countx=0; counto=0;
for(int i=0; i<9; i++) {
if (board[i].equals("X")) {
countx += 1;
}
else if (board[i].equals("O")) {
counto += 1;
}
}
diff = Math.abs(countx - counto);
xwins = checkWinner("XXX");
owins = checkWinner("OOO");
if (xwins.equals("YES") && owins.equals("YES")) {
return "YES";
} else if ( diff > 1 ) {
return "YES";
} else {
return "NO";
}
}
}