Header Ads

[OOP] Game Tic Tac Toe


Nguồn: Click here

Let's create a game called "Tic Tac Toe". In this game, two players will take turns marking a 3x3 grid with their symbols (X or O), and the player who manages to get three of their symbols in a row, column, or diagonal wins the game.

To implement this game in OOP Java, we can create four classes:

GameBoard: This class will manage the game board and store the current state of the game.
Player: This class will represent the player and their actions.
Game: This class will manage the game flow and user input.
Main: This class will contain the main method and start the game.

1. GameBoard class

public class GameBoard {
    private char[][] board;
    private int rows;
    private int columns;

    public GameBoard(int rows, int columns) {
        this.rows = rows;
        this.columns = columns;
        board = new char[rows][columns];
        reset();
    }

    public void reset() {
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                board[i][j] = ' ';
            }
        }
    }

    public boolean isValidMove(int row, int column) {
        return row >= 0 && row < rows && column >= 0 && column < columns && board[row][column] == ' ';
    }

    public void makeMove(int row, int column, char symbol) {
        board[row][column] = symbol;
    }

    public boolean isWinner(char symbol) {
        // Check rows
        for (int i = 0; i < rows; i++) {
            if (board[i][0] == symbol && board[i][1] == symbol && board[i][2] == symbol) {
                return true;
            }
        }

        // Check columns
        for (int j = 0; j < columns; j++) {
            if (board[0][j] == symbol && board[1][j] == symbol && board[2][j] == symbol) {
                return true;
            }
        }

        // Check diagonals
        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 < rows; i++) {
            for (int j = 0; j < columns; j++) {
                if (board[i][j] == ' ') {
                    return false;
                }
            }
        }

        return true;
    }

    public void print() {
        System.out.println("-------------");

        for (int i = 0; i < rows; i++) {
            System.out.print("| ");

            for (int j = 0; j < columns; j++) {
                System.out.print(board[i][j] + " | ");
            }

            System.out.println("\n-------------");
        }
    }
}

2. Player class

import java.util.Scanner;

public class Player {
    private String name;
    private char symbol;
    

    public Player(String name, char symbol) {
        this.name = name;
        this.symbol = symbol;
        
    }

    public String getName() {
        return name;
    }

    public char getSymbol() {
        return symbol;
    }

    public int[] getMove() {
        int row, col;
        System.out.println(name + ", please enter your move (row column): ");
        Scanner scanner = new Scanner(System.in);
        row = scanner.nextInt();
        col = scanner.nextInt();
        return new int[] { row, col };
    }
}

3. Game class

public class Game {
    private Player player1;
    private Player player2;
    private GameBoard board;
    private int currentPlayerIndex;

    public Game(Player player1, Player player2, GameBoard board) {
        this.player1 = player1;
        this.player2 = player2;
        this.board = board;
        currentPlayerIndex = 0;
    }

    public void start() {
        System.out.println("Let's play Tic Tac Toe!");

        while (true) {
            Player currentPlayer = currentPlayerIndex == 0 ? player1 : player2;
            Player otherPlayer = currentPlayerIndex == 0 ? player2 : player1;

            board.print();

            int[] move = currentPlayer.getMove();

            while (!board.isValidMove(move[0], move[1])) {
                System.out.println("Invalid move. Please try again.");
                move = currentPlayer.getMove();
            }

            board.makeMove(move[0], move[1], currentPlayer.getSymbol());

            if (board.isWinner(currentPlayer.getSymbol())) {
                board.print();
                System.out.println(currentPlayer.getName() + " wins!");
                break;
            }

            if (board.isFull()) {
                board.print();
                System.out.println("The game is a tie.");
                break;
            }

            currentPlayerIndex = (currentPlayerIndex + 1) % 2;
        }
    }
}

4. Main class

public class Main {
    public static void main(String[] args) {
        GameBoard board = new GameBoard(8, 8); // create game board
        Player player1 = new Player("Player 1", 'X'); // create player 1
        Player player2 = new Player("Player 2", 'O'); // create player 2
        Game game = new Game(player1, player2, board); // create game instance
        game.start(); // start the game
    }
}



Không có nhận xét nào

Được tạo bởi Blogger.