forked from dimpeshpanwar/Java-Advance-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
145 lines (128 loc) · 4.02 KB
/
TicTacToe.java
File metadata and controls
145 lines (128 loc) · 4.02 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
/**
* TicTacToe Game in Java
* Hacktoberfest 2025 Contribution
*
* Features:
* - Two-player console game (X and O)
* - Validates moves
* - Detects win or tie
* - Uses OOP concepts
*
*/
import java.util.Scanner;
public class TicTacToe {
private char[][] board; // 3x3 board for Tic-Tac-Toe
private char currentPlayer; // Current player symbol: 'X' or 'O'
// Constructor to initialize the board and set starting player
public TicTacToe() {
board = new char[3][3];
currentPlayer = 'X';
initializeBoard();
}
// Initialize the board with '-' to indicate empty positions
private void initializeBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '-';
}
}
}
// Print the current state of the board
private void printBoard() {
System.out.println("Current Board:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
// Check if the board is full (tie condition)
private boolean isBoardFull() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
return false;
}
}
}
return true;
}
// Check if the current player has won
private boolean checkWin() {
// Check rows
for (int i = 0; i < 3; i++) {
if (board[i][0] == currentPlayer &&
board[i][1] == currentPlayer &&
board[i][2] == currentPlayer) {
return true;
}
}
// Check columns
for (int j = 0; j < 3; j++) {
if (board[0][j] == currentPlayer &&
board[1][j] == currentPlayer &&
board[2][j] == currentPlayer) {
return true;
}
}
// Check diagonals
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;
}
// Switch player from X to O or O to X
private void changePlayer() {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
// Place a mark on the board; returns true if move is valid
private boolean placeMark(int row, int col) {
if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == '-') {
board[row][col] = currentPlayer;
return true;
}
return false;
}
// Main game loop
public void playGame() {
Scanner scanner = new Scanner(System.in);
boolean gameEnded = false;
System.out.println("Welcome to Tic-Tac-Toe!");
printBoard();
while (!gameEnded) {
System.out.println("Player " + currentPlayer + ", enter your move (row and column: 0,1,2): ");
int row = scanner.nextInt();
int col = scanner.nextInt();
// Check if move is valid
if (!placeMark(row, col)) {
System.out.println("Invalid move! Try again.");
continue;
}
printBoard();
// Check for win or tie
if (checkWin()) {
System.out.println("Congratulations! Player " + currentPlayer + " wins!");
gameEnded = true;
} else if (isBoardFull()) {
System.out.println("The game is a tie!");
gameEnded = true;
} else {
changePlayer(); // Switch turns
}
}
scanner.close();
}
// Entry point
public static void main(String[] args) {
TicTacToe game = new TicTacToe();
game.playGame();
}
}