iT邦幫忙

2024 iThome 鐵人賽

DAY 14
0

這篇要實作的是PlayerChessboardData,作用是可以取出座標字串或數值的對應,以及查詢棋子的圖片
未命名


PlayerChessboardData

  • 變數及建構元:
private String[] coordinateStr = new String[64];
    public PlayerChessboardData(String player){
        if(player.equals("1")){
            coordinateStr = new String[]{
                    "a8","b8","c8","d8","e8","f8","g8","h8",
                    "a7","b7","c7","d7","e7","f7","g7","h7",
                    "a6","b6","c6","d6","e6","f6","g6","h6",
                    "a5","b5","c5","d5","e5","f5","g5","h5",
                    "a4","b4","c4","d4","e4","f4","g4","h4",
                    "a3","b3","c3","d3","e3","f3","g3","h3",
                    "a2","b2","c2","d2","e2","f2","g2","h2",
                    "a1","b1","c1","d1","e1","f1","g1","h1",
            };
        }else{
            coordinateStr = new String[]{
                    "h1","g1","f1","e1","d1","c1","b1","a1",
                    "h2","g2","f2","e2","d2","c2","b2","a2",
                    "h3","g3","f3","e3","d3","c3","b3","a3",
                    "h4","g4","f4","e4","d4","c4","b4","a4",
                    "h5","g5","f5","e5","d5","c5","b5","a5",
                    "h6","g6","f6","e6","d6","c6","b6","a6",
                    "h7","g7","f7","e7","d7","c7","b7","a7",
                    "h8","g8","f8","e8","d8","c8","b8","a8",
            };
        }
    }

會有兩種初始值是因為,player1及player2的棋盤視角相反,所以需要個別設置對應

  • 將棋盤座標int轉string的function
public String getStringBoardData(int coordinate){
        switch (coordinate){
            case 0:
                return coordinateStr[0];
            case 1:
                return coordinateStr[1];
            case 2:
                return coordinateStr[2];
            case 3:
                return coordinateStr[3];
            case 4:
                return coordinateStr[4];
            case 5:
                return coordinateStr[5];
            case 6:
                return coordinateStr[6];
            case 7:
                return coordinateStr[7];

            case 8:
                return coordinateStr[8];
            case 9:
                return coordinateStr[9];
            case 10:
                return coordinateStr[10];
            case 11:
                return coordinateStr[11];
            case 12:
                return coordinateStr[12];
            case 13:
                return coordinateStr[13];
            case 14:
                return coordinateStr[14];
            case 15:
                return coordinateStr[15];

            case 16:
                return coordinateStr[16];
            case 17:
                return coordinateStr[17];
            case 18:
                return coordinateStr[18];
            case 19:
                return coordinateStr[19];
            case 20:
                return coordinateStr[20];
            case 21:
                return coordinateStr[21];
            case 22:
                return coordinateStr[22];
            case 23:
                return coordinateStr[23];

            case 24:
                return coordinateStr[24];
            case 25:
                return coordinateStr[25];
            case 26:
                return coordinateStr[26];
            case 27:
                return coordinateStr[27];
            case 28:
                return coordinateStr[28];
            case 29:
                return coordinateStr[29];
            case 30:
                return coordinateStr[30];
            case 31:
                return coordinateStr[31];

            case 32:
                return coordinateStr[32];
            case 33:
                return coordinateStr[33];
            case 34:
                return coordinateStr[34];
            case 35:
                return coordinateStr[35];
            case 36:
                return coordinateStr[36];
            case 37:
                return coordinateStr[37];
            case 38:
                return coordinateStr[38];
            case 39:
                return coordinateStr[39];

            case 40:
                return coordinateStr[40];
            case 41:
                return coordinateStr[41];
            case 42:
                return coordinateStr[42];
            case 43:
                return coordinateStr[43];
            case 44:
                return coordinateStr[44];
            case 45:
                return coordinateStr[45];
            case 46:
                return coordinateStr[46];
            case 47:
                return coordinateStr[47];

            case 48:
                return coordinateStr[48];
            case 49:
                return coordinateStr[49];
            case 50:
                return coordinateStr[50];
            case 51:
                return coordinateStr[51];
            case 52:
                return coordinateStr[52];
            case 53:
                return coordinateStr[53];
            case 54:
                return coordinateStr[54];
            case 55:
                return coordinateStr[55];

            case 56:
                return coordinateStr[56];
            case 57:
                return coordinateStr[57];
            case 58:
                return coordinateStr[58];
            case 59:
                return coordinateStr[59];
            case 60:
                return coordinateStr[60];
            case 61:
                return coordinateStr[61];
            case 62:
                return coordinateStr[62];
            case 63:
                return coordinateStr[63];

            default:
                return "";
        }
    }

因為我們的棋盤介面是以索引值來創建,我們可以透過這個索引值(int)跟棋盤座標的名稱(string)做轉換

  • 將棋盤座標string轉int的function
    也可用直接設值的方式
