Tic Tac Toe
Tic-Tac-Toe pseudocode for the game:
START
CREATE 3x3 array board filled with '-'
SET currentPlayer = 'X'
SET gameOver = false
WHILE gameOver is false
DISPLAY board
PRINT "Player " + currentPlayer + ", enter row and column (0-2):"
READ row, col
IF board[row][col] is '-'
board[row][col] = currentPlayer
ELSE
PRINT "Invalid move, try again"
CONTINUE loop
ENDIF
IF checkWin(board, currentPlayer) is true
DISPLAY board
PRINT "Player " + currentPlayer + " wins!"
SET gameOver = true
ELSE IF board is full
DISPLAY board
PRINT "It's a draw!"
SET gameOver = true
ELSE
SWITCH currentPlayer:
IF 'X' THEN 'O'
ELSE 'X'
END SWITCH
ENDIF
END WHILE
END
FUNCTION checkWin(board, player):
FOR each row in board
IF all 3 values in row equal player
RETURN true
END FOR
FOR each column in board
IF all 3 values in column equal player
RETURN true
END FOR
IF board[0][0] == player AND board[1][1] == player AND board[2][2] == player
RETURN true
IF board[0][2] == player AND board[1][1] == player AND board[2][0] == player
RETURN true
RETURN false
END FUNCTION
Tic-Tac-Toe Java code for the game:
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
char[][] board = {
{'-', '-', '-'},
{'-', '-', '-'},
{'-', '-', '-'}
};
char currentPlayer = 'X';
boolean gameOver = false;
Scanner scanner = new Scanner(System.in);
while (!gameOver) {
printBoard(board);
System.out.println("Player " + currentPlayer + ", enter row and column (0-2): ");
if (!scanner.hasNextInt()) {
System.out.println("Invalid input. Please enter numbers only.");
scanner.next(); // discard invalid token
continue;
}
int row = scanner.nextInt();
System.out.println("Player " + currentPlayer + ", enter row and column (0-2): ");
if (!scanner.hasNextInt()) {
System.out.println("Invalid input. Please enter numbers only.");
scanner.next(); // discard invalid token
continue;
}
int col = scanner.nextInt();
if (row < 0 || row > 2 || col < 0 || col > 2) {
System.out.println("Invalid position, try again.");
continue;
}
if (board[row][col] == '-') {
board[row][col] = currentPlayer;
} else {
System.out.println("Invalid move, try again.");
continue;
}
if (checkWin(board, currentPlayer)) {
printBoard(board);
System.out.println("Player " + currentPlayer + " wins!");
gameOver = true;
} else if (isBoardFull(board)) {
printBoard(board);
System.out.println("It's a draw!");
gameOver = true;
} else {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
}
scanner.close();
}
public static void printBoard(char[][] board) {
System.out.println("-------------");
for (int i = 0; i < 3; i++) {
System.out.print("| ");
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j] + " | ");
}
System.out.println();
System.out.println("-------------");
}
}
public static boolean checkWin(char[][] board, char player) {
// Check rows
for (int i = 0; i < 3; i++) {
if (board[i][0] == player &&
board[i][1] == player &&
board[i][2] == player) {
return true;
}
}
// Check columns
for (int j = 0; j < 3; j++) {
if (board[0][j] == player &&
board[1][j] == player &&
board[2][j] == player) {
return true;
}
}
// Check diagonals
if (board[0][0] == player &&
board[1][1] == player &&
board[2][2] == player) {
return true;
}
if (board[0][2] == player &&
board[1][1] == player &&
board[2][0] == player) {
return true;
}
return false;
}
public static boolean isBoardFull(char[][] board) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
return false;
}
}
}
return true;
}
}
Screenshots of running the application on the terminal:
Click here to get the code on Github:
Comments
Post a Comment