先前創建房間時有呼叫一個外部的class,叫CreateBoardData,我們可將一些棋盤棋子的初始設置儲存在這裡
先加入一些變數用來儲存值
private String player;
private Activity activity;
private PlayerChessboardData playerChessboardData;
private HashMap<String,Object> chessboardData = new HashMap<>();
private HashMap<String,Object> chessmanMoveData = new HashMap<>();
private HashMap<String,Object> pawnMove1 = new HashMap<>();
private HashMap<String,Object> pawnMove2 = new HashMap<>();
private HashMap<String,Object> castlingMove1 = new HashMap<>();
private HashMap<String,Object> castlingMove2 = new HashMap<>();
private ArrayList<TextView> chessboard = new ArrayList<>();
private ArrayList<Button> chessman = new ArrayList<>();
private ArrayList<TextView> chessmanSelect = new ArrayList<>();
private String[] board_coordinate;
private ColorData colorData = new ColorData();
接著我們加入建構元,讓呼叫class時可以直接傳入參數,以及作相對應的初始化
public CreateBoardData(Activity activity, String player){
this.activity = activity;
this.player = player;
if(player.equals("1")){
playerChessboardData = new PlayerChessboardData("1");
board_coordinate = new String[]{
"","a","b","c","d","e","f","g","h","",
"8","7","6","5","4","3","2","1",
"8","7","6","5","4","3","2","1",
"","a","b","c","d","e","f","g","h",""
};
} else if (player.equals("2")) {
playerChessboardData = new PlayerChessboardData("2");
board_coordinate = new String[]{
"","h","g","f","e","d","c","b","a","",
"1","2","3","4","5","6","7","8",
"1","2","3","4","5","6","7","8",
"","h","g","f","e","d","c","b","a",""
};
}else{
board_coordinate = new String[]{};
}
}
// 棋盤資料
public HashMap<String,Object> CreateChessboard(){
chessboardData.put("a8","bR");
chessboardData.put("b8","bN");
chessboardData.put("c8","bB");
chessboardData.put("d8","bQ");
chessboardData.put("e8","bK");
chessboardData.put("f8","bB");
chessboardData.put("g8","bN");
chessboardData.put("h8","bR");
chessboardData.put("a7","bP");
chessboardData.put("b7","bP");
chessboardData.put("c7","bP");
chessboardData.put("d7","bP");
chessboardData.put("e7","bP");
chessboardData.put("f7","bP");
chessboardData.put("g7","bP");
chessboardData.put("h7","bP");
chessboardData.put("a6"," ");
chessboardData.put("b6"," ");
chessboardData.put("c6"," ");
chessboardData.put("d6"," ");
chessboardData.put("e6"," ");
chessboardData.put("f6"," ");
chessboardData.put("g6"," ");
chessboardData.put("h6"," ");
chessboardData.put("a5"," ");
chessboardData.put("b5"," ");
chessboardData.put("c5"," ");
chessboardData.put("d5"," ");
chessboardData.put("e5"," ");
chessboardData.put("f5"," ");
chessboardData.put("g5"," ");
chessboardData.put("h5"," ");
chessboardData.put("a4"," ");
chessboardData.put("b4"," ");
chessboardData.put("c4"," ");
chessboardData.put("d4"," ");
chessboardData.put("e4"," ");
chessboardData.put("f4"," ");
chessboardData.put("g4"," ");
chessboardData.put("h4"," ");
chessboardData.put("a3"," ");
chessboardData.put("b3"," ");
chessboardData.put("c3"," ");
chessboardData.put("d3"," ");
chessboardData.put("e3"," ");
chessboardData.put("f3"," ");
chessboardData.put("g3"," ");
chessboardData.put("h3"," ");
chessboardData.put("a2","wP");
chessboardData.put("b2","wP");
chessboardData.put("c2","wP");
chessboardData.put("d2","wP");
chessboardData.put("e2","wP");
chessboardData.put("f2","wP");
chessboardData.put("g2","wP");
chessboardData.put("h2","wP");
chessboardData.put("a1","wR");
chessboardData.put("b1","wN");
chessboardData.put("c1","wB");
chessboardData.put("d1","wQ");
chessboardData.put("e1","wK");
chessboardData.put("f1","wB");
chessboardData.put("g1","wN");
chessboardData.put("h1","wR");
return chessboardData;
}
// 棋譜資料
public HashMap<String,Object> CreateChessmanMove(){
chessmanMoveData.put("0","");
return chessmanMoveData;
}
// 玩家1士兵升變資料
public HashMap<String,Object> CreatePawnMove1(){
pawnMove1.put("a4",false);
pawnMove1.put("b4",false);
pawnMove1.put("c4",false);
pawnMove1.put("d4",false);
pawnMove1.put("e4",false);
pawnMove1.put("f4",false);
pawnMove1.put("g4",false);
pawnMove1.put("h4",false);
return pawnMove1;
}
// 玩家2士兵升變資料
public HashMap<String,Object> CreatePawnMove2(){
pawnMove2.put("h5",false);
pawnMove2.put("g5",false);
pawnMove2.put("f5",false);
pawnMove2.put("e5",false);
pawnMove2.put("d5",false);
pawnMove2.put("c5",false);
pawnMove2.put("b5",false);
pawnMove2.put("a5",false);
return pawnMove2;
}
// 玩家1王車易位資料
public HashMap<String,Object> CreateCastlingMove1(){
castlingMove1.put("a1",true);
castlingMove1.put("e1",true);
castlingMove1.put("h1",true);
return castlingMove1;
}
// 玩家2王車易位資料
public HashMap<String,Object> CreateCastlingMove2(){
castlingMove2.put("h8",true);
castlingMove2.put("e8",true);
castlingMove2.put("a8",true);
return castlingMove2;
}
這樣我們在先前創建房間時,就可以直接呼叫到這裡的function了