iT邦幫忙

2024 iThome 鐵人賽

DAY 20
0

關於choice方法,基本上就是接收棋子的指定位置,然後根據該位置上棋子的種類,再根據場況(棋盤數據)推算出走法(可行走的所有格子),最後將走法的格子合成一個Array回傳

走法的部分會根據棋子種類的不同而產生不同處理,我們先實作choice的部分


choice

public ArrayList choice(
            int position
            ,HashMap<String,Object>chessboardData
            ,HashMap<String,Object> pawnMove1
            ,HashMap<String,Object> pawnMove2
            ,HashMap<String,Object> castlingMove){
        canMoveList = new ArrayList<>();
        canEatList = new ArrayList<>();
        canCastlingList = new ArrayList<>();
        canSpMoveList = new ArrayList<>();
        canEpMoveList = new ArrayList<>();
        ArrayList callbackList = new ArrayList<>();
        if(String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData(position))).equals("  ")){
//            systemMessage("系統訊息:\n不可選擇空白格子");
        }else{
            if(String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData(position)).toString().charAt(0)).equals(own)){

                if(chessboardData.get(playerChessboardData.getStringBoardData(position)).equals(own+"P")){
                    canEatList = getArrChessmanEatData(chessboardData,(String) chessboardData.get(playerChessboardData.getStringBoardData(position)),position);

                    canSpMoveList = getArrSpMoveData(position);
                    for(int i=canSpMoveList.size()-1;i>=0;i--){
                        if(rules.isEnableMove(position,(int)canSpMoveList.get(i),chessboardData)==false
                                || (!(chessboardData.get(playerChessboardData.getStringBoardData((int)canSpMoveList.get(i))).equals("  "))
                                && String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData((int)canSpMoveList.get(i))).toString().charAt(0)).equals(own))
                                || (!(chessboardData.get(playerChessboardData.getStringBoardData((int)canSpMoveList.get(i))).equals("  "))
                                && String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData((int)canSpMoveList.get(i))).toString().charAt(0)).equals(other))){
                            canSpMoveList.remove(canSpMoveList.get(i));
                        }
                    }
                    canEpMoveList = getArrEpMoveData(chessboardData,pawnMove1,pawnMove2,position);

                    canMoveList = getArrChessmanMoveData((String) chessboardData.get(playerChessboardData.getStringBoardData(position)),position);
                    for(int i=canMoveList.size()-1;i>=0;i--){
                        if(rules.isEnableMove(position,(int)canMoveList.get(i),chessboardData)==false
                                || (!(chessboardData.get(playerChessboardData.getStringBoardData((int)canMoveList.get(i))).equals("  "))
                                && String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData((int)canMoveList.get(i))).toString().charAt(0)).equals(own))
                                || (!(chessboardData.get(playerChessboardData.getStringBoardData((int)canMoveList.get(i))).equals("  "))
                                && String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData((int)canMoveList.get(i))).toString().charAt(0)).equals(other))){
                            canMoveList.remove(canMoveList.get(i));
                        }
                    }
                }else if(chessboardData.get(playerChessboardData.getStringBoardData(position)).equals(own+"K")){
                    canCastlingList = getArrCastlingMoveData(chessboardData,position,castlingMove);

                    canMoveList = getArrChessmanMoveData((String) chessboardData.get(playerChessboardData.getStringBoardData(position)),position);
                    for(int i=canMoveList.size()-1;i>=0;i--){
                        if(rules.isEnableMove(position,(int)canMoveList.get(i),chessboardData)==false
                                || (!(chessboardData.get(playerChessboardData.getStringBoardData((int)canMoveList.get(i))).equals("  "))
                                && String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData((int)canMoveList.get(i))).toString().charAt(0)).equals(own))
                                || (rules.isCheck((int)canMoveList.get(i),chessboardData))){
                            canMoveList.remove(canMoveList.get(i));
                        }
                    }
                }else{
                    canMoveList = getArrChessmanMoveData((String) chessboardData.get(playerChessboardData.getStringBoardData(position)),position);
                    for(int i=canMoveList.size()-1;i>=0;i--){
                        if(rules.isEnableMove(position,(int)canMoveList.get(i),chessboardData)==false
                                || (!(chessboardData.get(playerChessboardData.getStringBoardData((int)canMoveList.get(i))).equals("  "))
                                && String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData((int)canMoveList.get(i))).toString().charAt(0)).equals(own))){
                            canMoveList.remove(canMoveList.get(i));
                        }
                    }
                }
            }else{
//                systemMessage("系統訊息:\n不可選擇白棋");
            }
        }
        callbackList.addAll(canEatList);
        callbackList.addAll(canEpMoveList);
        callbackList.addAll(canMoveList);
        callbackList.addAll(canSpMoveList);
        callbackList.addAll(canCastlingList);
        return callbackList;
    }

