-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAISearchAlgs.py
165 lines (155 loc) · 6.33 KB
/
AISearchAlgs.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
164
165
# This file is where I include the minimax algorithm for my chess AI which
# calls the neural network when it needs to evaluate a chess board.
import copy
from BackEndChess import *
from ChessBoard import *
import random
import NeuralNet
import ast
# Converts string of weights to actual list of weights.
with open("./RealWeights/RealWeights9.txt", "r") as myfile:
weightsF = myfile.read().replace('\n', '')
weightsL = ast.literal_eval(weightsF)
# Sets neural network weights to the weights stored in the text file.
myNet = NeuralNet.Net([64, 44, 18, 1])
for layer in range(len(myNet.layers)-1):
for neuron in range(len(myNet.layers[layer])):
myNet.layers[layer][neuron].outputWeights = weightsL[layer][neuron]
# Translates the chess board that is in the game to a form which is readable by the neural network.
def translateBoard(board):
tBoard = []
for row in range(7, -1, -1):
for col in range(8):
if board[row][col] != None:
if isinstance(board[row][col], Pawn):
if board[row][col].color == "White":
tBoard.append(.01)
else:
tBoard.append(-.01)
elif isinstance(board[row][col], Knight):
if board[row][col].color == "White":
tBoard.append(.02)
else:
tBoard.append(-.02)
if isinstance(board[row][col], Bishop):
if board[row][col].color == "White":
tBoard.append(.03)
else:
tBoard.append(-.03)
if isinstance(board[row][col], Rook):
if board[row][col].color == "White":
tBoard.append(.04)
else:
tBoard.append(-.04)
if isinstance(board[row][col], Queen):
if board[row][col].color == "White":
tBoard.append(.05)
else:
tBoard.append(-.05)
if isinstance(board[row][col], King):
if board[row][col].color == "White":
tBoard.append(.06)
else:
tBoard.append(-.06)
else:
tBoard.append(0.0)
return tBoard
# Makes dictionary of all moves where each piece object has its own list of moves.
def getAllMoves(board, AIcolor):
allMoves = dict()
for row in range(len(board)):
for col in range(len(board[0])):
if board[row][col] != None and board[row][col].color == AIcolor:
board[row][col].getMoves(board)
allMoves[board[row][col]] = board[row][col].moves
return allMoves
# This is the minimax search algorithm.
def minimaxSearch(board, AIcolor, data):
allMoves = getAllMoves(board, AIcolor)
bestScore = float("-inf")
curBoard = translateBoard(board)
tempB = copy.deepcopy(board)
if isCheckMate(tempB, AIcolor):
return tempB
for key in allMoves:
for move in allMoves[key]:
objCopy = copy.deepcopy(key)
tempB = copy.deepcopy(board)
tempB[move[0]][move[1]] = objCopy
tempB[objCopy.posRow][objCopy.posCol] = None
objCopy.posRow = move[0]
objCopy.posCol = move[1]
newT = copy.deepcopy(tempB)
if isCheck(newT, "Black"):
continue
tBoard = translateBoard(tempB)
score = minPart(tempB, 0, "White")
if sum(tBoard) < sum(curBoard):
newSum = (sum(tBoard)*-1 + sum(curBoard))*100
if abs(score) <= abs(newSum):
score = newSum
if score > bestScore:
bestMove = [key, move]
bestScore = score
return makeMove(bestMove, board)
# Given a move and a board, this function makes the move.
def makeMove(bestMove, board):
board[bestMove[1][0]][bestMove[1][1]] = bestMove[0]
board[bestMove[0].posRow][bestMove[0].posCol] = None
board[bestMove[1][0]][bestMove[1][1]].posRow = bestMove[1][0]
board[bestMove[1][0]][bestMove[1][1]].posCol = bestMove[1][1]
return board
# Used to find which move is the best for the human player in response to the AI's move.
def minPart(gameBoard, level, color):
if level == 1:
tBoard = translateBoard(gameBoard)
myNet.feedForward(tBoard)
return myNet.getResults()
bestScore = float("inf")
newMoves = getAllMoves(gameBoard, color)
curSum = sum(translateBoard(gameBoard))
for key in newMoves:
for move in newMoves[key]:
objCopy = copy.deepcopy(key)
tempB = copy.deepcopy(gameBoard)
tempB[move[0]][move[1]] = objCopy
tempB[objCopy.posRow][objCopy.posCol] = None
objCopy.posRow = move[0]
objCopy.posCol = move[1]
tBoard = translateBoard(tempB)
newSum = sum(tBoard)
if newSum > curSum:
score = (newSum*-1+curSum)*100
else:
score = maxPart(tempB, level+1, "Black")
newT = copy.deepcopy(tempB)
if isCheck(newT, "White"):
continue
if score < bestScore:
bestScore = score
return bestScore
# Used to find what the best response is for the AI after the human player has made his/her move.
# Currently just returns an evaluatation of the board using the neural network because going through
# an additional layer would take too much time.
def maxPart(gameBoard, level, color):
if level == 1:
tBoard = translateBoard(gameBoard)
myNet.feedForward(tBoard)
return myNet.getResults()
bestScore = float("-inf")
newMoves = getAllMoves(gameBoard, color)
for key in newMoves:
for move in newMoves[key]:
objCopy = copy.deepcopy(key)
tempB = copy.deepcopy(gameBoard)
tempB[move[0]][move[1]] = objCopy
tempB[objCopy.posRow][objCopy.posCol] = None
objCopy.posRow = move[0]
objCopy.posCol = move[1]
newT = copy.deepcopy(tempB)
if isCheck(newT, "Black"):
continue
score = minPart(tempB, level+1, "White")
if score > bestScore:
bestScore = score
return bestScore