91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java實現五子棋小游戲的代碼怎么寫

發布時間:2022-01-10 16:09:20 來源:億速云 閱讀:136 作者:柒染 欄目:開發技術

這篇文章的內容主要圍繞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

航空| 鄂伦春自治旗| 平顶山市| 神池县| 永泰县| 三门县| 大安市| 栖霞市| 佛教| 曲水县| 扬州市| 乌什县| 高安市| 博湖县| 准格尔旗| 鹤山市| 绥德县| 赤城县| 开江县| 朝阳区| 定襄县| 凤城市| 乌审旗| 随州市| 凤山县| 万源市| 鄢陵县| 平昌县| 平邑县| 衢州市| 吐鲁番市| 类乌齐县| 桓仁| 汨罗市| 五常市| 古交市| 克东县| 托克逊县| 古丈县| 巍山| 胶南市|