ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java 控制台版五子棋

2021-12-23 23:33:40  阅读:151  来源: 互联网

标签:java stringArr ++ 五子棋 else int BOARD 控制台 SIZE


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;
    }
}

效果展示

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

标签:java,stringArr,++,五子棋,else,int,BOARD,控制台,SIZE
来源: https://blog.csdn.net/u013186651/article/details/122118636

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有