diff --git a/state.py b/state.py index fa8e00d..5b3b859 100644 --- a/state.py +++ b/state.py @@ -1,38 +1,51 @@ -import math +import math, copy WHITE = "W" BLACK = "B" +players = [BLACK, WHITE] playerColor = WHITE opponentColor = BLACK max_depth = 3 def Minimax(state, depth, maximizingPlayer, alpha, beta): if depth == max_depth: - return state.eval() + return state.eval(), None if maximizingPlayer == playerColor: - best_move = -1 * math.inf - for successor_state in state.successors(): - val = Minimax(successor_state, depth + 1, False, alpha, beta) - best_move = max(best_move, val) - alpha = max(alpha, best_move) - if beta <= alpha: - break - return best_move + best_move = None + for successor_state in state.generate_successors(state.board, maximizingPlayer): + val, move = Minimax(successor_state, depth + 1, False, alpha, beta) + if val == max(val, alpha): + alpha = val + best_move = successor_state.last_move + if alpha >= beta: + return beta, best_move + #best_val = max(best_move, val) + #alpha = max(alpha, best_move) + #if beta <= alpha: + # break + return alpha, best_move else: - best_move = math.inf - for successor_state in state.successors(): - val = Minimax(successor_state, depth + 1, True, alpha, beta) - best_move = min(best_move, val) - beta = min(beta, best_move) + best_move = None + for successor_state in state.generate_successors(state.board, maximizingPlayer): + val, move = Minimax(successor_state, depth + 1, True, alpha, beta) + if val == min(val, beta): + beta = val + best_move = successor_state.last_move if beta <= alpha: - break - return best_move + return alpha, best_move + #best_move = min(best_move, val) + #beta = min(beta, best_move) + #if beta <= alpha: + # break + #print(best_move) + return beta, best_move class State: - def __init__(self, board = []): + def __init__(self, board = [], player = 'B', last_move = ()): self.nrows = 18 self.ncols = 18 self.board = board + self.last_move = last_move if board == []: self.setup() self.gameOver = 0 @@ -59,13 +72,21 @@ def placePiece(self, row, col, color): print("Error: attempted to place piece on occupied spot") def movePiece(self, from_position, to_position): - piece_color = piece(from_position[0],from_position[1]) - row_range = range(from_position[0], to_position[0]) - column_range = range(from_position[1], to_position[1]) + #print("Hello") + piece_color = self.piece(from_position[0],from_position[1]) + #print(from_position[0], to_position[0]+1) + #print(from_position[1], to_position[1]+1) + row_range = range(*sorted([from_position[0], to_position[0]+1])) + column_range = range(*sorted([from_position[1], to_position[1]+1])) + #print("row_range: ", row_range) + #print("column_range: ", column_range) + self.removePiece(from_position[0], from_position[1]) for x in row_range: + #print("row: ", x) for y in column_range: + #print("column: ", y) self.removePiece(x,y) - placePiece(to_position[0], to_position[1], piece_color) + self.placePiece(to_position[0], to_position[1], piece_color) def __str__(self): string = "" @@ -216,10 +237,12 @@ def generate_moves(self, board, color): def generate_successors(self, board, color): successors = [] for move in self.generate_moves(board, color): - board_copy = State(board) + board_copy = copy.deepcopy(State(board)) board_copy.movePiece(move[0], move[1]) - if color == WHITE: - successors.append(board_copy.board, color, move) + if color == BLACK: + successors.append(State(board_copy.board, WHITE, move)) + else: + successors.append(State(board_copy.board, BLACK, move)) return successors #Evaluation function @@ -235,6 +258,6 @@ 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) - self.movePiece(move[0], move[1]) + self.movePiece(move[1][0], move[1][1]) else: self.gameOver = 1