diff --git a/state.py b/state.py index 4284965..fa8e00d 100644 --- a/state.py +++ b/state.py @@ -1,10 +1,15 @@ import math -max_depth = 0 +WHITE = "W" +BLACK = "B" +playerColor = WHITE +opponentColor = BLACK +max_depth = 3 + def Minimax(state, depth, maximizingPlayer, alpha, beta): if depth == max_depth: return state.eval() - if maximizingPlayer: + if maximizingPlayer == playerColor: best_move = -1 * math.inf for successor_state in state.successors(): val = Minimax(successor_state, depth + 1, False, alpha, beta) @@ -23,15 +28,14 @@ def Minimax(state, depth, maximizingPlayer, alpha, beta): break return best_move -WHITE = "W" -BLACK = "B" - class State: - def __init__(self): + def __init__(self, board = []): self.nrows = 18 self.ncols = 18 - self.board = [] - self.setup() + self.board = board + if board == []: + self.setup() + self.gameOver = 0 def setup(self): for row in range(self.nrows): @@ -49,7 +53,7 @@ def removePiece(self, row, col): self.board[row][col] = ' ' def placePiece(self, row, col, color): - if(not self.board[row][col]): + if self.board[row][col] == ' ': self.board[row][col] = color else: print("Error: attempted to place piece on occupied spot") @@ -138,8 +142,10 @@ def generate_moves(self, board, color): jump_to = (0, 0) up = down = left = right = 2 - for row in range(self.nrows + 1): - for col in range(self.ncols + 1): + for row in range(self.nrows): + #print("row: ", row ) + for col in range(self.ncols): + #print("col: ", col ) #Checking if it should search for black or white moves. if (board[row][col] == color): #Searching moves that can go up @@ -147,8 +153,9 @@ def generate_moves(self, board, color): #Current position curr_pos = (row, col) #Is move within the scope of the board? - while ((curr_pos[0] - up) > 0): + while ((curr_pos[0] - up) >= 0): jump_to = (curr_pos[0] - up, col) + #print(jump_to[0], jump_to[1]) #Is there a blank space where we need to jump, and is there an opponent's piece to jump over? if (board[jump_to[0]][jump_to[1]] == ' ' and board[jump_to[0]+1][jump_to[1]] != ' '): #Append move if possible @@ -161,8 +168,9 @@ def generate_moves(self, board, color): #Searching moves that can go down curr_pos = (row, col) #Is move within the scope of the board? - while ((curr_pos[0] + down) < self.ncols+1): + while ((curr_pos[0] + down) < self.ncols): jump_to = (curr_pos[0] + down, col) + #print(jump_to[0], jump_to[1]) #Is there a blank space where we need to jump, and is there an opponent's piece to jump over? if (board[jump_to[0]][jump_to[1]] == ' ' and board[jump_to[0]-1][jump_to[1]] != ' '): #Append move if possible @@ -176,8 +184,9 @@ def generate_moves(self, board, color): #Current position curr_pos = (row, col) #Is move within scope of the board? - while ((curr_pos[1] + right) < self.nrows+1): + while ((curr_pos[1] + right) < self.nrows): jump_to = (row, curr_pos[1] + right) + #print(jump_to[0], jump_to[1]) #Is there a blank space where we need to jump, and is there an opponent's piece to jump over? if (board[jump_to[0]][jump_to[1]] == ' ' and board[jump_to[0]][jump_to[1]-1] != ' '): #Append move if possible @@ -190,8 +199,9 @@ def generate_moves(self, board, color): #Searching for moves that can go right curr_pos = (row, col) #Is move within scope of the board? - while ((curr_pos[1] - left) > 0): + while ((curr_pos[1] - left) >= 0): jump_to = (row, curr_pos[1] - left) + #print(jump_to[0], jump_to[1]) #Is there a blank space where we need to jump, and is there an opponent's piece to jump over? if (board[jump_to[0]][jump_to[1]] == ' ' and board[jump_to[0]][jump_to[1]+1] != ' '): #Append move if possible @@ -202,25 +212,26 @@ def generate_moves(self, board, color): break return possible_moves - #Generate successors for moves + #Generate successors for moves, needs implemented def generate_successors(self, board, color): - successors = [] - for move in self.generate_moves(board, color): + successors = [] + for move in self.generate_moves(board, color): board_copy = State(board) board_copy.movePiece(move[0], move[1]) + if color == WHITE: successors.append(board_copy.board, color, move) - return successors + return successors #Evaluation function def eval(self, board, playerColor, opponentColor): player_moves = self.generate_moves(playerColor) opponent_moves = self.generate_moves(opponentColor) if player_moves == 0: - return -1 * math.inf - if opponent_moves == 0: - return math.inf - return len(player_moves) - (len(opponent_moves)*3) - + return -1 * math.inf + if opponent_moves == 0: + return math.inf + return len(player_moves) - (len(opponent_moves)*3) + def makeMove(self, color): if len(self.generate_moves(self.board, color)) > 0: move = Minimax(self, 0, color, math.inf * -1, math.inf)