import java.util.Random;
import java.util.Scanner;
public class TicTacToeGame {
private static final int SIZE = 3;
private char[][] board = new char[SIZE][SIZE];
private char playerMark, computerMark;
private Random random = new Random();
public TicTacToeGame() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = '-';
}
}
}
public boolean isBoardFull() {
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();
}
}
public boolean checkWin() {
return checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin();
}
private boolean checkRowsForWin() {
for (int i = 0; i < SIZE; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != '-') {
return true;
}
}
return false;
}
private boolean checkColumnsForWin() {
for (int i = 0; i < SIZE; i++) {
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != '-') {
return true;
}
}
return false;
}
private boolean checkDiagonalsForWin() {
return (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != '-') ||
(board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != '-');
}
public void changePlayer() {
playerMark = (playerMark == 'X') ? 'O' : 'X';
computerMark = (computerMark == 'X') ? 'O' : 'X';
}
public void computerTurn() {
int computerMove;
while (true) {
computerMove = random.nextInt(9) + 1;
if (isValidMove(String.valueOf(computerMove))) {
break;
}
}
System.out.println("Computer chose " + computerMove);
placeMark(String.valueOf(computerMove), computerMark);
}
public boolean isValidMove(String position) {
switch (position) {
case "1":
return (board[0][0] == '-');
case "2":
return (board[0][1] == '-');
case "3":
return (board[0][2] == '-');
case "4":
return (board[1][0] == '-');
case "5":
return (board[1][1] == '-');
case "6":
return (board[1][2] == '-');
case "7":
return (board[2][0] == '-');
case "8":
return (board[2][1] == '-');
case "9":
return (board[2][2] == '-');
default:
return false;
}
}
public void placeMark(String position, char mark) {
switch (position) {
case "1":
board[0][0] = mark;
break;
case "2":
board[0][1] = mark;
break;
case "3":
board[0][2] = mark;
break;
case "4":
board[1][0] = mark;
break;
case "5":
board[1][1] = mark;
break;
case "6":
board[1][2] = mark;
break;
case "7":
board[2][0] = mark;
break;
case "8":
board[2][1] = mark;
break;
case "9":
board[2][2] = mark;
break;
default:
System.out.println("Invalid position");
}
}
public void playGame() {
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to Akshat's Tic Tac Toe!");
System.out.println("To play, enter any number from 1-9 to indicate your move!");
printBoard();
System.out.println("Choose your mark: X or O");
playerMark = scan.nextLine().charAt(0);
if (playerMark == 'X') {
computerMark = 'O';
} else {
computerMark = 'X';
}
while (true) {
System.out.println("Enter your placement (1-9):");
String userInput = scan.nextLine();
if (isValidMove(userInput)) {
placeMark(userInput, playerMark);
printBoard();
if (checkWin()) {
System.out.println("Player wins!");
break;
}
if (isBoardFull()) {
System.out.println("It's a tie!");
break;
}
computerTurn();
printBoard();
if (checkWin()) {
System.out.println("Computer wins!");
break;
}
if (isBoardFull()) {
System.out.println("It's a tie!");
break;
}
} else {
System.out.println("Invalid position. Try again.");
}
}
}
}
TicTacToeGame game = new TicTacToeGame();
game.playGame();
Welcome to Akshat's Tic Tac Toe!
To play, enter any number from 1-9 to indicate your move!
- - -
- - -
- - -
Choose your mark: X or O
Enter your placement (1-9):
- X -
- - -
- - -
Computer chose 4
- X -
O - -
- - -
Enter your placement (1-9):
X X -
O - -
- - -
Computer chose 6
X X -
O - O
- - -
Enter your placement (1-9):
X X X
O - O
- - -
Player wins!