-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle-test.py
131 lines (111 loc) · 4.99 KB
/
puzzle-test.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from utils.xiangqi import Xiangqi
from randomalgo import RandomAlgo
from evaluationalgo import EvaluationAlgo
from PValgo import PVAlgo
# Replace this line with your algo
algo_p = lambda: PVAlgo()
def test_puzzle(puzzle_name, move_limit, expected_moves):
"""Tests a puzzle, a definite sequence of checks/kills to checkmate.
"""
print(f"Testing puzzle {puzzle_name}")
with open(f"puzzles/{puzzle_name}.in", 'r') as f:
board_string = f.read()
board = Xiangqi.from_string(board_string)
moves = []
algo = algo_p()
for _ in range(2 * move_limit):
move = algo.next_move(board)
if move is None:
break
print(f"Move: {move.to_notation(board)}")
moves.append(move.to_notation(board))
board = board.move(move)
print(f"Result: {moves}")
print(f"Expected: {expected_moves}")
actions = board.actions()
if len(actions) == 0 and not board.turn and len(moves) >= len(expected_moves):
print("Puzzle SOLVED!")
else:
print("Puzzle FAILED!")
print()
def test_endgame(endgame_name, is_red_win, move_limit=40):
"""Tests an endgame.
is_red_win indicates whether red is expected to win.
"""
print(f"Testing endgame {endgame_name}")
if is_red_win:
print("Red is expected to win")
else:
print("A draw is expected")
with open(f"endgames/{endgame_name}.in", 'r') as f:
board_string = f.read()
board = Xiangqi.from_string(board_string)
moves = []
algo = algo_p()
for i in range(2 * move_limit):
if i % 10 == 0:
print(board)
move = algo.next_move(board)
if move is None:
break
print(f"Move: {move.to_notation(board)}")
moves.append(move.to_notation(board))
board = board.move(move)
print(f"Moves: {moves}")
print(f"Final result: {winner(board)}")
print()
def winner(board):
"""Returns a string representing the result of the game.
board is the final board of the game.
"""
actions = board.actions()
if len(actions) == 0:
return 'Red wins' if not board.turn else 'Black wins'
else:
return 'Draw'
def test_puzzles():
test_puzzle("3-move/wocaoma", 5, ["H8+7", "K5.6", "R3.4", "A5+6", "R4+1"])
test_puzzle("3-move/advisor-hand", 5, ["R6+1", "A5-4", "H8-6", "K5+1", "R7+3"])
test_puzzle("3-move/double-rooks", 5, ["R6+1", "K5+1", "R3+4", "K5+1", "R6-2"])
test_puzzle("3-move/rook-double-cannons", 5, ["R2+6", "K6+1", "R2-1", "K6-1", "C3+2"])
print()
test_puzzle("4-move/cannon-behind-horse", 6, ["R5+3", "K4+1", "R5.6", "K4-1", "C7.6", "A4-5", "H4+6"])
test_puzzle("4-move/double-cannons", 6, ["R4+5", "K5+1", "R4.5", "K5.4", "C5.6", "A4-5", "C2.6"])
test_puzzle("4-move/elephant-block-king", 6, ["R7-1", "K4+1", "R7-1", "K4-1", "R7+1", "K4+1", "A5+6"])
test_puzzle("4-move/wocaoma", 6, ["H2+3", "K5+1", "H3-4", "K5-1", "R2.5", "A4+5", "H4+3"])
print()
test_puzzle("6-move/fishing-horse", 7, ["R1+2", "K6+1", "H1+2", "A5+4", "R1.5", "+A-5", "H2+3", "K6+1", "R5.2", "P3.4", "R2-2"])
test_puzzle("6-move/wocaoma-double-cannons", 7, ["H5+7", "K5.4", "R8.6", "C1.4", "R6+1", "A5+4", "C5.6", "A4-5", "C6-3", "H8+6", "C9.6"])
print()
test_puzzle("7-move/fishing-horse", 9, ["R4+7", "K5.6", "R2+3", "E5-7", "R2.3", "K6+1", "H2+3", "K6+1", "R3-2", "K6-1", "R3.2", "K6-1", "R3+2"])
test_puzzle("7-move/rook-double-cannons", 9, ["C2+3", "K5+1", "R2+5", "K5+1", "C1-2", "A6-5", "R2-1", "A5+6", "R2-1", "K5-1", "R2+2", "K5-1", "C1+2"])
test_puzzle("7-move/double-rooks-king", 9, ["R5.4", "A5+6", "R4+3", "K6.5", "R7+4", "K5-1", "R7+1", "K5+1", "R7-1", "K5-1", "R4.5", "A4-5", "R7+1"])
test_puzzle("7-move/advisor-hand-cannon-horse", 9, [
["H2+4", "K5.6", "C5.4", "R9.6", "C1+7", "K6+1", "H4+2", "R6.9", "C1-1", "R9-5", "H2-4", "K6+1", "P3.4"],
["H2+4", "K5.6", "C5.4", "R9.6", "C1+7", "K6+1", "H4+2", "R6.9", "H2-3", "K6+1", "P3.4", "R9.6", "P4+1"],
])
def test_endgames():
print()
test_endgame("easy/rook-vs-advisors", True)
test_endgame("easy/rook-vs-advisors-cannon", False)
test_endgame("easy/rook-vs-elephants", True)
test_endgame("easy/cannon-vs-advisors", True)
test_endgame("easy/cannon-vs-advisors-elephant", False)
test_endgame("easy/one-pawn", True)
test_endgame("easy/one-horse", True)
test_endgame("easy/rook-cannon-vs-rook-central", True)
test_endgame("easy/rook-cannon-vs-rook-no-central", False)
print()
test_endgame("medium/cannon-horse-vs-full-defenders", True)
test_endgame("medium/one-horse-vs-advisor", True)
test_endgame("medium/rook-horse-vs-rook-advisors", True)
test_endgame("medium/rook-horse-vs-rook-full-defenders", False)
print()
test_endgame("hard/rook-vs-advisors-horse", True)
test_endgame("hard/cannon-horse-vs-full-defenders-horse", True)
test_endgame("hard/horses-vs-full-defenders", True)
def main():
# test_puzzles()
test_endgames()
if __name__ == '__main__':
main()