public int getIntBoardData1(String coordinate){
        switch (coordinate){
            case "a8":
                return 0;
            case "b8":
                return 1;
            case "c8":
                return 2;
            case "d8":
                return 3;
            case "e8":
                return 4;
            case "f8":
                return 5;
            case "g8":
                return 6;
            case "h8":
                return 7;

            case "a7":
                return 8;
            case "b7":
                return 9;
            case "c7":
                return 10;
            case "d7":
                return 11;
            case "e7":
                return 12;
            case "f7":
                return 13;
            case "g7":
                return 14;
            case "h7":
                return 15;

            case "a6":
                return 16;
            case "b6":
                return 17;
            case "c6":
                return 18;
            case "d6":
                return 19;
            case "e6":
                return 20;
            case "f6":
                return 21;
            case "g6":
                return 22;
            case "h6":
                return 23;

            case "a5":
                return 24;
            case "b5":
                return 25;
            case "c5":
                return 26;
            case "d5":
                return 27;
            case "e5":
                return 28;
            case "f5":
                return 29;
            case "g5":
                return 30;
            case "h5":
                return 31;

            case "a4":
                return 32;
            case "b4":
                return 33;
            case "c4":
                return 34;
            case "d4":
                return 35;
            case "e4":
                return 36;
            case "f4":
                return 37;
            case "g4":
                return 38;
            case "h4":
                return 39;

            case "a3":
                return 40;
            case "b3":
                return 41;
            case "c3":
                return 42;
            case "d3":
                return 43;
            case "e3":
                return 44;
            case "f3":
                return 45;
            case "g3":
                return 46;
            case "h3":
                return 47;

            case "a2":
                return 48;
            case "b2":
                return 49;
            case "c2":
                return 50;
            case "d2":
                return 51;
            case "e2":
                return 52;
            case "f2":
                return 53;
            case "g2":
                return 54;
            case "h2":
                return 55;

            case "a1":
                return 56;
            case "b1":
                return 57;
            case "c1":
                return 58;
            case "d1":
                return 59;
            case "e1":
                return 60;
            case "f1":
                return 61;
            case "g1":
                return 62;
            case "h1":
                return 63;

            default:
                return 99;
        }
    }

    public int getIntBoardData2(String coordinate){
        switch (coordinate){
            case "a8":
                return 63;
            case "b8":
                return 62;
            case "c8":
                return 61;
            case "d8":
                return 60;
            case "e8":
                return 59;
            case "f8":
                return 58;
            case "g8":
                return 57;
            case "h8":
                return 56;

            case "a7":
                return 55;
            case "b7":
                return 54;
            case "c7":
                return 53;
            case "d7":
                return 52;
            case "e7":
                return 51;
            case "f7":
                return 50;
            case "g7":
                return 49;
            case "h7":
                return 48;

            case "a6":
                return 47;
            case "b6":
                return 46;
            case "c6":
                return 45;
            case "d6":
                return 44;
            case "e6":
                return 43;
            case "f6":
                return 42;
            case "g6":
                return 41;
            case "h6":
                return 40;

            case "a5":
                return 39;
            case "b5":
                return 38;
            case "c5":
                return 37;
            case "d5":
                return 36;
            case "e5":
                return 35;
            case "f5":
                return 34;
            case "g5":
                return 33;
            case "h5":
                return 32;

            case "a4":
                return 31;
            case "b4":
                return 30;
            case "c4":
                return 29;
            case "d4":
                return 28;
            case "e4":
                return 27;
            case "f4":
                return 26;
            case "g4":
                return 25;
            case "h4":
                return 24;

            case "a3":
                return 23;
            case "b3":
                return 22;
            case "c3":
                return 21;
            case "d3":
                return 20;
            case "e3":
                return 19;
            case "f3":
                return 18;
            case "g3":
                return 17;
            case "h3":
                return 16;

            case "a2":
                return 15;
            case "b2":
                return 14;
            case "c2":
                return 13;
            case "d2":
                return 12;
            case "e2":
                return 11;
            case "f2":
                return 10;
            case "g2":
                return 9;
            case "h2":
                return 8;

            case "a1":
                return 7;
            case "b1":
                return 6;
            case "c1":
                return 5;
            case "d1":
                return 4;
            case "e1":
                return 3;
            case "f1":
                return 2;
            case "g1":
                return 1;
            case "h1":
                return 0;

            default:
                return 99;
        }
    }
  • 取得棋子圖片
    使用棋子代號取出棋子圖片
public int getIntChessmanData(String chessman){
        switch (chessman){
            case "bR":
                return R.drawable.black_rook;
            case "bN":
                return R.drawable.black_knight;
            case "bB":
                return R.drawable.black_bishop;
            case "bK":
                return R.drawable.black_king;
            case "bQ":
                return R.drawable.black_queen;
            case "bP":
                return R.drawable.black_pawn;

            case "wR":
                return R.drawable.white_rook;
            case "wN":
                return R.drawable.white_knight;
            case "wB":
                return R.drawable.white_bishop;
            case "wK":
                return R.drawable.white_king;
            case "wQ":
                return R.drawable.white_queen;
            case "wP":
                return R.drawable.white_pawn;

            case "":
                return R.drawable.blank;

            default:
                return 0;
        }
    }

完成之後,我們之後轉換棋盤座標或需要取得棋子圖片就可以呼叫這些function了


上一篇
【DAY 13】data - CreateBoardData
下一篇
【DAY 15】keyCode攔截返回event
系列文
基於Firebase整合生成式AI研究開發雙人國際象棋系統(Based on Firebase and AI to research chess system)30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言