温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java实现五子棋小游戏的代码怎么写

发布时间:2022-01-10 16:09:20 来源:亿速云 阅读:134 作者:柒染 栏目:开发技术

这篇文章的内容主要围绕java实现五子棋小游戏的代码怎么写进行讲述,文章内容清晰易懂,条理清晰,非常适合新手学习,值得大家去阅读。感兴趣的朋友可以跟随小编一起阅读吧。希望大家通过这篇文章有所收获!

讲解

在第一步实现的基础上,添加游戏结束条件。
五子棋游戏中的相同棋子如果同时有五个连接成一条线就说明游戏结束。

代码实现如下:

if(count!=0){
                //判断每一行
                for(int j=0;j<11;j++){
                    for(int i=0;i<7;i++){
                        if(chessboard[j][i]==1&&chessboard[j][i+1]==1&&chessboard[j][i+2]==1&&chessboard[j][i+3]==1&&chessboard[j][i+4]==1){
                            System.out.println("游戏结束,黑棋获胜");
                            break;
                        }
                        if(chessboard[j][i]==2&&chessboard[j][i+1]==2&&chessboard[j][i+2]==2&&chessboard[j][i+3]==2&&chessboard[j][i+4]==2){
                            System.out.println("游戏结束,白棋获胜");
                            break;
                        }
                    }
                }
                //判断每一列
                for(int j=0;j<11;j++){
                    for(int i=0;i<7;i++){
                        if(chessboard[i][j]==1&&chessboard[i+1][j]==1&&chessboard[i+2][j]==1&&chessboard[i+3][j]==1&&chessboard[i+4][j]==1){
                            System.out.println("游戏结束,黑棋获胜");
                            break;
                        }
                        if(chessboard[i][j]==2&&chessboard[i+1][j]==2&&chessboard[i+2][j]==2&&chessboard[i+3][j]==2&&chessboard[i+4][j]==2){
                            System.out.println("游戏结束,白棋获胜");
                            break;
                        }
                    }
                }
                //左上到右下
                for(int j=0;j<7;j++){
                    for(int i=0;i<7;i++){
                        if(chessboard[j][i]==1&&chessboard[j+1][i+1]==1&&chessboard[j+2][i+2]==1&&chessboard[j+3][i+3]==1&&chessboard[j+4][i+4]==1){
                            System.out.println("游戏结束,黑棋获胜");
                            break;
                        }
                        if(chessboard[j][i]==2&&chessboard[j+1][i+1]==2&&chessboard[j+2][i+2]==2&&chessboard[j+3][i+3]==2&&chessboard[j+4][i+4]==2){
                            System.out.println("游戏结束,白棋获胜");
                            break;
                        }
                    }
                }
                //右上到左下
                for(int j=0;j<7;j++){
                    for(int i=10;i>=4;i--){
                        if(chessboard[j][i]==1&&chessboard[j+1][i-1]==1&&chessboard[j+2][i-2]==1&&chessboard[j+3][i-3]==1&&chessboard[j+4][i-4]==1){
                            System.out.println("游戏结束,黑棋获胜");
                            break;
                        }
                        if(chessboard[j][i]==2&&chessboard[j+1][i-1]==2&&chessboard[j+2][i-2]==2&&chessboard[j+3][i-3]==2&&chessboard[j+4][i-4]==2){
                            System.out.println("游戏结束,白棋获胜");
                            break;
                        }
                    }
                }
            }
            else{
                //System.out.println("游戏结束");
                int flag=0;
                for(int j=0;j<11;j++){
                    for(int i=0;i<7;i++){
                        if(chessboard[j][i]==1&&chessboard[j][i+1]==1&&chessboard[j][i+2]==1&&chessboard[j][i+3]==1&&chessboard[j][i+4]==1){
                            System.out.println("游戏结束,黑棋获胜");
                            flag=1;
                            break;
                        }
                        if(chessboard[j][i]==2&&chessboard[j][i+1]==2&&chessboard[j][i+2]==2&&chessboard[j][i+3]==2&&chessboard[j][i+4]==2){
                            System.out.println("游戏结束,白棋获胜");
                            flag=1;
                            break;
                        }
                    }
                }
                for(int j=0;j<11;j++){
                    for(int i=0;i<7;i++){
                        if(chessboard[i][j]==1&&chessboard[i+1][j]==1&&chessboard[i+2][j]==1&&chessboard[i+3][j]==1&&chessboard[i+4][j]==1){
                            System.out.println("游戏结束,黑棋获胜");
                            flag=1;
                            break;
                        }
                        if(chessboard[i][j]==2&&chessboard[i+1][j]==2&&chessboard[i+2][j]==2&&chessboard[i+3][j]==2&&chessboard[i+4][j]==2){
                            System.out.println("游戏结束,白棋获胜");
                            flag=1;
                            break;
                        }
                    }
                }
                for(int j=0;j<7;j++){
                    for(int i=0;i<7;i++){
                        if(chessboard[j][i]==1&&chessboard[j+1][i+1]==1&&chessboard[j+2][i+2]==1&&chessboard[j+3][i+3]==1&&chessboard[j+4][i+4]==1){
                            System.out.println("游戏结束,黑棋获胜");
                            flag=1;
                            break;
                        }
                        if(chessboard[j][i]==2&&chessboard[j+1][i+1]==2&&chessboard[j+2][i+2]==2&&chessboard[j+3][i+3]==2&&chessboard[j+4][i+4]==2){
                            System.out.println("游戏结束,白棋获胜");
                            flag=1;
                            break;
                        }
                    }
                }
                for(int j=0;j<7;j++){
                    for(int i=10;i>=4;i--){
                        if(chessboard[j][i]==1&&chessboard[j+1][i-1]==1&&chessboard[j+2][i-2]==1&&chessboard[j+3][i-3]==1&&chessboard[j+4][i-4]==1){
                            System.out.println("游戏结束,黑棋获胜");
                            flag=1;
                            break;
                        }
                        if(chessboard[j][i]==2&&chessboard[j+1][i-1]==2&&chessboard[j+2][i-2]==2&&chessboard[j+3][i-3]==2&&chessboard[j+4][i-4]==2){
                            System.out.println("游戏结束,白棋获胜");
                            flag=1;
                            break;
                        }
                    }
                }
                if(flag==0){
                    System.out.println("游戏结束,平局");
                }
            }

每次都要判断是否游戏结束,如果结束了就需要输出白棋还是黑棋胜。如果棋盘被填满了,那么我们还需要判断是否有平局的情况发生。

java实现五子棋小游戏的代码怎么写

感谢你的阅读,相信你对“java实现五子棋小游戏的代码怎么写”这一问题有一定的了解,快去动手实践吧,如果想了解更多相关知识点,可以关注亿速云网站!小编会继续为大家带来更好的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI