Java实现三子棋

TicTacToe.java

import java.util.Scanner;

public class TicTacToe {
    private static final int BOARD_SIZE = 3;
    private static final char EMPTY_CELL = '-';
    private static final char PLAYER_1_SYMBOL = 'X';
    private static final char PLAYER_2_SYMBOL = 'O';

    private char[][] board;
    private char currentPlayerSymbol;

    public TicTacToe() {
        board = new char[BOARD_SIZE][BOARD_SIZE];
        currentPlayerSymbol = PLAYER_1_SYMBOL;
        initializeBoard();
    }

    private void initializeBoard() {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                board[i][j] = EMPTY_CELL;
            }
        }
    }

    public void play() {
        Scanner scanner = new Scanner(System.in);
        boolean gameOver = false;
        int movesCount = 0;

        while (!gameOver) {
            displayBoard();

            int row, col;
            do {
                System.out.printf("Player %c's turn. Enter row (1-%d) and column (1-%d) separated by space: ",
                        currentPlayerSymbol, BOARD_SIZE, BOARD_SIZE);
                row = scanner.nextInt() - 1;
                col = scanner.nextInt() - 1;
            } while (!isValidMove(row, col));

            board[row][col] = currentPlayerSymbol;
            movesCount++;

            if (isWinningMove(row, col)) {
                displayBoard();
                System.out.printf("Player %c wins!\n", currentPlayerSymbol);
                gameOver = true;
            } else if (movesCount == BOARD_SIZE * BOARD_SIZE) {
                displayBoard();
                System.out.println("Game over. It's a draw.");
                gameOver = true;
            } else {
                currentPlayerSymbol = (currentPlayerSymbol == PLAYER_1_SYMBOL) ? PLAYER_2_SYMBOL : PLAYER_1_SYMBOL;
            }
        }
    }

    private boolean isValidMove(int row, int col) {
        if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
            System.out.printf("Invalid move. Row and column must be between 1 and %d.\n", BOARD_SIZE);
            return false;
        } else if (board[row][col] != EMPTY_CELL) {
            System.out.println("Invalid move. Cell is already occupied.");
            return false;
        } else {
            return true;
        }
    }

    private boolean isWinningMove(int row, int col) {
        // Check row
        boolean rowWin = true;
        for (int j = 0; j < BOARD_SIZE; j++) {
            if (board[row][j] != currentPlayerSymbol) {
                rowWin = false;
                break;
            }
        }
        if (rowWin) {
            return true;
        }

        // Check column
        boolean colWin = true;
        for (int i = 0; i < BOARD_SIZE; i++) {
            if (board[i][col] != currentPlayerSymbol) {
                colWin = false;
                break;
            }
        }
        if (colWin) {
            return true;
        }

        // Check diagonal
        boolean diagonalWin1 = true;
        boolean diagonalWin2 = true;
        for (int i = 0; i < BOARD_SIZE; i++) {
            if (board[i][i] != currentPlayerSymbol) {
                diagonalWin1 = false;
            }
            if (board[i][BOARD_SIZE - 1 - i] != currentPlayerSymbol) {
                diagonalWin2 = false;
            }
        }
        if (diagonalWin1 || diagonalWin2) {
            return true;
        }

        return false;
    }

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

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

运行效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值