Implementation of TicTacToe in java
TicTacToe program is a classic one. Here, we implement this with AI. When a player plays,AI plays opposite side.
The program is given below.
// importing built-in package.
import java.util.Scanner;
//public class definition
public class TTTAI {
static char[][]
board1 = {
{' ', ' ', '
'},
{' ', ' ', '
'},
{' ', ' ', '
'}
};
// main() function includes the play_Move(), ai_Move(),
isGameOver().
public static void
main(String[] args) {
Scanner s1 =
new Scanner(System.in);
while (true) {
printIt();
play_Move(s1);
if
(isGameOver()) break;
ai_Move();
if
(isGameOver()) break;
}
s1.close();
}
// printIt() function definition
static void
printIt() {
for (int i =
0; i < 3; i++) {
System.out.println(board1[i][0] + "|" + board1[i][1] +
"|" + board1[i][2]);
if (i <
2) System.out.println("-----");
}
}
//play_Move() gets the input from user and AI plays
according to it
static void
play_Move(Scanner s1) {
int row1,
col1;
while (true) {
System.out.print("Enter row and column (0-2): ");
row1 =
s1.nextInt();
col1 =
s1.nextInt();
if
(board1[row1][col1] == ' ') {
board1[row1][col1] = 'X';
break;
} else {
System.out.println("Cell is occupied! Try again with another
position.");
}
}
}
// Definition of ai_Move()
static void
ai_Move() {
int b_Score =
Integer.MIN_VALUE;
int moveRow =
-1, moveCol = -1;
for (int i =
0; i < 3; i++) {
for (int j
= 0; j < 3; j++) {
if
(board1[i][j] == ' ') {
board1[i][j] = 'O';
int score = minimax(false);
board1[i][j] = ' ';
if
(score > b_Score) {
b_Score = score;
moveRow = i;
moveCol = j;
}
}
}
}
board1[moveRow][moveCol] = 'O';
}
//check for min and max value
static int
minimax(boolean isMaximizing) {
if
(checkWinner('O')) return 10;
if
(checkWinner('X')) return -10;
if
(isBoardFull()) return 0;
int b_Score =
isMaximizing ? Integer.MIN_VALUE : Integer.MAX_VALUE;
for (int i =
0; i < 3; i++) {
for (int j
= 0; j < 3; j++) {
if
(board1[i][j] == ' ') {
board1[i][j] = isMaximizing ? 'O' : 'X';
int score = minimax(!isMaximizing);
board1[i][j] = ' ';
b_Score = isMaximizing ? Math.max(score, b_Score) : Math.min(score,
b_Score);
}
}
}
return
b_Score;
}
//Find the winner
static boolean
checkWinner(char player) {
for (int i =
0; i < 3; i++) {
if
(board1[i][0] == player && board1[i][1] == player &&
board1[i][2] == player) return true;
if
(board1[0][i] == player && board1[1][i] == player &&
board1[2][i] == player) return true;
}
if
(board1[0][0] == player && board1[1][1] == player &&
board1[2][2] == player) return true;
if
(board1[0][2] == player && board1[1][1] == player &&
board1[2][0] == player) return true;
return false;
}
//check for board full
static boolean
isBoardFull() {
for (int i =
0; i < 3; i++)
for (int j
= 0; j < 3; j++)
if
(board1[i][j] == ' ') return false;
return true;
}
//definition of game over or not
static boolean
isGameOver() {
if
(checkWinner('X')) {
printIt();
System.out.println("You win!");
return
true;
}
if
(checkWinner('O')) {
printIt();
System.out.println("AI wins!");
return
true;
}
if
(isBoardFull()) {
printIt();
System.out.println("It's a draw!");
return
true;
}
return false;
}
}
Output:
| |
-----
| |
-----
| |
Enter row and column (0-2): 1 1
O| |
-----
|X|
-----
| |
Enter row and column (0-2): 0 1
O|X|
-----
|X|
-----
|O|
Enter row and column (0-2): 1 0
O|X|
-----
X|X|O
-----
|O|
Enter row and column (0-2): 0 2
O|X|X
-----
X|X|O
-----
O|O|
Enter row and column (0-2): 2 2
O|X|X
-----
X|X|O
-----
O|O|X
It's a draw!
That’s all. The implementation of TicTacToe game in java was
done successfully. Keep Coding!!!
Comments
Post a Comment