-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
95 lines (78 loc) · 3.22 KB
/
TicTacToe.java
File metadata and controls
95 lines (78 loc) · 3.22 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
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
char[][] board = new char[3][3];
initializeBoard(board);
char currentPlayer = 'X';
boolean gameOver = false;
int moveCount = 0;
Scanner scanner = new Scanner(System.in);
printWelcomeMessage();
while (!gameOver && moveCount < 9) {
printBoard(board);
System.out.println("\n👉 Player " + currentPlayer + ", it's your turn.");
System.out.print("Enter row and column (0, 1, or 2): ");
int row = scanner.nextInt();
int col = scanner.nextInt();
if (!isValidMove(board, row, col)) {
System.out.println("❌ Invalid move. Try again.");
continue;
}
board[row][col] = currentPlayer;
moveCount++;
if (hasWon(board, currentPlayer)) {
printBoard(board);
System.out.println("\n🎉 Player " + currentPlayer + " wins! Congratulations! 🎉");
gameOver = true;
} else {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
}
if (!gameOver) {
printBoard(board);
System.out.println("\n🤝 It's a draw! Good game!");
}
scanner.close();
}
static void initializeBoard(char[][] board) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
board[row][col] = ' ';
}
}
}
static void printWelcomeMessage() {
System.out.println("═══════════════════════════════════");
System.out.println("🎮 Welcome to Java Tic Tac Toe!");
System.out.println("Players: X and O");
System.out.println("Enter row and column values between 0 and 2");
System.out.println("═══════════════════════════════════");
}
static void printBoard(char[][] board) {
System.out.println("\n 0 1 2");
System.out.println(" -------------");
for (int row = 0; row < 3; row++) {
System.out.print(row + " |");
for (int col = 0; col < 3; col++) {
System.out.print(" " + board[row][col] + " |");
}
System.out.println("\n -------------");
}
}
static boolean isValidMove(char[][] board, int row, int col) {
return row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == ' ';
}
static boolean hasWon(char[][] board, char player) {
// Rows & Columns
for (int i = 0; i < 3; i++) {
if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) ||
(board[0][i] == player && board[1][i] == player && board[2][i] == player))
return true;
}
// Diagonals
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player))
return true;
return false;
}
}