java 控制台版五子棋

java 控制台版五子棋

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.PatternSyntaxException;

public class ConsoleGobang {
    public static final int BOARD_SIZE = 16;  // 棋盘大小,包含上指示器和左指示器
    public static String[][] stringArr;  // 存放棋盘
    public static Random random = new Random();  // 生成电脑下棋的随机数
    public static Scanner scanner = new Scanner(System.in); // 用于扫描用户输入
    public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 获取用户输入
    public static int userX = 0;  // 用于计算水平方向用户
    public static int compX = 0;  // 用于计算水平方向电脑
    public static int userY = 0;  // 用于计算垂直方向用户
    public static int compY = 0;  // 用于计算垂直方向电脑
    public static int userXN = 0; // 用于计算西南方向用户
    public static int compXN = 0; // 用于计算西南方向电脑
    public static int userDB = 0; // 用于计算东北方向用户
    public static int compDB = 0; // 用于计算西东北向电脑


    public static void main(String[] args) throws IOException {
        System.out.println("开始五子棋游戏!");
        initCheckerBoard();// 初始化棋盘
        printCheckerBoard();// 打印棋盘
        // 谁先下棋
        System.out.println("输入1玩家线下,输入2电脑线下,其他选项退出游戏:");
        int i = scanner.nextInt();
        if (i == 1) {  // 玩家先下
            while (true) {
                System.out.println("请输入下棋的坐标,格式为[x,y]:");
                // 人下棋
                String line;
                while ((line = br.readLine()) != null) {
                    try {
                        String[] split = line.split(","); // 分割用户输入的坐标
                        int x = Integer.parseInt(split[0]);
                        int y = Integer.parseInt(split[1]);
                        if (x > BOARD_SIZE || x < 1 || y > BOARD_SIZE || y < 1) {  // 判断用户输入的坐标
                            System.out.println("棋盘上没有位置[" + x + "," + y + "],请输入[1~15]之间的数字:");
                        } else {
                            System.out.println("玩家下棋坐标[x=" + x + ",y=" + y + "]");
                            // 如果用户输入的坐标上有棋子则提示重新输入坐标
                            if (isHavePiece(x, y)) {
                                System.out.println("棋盘上该位置[" + x + "," + y + "]已经有棋子,请输入下棋的坐标,格式为[x,y]:");
                            } else {
                                stringArr[x][y] = "●";
                                break;
                            }
                        }
                    } catch (PatternSyntaxException | NumberFormatException e) {
                        System.out.println("输入坐标格式有误,请输入下棋的坐标,格式为[x,y]:");
                    }
                }
                // 电脑下棋
                computer();
                // 打印棋盘
                printCheckerBoard();

                if (isSuccess()) {  // 判断输赢
                    System.out.println("游戏结束");
                    return;
                }
            }

        } else if (i == 2) {  // 电脑线下
            while (true) {
                // 电脑下棋
                computer();
                printCheckerBoard(); // 打印棋盘

                // 人下棋
                System.out.println("请输入下棋的坐标,格式为[x,y]:");
                String line;
                while ((line = br.readLine()) != null) {
                    try {
                        String[] split = line.split(","); // 分割用户输入的坐标
                        int x = Integer.parseInt(split[0]);
                        int y = Integer.parseInt(split[1]);
                        if (x > BOARD_SIZE || x < 1 || y > BOARD_SIZE || y < 1) {  // 判断用户输入的坐标
                            System.out.println("棋盘上没有位置[" + x + "," + y + "],请输入[1~15]之间的数字:");
                        } else {
                            System.out.println("玩家下棋坐标[x=" + x + ",y=" + y + "]");
                            // 如果用户输入的坐标上有棋子则提示重新输入坐标
                            if (isHavePiece(x, y)) {
                                System.out.println("棋盘上该位置[" + x + "," + y + "]已经有棋子,请输入下棋的坐标,格式为[x,y]:");
                            } else {
                                stringArr[x][y] = "●";
                                break;
                            }
                        }
                    } catch (PatternSyntaxException | NumberFormatException e) {
                        System.out.println("输入坐标格式有误,请输入下棋的坐标,格式为[x,y]:");
                    }
                }

                printCheckerBoard(); // 打印棋盘
                // 判断输赢
                if (isSuccess()) {
                    System.out.println("游戏结束");
                    return;
                }
            }

        } else {
            System.out.println("退出");
        }

    }

