基于Java实现的五子棋游戏

构思:

Step1:打印棋盘

Step2:写出黑白两子下棋的方法以及判定

Step3:保存下完棋之后的棋盘,进行输赢判断

打印棋盘:

String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖", " "};//该字符串数组用来标明横纵坐标
            String[][] str = new String[16][16];
            //定义棋盘大小,边长为15,多出来的边换为坐标
            for (int i = 0; i < 16; i++) {
                for (int j = 0; j < 16; j++) {
                    str[i][j] = "十";//用"十"填充二维字符串数组
                    if (j == 15) {
                        str[i][j] = num[i];//用坐标标明行数
                    }
                }
            }
            for (int i = 0; i < 16; i++) {
                str[15][i] = num[i];//用坐标表明列数
            }
             //下面2个for循环用来打印棋盘
            for (int i = 0; i < 16; i++) {
                for (int j = 0; j < 16; j++) {
                    System.out.print(str[i][j]);
                }
                System.out.println();
            }

效果如下:

 

 黑白两子下棋方法:

         黑子:

public static void black(String str [][]){
        System.out.println("————————————————————————");
        System.out.println("请黑子下棋:");
        int x=1,y=1;
        boolean flag = true;
        while (flag) {
            Scanner scanner = new Scanner(System.in);
            //接收下棋坐标
            System.out.println("输入横坐标:");
            y = scanner.nextInt();
            System.out.println("输入纵坐标:");
            x = scanner.nextInt();
            //判断坐标是否满足要求,不满足的话重新输入坐标
            if (x - 2 > 15 || y - 2 > 15) {
                System.out.println("输入有误!请重新输入:");
            } else if (str[x - 1][y - 1] == "●" || str[x - 1][y - 1] == "○") {
                System.out.println("已经有棋子了!!请重新输入:");
            }else flag=false;
        }
        //下完后打印下完黑棋的棋盘
        str[x - 1][y - 1] = "●";
        for (int i = 0; i < 16; i++) {
            for (int j = 0; j < 16; j++) {
                System.out.print(str[i][j]);
            }
            System.out.println();
        }
        System.out.println("————————————————————————");
    }

白子下棋方法与黑子一样,仅仅是把"●"改成"○"。

输赢判定:

public static int judge(String str [][]){
        int count,count2;
        for(int i=0;i<16;i++){
            for(int j=0;j<16;j++){
                //上两个循环用来遍历数组
                if(str[i][j]=="●"){//如果遍历到黑子,就进入黑子胜负的判断
                    if(j+4<16){
                        //判断黑子是否横向连接5个
                    for(count=j+4;str[i][count]=="●";count--){
                        if(count==j){
                            return 1;//如果黑子获胜则返回1
                        }
                    }}else {for(count=j-4;str[i][count]=="●";count++){
                        if(count==j){
                            return 1;
                        }}}
                    //判断黑子是否纵向连接5个
                    if(i+4<16){
                    for(count=i+4;str[count][j]=="●";count--){
                        if(count==i){
                            return 1;
                        }
                    }}else {for(count=i-4;str[count][j]=="●";count++){
                        if(count==j){
                            return 1;
                        }}}
                    //判断黑子是否斜向连接5个
                    if(j+4<16&&i+4<16){
                    for(count=i+4,count2=j+4;str[count][count2]=="●";count--,count2--){
                        if(count==i&&count2==j){
                            return 1;
                        }
                    }}else {for(count=j-4,count2=i-4;str[count2][count]=="●";count++,count2++){
                        if(count==j&&count2==i){
                            return 1;
                        }}}
                }if(str[i][j]=="○"){//如果遍历到白子,就进入白子胜负的判断
                    //判断白子是否横向连成5个
                    if(j+4<16){
                        for(count=j+4;str[i][count]=="○";count--){
                            if(count==j){
                                return 0;//如果白子获胜则返回0
                            }
                        }}else {for(count=j-4;str[i][count]=="○";count++){
                        if(count==j){
                            return 0;
                        }}}
                    //判断白子是否纵向连成5个
                    if(i+4<16){
                        for(count=i+4;str[count][j]=="○";count--){
                            if(count==i){
                                return 0;
                            }
                        }}else {for(count=i-4;str[count][j]=="○";count++){
                        if(count==j){
                            return 0;
                        }}}
                    判断白子是否斜向连成5个
                    if(j+4<16&&i+4<16){
                        for(count=i+4,count2=j+4;str[count][count2]=="○";count--,count2--){
                            if(count==i&&count2==j){
                                return 0;
                            }
                        }}else {for(count=j-4,count2=i-4;str[count2][count]=="○";count++,count2++){
                        if(count==j&&count2==i){
                            return 0;
                        }}}
                }else continue;
            }
        }
        return -1;//如果遍历完黑白子没有任何一方获胜,则返回-1
    }

     判断获胜的方法如下图,遍历每一个元素时,判断该元素往右,往下,往右下方是否有相邻的4个一样的棋子,如果有则获胜。

 if(str[i][j]=="●"){//如果遍历到黑子,就进入黑子胜负的判断
                    if(j+4<16){
                        //判断黑子是否横向连接5个
                    for(count=j+4;str[i][count]=="●";count--){
                        if(count==j){
                            return 1;//如果黑子获胜则返回1
                        }
                    }}else {for(count=j-4;str[i][count]=="●";count++){
                        if(count==j){
                            return 1;
                        }}}

       但是这里的判断条件我使用的是(j+4<16)这是说如果往右边数4个没有超过棋盘右边界的话,则进入循环,如果超过了,则进入else 语句后,改变判断方法,变成从该元素往左,往上,往左上,数4个棋子 ,即:

 这样不会出现越界的报错问题

接下来将上述方法拼接进主程序中:

//游戏开始程序
    public static void main(String[] args) {
        int flag2=1;//用来判断游戏是否重新开始
        while(flag2==1) {
            //在主函数中打印棋盘,一局游戏只使用这一个棋盘,避免刷新棋盘刷掉上一步的棋子
            String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖", " "};
            String[][] str = new String[16][16];
            for (int i = 0; i < 16; i++) {
                for (int j = 0; j < 16; j++) {
                    str[i][j] = "十";
                    if (j == 15) {
                        str[i][j] = num[i];
                    }
                }
            }
            for (int i = 0; i < 16; i++) {
                str[15][i] = num[i];
            }
            for (int i = 0; i < 16; i++) {
                for (int j = 0; j < 16; j++) {
                    System.out.print(str[i][j]);
                }
                System.out.println();
            }
            //黑白双子轮换下棋,各下完一步之后都进行获胜判断
            boolean flag = true;
            outloop:
            while (flag) {
                black(str);
                if (judge(str) == 1) {
                    System.out.println("黑棋获胜!");
                    break outloop;
                } else if (judge(str) == 0) {
                    System.out.println("白棋获胜!");
                    break outloop;
                }
                white(str);
                if (judge(str) == 1) {
                    System.out.println("黑棋获胜!");
                    break outloop;
                } else if (judge(str) == 0) {
                    System.out.println("白棋获胜!");
                    break outloop;
                } else if (judge(str) == -1) {
                    continue;
                }
            }
            System.out.println("游戏结束!\n------------------------\n是否进行下一局?\n 1 重新开始\n 2 退出游戏\n------------------------");
            Scanner scanner = new Scanner(System.in);
            flag2 = scanner.nextInt();
        }
    }

游戏界面:

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值