chessai
college code for ai playing chess in java
git clone https://9o.is/git/chessai.git
ChessBoard.java
(3452B)
1 package chess;
2
3 import chess.utils.ChessColor;
4 import chess.utils.ChessDirection;
5
6 import java.util.ArrayList;
7 import java.util.List;
8
9 /**
10 *
11 */
12 public class ChessBoard extends ChessState implements Cloneable {
13
14 public ChessBoard() {
15 super();
16 init();
17 }
18
19 public ChessBoard(ChessBoard chessBoard) {
20 super(chessBoard);
21 }
22
23 //CHECK
24 public void init() {
25 set(new ChessCoordinate('a', 8), ChessPiece.BLACK_ROOK);
26 set(new ChessCoordinate('b', 8), ChessPiece.BLACK_KNIGHT);
27 set(new ChessCoordinate('c', 8), ChessPiece.BLACK_BISHOP);
28 set(new ChessCoordinate('d', 8), ChessPiece.BLACK_QUEEN);
29 set(new ChessCoordinate('e', 8), ChessPiece.BLACK_KING);
30 set(new ChessCoordinate('f', 8), ChessPiece.BLACK_BISHOP);
31 set(new ChessCoordinate('g', 8), ChessPiece.BLACK_KNIGHT);
32 set(new ChessCoordinate('h', 8), ChessPiece.BLACK_ROOK);
33
34 for(int i=1; i<= ChessCoordinate.MAX; i++)
35 set(new ChessCoordinate(i,7), ChessPiece.BLACK_PAWN);
36
37 set(new ChessCoordinate('a',1), ChessPiece.WHITE_ROOK);
38 set(new ChessCoordinate('b', 1), ChessPiece.WHITE_KNIGHT);
39 set(new ChessCoordinate('c', 1), ChessPiece.WHITE_BISHOP);
40 set(new ChessCoordinate('d', 1), ChessPiece.WHITE_QUEEN);
41 set(new ChessCoordinate('e', 1), ChessPiece.WHITE_KING);
42 set(new ChessCoordinate('f', 1), ChessPiece.WHITE_BISHOP);
43 set(new ChessCoordinate('g', 1), ChessPiece.WHITE_KNIGHT);
44 set(new ChessCoordinate('h', 1), ChessPiece.WHITE_ROOK);
45
46 for(int i=1; i<= ChessCoordinate.MAX; i++)
47 set(new ChessCoordinate(i,2), ChessPiece.WHITE_PAWN);
48 }
49
50
51 public boolean move(ChessMove move) {
52 ChessCoordinate src = move.getCoordinate();
53 return move(src, src.getCoordinate(this, move));
54 }
55 //CHECK
56 public boolean move(
57 ChessCoordinate src,
58 ChessDirection direction,
59 int steps) {
60 ChessMove move = new ChessMove(src, direction, steps);
61 return move(src, src.getCoordinate(this, move));
62 }
63
64 //CHECK
65 public boolean move(ChessCoordinate src, ChessCoordinate dest) {
66 ChessRules.INSTANCE.setState(this);
67 ChessRules.INSTANCE.setSrc(src);
68 ChessRules.INSTANCE.setDest(dest);
69
70 ChessRules.INSTANCE.validMove();
71 ChessRules.INSTANCE.applyCaptureRule();
72 super.move(src, dest);
73 return true;
74
75 // TODO implement all rules to not allow opponent make whatever move
76 /*
77 if(ChessRules.INSTANCE.validMove()) {
78 ChessRules.INSTANCE.applyCaptureRule();
79 super.move(src, dest);
80 return true;
81 } else
82 return false;
83 */
84 }
85
86 public List<ChessCoordinate> getPieces(ChessColor color) {
87 List<ChessCoordinate> playerPieces = new ArrayList<ChessCoordinate>();
88 for(int i=1; i<=ChessCoordinate.MAX; i++)
89 for(int j=1; j<=ChessCoordinate.MAX; j++) {
90 ChessPiece piece = ChessPiece.find(get(new ChessCoordinate(j, i)));
91 if(piece.getColor() == color)
92 playerPieces.add(new ChessCoordinate(j,i));
93 }
94 return playerPieces;
95 }
96
97 //CHECK
98 public String printState() {
99 String result = "";
100
101 for(int i=0; i< ChessCoordinate.MAX; i++) {
102 for(int j=0; j< ChessCoordinate.MAX; j++)
103 result += ChessPiece.find(state[i][j])+" | ";
104 result += "\n";
105 }
106 return result;
107 }
108 }