-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToeApp.java
More file actions
200 lines (167 loc) · 6.15 KB
/
Copy pathTicTacToeApp.java
File metadata and controls
200 lines (167 loc) · 6.15 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
192
193
194
195
196
197
198
199
200
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
// Board class to manage the board
class Board {
private char[][] board;
int size;
public Board(int n) {
if (n < 3) throw new IllegalArgumentException("Board size must be at least 3x3");
size = n;
board = new char[n][n];
for (char[] row : board) {
for (int i = 0; i < n; i++) {
row[i] = '-';
}
}
}
public boolean makeMove(int row, int col, char symbol) {
if (row < 0 || row >= size || col < 0 || col >= size || board[row][col] != '-') {
return false;
}
board[row][col] = symbol;
return true;
}
public boolean checkWinner(char symbol) {
for (int i = 0; i < size; i++) {
if (board[i][0] == symbol && board[i][1] == symbol && board[i][2] == symbol) return true;
if (board[0][i] == symbol && board[1][i] == symbol && board[2][i] == symbol) return true;
}
if (board[0][0] == symbol && board[1][1] == symbol && board[2][2] == symbol) return true;
if (board[0][2] == symbol && board[1][1] == symbol && board[2][0] == symbol) return true;
return false;
}
public boolean isFull() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (board[i][j] == '-') return false;
}
}
return true;
}
public void printBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
}
// Abstract Player class for human and bot players
abstract class Player {
protected char symbol;
public Player(char symbol) {
this.symbol = symbol;
}
public abstract int[] makeMove(Board board);
}
// HumanPlayer class for human players
class HumanPlayer extends Player {
public HumanPlayer(char symbol) {
super(symbol);
}
@Override
public int[] makeMove(Board board) {
Scanner scanner = new Scanner(System.in);
int row = -1, col = -1; // Initialize row and col to invalid values
boolean validMove = false;
// Ensure the move is valid
while (!validMove) {
System.out.print("Enter row and column (0-based index): ");
row = scanner.nextInt();
col = scanner.nextInt();
validMove = board.makeMove(row, col, symbol);
if (!validMove) {
System.out.println("Invalid move. Try again.");
}
}
return new int[]{row, col};
}
}
// BotPlayer class for bot players (random moves)
class BotPlayer extends Player {
private Random random;
public BotPlayer(char symbol) {
super(symbol);
this.random = new Random();
}
@Override
public int[] makeMove(Board board) {
int row, col;
do {
row = random.nextInt(board.size);
col = random.nextInt(board.size);
} while (!board.makeMove(row, col, symbol));
return new int[]{row, col};
}
}
// TicTacToeGame class to handle the game logic
class TicTacToeGame {
private Board board;
private List<Player> players;
private int currentPlayer;
public TicTacToeGame(int n, int numPlayers, int botPlayers) {
board = new Board(n);
players = new ArrayList<>();
char symbol = 'X';
// Add human players
for (int i = 0; i < numPlayers - botPlayers; i++) {
players.add(new HumanPlayer(symbol));
symbol = (symbol == 'X') ? 'O' : 'X';
}
// Add bot players
for (int i = 0; i < botPlayers; i++) {
players.add(new BotPlayer(symbol));
symbol = (symbol == 'X') ? 'O' : 'X';
}
currentPlayer = 0;
}
public void play() {
Scanner scanner = new Scanner(System.in);
boolean gameOver = false;
while (!gameOver) {
Player player = players.get(currentPlayer);
System.out.println("Player " + (currentPlayer + 1) + " (" + player.symbol + ") turn:");
int[] move = player.makeMove(board);
board.printBoard();
if (board.checkWinner(player.symbol)) {
System.out.println("Player " + (currentPlayer + 1) + " (" + player.symbol + ") wins!");
gameOver = true;
} else if (board.isFull()) {
System.out.println("It's a draw!");
gameOver = true;
}
currentPlayer = (currentPlayer + 1) % players.size();
}
scanner.close();
}
}
// Main class to initialize and run the game
public class TicTacToeApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter board size (N x N): ");
int n = scanner.nextInt();
if (n == 2) {
System.out.println("2x2 board is not allowed!");
return;
}
// Ensure number of players is strictly n-1
System.out.print("Enter the number of players (must be n - 1): ");
int numPlayers = scanner.nextInt();
if (numPlayers != n - 1) {
System.out.println("Error: Number of players must be strictly " + (n - 1));
return;
}
System.out.print("Enter the number of bot players: ");
int botPlayers = scanner.nextInt();
if (botPlayers >= numPlayers) {
System.out.println("Number of bot players cannot be greater than or equal to total players.");
return;
}
TicTacToeGame game = new TicTacToeGame(n, numPlayers, botPlayers);
game.play();
}
}