-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTicTacToe.java
128 lines (116 loc) · 3.57 KB
/
TicTacToe.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
/** Java lvl 1. Homework 4
* @author Artur
* @version 17.10.2021
*/
import java.util.Random;
import java.util.Scanner;
class TicTacToe {
final char SIGN_X = 'X';
final char SIGN_O = 'O';
final char SIGN_EMPTY = '.';
char[] [] table;
Random random;
Scanner scanner;
public static void main(String[] args) {
new TicTacToe().game();
}
TicTacToe() {
// инициализация полей
table = new char [3] [3];
random = new Random();
scanner = new Scanner(System.in);
}
void game() {
initTable();
while (true) {
printTable();
turnHuman();
if (isWin(SIGN_X)) {
System.out.println("you win");
break;
}
if (isTableFull()) {
System.out.println("Sorry, DRAW...");
break;
}
turnAi();
if (isWin(SIGN_O)) {
System.out.println("Ai win");
break;
}
if (isTableFull()) {
System.out.println("Sorry, DRAW...");
break;
}
}
printTable();
}
void initTable() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
table[i][j] = SIGN_EMPTY;
}
}
}
void printTable() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(table[j][i] + " ");
}
System.out.println();
}
}
void turnHuman() {
int x, y;
do {
System.out.print("Enter [1...3] x y: ");
x = scanner.nextInt() - 1;
y = scanner.nextInt() - 1;
} while (!isCellValid(x, y));
table[x][y] = SIGN_X;
}
void turnAi() {
int x, y;
do {
x = random.nextInt(3);
y = random.nextInt(3);
} while (!isCellValid(x, y));
table[x][y] = SIGN_O;
}
boolean isCellValid(int x, int y) {
if ( x < 0 || x > 2 || y < 0 || y > 2) {
return false;
}
return table[x][y] == SIGN_EMPTY;
}
boolean isWin(char ch) {
boolean toright = true;
for (int i = 0; i < 2; i++) {
toright = toright & (table[i][i] == ch);
}
if (toright) return true;
return false;
//x
// if (table[0][0] == ch && table[1][0] == ch && table [2][0] == ch) return true;
// if (table[0][1] == ch && table[1][1] == ch && table [2][1] == ch) return true;
// if (table[0][2] == ch && table[1][2] == ch && table [2][2] == ch) return true;
// //y
// if (table[0][0] == ch && table[0][1] == ch && table [0][2] == ch) return true;
// if (table[1][0] == ch && table[1][1] == ch && table [1][2] == ch) return true;
// if (table[2][0] == ch && table[2][1] == ch && table [2][2] == ch) return true;
// //d
// if (table[0][0] == ch && table[1][1] == ch && table [2][2] == ch) return true;
// if (table[2][0] == ch && table[1][1] == ch && table [0][2] == ch) return true;
// return false;
}
boolean isTableFull() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (table[i][j] == SIGN_EMPTY) {
return false;
}
}
}
return true;
}
}