forked from LichessBot-Coders/Lichess-Coded-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessGame.java
82 lines (57 loc) · 1.7 KB
/
ChessGame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package chai;
import chesspresso.Chess;
import chesspresso.move.IllegalMoveException;
import chesspresso.move.Move;
import chesspresso.position.Position;
public class ChessGame {
public Position position;
public int rows = 8;
public int columns = 8;
public ChessGame() {
position = new Position(
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
}
public ChessGame(String str) {
position = new Position(str);
}
public int getStone(int col, int row) {
return position.getStone(Chess.coorToSqi(col, row));
}
public boolean squareOccupied(int sqi) {
return position.getStone(sqi) != 0;
}
public boolean legalMove(short move) {
for(short m: position.getAllMoves()) {
if(m == move) return true;
}
System.out.println(java.util.Arrays.toString(position.getAllMoves()));
System.out.println(move);
return false;
}
// find a move from the list of legal moves from fromSqi to toSqi
// return 0 if none available
public short findMove(int fromSqi, int toSqi) {
for(short move: position.getAllMoves()) {
if(Move.getFromSqi(move) == fromSqi &&
Move.getToSqi(move) == toSqi) return move;
}
return 0;
}
public void doMove(short move) {
try {
System.out.println("making move " + move);
position.doMove(move);
System.out.println(position);
} catch (IllegalMoveException e) {
System.out.println("illegal move!");
}
}
public static void main(String[] args) {
System.out.println();
// Create a starting position using "Forsyth–Edwards Notation". (See
// Wikipedia.)
Position position = new Position(
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
System.out.println(position);
}
}