Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.1 kB
3
Indexable
Never
import java.util.*;

public class TicTacToe {
    private char[][] board;
    private char currentPlayer;
    private Map<String, Integer> leaderboard;

    public TicTacToe() {
        board = new char[3][3];
        currentPlayer = 'X';
        leaderboard = new HashMap<>();
    }

    public void playGame() {
        initializeBoard();
        boolean gameFinished = false;

        while (!gameFinished) {
            printBoard();
            int[] move = getPlayerMove();

            if (isValidMove(move[0], move[1])) {
                makeMove(move[0], move[1]);

                if (isWinningMove()) {
                    gameFinished = true;
                    System.out.println("Player " + currentPlayer + " wins!");
                    updateLeaderboard();
                } else if (isBoardFull()) {
                    gameFinished = true;
                    System.out.println("It's a draw!");
                } else {
                    switchPlayer();
                }
            } else {
                System.out.println("Invalid move. Please try again.");
            }
        }

        printLeaderboard();
    }

    private void initializeBoard() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                board[i][j] = '-';
            }
        }
    }

    private void printBoard() {
        System.out.println("Current Board:");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }

    private int[] getPlayerMove() {
        Scanner scanner = new Scanner(System.in);
        int[] move = new int[2];

        System.out.println("Player " + currentPlayer + ", enter your move (row[0-2] column[0-2]):");
        move[0] = scanner.nextInt();
        move[1] = scanner.nextInt();

        return move;
    }

    private boolean isValidMove(int row, int col) {
        if (row < 0 || row >= 3 || col < 0 || col >= 3) {
            return false;
        }
        return board[row][col] == '-';
    }

    private void makeMove(int row, int col) {
        board[row][col] = currentPlayer;
    }

    private boolean isWinningMove() {
        return checkRows() || checkColumns() || checkDiagonals();
    }

    private boolean checkRows() {
        for (int i = 0; i < 3; i++) {
            if (board[i][0] != '-' && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
                return true;
            }
        }
        return false;
    }

    private boolean checkColumns() {
        for (int i = 0; i < 3; i++) {
            if (board[0][i] != '-' && board[0][i] == board[1][i] && board[1][i] == board[2][i]) {
                return true;
            }
        }
        return false;
    }

    private boolean checkDiagonals() {
        if (board[0][0] != '-' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
            return true;
        }
        if (board[0][2] != '-' && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
            return true;
        }
        return false;
    }

    private boolean isBoardFull() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j] == '-') {
                    return false;
                }
            }
        }
        return true;
    }

    private void switchPlayer() {
        currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
    }

    private void updateLeaderboard() {
        String playerName = "Player " + currentPlayer;
        int playerScore = leaderboard.getOrDefault(playerName, 0);
        leaderboard.put(playerName, playerScore + 1);
    }

    private void printLeaderboard() {
        System.out.println("Leaderboard:");
        for (String player : leaderboard.keySet()) {
            System.out.println(player + ": " + leaderboard.get(player));
        }
    }

    public static void main(String[] args) {
        TicTacToe game = new TicTacToe();
        game.playGame();
    }
}