-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (54 loc) · 1.85 KB
/
main.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
from utils.xiangqi import Xiangqi, InvalidMoveException
from PValgo import PVAlgo
import sys
# Replace this line with your algo
algo = PVAlgo()
def main():
print('Welcome! In this programme, you can test out your algorithm by manually playing against it.')
print('Note that the board will always be displayed with red side at the bottom and black side on top.')
if len(sys.argv) > 1:
board = read_board(sys.argv[1])
else:
board = Xiangqi()
turn_string = input('Please decide if you want to play as red (go first) or black (go second): (R/B) ')
while turn_string != 'R' and turn_string != 'B':
turn_string = input('Invalid, please key in again: (R/B) ')
if turn_string == 'R':
human_turn, algo_turn = True, False
else:
human_turn, algo_turn = False, True
actions = board.actions()
while len(actions) != 0:
print('Current board:')
print(board)
print()
if board.turn == algo_turn:
move = algo.next_move(board)
print(f'Algo choose: {move.to_notation(board)}')
else:
move_string = input('Please key in your move: ')
try:
move = board.parse_move(move_string)
except InvalidMoveException as e:
print(e)
continue
except Exception as e:
print(f"Unhandled exception: {e}")
continue
board = board.move(move)
print()
actions = board.actions()
print('Current board:')
print(board)
print()
if board.turn == algo_turn:
print("You won!")
else:
print("Algo won!")
def read_board(filename):
print("Reading from board")
with open(filename) as f:
board_string = f.read()
return Xiangqi.from_string(board_string)
if __name__ == '__main__':
main()