以下是一个简单的 Java 井字棋游戏:

import java.util.Scanner;

public class TicTacToe {
    private char[][] board;
    private char currentPlayerMark;

    public TicTacToe() {
        board = new char[3][3];
        currentPlayerMark = 'X';
        initializeBoard();
    }

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

    public void printBoard() {
        System.out.println("-------------\n");
        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("-------------\n");
        }
    }

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

    public boolean checkForWin() {
        return (checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin());
    }

    private boolean checkRowsForWin() {
        for (int i = 0; i < 3; i++) {
            if (checkRowCol(board[i][0], board[i][1], board[i][2])) {
                return true;
            }
        }
        return false;
    }

    private boolean checkColumnsForWin() {
        for (int i = 0; i < 3; i++) {
            if (checkRowCol(board[0][i], board[1][i], board[2][i])) {
                return true;
            }
        }
        return false;
    }

    private boolean checkDiagonalsForWin() {
        return (checkRowCol(board[0][0], board[1][1], board[2][2]) || checkRowCol(board[0][2], board[1][1], board[2][0]));
    }

    private boolean checkRowCol(char c1, char c2, char c3) {
        return ((c1 != '-') && (c1 == c2) && (c2 == c3));
    }

    public void changePlayer() {
        if (currentPlayerMark == 'X') {
            currentPlayerMark = 'O';
        } else {
            currentPlayerMark = 'X';
        }
    }

    public boolean placeMark(int row, int col) {
        if ((row >= 0) && (row < 3)) {
            if ((col >= 0) && (col < 3)) {
                if (board[row][col] == '-') {
                    board[row][col] = currentPlayerMark;
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        TicTacToe game = new TicTacToe();
        Scanner scanner = new Scanner(System.in);
        System.out.println("Tic Tac Toe");
        System.out.println("Enter row and column numbers to place the mark (e.g. 1 2):");
        while (!game.isBoardFull() && !game.checkForWin()) {
            game.printBoard();
            int row = scanner.nextInt() - 1;
            int col = scanner.nextInt() - 1;
            if (game.placeMark(row, col)) {
                game.changePlayer();
            } else {
                System.out.println("Invalid input. Try again.");
            }
        }
        scanner.close();
        game.printBoard();
        if (game.checkForWin()) {
            game.changePlayer();
            System.out.println(game.currentPlayerMark + " wins!");
        } else {
            System.out.println("It's a tie.");
        }
    }
}

该程序使用一个 board 数组来代表井字棋盘,'X' 和 'O' 分别代表两个玩家。initializeBoard() 方法用于初始化井字棋盘,printBoard() 方法用于打印井字棋盘。isBoardFull() 方法用于检查井字棋盘是否已满,checkForWin() 方法用于检查是否有玩家胜利。changePlayer() 方法用于切换当前玩家,placeMark(int row, int col) 方法用于在指定位置放置标记。main() 方法用于驱动游戏的整个过程。

Java 井字棋游戏代码示例:实现规则和玩法

原文地址: https://www.cveoy.top/t/topic/nO65 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录