溫馨提示×

溫馨提示×

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

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

java實現(xiàn)五子棋小游戲的代碼怎么寫

發(fā)布時間:2022-01-10 16:09:20 來源:億速云 閱讀:134 作者:柒染 欄目:開發(fā)技術(shù)

這篇文章的內(nèi)容主要圍繞java實現(xiàn)五子棋小游戲的代碼怎么寫進行講述,文章內(nèi)容清晰易懂,條理清晰,非常適合新手學習,值得大家去閱讀。感興趣的朋友可以跟隨小編一起閱讀吧。希望大家通過這篇文章有所收獲!

講解

在第一步實現(xiàn)的基礎上,添加游戲結(jié)束條件。
五子棋游戲中的相同棋子如果同時有五個連接成一條線就說明游戲結(jié)束。

代碼實現(xiàn)如下:

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("游戲結(jié)束,黑棋獲勝");
                            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("游戲結(jié)束,白棋獲勝");
                            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("游戲結(jié)束,黑棋獲勝");
                            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("游戲結(jié)束,白棋獲勝");
                            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("游戲結(jié)束,黑棋獲勝");
                            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("游戲結(jié)束,白棋獲勝");
                            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("游戲結(jié)束,黑棋獲勝");
                            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("游戲結(jié)束,白棋獲勝");
                            break;
                        }
                    }
                }
            }
            else{
                //System.out.println("游戲結(jié)束");
                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("游戲結(jié)束,黑棋獲勝");
                            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("游戲結(jié)束,白棋獲勝");
                            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("游戲結(jié)束,黑棋獲勝");
                            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("游戲結(jié)束,白棋獲勝");
                            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("游戲結(jié)束,黑棋獲勝");
                            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("游戲結(jié)束,白棋獲勝");
                            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("游戲結(jié)束,黑棋獲勝");
                            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("游戲結(jié)束,白棋獲勝");
                            flag=1;
                            break;
                        }
                    }
                }
                if(flag==0){
                    System.out.println("游戲結(jié)束,平局");
                }
            }

每次都要判斷是否游戲結(jié)束,如果結(jié)束了就需要輸出白棋還是黑棋勝。如果棋盤被填滿了,那么我們還需要判斷是否有平局的情況發(fā)生。

java實現(xiàn)五子棋小游戲的代碼怎么寫

感謝你的閱讀,相信你對“java實現(xiàn)五子棋小游戲的代碼怎么寫”這一問題有一定的了解,快去動手實踐吧,如果想了解更多相關知識點,可以關注億速云網(wǎng)站!小編會繼續(xù)為大家?guī)砀玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI