-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe_Console.java
More file actions
191 lines (173 loc) · 5.84 KB
/
TicTacToe_Console.java
File metadata and controls
191 lines (173 loc) · 5.84 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
import java.util.*;
class TicTacToe_Console {
private char[] board;
private char currentPlayer;
TicTacToe_Console() {
this.board = new char[9];
resetBoard();
this.currentPlayer = 'X';
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TicTacToe_Console game = new TicTacToe_Console();
while (true) {
boolean playerFirst = game.promptPlayerFirst(sc);
game.resetBoard();
game.printBoard();
while (true) {
if (playerFirst) {
game.playerMove(sc);
if (game.isGameOver()) break;
game.printBoard();
game.aiMove();
if (game.isGameOver()) break;
game.printBoard();
} else {
game.aiMove();
if (game.isGameOver()) break;
game.printBoard();
game.playerMove(sc);
if (game.isGameOver()) break;
game.printBoard();
}
}
if (!game.promptPlayAgain(sc)) {
break;
}
}
sc.close();
}
private boolean promptPlayerFirst(Scanner scanner) {
while (true) {
System.out.print("Do you want to go first? (yes/no): ");
String choice = scanner.next().toLowerCase();
if (choice.equals("yes")) {
return true;
} else if (choice.equals("no")) {
return false;
} else {
System.out.println("Invalid input. Please enter 'yes' or 'no'.");
}
}
}
private boolean promptPlayAgain(Scanner scanner) {
while (true) {
System.out.print("\nDo you want to play again? (yes/no): ");
String choice = scanner.next().toLowerCase();
if (choice.equals("yes")) {
return true;
} else if (choice.equals("no")) {
return false;
} else {
System.out.println("Invalid input. Please enter 'yes' or 'no'.");
}
}
}
private void resetBoard() {
for (int i = 0; i < 9; i++) {
board[i] = ' ';
}
}
private void printBoard() {
System.out.println(" " + board[0] + " | " + board[1] + " | " + board[2]);
System.out.println("---+---+---");
System.out.println(" " + board[3] + " | " + board[4] + " | " + board[5]);
System.out.println("---+---+---");
System.out.println(" " + board[6] + " | " + board[7] + " | " + board[8]);
}
private void playerMove(Scanner scanner) {
int move;
while (true) {
System.out.print("Enter your move (1-9): ");
move = scanner.nextInt() - 1;
if (move >= 0 && move < 9 && board[move] == ' ') {
board[move] = currentPlayer;
break;
}
System.out.println("This move is not valid");
}
}
private void aiMove() {
int bestScore = Integer.MIN_VALUE;
int move = 0;
for (int i = 0; i < 9; i++) {
if (board[i] == ' ') {
board[i] = 'O';
int score = minimax(board, 0, false);
board[i] = ' ';
if (score > bestScore) {
bestScore = score;
move = i;
}
}
}
board[move] = 'O';
}
private boolean isGameOver() {
if (hasPlayerWon('X')) {
printBoard();
System.out.println("Player X wins!");
return true;
}
if (hasPlayerWon('O')) {
printBoard();
System.out.println("Player O wins!");
return true;
}
if (isBoardFull(board)) {
printBoard();
System.out.println("The game is a draw!");
return true;
}
return false;
}
private boolean hasPlayerWon(char player) {
int[][] winPatterns = {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
{0, 3, 6}, {1, 4, 7}, {2, 5, 8},
{0, 4, 8}, {2, 4, 6}
};
for (int[] pattern : winPatterns) {
if (board[pattern[0]] == player && board[pattern[1]] == player && board[pattern[2]] == player) {
return true;
}
}
return false;
}
private int minimax(char[] board, int depth, boolean isMaximizing) {
if (hasPlayerWon('O')) return 1;
if (hasPlayerWon('X')) return -1;
if (isBoardFull(board)) return 0;
if (isMaximizing) {
int bestScore = Integer.MIN_VALUE;
for (int i = 0; i < 9; i++) {
if (board[i] == ' ') {
board[i] = 'O';
int score = minimax(board, depth + 1, false);
board[i] = ' ';
bestScore = Math.max(score, bestScore);
}
}
return bestScore;
} else {
int bestScore = Integer.MAX_VALUE;
for (int i = 0; i < 9; i++) {
if (board[i] == ' ') {
board[i] = 'X';
int score = minimax(board, depth + 1, true);
board[i] = ' ';
bestScore = Math.min(score, bestScore);
}
}
return bestScore;
}
}
private boolean isBoardFull(char[] board) {
for (char c : board) {
if (c == ' ') {
return false;
}
}
return true;
}
}