溫馨提示×

溫馨提示×

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

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

Java如何實現(xiàn)簡單無界面五子棋

發(fā)布時間:2023-04-28 09:43:14 來源:億速云 閱讀:84 作者:zzz 欄目:編程語言

本篇內(nèi)容介紹了“Java如何實現(xiàn)簡單無界面五子棋”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

項目實現(xiàn)思路:

1、棋盤設(shè)計為10*10格,棋盤類型Chess[][] 二維數(shù)組,所含屬性String chessType; 棋盤首先chessType值是”?”。
2、初始化二維數(shù)組
3、玩家選擇黑白圈后,開始下棋。輸入要下棋子的行列坐標(biāo),黑白棋子輪流落子,當(dāng)一方連成五子或下滿棋盤時,游戲結(jié)束(連成五子的一方獲勝,下滿棋盤為和棋)。
4、每一次落子成功后,馬上判斷以該位置為中心的八個方向:上、下、左、右、左上、左下、右上、右下是否有相同顏色的棋子連成五子,如果連成五子,則游戲結(jié)束,輸出相應(yīng)的信息。
5、當(dāng)游戲一方勝利后顯示勝利信息。從程序表面看,這是一個二維平面圖,所以數(shù)據(jù)用二維數(shù)組來表示,數(shù)組兩個下標(biāo)可以表示棋盤上的位置,數(shù)組元素的值代表棋格上的狀態(tài),共有三種情況,分別是,?代表白棋,●代表黑棋,?代表格子。

源代碼

1.棋子

/**
 * @author hudongsheng
 * @date 2020/10/29 - 9:28
 */
public class ChessType {
    private String chessType;
    private int x;
    private int y;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }


    public ChessType() {

    }

    public String getChessType() {
        return chessType;
    }

    public void setChessType(String chessType) {
        this.chessType = chessType;
    }
}

2.下棋

/**
 * @author hudongsheng
 * @date 2020/10/29 - 9:27
 */
public class Gobang {
    private int size = 1;
    private ChessType[][] chessTypes;
    private int row;
    private int colum;
    private int x;
    private int y;


    //創(chuàng)建一個棋盤
    public Gobang(int row,int colum){
        this.row = row;
        this.colum = colum;
        chessTypes = new ChessType[row][colum];
    }

    //初始化棋盤
    public void initChessType(){

        for(int i = 0; i< chessTypes.length; i++){
            for (int j = 0; j< chessTypes[i].length; j++){
                chessTypes[i][j] = new ChessType();
                chessTypes[i][j].setChessType("?");
            }
        }
    }

    //下白棋
    public void setWhiteChess(int x,int y){
        chessTypes[x][y].setChessType("?");
    }

    //下黑棋
    public void setBlackChess(int x,int y){
        chessTypes[x][y].setChessType("●");
    }

   //判斷是否勝利
        public boolean checkWin(int i,int j) {
            // TODO Auto-generated method stub
            boolean flag = false;
            //判斷縱向是否有五個棋子是相同的顏色
            int count1 = 1;//相同顏色棋子的個數(shù)
            String color = chessTypes[i][j].getChessType(); //剛下的棋子的顏色
            int a = 1;  //棋子索引的增量
            while((i+a)<row && color == chessTypes[i+a][j].getChessType()){
                count1++;
                a++;
            }
            a = 1;
            while((i-a)>=0 && color == chessTypes[i-a][j].getChessType()){
                count1++;
                a++;
            }
            if(count1 >= 5){
                flag = true;
            }

            //判斷縱向是否有五個棋子是相同的顏色
            int count2 = 1;
            a = 1;
            while((j+a)<colum && color == chessTypes[i][j+a].getChessType()){
                count2++;
                a++;
            }
            a = 1;
            while((j-a)>=0 && color == chessTypes[i][j-a].getChessType()){
                count2++;
                a++;
            }
            if(count2 >= 5){
                flag = true;
            }

            //右上    左下 是否有五個棋子是相同的顏色
            int count3 = 1;
            a = 1;
            while((i+a)<row && (j-a)>=0 && color == chessTypes[i+a][j-a].getChessType()){
                count3++;
                a++;
            }
            a = 1;
            while((i-a)>=0 && (j+a)<colum && color == chessTypes[i-a][j+a].getChessType()){
                count3++;
                a++;
            }
            if(count3 >= 5){
                flag = true;
            }

            //左上  右下  是否有五個棋子是相同的顏色
            int count4 = 1;
            a = 1;
            while((i-a)>0 && (j-a)>=0 && color == chessTypes[i-a][j-a].getChessType()){
                count4++;
                a++;
            }
            a = 1;
            while((i+a)<row && (j+a)<colum && color == chessTypes[i+a][j+a].getChessType()){
                count4++;
                a++;
            }
            if(count4 >= 5){
                flag = true;
            }
            return flag;
        }

        //落子后打印棋盤
    public void print(){
        for(int i = 0; i< chessTypes.length; i++){
            for (int j = 0; j< chessTypes[i].length; j++){
                System.out.print(chessTypes[i][j].getChessType());
            }
            System.out.println();
        }
    }

}

3.測試

**
 * @author hudongsheng
 * @date 2020/10/29 - 9:27
 */
public class Test {
    public static void main(String[] args) {
        boolean flag = true;
        int x;
        int y;

        Gobang gobang = new Gobang(10,10);
        Scanner scanner = new Scanner(System.in);

        gobang.initChessType();
        //下棋

        System.out.println("黑棋執(zhí)先");
        while (true){
            gobang.print();
            System.out.println("請輸入下黑棋的坐標(biāo):");
            x = scanner.nextInt();
            y = scanner.nextInt();
            gobang.setBlackChess(x,y);
            if(gobang.checkWin(x,y)){
                gobang.print();
                System.out.println("黑棋勝!");
                break;
            }

            gobang.print();
            System.out.println("請輸入下白棋的坐標(biāo):");
            x = scanner.nextInt();
            y = scanner.nextInt();
            gobang.setWhiteChess(x,y);

            if(gobang.checkWin(x,y)){
                gobang.print();
                System.out.println("白棋勝!");
                break;
            }

        }
    }
}

“Java如何實現(xiàn)簡單無界面五子棋”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細(xì)節(jié)

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

AI