可以看到其中我們有呼叫其他處理走法的方法,包括:

  • getArrChessmanMoveData() 處理棋子移動走法
  • getArrChessmanEatData() 處理士兵吃棋走法
  • getArrCastlingMoveData() 處理王車易位走法
  • getArrSpMoveData() 處理士兵初始可走2格走法
  • getArrEpMoveData() 處理士兵吃過路兵走法

getArrChessmanMoveData()

主要的概念是透過傳入的棋子種類(chessman)將指定位置的座標回傳

public ArrayList getArrChessmanMoveData(String chessman, int position){
        ArrayList moveList = new ArrayList<>();
        int up = position/8;
        int left = position%8;
        int down = 7-position/8;
        int right = 7-position%8;

        if(chessman.equals(own+"R")){
            for(int i=0;i<64;i++){
                if(i != position){
                    if((i % 8 == position % 8) || (i / 8 == position / 8)){
                        moveList.add(i);
                    }
                }
            }
        } else if (chessman.equals(own+"N")) {
            for(int i=0;i<64;i++) {
                if (i != position) {
                    if (up > 1 && left > 0) {
                        if (i == position - 17) {
                            moveList.add(i);
                        }
                    }
                    if (left > 1 && up > 0) {
                        if (i == position - 10) {
                            moveList.add(i);
                        }
                    }
                    if (left > 1 && down > 0) {
                        if (i == position + 6) {
                            moveList.add(i);
                        }
                    }
                    if (down > 1 && left > 0) {
                        if (i == position + 15) {
                            moveList.add(i);
                        }
                    }
                    if (down > 1 && right > 0) {
                        if (i == position + 17) {
                            moveList.add(i);
                        }
                    }
                    if (right > 1 && down > 0) {
                        if (i == position + 10) {
                            moveList.add(i);
                        }
                    }
                    if (right > 1 && up > 0) {
                        if (i == position - 6) {
                            moveList.add(i);
                        }
                    }
                    if (up > 1 && right > 0) {
                        if (i == position - 15) {
                            moveList.add(i);
                        }
                    }
                }
            }
        } else if (chessman.equals(own+"B")) {
            for(int i=0;i<64;i++){
                if(i != position){
                    for(int j=1;j<=Math.min(up,left);j++){
                        if(i == position - 9*j){
                            moveList.add(i);
                        }
                    }
                    for(int j=1;j<=Math.min(down,left);j++){
                        if(i == position + 7*j){
                            moveList.add(i);
                        }
                    }
                    for(int j=1;j<=Math.min(down,right);j++){
                        if(i == position + 9*j){
                            moveList.add(i);
                        }
                    }
                    for(int j=1;j<=Math.min(up,right);j++){
                        if(i == position - 7*j){
                            moveList.add(i);
                        }
                    }
                }
            }
        } else if (chessman.equals(own+"K")) {
            for(int i=0;i<64;i++){
                if(i != position){
                    if(up>0 && left>0){
                        if(i == position - 9){
                            moveList.add(i);
                        }
                    }
                    if(down>0 && left>0){
                        if(i == position + 7){
                            moveList.add(i);
                        }
                    }
                    if(down>0 && right>0){
                        if(i == position + 9){
                            moveList.add(i);
                        }
                    }
                    if(up>0 && right>0){
                        if(i == position - 7){
                            moveList.add(i);
                        }
                    }
                    if(up>0){
                        if(i == position - 8){
                            moveList.add(i);
                        }
                    }
                    if(left>0){
                        if(i == position - 1){
                            moveList.add(i);
                        }
                    }
                    if(down>0){
                        if(i == position + 8){
                            moveList.add(i);
                        }
                    }
                    if(right>0){
                        if(i == position + 1){
                            moveList.add(i);
                        }
                    }
                }
            }
        } else if (chessman.equals(own+"Q")) {
            for(int i=0;i<64;i++){
                if(i != position){
                    for(int j=1;j<=Math.min(up,left);j++){
                        if(i == position - 9*j){
                            moveList.add(i);
                        }
                    }
                    for(int j=1;j<=Math.min(down,left);j++){
                        if(i == position + 7*j){
                            moveList.add(i);
                        }
                    }
                    for(int j=1;j<=Math.min(down,right);j++){
                        if(i == position + 9*j){
                            moveList.add(i);
                        }
                    }
                    for(int j=1;j<=Math.min(up,right);j++){
                        if(i == position - 7*j){
                            moveList.add(i);
                        }
                    }
                    if((i % 8 == position % 8) || (i / 8 == position / 8)){
                        moveList.add(i);
                    }
                }
            }
        } else if (chessman.equals(own+"P")) {
            for(int i=0;i<64;i++){
                if(i != position){
                    if(up != 0){
                        if(i == position - 8){
                            moveList.add(i);
                        }
                    }else{
                    }
                }
            }
        }

        return moveList;
    }

