-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchessbots.py
More file actions
134 lines (110 loc) · 3.8 KB
/
Copy pathchessbots.py
File metadata and controls
134 lines (110 loc) · 3.8 KB
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
import chess
import numpy as np
import time
import threading
from random import shuffle
import pygame
from pygame.locals import (
KEYDOWN,
K_ESCAPE,
QUIT,
MOUSEBUTTONUP,
MOUSEBUTTONDOWN
)
class Player:
def __init__(self):
self.move_ready = False
self.move = None
def is_move_ready(self):
return self.move_ready
def get_move(self):
self.move_ready = False
move = self.move
self.move = None
return move
def request_move(self, state: chess.State):
return
class Human(Player):
def __init__(self):
super().__init__()
return
def _find_move(self, state: chess.State):
while True:
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
x_pos, y_pos = pygame.mouse.get_pos()
from_coords = [y_pos//100, x_pos//100]
elif event.type == MOUSEBUTTONUP:
x_pos, y_pos = pygame.mouse.get_pos()
to_coords = [y_pos//100, x_pos//100]
move = from_coords + to_coords
if state.is_valid_move(move):
self.move = move
self.move_ready = True
return
def request_move(self, state: chess.State):
thr = threading.Thread(target=self._find_move, args=(state,))
thr.start()
return
class RandomBot(Player):
def __init__(self):
super().__init__()
def _find_move(self, state: chess.State):
time.sleep(2)
poss_moves = state.get_possible_moves()
ind = np.random.randint(0, len(poss_moves))
self.move = poss_moves[ind]
self.move_ready = True
return
def request_move(self, state: chess.State):
thr = threading.Thread(target=self._find_move, args=(state,))
thr.start()
return
class MinMaxBot(Player):
class Node:
def __init__(self, state: chess.State):
self.state = state
self.eval = state.get_imbalance()
self.childs = []
def _create_childs(self):
self.poss_moves = self.state.get_possible_moves()
shuffle(self.poss_moves)
for move in self.poss_moves:
self.childs.append(MinMaxBot.Node(self.state.play_move(move)))
def build_tree(self, max_depth, depth=0):
if depth >= max_depth:
del self.state
return
self._create_childs()
color_to_move = self.state.color_to_move
del self.state
for child in self.childs:
child.build_tree(max_depth, depth+1)
# with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
# futures = [
# executor.submit(child.build_tree, max_depth, depth+1) for child in self.childs
# ]
# concurrent.futures.wait(futures)
childs_evals = [child.eval for child in self.childs]
if color_to_move == chess.Color.WHITE:
ind = np.argmax(childs_evals)
else:
ind = np.argmin(childs_evals)
self.eval = childs_evals[ind]
self.best_ind = ind
return
def get_best_move(self):
return self.poss_moves[self.best_ind]
def __init__(self, max_depth):
super().__init__()
self.max_depth = max_depth
def _find_move(self, state: chess.State):
root = MinMaxBot.Node(state)
root.build_tree(self.max_depth)
self.move = root.get_best_move()
self.move_ready = True
return
def request_move(self, state: chess.State):
thr = threading.Thread(target=self._find_move, args=(state,))
thr.start()
return