【Java】使用AI助手实现一个五子棋,手写代码0

发布时间:2023年12月25日

使用Java实现一个五子棋游戏

import java.util.Scanner;

public class FiveChessGame {
    private char[][] board;
    private char currentPlayer;
    
    public FiveChessGame() {
        board = new char[15][15];
        currentPlayer = 'X';
    }
    
    public void startGame() {
        boolean gameEnded = false;
        while (!gameEnded) {
            drawBoard();
            System.out.println("当前玩家:" + currentPlayer);
            int[] move = getPlayerMove();
            int row = move[0];
            int col = move[1];
            board[row][col] = currentPlayer;
            
            if (isWin(row, col)) {
                drawBoard();
                System.out.println("玩家 " + currentPlayer + " 获胜!");
                gameEnded = true;
            }
            
            if (isBoardFull()) {
                drawBoard();
                System.out.println("平局!");
                gameEnded = true;
            }
            
            currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
        }
    }
    
    public void drawBoard() {
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }
    
    public int[] getPlayerMove() {
        Scanner scanner = new Scanner(System.in);
        int[] move = new int[2];
        System.out.print("请输入行号(0-14):");
        move[0] = scanner.nextInt();
        System.out.print("请输入列号(0-14):");
        move[1] = scanner.nextInt();
        return move;
    }
    
    public boolean isWin(int row, int col) {
        // 检查行
        int count = 1;
        for (int i = col - 1; i >= 0 && board[row][i] == currentPlayer; i--) {
            count++;
        }
        for (int i = col + 1; i < 15 && board[row][i] == currentPlayer; i++) {
            count++;
        }
        if (count >= 5) {
            return true;
        }
        
        // 检查列
        count = 1;
        for (int i = row - 1; i >= 0 && board[i][col] == currentPlayer; i--) {
            count++;
        }
        for (int i = row + 1; i < 15 && board[i][col] == currentPlayer; i++) {
            count++;
        }
        if (count >= 5) {
            return true;
        }
        
        // 检查斜线
        count = 1;
        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0 && board[i][j] == currentPlayer; i--, j--) {
            count++;
        }
        for (int i = row + 1, j = col + 1; i < 15 && j < 15 && board[i][j] == currentPlayer; i++, j++) {
            count++;
        }
        if (count >= 5) {
            return true;
        }
        
        // 检查反斜线
        count = 1;
        for (int i = row - 1, j = col + 1; i >= 0 && j < 15 && board[i][j] == currentPlayer; i--, j++) {
            count++;
        }
        for (int i = row + 1, j = col - 1; i < 15 && j >= 0 && board[i][j] == currentPlayer; i++, j--) {
            count++;
        }
        if (count >= 5) {
            return true;
        }
        
        return false;
    }
    
    public boolean isBoardFull() {
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                if (board[i][j] == '\u0000') {
                    return false;
                }
            }
        }
        return true;
    }
    
    public static void main(String[] args) {
        FiveChessGame game = new FiveChessGame();
        game.startGame();
    }
}

在上述代码中,创建了一个名为FiveChessGame的类来表示五子棋游戏。游戏的棋盘通过一个二维字符数组来表示,'X’代表玩家1,'O’代表玩家2。游戏通过循环进行,每次循环中,先绘制棋盘,然后让当前玩家输入移动的位置,然后判断是否有玩家获胜或者棋盘已满的情况,若满足条件则结束游戏。游戏的终止条件通过isWin和isBoardFull方法判断。
文章来源:https://blog.csdn.net/like_start/article/details/135204004
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。