值得注意的是,由於士兵行走與吃棋的方式不一致,所以我們需多做一個方法處理士兵吃棋

getArrChessmanEatData()

public ArrayList getArrChessmanEatData(HashMap<String,Object> chessboardData, String chessman, int position){
        int up = position/8;
        int left = position%8;
        int down = 7-position/8;
        int right = 7-position%8;
        ArrayList eatList = new ArrayList<>();
        if(chessman.equals(own+"P")){
            for(int i=0;i<64;i++){
                if(i != position){
                    if(left == 0 && up != 0){
                        if(i == position-7){
                            if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals("  ")){
                            } else if (String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData(i)).toString().charAt(0)).equals(other)) {
                                eatList.add(i);
                            } else if (String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData(i)).toString().charAt(0)).equals(own)) {
                            }
                        }
                    } else if(right == 0 && up != 0){
                        if(i == position-9){
                            if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals("  ")){
                            } else if (String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData(i)).toString().charAt(0)).equals(other)) {
                                eatList.add(i);
                            } else if (String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData(i)).toString().charAt(0)).equals(own)) {
                            }
                        }
                    } else if(right != 0 && up != 0 && left != 0){
                        if(i == position-7){
                            if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals("  ")){
                            } else if (String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData(i)).toString().charAt(0)).equals(other)) {
                                eatList.add(i);
                            } else if (String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData(i)).toString().charAt(0)).equals(own)) {
                            }
                        }
                        if(i == position-9){
                            if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals("  ")){
                            } else if (String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData(i)).toString().charAt(0)).equals(other)) {
                                eatList.add(i);
                            } else if (String.valueOf(chessboardData.get(playerChessboardData.getStringBoardData(i)).toString().charAt(0)).equals(own)) {
                            }
                        }
                    }
                }
            }
        }
        return eatList;
    }

如此我們便完成主要行走走法的內容,下篇我們來實作其他的方法


上一篇
【DAY 19】activity - Player1ChessGame - selectChessmanListen
下一篇
【DAY 21】activity - Player1ChessGame - getArrCastlingMoveData
系列文
基於Firebase整合生成式AI研究開發雙人國際象棋系統(Based on Firebase and AI to research chess system)30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言