    /**
     * 初始化创建18*18的棋盘
     */
    public static void initCheckerBoard() {
        stringArr = new String[BOARD_SIZE][BOARD_SIZE];
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                // 绘制棋盘边界的指示器
                if (i == 0) {
                    stringArr[i][j] = String.valueOf(j);
                } else if (j == 0) {
                    stringArr[i][j] = String.valueOf(i);
                } else {  // 绘制棋盘的格子
                    stringArr[i][j] = "✚";
                }
            }
        }
    }

    /**
     * 打印棋盘
     */
    public static void printCheckerBoard() {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                System.out.print(stringArr[i][j] + "\t");
            }
            System.out.println();
        }
    }

    /**
     * 电脑下棋
     */
    public static void computer() {
        // random.nextInt(BOARD_SIZE+1)/*0,BOARD_SIZE之间的随机整数*/;
        int i = random.nextInt(BOARD_SIZE - 1) + 1;//范围1-BOARD_SIZE减1
        int j = random.nextInt(BOARD_SIZE - 1) + 1;//范围1-BOARD_SIZE减1

        if (isHavePiece(i, j)) { // 递归
            // 有棋子
            computer();
            return;
        } else {
            System.out.println("电脑下棋坐标[x=" + i + ",y=" + j + "]");
            stringArr[i][j] = "○";
        }


    }
    
    /**
     * 判断棋盘上某个位置是否有棋子
     *
     * @param x 电脑或者用户输入的坐标x
     * @param y 电脑或者用户输入的坐标y
     * @return 如果坐标上有棋子则返回true,没有返回false
     */
    public static boolean isHavePiece(int x, int y) {
        return stringArr[x][y].equals("●") || stringArr[x][y].equals("○");
    }


    /**
     * 判断输赢
     *
     * @return 返回true则代表胜利,false则代表失败
     */
    public static boolean isSuccess() {

        // 一、横向x判断
        for (int i = 0; i < stringArr.length; i++) {
            for (int j = 0; j < stringArr[i].length; j++) {
                if (stringArr[i][j].equals("●")) {  // 循环统计连续出现则让计数器自加1
                    userX++;
                } else {
                    userX = 0;  // 如果不连续则让计数器归零
                }
                if (stringArr[i][j].equals("○")) {
                    compX++;
                } else {
                    compX = 0;
                }
                // 如果横向大于5返回true
                if (compX >= 5 || userX >= 5) return true;
            }
        }


        // 二、纵向y判断
        for (int i = 0; i < stringArr.length; i++) {
            for (int j = 0; j < stringArr[i].length; j++) {
                if (stringArr[j][i].equals("●")) {
                    userY++;
                } else {
                    userY = 0;
                }
                if (stringArr[j][i].equals("○")) {
                    compY++;

                } else {
                    compY = 0;
                }
                // 判断纵向是否为5
                if (userY >= 5 || compY >= 5) return true;
            }
        }


        // 三、西南向判断
        // 右斜↗(西南)
        // 对角线左上方
        for (int k = 0; k < BOARD_SIZE - 1; k++) {
            for (int i = 0, j = k; i < BOARD_SIZE - 1 && j >= 0; i++, j--) {
                if (stringArr[i][j].equals("●")) {
                    userXN++;
                } else {
                    userXN = 0;
                }
                if (stringArr[i][j].equals("○")) {
                    compXN++;
                } else {
                    compXN = 0;
                }

                // 判断和是否为5
                if (userXN >= 5 || compXN >= 5) return true;
            }

        }
        // 对角线↗
        for (int i = 0, j = BOARD_SIZE - 1; i < BOARD_SIZE - 1 && j >= 0; i++, j--) {
            if (stringArr[i][j].equals("●")) {
                userXN++;
            } else {
                userXN = 0;
            }
            if (stringArr[i][j].equals("○")) {
                compXN++;
            } else {
                compXN = 0;
            }

            // 判断和是否为5
            if (userXN >= 5 || compXN >= 5) return true;

        }

        // 对角线右下方
        for (int k = 1; k < BOARD_SIZE; k++) {
            for (int i = k, j = BOARD_SIZE - 1; i < BOARD_SIZE && j >= 0; i++, j--) {
                if (stringArr[i][j].equals("●")) {
                    userXN++;
                } else {
                    userXN = 0;
                }
                if (stringArr[i][j].equals("○")) {
                    compXN++;
                } else {
                    compXN = 0;
                }

                // 判断和是否为5
                if (userXN >= 5 || compXN >= 5) return true;
            }
        }


        // 四、东北方向
        // 左斜方↖
        // 对角线右上方
        for (int k = 1; k < BOARD_SIZE; k++) {
            for (int i = 0, j = k; i < BOARD_SIZE - 1 && j < BOARD_SIZE; i++, j++) {
                if (stringArr[i][j].equals("●")) {
                    userDB++;
                } else {
                    userDB = 0;
                }
                if (stringArr[i][j].equals("○")) {
                    compDB++;
                } else {
                    compDB = 0;
                }

                // 判断和是否为5
                if (userDB >= 5 || compDB >= 5) return true;
            }

        }
        // 对角线↖
        for (int i = 0, j = 0; i < BOARD_SIZE - 1 && j < BOARD_SIZE; i++, j++) {
            if (stringArr[i][j].equals("●")) {
                userDB++;
            } else {
                userDB = 0;
            }
            if (stringArr[i][j].equals("○")) {
                compDB++;
            } else {
                compDB = 0;
            }

            // 判断和是否为5
            if (userDB >= 5 || compDB >= 5) return true;
        }


        // 对角线左下方
        for (int k = 1; k < BOARD_SIZE; k++) {
            for (int i = k, j = 0; i < BOARD_SIZE && j < BOARD_SIZE; i++, j++) {
                if (stringArr[i][j].equals("●")) {
                    userDB++;
                } else {
                    userDB = 0;
                }
                if (stringArr[i][j].equals("○")) {
                    compDB++;
                } else {
                    compDB = 0;
                }

                // 判断和是否为5
                if (userDB >= 5 || compDB >= 5) return true;
            }

        }
        return false;
    }
}

效果展示

在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值