-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
82 lines (55 loc) · 2.35 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
import os
import time
import torch
from zechpaz.common.utils import b_to_array,print_board,eval_pos,search_positions
class Game:
def __init__(self,board,net,color='white',depth=2):
if color not in ['white','black']:
raise ValueError('color must be black or white')
self.board = board
self.net = net
self.color = color
self.ai_mode = 'max' if self.color == 'black' else 'min'
self.depth = depth
def make_move_ai(self):
if self.board.turn == (self.color == 'black'):
legal_moves = [self.board.san(i) for i in list(self.board.legal_moves)]
poss = search_positions(self.board,depth=self.depth)
try:
self.board.push_san(legal_moves[eval_pos(poss[-1],self.net,mode=self.ai_mode)])
except IndexError:
if self.board.is_checkmate():
print('AI checkmate')
else:
print('Jeu terminé')
return self.board.is_game_over()
def make_move_human(self):
if self.board.turn == (self.color == 'white'):
legal_moves = [self.board.san(i) for i in list(self.board.legal_moves)]
while True:
move = input('Move >_ ')
if move in legal_moves:
self.board.push_san(move)
break
print(legal_moves)
if self.board.is_checkmate():
print('AI checkmate')
elif self.board.is_game_over():
print('Jeu terminé')
return self.board.is_game_over()
def make_move_ai__(self):
mode = 'max' if self.board.turn else 'min'
legal_moves = [self.board.san(i) for i in list(self.board.legal_moves)]
poss = search_positions(self.board,depth=self.depth)
try:
self.board.push_san(legal_moves[eval_pos(poss[-1],self.net,mode=mode)])
except IndexError:
if self.board.is_checkmate():
print(f'AI({"White" if self.board.turn else "Black"}) checkmate')
else:
print('Jeu terminé')
return self.board.is_game_over()
def print_board(self):
if os.system('cls') == 1:
os.system('clear')
print_board(self.board,self.color=='black')