-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI.py
122 lines (111 loc) · 4.4 KB
/
AI.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
from MoveEval import MoveEval
import chess
from Player import Player
from hardware import *
import Points
import random
from math import inf
import copy
import pandas as pd
class AI(Player):
difficulty = 0
def __init__(self, recommendMoves, difficulty):
super().__init__(recommendMoves)
self.difficulty = difficulty
def minimax(self, board, depth, turn, alpha, beta):
#boardCopy = copy.deepcopy(board)
if depth == 0 or board.is_checkmate() or board.is_stalemate():
eval = Points.heuristic(board, turn, self.difficulty)
# print('BOARD:',board,'RETURNED EVALUATION OF:',eval)
endEval = MoveEval("empty", eval)
return endEval
if(not turn):
maxValue = MoveEval("", -inf)
for i in board.legal_moves:
#boardCopy = copy.deepcopy(board)
# print(boardCopy)
# print('\n')
board.push(i)
value = self.minimax(board, depth - 1, True,
alpha, beta)
board.pop()
# print(value)
if(value.evaluation > maxValue.evaluation):
maxValue = value
maxValue.move = i.uci()
if (maxValue.evaluation > alpha.evaluation):
alpha = maxValue
if (beta.evaluation <= alpha.evaluation):
break
return maxValue
else:
minValue = MoveEval("", inf)
for i in board.legal_moves:
#boardCopy = copy.deepcopy(board)
# print(boardCopy)
# print('\n')
board.push(i)
value = self.minimax(
board, depth - 1, False, alpha, beta)
board.pop()
# print(value)
if(value.evaluation < minValue.evaluation):
minValue = value
minValue.move = i.uci()
if (minValue.evaluation < beta.evaluation):
beta = minValue
if (beta.evaluation <= alpha.evaluation):
break
return minValue
def makeMove(self, board, depth, turn, historyFile):
print('\n'+'\U0001F914'+"...Thinking..."+'\U0001F914'+'\n')
tuple = self.minimax(board, depth, turn, MoveEval(
"", -inf), MoveEval("", inf))
print('MAKING A MOVE OF EVALUATION:', tuple.evaluation)
move = tuple.move
# Light up board for AI move: BLUE
setLEDS([(move[-2:], BLUE), (move[0:2], BLUE)])
print("Move made is: ", move)
get_move([move],recMove=move,inCheck=board.is_check())
board.push_san(move)
# string form of the board
boardlist = "" # list()
columns = chess.FILE_NAMES
for j in reversed(range(1, 9)):
for i in columns:
sqr = board.piece_at(chess.parse_square(i+str(j)))
if (sqr != None):
boardlist += sqr.symbol()
else:
boardlist += '.'
# adding the string to the csv
data = {'Moves': [boardlist]}
df = pd.DataFrame(data)
df.to_csv(historyFile, mode='a', index=False, header=False)
return (board)
# history.append(board.fen())
def makeFirstMove(self, board, historyFile):
firstmoves = []
for i in board.legal_moves:
firstmoves.append(i.uci())
rnd = random.randint(0, len(firstmoves)-1)
move = firstmoves[rnd]
setLEDS([(move[-2:], BLUE), (move[0:2], BLUE)])
get_move([move],recMove=move,inCheck=board.is_check())
board.push_san(move)
# string form of the board
boardlist = ""
columns = chess.FILE_NAMES
for j in reversed(range(1, 9)):
for i in columns:
sqr = board.piece_at(chess.parse_square(i+str(j)))
if (sqr != None):
boardlist += sqr.symbol()
else:
boardlist += '.'
# adding the string to the csv
data = {'Moves': [boardlist]}
df = pd.DataFrame(data)
df.to_csv(historyFile, mode='a', index=False, header=False)
return (board)
# history.append(board.fen())