-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.py
102 lines (95 loc) · 3.88 KB
/
Player.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
import chess
from hardware import *
from Points import piecePoints
import Points
from MoveEval import MoveEval
from math import inf
import pandas as pd
class Player:
# parameters
recommendMoves = False
def __init__(self, recommendMoves):
self.recommendMoves = recommendMoves
def recommend(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, 4)
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.recommend(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.recommend(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, legalMoves):
recMove=MoveEval("",0)
if(self.recommendMoves == True):
recMove = self.recommend(board, depth, turn, MoveEval(
"", -inf), MoveEval("", inf))
# Light up board for AI recommended move: BLUE
setLEDS([(recMove.move[-2:], BLUE), (recMove.move[0:2], BLUE)])
print('\nRECOMMENDED MOVE:', recMove.move)
flag = False
for x in range(len(legalMoves)):
if (len(legalMoves[x]) == 5):
flag = True
legalMoves[x] = legalMoves[x][:-1]
move = get_move(legalMoves,recMove=recMove.move,inCheck=board.is_check())
while(1):
print('TRIED TO PUSH:',move)
try:
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 += '.'
# print(boardlist)
# adding the string to the csv
#df = pd.read_csv(historyFile)
data = {'Moves': [boardlist]}
df2 = pd.DataFrame(data)
df2.to_csv(historyFile, mode='a', index=False, header=False)
return (board)
except Exception as e:
print('INVALID MOVE\n')
if (flag):
move += 'q'
print('PROMOTING TO QUEEN', move)
print(e)