-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.py
163 lines (133 loc) · 5.89 KB
/
game.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import hashlib
import subprocess
from calendar import timegm
from datetime import datetime
from typing import List, Tuple, Union, Set
import pygame.display
from board import build_pieces
from color import Color
from game_result import GameResult
from game_state import GameState
from log import get_session_id, init_files, write_board, write_message, MESSAGE_FILENAME, write_an, GAMES_FILENAME, \
init_game_log_file, write_game
from move import Move
from moves import get_all_moves
from moves_applier import apply_move
from piece import Piece
from utils import board_to_string, show_board
def start_game(args: List[str]) -> None:
"""Starts 10 games"""
path = get_session_id()
init_game_log_file(path)
white_points = 0.0
black_points = 0.0
pygame.init() # pylint: disable=maybe-no-member
screen = pygame.display.set_mode((1280, 720))
for i in range(0, 10):
result = play(screen, 8, args, i, path)
white_points += result.white
black_points += result.black
message = f'{result.get_white()}-{result.get_black()} ({result.message})'
print(message)
write_game(path, message + "\n")
show_board(screen, 8, result.pieces, path + GAMES_FILENAME)
start = timegm(datetime.utcnow().utctimetuple())
while timegm(datetime.utcnow().utctimetuple()) - start < 3:
pygame.event.pump()
message = f'Results: {white_points}-{black_points}'
print(message)
write_game(path, message)
show_board(screen, 8, {Piece(Color.WHITE, "", -1, -1)}, path + GAMES_FILENAME)
def play(screen: pygame.surface.Surface, board_size: int, args: List[str], game_id: int, base_path: str) -> GameResult:
"""Starts the game"""
message = f'Game {game_id}: '
write_game(base_path, message)
print(message, end="")
turn_number = 1
pieces = build_pieces(board_size)
path = init_files(base_path, game_id)
board = board_to_string(board_size, pieces)
write_board(path, board)
fifty_rule_count = 0
repetitions: dict[str, int] = {}
show_board(screen, board_size, pieces, base_path + GAMES_FILENAME)
while turn_number < 1000:
pygame.event.pump()
write_an(path, f'{turn_number}. ')
turn_color = Color.WHITE
result = play_turn(board_size, turn_number, turn_color, pieces, args[0],
path + "/" + MESSAGE_FILENAME)
pieces = result[1]
write_an(path, result[0])
board = board_to_string(board_size, pieces)
show_board(screen, board_size, pieces, base_path + GAMES_FILENAME)
write_board(path, board)
board_hash = hashlib.md5(board.encode('utf-8')).hexdigest()
board_count = repetitions.get(board_hash, 0)
repetitions[board_hash] = board_count + 1
end = check_end_turn(board_count + 1, fifty_rule_count, result[0], turn_color, pieces)
if end is not None:
write_an(path, f' {end.get_white()}-{end.get_black()}')
return end
if "+" in result[0]:
fifty_rule_count = 0
turn_color = Color.BLACK
result = play_turn(board_size, turn_number, turn_color, pieces, args[1],
path + "/" + MESSAGE_FILENAME)
pieces = result[1]
write_an(path, f' {result[0]}\n')
board = board_to_string(board_size, pieces)
show_board(screen, board_size, pieces, base_path + GAMES_FILENAME)
write_board(path, board)
board_hash = hashlib.md5(board.encode('utf-8')).hexdigest()
board_count = repetitions.get(board_hash, 0)
repetitions[board_hash] = board_count + 1
end = check_end_turn(board_count + 1, fifty_rule_count, result[0], turn_color, pieces)
if end is not None:
write_an(path, f' {end.get_white()}-{end.get_black()}')
return end
if "+" in result[0]:
fifty_rule_count = 0
turn_number += 1
fifty_rule_count += 1
return GameResult(0.5, 0.5, "Turns elapsed", pieces)
def check_end_turn(board_count: int, fifty_rule_count: int, move: str, color: Color, pieces: Set[Piece]) \
-> Union[GameResult, None]:
"""Check if the match is ended"""
if board_count == 3:
return GameResult(0.5, 0.5, "Repetition", pieces)
if fifty_rule_count == 50:
return GameResult(0.5, 0.5, "50 Rule", pieces)
if "#" in move:
return GameResult(1 if color == Color.WHITE else 0, 0 if color == Color.WHITE else 1, "Checkmate", pieces)
if "Stalemate" in move:
return GameResult(0.5, 0.5, "Stalemate", pieces)
return None
def encode_game_state(state: GameState) -> str:
"""Encodes the game state"""
return str(state)
def play_turn(board_size: int, turn_number: int, turn_color: Color, pieces: Set[Piece], cmd: str, path: str) \
-> Tuple[str, Set[Piece]]:
"""Asks the AI to return the move to be played"""
moves = get_all_moves(board_size, pieces, turn_color)
if len(moves) == 0:
return "Stalemate", pieces
state = GameState(board_size, turn_number, turn_color, pieces, moves)
encoded = encode_game_state(state)
write_message(path, encoded)
try:
process = subprocess.run(f'{cmd} "{path}"', timeout=5, shell=True, stdout=subprocess.PIPE,
bufsize=1,
check=True,
universal_newlines=True)
return process_move(pieces, process.stdout.replace("\n", ""), moves)
except subprocess.TimeoutExpired:
return "TimeoutExpired", pieces
except subprocess.CalledProcessError:
return "CalledProcessError", pieces
def process_move(pieces: Set[Piece], move: str, moves: List[Move]) -> Tuple[str, Set[Piece]]:
"""Process the move returned by the AI"""
move_detail = [m for m in moves if m.an_string == move]
if len(move_detail) == 0:
return f'Invalid Move {move}', pieces
return move, apply_move(pieces, move_detail[0])