Skip to content

Commit

Permalink
Update state.py
Browse files Browse the repository at this point in the history
  • Loading branch information
zew15101 authored Dec 12, 2019
1 parent 86f9825 commit 352b3f8
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@

def Minimax(state, depth, maximizingPlayer, alpha, beta):
if depth == max_depth:
return state.eval(), None
return state.eval(state.board, playerColor, opponentColor), None
if maximizingPlayer == playerColor:
best_move = None
for successor_state in state.generate_successors(state.board, maximizingPlayer):
val, move = Minimax(successor_state, depth + 1, False, alpha, beta)
if players[0] == maximizingPlayer:
val, move = Minimax(successor_state, depth + 1, players[1], alpha, beta)
else:
val, move = Minimax(successor_state, depth + 1, players[0], alpha, beta)
if val == max(val, alpha):
alpha = val
best_move = successor_state.last_move
Expand All @@ -27,7 +30,10 @@ def Minimax(state, depth, maximizingPlayer, alpha, beta):
else:
best_move = None
for successor_state in state.generate_successors(state.board, maximizingPlayer):
val, move = Minimax(successor_state, depth + 1, True, alpha, beta)
if players[0] == maximizingPlayer:
val, move = Minimax(successor_state, depth + 1, players[1], alpha, beta)
else:
val, move = Minimax(successor_state, depth + 1, players[0], alpha, beta)
if val == min(val, beta):
beta = val
best_move = successor_state.last_move
Expand Down Expand Up @@ -247,8 +253,8 @@ def generate_successors(self, board, color):

#Evaluation function
def eval(self, board, playerColor, opponentColor):
player_moves = self.generate_moves(playerColor)
opponent_moves = self.generate_moves(opponentColor)
player_moves = self.generate_moves(board, playerColor)
opponent_moves = self.generate_moves(board, opponentColor)
if player_moves == 0:
return -1 * math.inf
if opponent_moves == 0:
Expand All @@ -258,6 +264,7 @@ def eval(self, board, playerColor, opponentColor):
def makeMove(self, color):
if len(self.generate_moves(self.board, color)) > 0:
move = Minimax(self, 0, color, math.inf * -1, math.inf)
print(move)
self.movePiece(move[1][0], move[1][1])
else:
self.gameOver = 1

0 comments on commit 352b3f8

Please sign in to comment.