-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathTicTacToe.java
122 lines (104 loc) · 3.6 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
import java.util.Scanner;
public class TicTacToe {
private char[][] board;
private char currentPlayer;
private String player1Name;
private String player2Name;
public TicTacToe(String player1Name, String player2Name) {
board = new char[3][3];
currentPlayer = 'X';
this.player1Name = player1Name;
this.player2Name = player2Name;
initializeBoard();
}
public void initializeBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '-';
}
}
}
public 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] + " ");
}
System.out.println();
}
}
public boolean makeMove(int row, int col) {
if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == '-') {
board[row][col] = currentPlayer;
return true;
}
return false;
}
public boolean checkWin() {
return checkRows() || checkColumns() || checkDiagonals();
}
private boolean checkRows() {
for (int i = 0; i < 3; i++) {
if (board[i][0] == currentPlayer && board[i][1] == currentPlayer && board[i][2] == currentPlayer) {
return true;
}
}
return false;
}
private boolean checkColumns() {
for (int j = 0; j < 3; j++) {
if (board[0][j] == currentPlayer && board[1][j] == currentPlayer && board[2][j] == currentPlayer) {
return true;
}
}
return false;
}
private boolean checkDiagonals() {
return (board[0][0] == currentPlayer && board[1][1] == currentPlayer && board[2][2] == currentPlayer) ||
(board[0][2] == currentPlayer && board[1][1] == currentPlayer && board[2][0] == currentPlayer);
}
public boolean isBoardFull() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
return false;
}
}
}
return true;
}
public void switchPlayer() {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Player 1's name: ");
String player1Name = scanner.nextLine();
System.out.print("Enter Player 2's name: ");
String player2Name = scanner.nextLine();
TicTacToe game = new TicTacToe(player1Name, player2Name);
System.out.println("Welcome to Tic-Tac-Toe, " + player1Name + " and " + player2Name + "!");
while (true) {
System.out.println("Current board:");
game.printBoard();
System.out.println(player1Name + ", enter your move (row and column): ");
int row = scanner.nextInt();
int col = scanner.nextInt();
if (game.makeMove(row, col)) {
if (game.checkWin()) {
System.out.println(player1Name + " wins!");
break;
} else if (game.isBoardFull()) {
System.out.println("It's a draw!");
break;
} else {
game.switchPlayer();
}
} else {
System.out.println("Invalid move. Try again.");
}
}
scanner.close();
}
}