Java基础版——五子棋游戏

本文详细介绍了如何使用Java编程实现一个简单的五子棋游戏,包括初始化棋盘、打印棋盘、玩家交替下棋、坐标合法性检查以及判断胜负的算法。
摘要由CSDN通过智能技术生成

目录

一、基本思路

1、提供一个启动五子棋游戏的方法

2、开始下棋,黑白棋交替下棋

3、判断坐标是否合法,是否重复

4、判断输赢

 二、总结


一、基本思路

1、提供一个启动五子棋游戏的方法

初始化棋盘
打印棋盘

 1、首先做好基本工具,创建一个fiveInRow 的类,使用static修饰的成员变量,构成静态成员变量,用来创建五子棋的棋盘。这里用 "☆" 表示白棋用 "★" 表示黑棋,并为每一行和列标上序号。

public class fiveInRow {
  
    static String white = "☆";
    static String black = "★";
    static String[][] qp = new String[15][15];
    static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
    static String line = "十";
}

 2、写一个方法来用来启动五子棋游戏,其中包含初始化棋盘和打印棋盘以及玩家界面。做好这些初始工作,我们就可以继续创建了。

//1、启动五子棋游戏 
public static void startGame() {
        init();//初始化棋盘
        print(qp);//打印棋盘
        play(qp);//玩家界面
    }

 3、初始化棋盘,将棋盘格子以及行坐标,列坐标通过for循环。对其数组进行初始化。

//1.1、初始化棋盘
    public static void init() {
        //棋盘格子
        for (int i = 0; i < qp.length; i++) {
            for (int j = 0; j < qp[i].length; j++) {
                qp[i][j] = line;
            }
        }
        //打印列坐标
        for (int i = 0; i < num.length; i++) {
            qp[i][qp[i].length - 1] = num[i];
        }
        //打印行坐标
        for (int i = 0; i < num.length; i++) {
            qp[qp[i].length - 1][i] = num[i];
        }
    }

 4、重新写一个方法用来打印棋盘。

//1.2、打印棋盘
    public static void print(String array[][]) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length; j++) {
                System.out.print(array[i][j]);
            }
            System.out.println("");
        }
    }

2、开始下棋,黑白棋交替下棋

进入玩家界面,创建一个scanner用来接收玩家下棋的位置。再创建一个boolean flag,用于黑白两棋交替下棋。但是,在这个过程当中我们要判断两个问题,第一个是判断位置是否重复,是否合法。第二个是判断输赢。

     //玩家界面
    public static void play(String array[][]) {
        System.out.println("欢迎来到五子棋游戏");
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        while (true) {
            if (flag) {
                System.out.println("黑子下棋" + "\n" + "输入行和列:");
                int row1 = scanner.nextInt() - 1;//行,索引要-1
                int col1 = scanner.nextInt() - 1;//列,索引要-1
                //判断是否重复,合法
                boolean result = check(row1, col1);
                if (result) {
                    array[row1][col1] = black;
                    print(qp);//打印棋盘
                    flag = false;
                    //判断输赢
                    if (isWin(row1, col1, black)) {
                        System.out.println("黑棋胜利!游戏结束");
                        return;
                    }
                }
            } else {
                System.out.println("白棋下棋" + "\n" + "输入行和列:");
                int row2 = scanner.nextInt() - 1;//
                int col2 = scanner.nextInt() - 1;
                //判断
                boolean result2 = check(row2, col2);
                if (result2) {
                    array[row2][col2] = white;
                    print(qp);//打印棋盘
                    flag = true;
                    //判断输赢
                    if (isWin(row2, col2, white)) {
                        System.out.println("白棋胜利!游戏结束");
                        return;
                    }
                }
            }
        }
    }

3、判断坐标是否合法,是否重复

 public static boolean check(int row, int col) {
        //检查坐标合法
        if (row < 0 || col < 0 || col > qp[0].length || row > qp.length) {
            System.out.println("超出五子棋坐标范围,请重新下棋!");
            return false;
        }
        //检查重复
        if (qp[row][col] != line) {
            System.out.println("该位置已有棋子,请重新下棋!");
            return false;
        }
        return true;
    }

