Java版-五子棋(二维数组)

import java.util.Scanner;

public class Gobang {
    public static int length = 15;
    public static String[][] chessBoard = new String[length][length];

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String black = "★";
        String white = "☆";
        String grid = "╋";
        String[] grade = { "⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖" };
        // 1.创建棋盘
        for (int i = 0; i < chessBoard.length; i++) {
            for (int j = 0; j < chessBoard.length; j++) {
                chessBoard[i][j] = grid;
                if (j == (chessBoard.length - 1)) {
                    chessBoard[i][j] = grade[i];
                }
                if (i == (chessBoard.length - 1)) {
                    chessBoard[i][j] = grade[j];
                }
                System.out.print(chessBoard[i][j]);
            }
            System.out.println();
        }
        // 下子
        boolean flage = true;
        boolean bool = true;
        while (flage) {
            System.out.println("请" + (bool ? "黑" : "白") + "子下子");
            String seed = scanner.next();
            String[] split = seed.split(",");
            int x = Integer.parseInt(split[0]) - 1;
            int y = Integer.parseInt(split[1]) - 1;
            // 判断棋子的有效性
            if (x < 0 || y < 0 || x > 13 || y > 13) {
                System.out.println("超出棋盘区域,请重新下子");
            } else {
                if (!chessBoard[x][y].equals(grid)) {
                    System.out.println("此处有子,请重新下子:");

                } else {
                    if (bool) {
                        chessBoard[x][y] = black;
                        if (isWin(x, y, black)) {
                            System.out.println("黑棋获胜");
                            flage = false;
                        }
                    } else {
                        chessBoard[x][y] = white;
                        if (isWin(x, y, white)) {
                            System.out.println("白棋获胜");
                            flage = false;
                        }
                    }
                    bool = !bool;
                }
            }
            for (String[] strings : chessBoard) {
                for (String string : strings) {
                    System.out.print(string);
                }
                System.out.println();
            }
        }
    }

    // 判断输赢的方法
    public static boolean isWin(int x, int y, String color) {
        // 横向
        for (int i = 0; i < chessBoard.length - 5; i++) {
            if (chessBoard[x][i].equals(color) && chessBoard[x][i + 1].equals(color)
                    && chessBoard[x][i + 2].equals(color) && chessBoard[x][i + 3].equals(color)
                    && chessBoard[x][i + 4].equals(color)) {
                return true;
            }
        }
        // 竖向
        for (int i = 0; i < chessBoard.length - 5; i++) {
            if (chessBoard[i][y].equals(color) && chessBoard[i + 1][y].equals(color)
                    && chessBoard[i + 2][y].equals(color) && chessBoard[i + 3][y].equals(color)
                    && chessBoard[i + 4][y].equals(color)) {
                return true;
            }
        }
        // 斜向下
        if (x >= y) {
            int j = 0;
            for (int i = x - y; i < chessBoard.length - 5; i++) {
                if (chessBoard[i][j].equals(color) && chessBoard[i + 1][j + 1].equals(color)
                        && chessBoard[i + 2][j + 2].equals(color) && chessBoard[i + 3][j + 3].equals(color)
                        && chessBoard[i + 4][j + 4].equals(color)) {
                    return true;
                }
                j++;
            }
        } else {
            int i = 0;
            for (int j = y - x; j < chessBoard.length - 5; j++) {
                if (chessBoard[i][j].equals(color) && chessBoard[i + 1][j + 1].equals(color)
                        && chessBoard[i + 2][j + 2].equals(color) && chessBoard[i + 3][j + 3].equals(color)
                        && chessBoard[i + 4][j + 4].equals(color)) {
                    return true;
                }
                i++;
            }
        }
        // 斜向上
        int i = 0;
        for (int k = x + y - 2; k >= 0; k--) {
            if (chessBoard[i][k].equals(color) && chessBoard[i + 1][k - 1].equals(color)
                    && chessBoard[i + 2][k - 2].equals(color) && chessBoard[i + 3][k - 3].equals(color)
                    && chessBoard[i + 4][k - 4].equals(color)) {
                return true;
            }
            i++;
        }
        return false;
    }
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值