龙空技术网

上班摸鱼,java实现最简单五子棋

伊伊动漫 93

前言:

现在咱们对“java五子棋课程设计ppt”大致比较注意,你们都想要学习一些“java五子棋课程设计ppt”的相关内容。那么小编也在网上汇集了一些关于“java五子棋课程设计ppt””的相关知识,希望各位老铁们能喜欢,同学们快快来了解一下吧!

上班无聊,来摸鱼;

一个JAVA类,实现最简单的单机五子棋游戏;

上代码:

package store.tbfl;/** * @version 1.0.0 * @ClassName FiveInARowGame.java * @Description TODO * @createTime 2023年04月14日 15:27:00 */import java.util.Scanner;public class FiveInARowGame {    private char[][] board; // 棋盘    private char currentPlayer; // 当前玩家    private boolean gameover; // 游戏是否结束    private int boardSize; // 棋盘大小    private int targetNum; // 连成一线所需棋子数    public FiveInARowGame(int boardSize, int targetNum) {        this.boardSize = boardSize;        this.targetNum = targetNum;        board = new char[boardSize][boardSize];        currentPlayer = 'X';        gameover = false;        initBoard();    }    // 初始化棋盘    private void initBoard() {        for (int i = 0; i < boardSize; i++) {            for (int j = 0; j < boardSize; j++) {                board[i][j] = ' ';            }        }    }    // 绘制棋盘    private void drawBoard() {        System.out.println("五子棋游戏");        System.out.print(" ");        for (int i = 0; i < boardSize; i++) {            System.out.print(" " + (i + 1));        }        System.out.println();        for (int i = 0; i < boardSize; i++) {            System.out.print((i + 1) + " ");            for (int j = 0; j < boardSize; j++) {                System.out.print(board[i][j] + " ");            }            System.out.println();        }    }    // 切换玩家    private void switchPlayer() {        if (currentPlayer == 'X') {            currentPlayer = 'O';        } else {            currentPlayer = 'X';        }    }    // 下棋    private void makeMove(int row, int col) {        if (board[row][col] == ' ') {            board[row][col] = currentPlayer;            switchPlayer();        } else {            System.out.println("该位置已经有棋子,请重新选择!");        }    }    // 检查是否连成一线    private boolean checkWin(int row, int col) {        char target = board[row][col];        int count = 1; // 当前位置已经有一个棋子        // 检查水平方向        for (int i = col - 1; i >= 0; i--) {            if (board[row][i] == target) {                count++;            } else {                break;            }        }        for (int i = col + 1; i < boardSize; i++) {            if (board[row][i] == target) {                count++;            } else {                break;            }        }        if (count >= targetNum) {            return true;        }        // 检查垂直方向        count = 1;        for (int i = row - 1; i >= 0; i--) {            if (board[i][col] == target) {                count++;            } else {                break;            }        }        for (int i = row + 1; i < boardSize; i++) {            if (board[i][col] == target) {                count++;            } else {                break;            }        }        if (count >= targetNum) {            return true;        }        // 检查主对角线方向        count = 1;        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {            if (board[i][j] == target) {                count++;            } else {                break;            }        }        for (int i = row + 1, j = col + 1; i < boardSize && j < boardSize; i++, j++) {            if (board[i][j] == target) {                count++;            } else {                break;            }        }        if (count >= targetNum) {            return true;        }        // 检查副对角线方向        count = 1;        for (int i = row - 1, j = col + 1; i >= 0 && j < boardSize; i--, j++) {            if (board[i][j] == target) {                count++;            } else {                break;            }        }        for (int i = row + 1, j = col - 1; i < boardSize && j >= 0; i++, j--) {            if (board[i][j] == target) {                count++;            } else {                break;            }        }        if (count >= targetNum) {            return true;        }        return false;    }    // 检查是否平局    private boolean checkDraw() {        for (int i = 0; i < boardSize; i++) {            for (int j = 0; j < boardSize; j++) {                if (board[i][j] == ' ') {                    return false;                }            }        }        return true;    }    // 游戏主循环    public void play() {        Scanner scanner = new Scanner(System.in);        while (!gameover) {            drawBoard();            System.out.println("当前玩家:" + currentPlayer);            System.out.print("请输入要落子的行号(1-" + boardSize + "):");            int row = scanner.nextInt() - 1;            System.out.print("请输入要落子的列号(1-" + boardSize + "):");            int col = scanner.nextInt() - 1;            if (row >= 0 && row < boardSize && col >= 0 && col < boardSize) {                makeMove(row, col);                if (checkWin(row, col)) {                    drawBoard();                    System.out.println("玩家 " + currentPlayer + " 胜利!");                    gameover = true;                } else if (checkDraw()) {                    drawBoard();                    System.out.println("平局!");                    gameover = true;                }            } else {                System.out.println("输入无效,请重新输入!");            }        }        scanner.close();    }    public static void main(String[] args) {        FiveInARowGame game = new FiveInARowGame(15, 5); // 创建一个15x15的五子棋游戏,目标是连成5子        game.play(); // 开始游戏    }}

直接运行main即可;

已经玩得不亦乐乎了;哈哈

可举一反三,用图片绘制棋盘,再加上Socket链接,就是网络五子棋了~

标签: #java五子棋课程设计ppt