我用Java开发了一个五子棋小游戏

java开发一个五子棋小游戏代码

import java.util.Scanner;

public class Gomoku {
    private static final int BOARD_SIZE = 15;
    private static final char EMPTY = '.';
    private static final char PLAYER1 = 'X';
    private static final char PLAYER2 = 'O';

    public static void main(String[] args) {
        char[][] board = new char[BOARD_SIZE][BOARD_SIZE];
        initBoard(board);
        printBoard(board);

        Scanner scanner = new Scanner(System.in);
        boolean player1Turn = true;
        while (true) {
            System.out.println("轮到" + (player1Turn ? "玩家1" : "玩家2") + "下棋,请输入行和列(0-14):");
            int row = scanner.nextInt();
            int col = scanner.nextInt();

            if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE || board[row][col] != EMPTY) {
                System.out.println("无效的坐标,请重新输入!");
                continue;
            }

            board[row][col] = player1Turn ? PLAYER1 : PLAYER2;
            printBoard(board);

            if (checkWin(board, row, col)) {
                System.out.println("恭喜" + (player1Turn ? "玩家1" : "玩家2") + "获胜!");
                break;
            }

            player1Turn = !player1Turn;
        }
        scanner.close();
    }

    private static void initBoard(char[][] board) {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                board[i][j] = EMPTY;
            }
        }
    }

    private static void printBoard(char[][] 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();
        }
    }

    private static boolean checkWin(char[][] board, int row, int col) {
        return checkDirection(board, row, col, 1, 0) || checkDirection(board, row, col, 0, 1) ||
               checkDirection(board, row, col, 1, 1) || checkDirection(board, row, col, 1, -1);
    }

    private static boolean checkDirection(char[][] board, int row, int col, int dRow, int dCol) {
        char player = board[row][col];
        if (player == EMPTY) {
            return false;
        }

        int count = 1;
        for (int i = 1; i < 5; i++) {
            int newRow = row + i * dRow;
            int newCol = col + i * dCol;
            if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE || board[newRow][newCol] != player) {
                break;
            }
            count++;
        }

        for (int i = 1; i < 5; i++) {
            int newRow = row - i * dRow;
            int newCol = col - i * dCol;
            if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE || board[newRow][newCol] != player) {
                break;
            }
            count++;
        }

        return count >= 5;
    }
}

这个代码实现了一个简单的五子棋游戏,玩家可以在控制台输入行和列坐标来下棋。游戏会检查是否有一方获胜,如果有一方获胜,则输出获胜方并结束游戏。

你可能感兴趣的:(java服务端,java,开发语言)