chessai
college code for ai playing chess in java
git clone https://9o.is/git/chessai.git
ChessState.java
(1617B)
1 package chess;
2
3 /**
4 * Check
5 */
6 public class ChessState {
7 protected byte[][] state;
8
9 public ChessState() {
10 state = new byte[ChessCoordinate.MAX][ChessCoordinate.MAX];
11 }
12
13 public ChessState(ChessState chessState) {
14 byte[][] state = chessState.state;
15 this.state = new byte[state.length][];
16
17 for(int i=0; i<state.length; i++) {
18 this.state[i] = state[i].clone();
19 }
20 }
21
22 protected void set(ChessCoordinate coor, ChessPiece piece) {
23 final int rank = fixRankOrder(coor.getRank()) - 1;
24 final int file = coor.getFileNumber() - 1;
25
26 state[rank][file] = (byte) piece.getNum();
27 }
28
29 public byte get(ChessCoordinate coor) {
30 final int rank = fixRankOrder(coor.getRank()) - 1;
31 final int file = coor.getFileNumber() - 1;
32
33 return state[rank][file];
34 }
35
36 protected boolean move(ChessCoordinate coor, ChessCoordinate coor1) {
37 final int rank = fixRankOrder(coor.getRank()) - 1;
38 final int rank1 = fixRankOrder(coor1.getRank()) - 1;
39
40 final int file = coor.getFileNumber() - 1;
41 final int file1 = coor1.getFileNumber() - 1;
42
43 state[rank1][file1] = state[rank][file];
44 state[rank][file] = 0;
45 return true;
46 }
47
48 private int fixRankOrder(int rank) {
49 final int max = ChessCoordinate.MAX;
50 if(rank > max/2) {
51 return max % rank + 1;
52 } else {
53 // TODO algorithm?
54 if(rank==4) return 5;
55 if(rank==3) return 6;
56 if(rank==2) return 7;
57 else return 8;
58 }
59 }
60 }