4、判断输赢

判断输赢这里有点麻烦。每下一个棋子,就从二维数组中的第一个位置开始分别向左,右,上,下,斜向上,斜向下等进行查找,直到有满足五个相连即可以终止。
 即:以下落的棋子为中心,向四周扫描(水平判断输赢,垂直判断输赢,斜向判断输赢)

  public static boolean isWin(int row, int col, String qz) {
        //水平判断输赢
        //水平向左
        int input = 0;
        for (int left = col; left >= 0; left--) {
            if (qp[row][left] == qz) {//如果相同棋子
                input++;//计数,5个为满
            } else {
                break;
            }
        }
        if (input == 5) {
            return true;
        }
        //水平向右
        for (int right = col + 1; right < qp[0].length - 1; right++) {
            if (qp[row][right] == qz) {
                input++;//计数,5个为满
            } else {
                break;
            }
        }
        if (input == 5) {
            return true;
        } else
            input = 1;
        //垂直判断输赢
        //竖直向上
        for (int up = row-1; up >= 0; up--) {
            if (qp[up][col] == qz) {
                input++;//计数,5个为满
            } else {
                break;
            }
        }
        if (input == 5) {
            return true;
        }
        //竖直向下
        for (int down = row+1; down < qp.length-1; down++) {
            if (qp[down][col] == qz) {
                input++;//计数,5个为满
            } else {
                break;
            }
        }
        if (input == 5) {
            return true;
        }else
            input=1;
        //斜向判断输赢\
        //右下
        for (int i = row + 1, j = col + 1; i < qp.length-1 && j < qp[0].length-1; i++, j++) {
            if (qp[i][j] == qz) {
                input++;
            } else {
                break;
            }
        }
        if (input == 5) {
            return true;
        }
        //左上
        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
            if (qp[i][j] == qz) {
                input++;
            } else {
                break;
            }
        }
        if (input == 5) {
            return true;
        }else
            input=1;
        //斜向判断输赢/
        //左下
        for (int i = row + 1, j = col - 1; i >=0 && j >= 0; i++, j--) {
            if (qp[i][j] == qz) {
                input++;
            } else {
                break;
            }
        }
        if (input == 5) {
            return true;
        }
        //右上
        for (int i = row - 1, j = col + 1; i >= 0 && j < qp[0].length-1; i--, j++) {
            if (qp[i][j] == qz) {
                input++;
            } else {
                break;
            }
        }
        if(input==5) {
            return true;
        }
        else
            return false;
    }
}

 二、总结

全部代码如下:


import java.util.Scanner;

public class fiveInRow {
    /*
    1、提供一个启动五子棋游戏的方法
         初始化棋盘
         方法:打印棋盘
    2、开始下棋,黑白棋交替下棋
    3、判断坐标是否合法,是否重复
    4、判断输赢
     */
    static String white = "☆";
    static String black = "★";
    static String[][] qp = new String[15][15];
    static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
    static String line = "十";

    //主函数
    public static void main(String[] args) {
        startGame();
    }
    //1、启动五子棋游戏
    public static void startGame() {
        init();//初始化棋盘
        print(qp);//打印棋盘
        play(qp);
    }

    //1.1、初始化棋盘
    public static void init() {
        for (int i = 0; i < qp.length; i++) {
            for (int j = 0; j < qp[i].length; j++) {
                qp[i][j] = line;
//                //打印坐标
//                if (j == qp[i].length - 1) {
//                    qp[i][j] = num[i];
//                }
//                if (i == qp.length - 1) {
//                    qp[i][j] = num[j];
//                }
            }
        }
        //打印列坐标
        for (int i = 0; i < num.length; i++) {
            qp[i][qp[i].length - 1] = num[i];
        }
        //打印行坐标
        for (int i = 0; i < num.length; i++) {
            qp[qp[i].length - 1][i] = num[i];
        }
    }

