-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvancedTicTacToe.java
More file actions
112 lines (95 loc) · 3.49 KB
/
AdvancedTicTacToe.java
File metadata and controls
112 lines (95 loc) · 3.49 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
import java.util.Random;
import java.util.Scanner;
public class AdvancedTicTacToe {
private static char[][] board = { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } };
private static char currentPlayer = 'X';
private static char computerPlayer = 'O';
public static void main(String[] args) {
boolean gameWon = false;
while (true) {
printBoard();
if (currentPlayer == 'X') {
System.out.println("Player X, enter row (0-2) and column (0-2) separated by a space (e.g., 1 1): ");
getPlayerMove();
} else {
System.out.println("Computer's move: ");
getComputerMove();
}
gameWon = checkWinner();
if (gameWon) {
printBoard();
if (currentPlayer == 'X') {
System.out.println("Player X wins!");
} else {
System.out.println("Computer wins!");
}
break;
}
if (boardIsFull()) {
printBoard();
System.out.println("It's a draw!");
break;
}
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
}
private static void printBoard() {
System.out.println(" 0 1 2");
for (int i = 0; i < 3; i++) {
System.out.print(i + " ");
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j]);
if (j < 2)
System.out.print("|");
}
System.out.println();
if (i < 2)
System.out.println(" -----");
}
}
private static void getPlayerMove() {
Scanner scanner = new Scanner(System.in);
int row = scanner.nextInt();
int col = scanner.nextInt();
if (isValidMove(row, col)) {
board[row][col] = currentPlayer;
} else {
System.out.println("Invalid move. Try again.");
getPlayerMove();
}
}
private static void getComputerMove() {
Random random = new Random();
int row, col;
do {
row = random.nextInt(3);
col = random.nextInt(3);
} while (!isValidMove(row, col));
board[row][col] = computerPlayer;
}
private static boolean isValidMove(int row, int col) {
return row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == ' ';
}
private static boolean checkWinner() {
for (int i = 0; i < 3; i++) {
if (board[i][0] == currentPlayer && board[i][1] == currentPlayer && board[i][2] == currentPlayer)
return true;
if (board[0][i] == currentPlayer && board[1][i] == currentPlayer && board[2][i] == currentPlayer)
return true;
}
if (board[0][0] == currentPlayer && board[1][1] == currentPlayer && board[2][2] == currentPlayer)
return true;
if (board[0][2] == currentPlayer && board[1][1] == currentPlayer && board[2][0] == currentPlayer)
return true;
return false;
}
private static boolean boardIsFull() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == ' ')
return false;
}
}
return true;
}
}