    //1.2、打印棋盘
    public static void print(String array[][]) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length; j++) {
                System.out.print(array[i][j]);
            }
            System.out.println("");
        }
    }

    //玩家界面
    public static void play(String array[][]) {
        System.out.println("欢迎来到五子棋游戏");
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        while (true) {
            if (flag) {
                System.out.println("黑子下棋" + "\n" + "输入行和列:");
                int row1 = scanner.nextInt() - 1;//行,索引要-1
                int col1 = scanner.nextInt() - 1;//列,索引要-1
                //判断是否重复,合法
                boolean result = check(row1, col1);
                if (result) {
                    array[row1][col1] = black;
                    print(qp);//打印棋盘
                    flag = false;
                    //判断输赢
                    if (isWin(row1, col1, black)) {
                        System.out.println("黑棋胜利!游戏结束");
                        return;
                    }
                }
            } else {
                System.out.println("白棋下棋" + "\n" + "输入行和列:");
                int row2 = scanner.nextInt() - 1;//
                int col2 = scanner.nextInt() - 1;
                //判断
                boolean result2 = check(row2, col2);
                if (result2) {
                    array[row2][col2] = white;
                    print(qp);//打印棋盘
                    flag = true;
                    //判断输赢
                    if (isWin(row2, col2, white)) {
                        System.out.println("白棋胜利!游戏结束");
                        return;
                    }
                }
            }
        }
    }

    public static boolean check(int row, int col) {
        //检查坐标合法
        if (row < 0 || col < 0 || col > qp[0].length || row > qp.length) {
            System.out.println("超出五子棋坐标范围,请重新下棋!");
            return false;
        }
        //检查重复
        if (qp[row][col] != line) {
            System.out.println("该位置已有棋子,请重新下棋!");
            return false;
        }
        return true;
    }

    public static boolean isWin(int row, int col, String qz) {
        //每下一个棋子,就从二维数组中的第一个位置开始向右,下,斜向下进行查找,直到有满足五个相连即可以终止
        //以下落的棋子为中心,扫描
        //水平判断输赢
        //水平向左
        int input = 0;
        for (int left = col; left >= 0; left--) {
            if (qp[row][left] == qz) {//如果相同棋子
                input++;//计数,5个为满
            } else {
                break;
            }
        }
        if (input == 5) {
            return true;
        }
        //水平向右
        for (int right = col + 1; right < qp[0].length - 1; right++) {
            if (qp[row][right] == qz) {
                input++;//计数,5个为满
            } else {
                break;
            }
        }
        if (input == 5) {
            return true;
        } else
            input = 1;
        //垂直判断输赢
        //竖直向上
        for (int up = row-1; up >= 0; up--) {
            if (qp[up][col] == qz) {
                input++;//计数,5个为满
            } else {
                break;
            }
        }
        if (input == 5) {
            return true;
        }
        //竖直向下
        for (int down = row+1; down < qp.length-1; down++) {
            if (qp[down][col] == qz) {
                input++;//计数,5个为满
            } else {
                break;
            }
        }
        if (input == 5) {
            return true;
        }else
            input=1;
        //斜向判断输赢\
        //右下
        for (int i = row + 1, j = col + 1; i < qp.length-1 && j < qp[0].length-1; i++, j++) {
            if (qp[i][j] == qz) {
                input++;
            } else {
                break;
            }
        }
        if (input == 5) {
            return true;
        }
        //左上
        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
            if (qp[i][j] == qz) {
                input++;
            } else {
                break;
            }
        }
        if (input == 5) {
            return true;
        }else
            input=1;
        //斜向判断输赢/
        //左下
        for (int i = row + 1, j = col - 1; i >=0 && j >= 0; i++, j--) {
            if (qp[i][j] == qz) {
                input++;
            } else {
                break;
            }
        }
        if (input == 5) {
            return true;
        }
        //右上
        for (int i = row - 1, j = col + 1; i >= 0 && j < qp[0].length-1; i--, j++) {
            if (qp[i][j] == qz) {
                input++;
            } else {
                break;
            }
        }
        if(input==5) {
            return true;
        }
        else
            return false;
    }
}

 运行界面如下:通过输入行和列可进行游戏

 

 刚开始学,有什么问题可以在下方留言~

  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值