From a5bec75c8bd2935c0ddb52b858223a4d97161d13 Mon Sep 17 00:00:00 2001 From: Huuduc M Huynh Date: Fri, 6 Dec 2019 14:17:54 -0500 Subject: [PATCH] Update state.py --- state.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/state.py b/state.py index 842ffca..d2900e6 100644 --- a/state.py +++ b/state.py @@ -98,3 +98,72 @@ def second_moves_set(self): secondMoves.append((9, 7)) return secondMoves + #Function to generate all possible moves, including multiple jumps + def generate_moves(self, board, color): + possible_moves = [] + jump_to = (0, 0) + up = down = left = right = 2 + + for row in range(self.nrows + 1): + for col in range(self.ncols + 1): + #Checking if it should search for black or white moves. + if (board[row][col] == color): + #Searching moves that can go up + + #Current position + curr_pos = (row, col) + #Is move within the scope of the board? + while ((curr_pos[0] - up) > 0): + jump_to = (curr_pos[0] - up, col) + #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 + possible_moves.append((row, col, jump_to[0], jump_to[1])) + #Update how far it'll jump next time (for multiple jumps) + curr_pos = jump_to + else: + break + + #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): + jump_to = (curr_pos[0] + down, col) + #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 + possible_moves.append((row, col, jump_to[0], jump_to[1])) + #Update how far it'll jump next time (for multiple jumps) + curr_pos = jump_to + else: + break + + #Searching for moves that can go left of the board + #Current position + curr_pos = (row, col) + #Is move within scope of the board? + while ((curr_pos[1] + right) < self.nrows+1): + jump_to = (row, curr_pos[1] + right) + #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 + possible_moves.append((row, col, jump_to[0], jump_to[1])) + #Update how far you'll jump next time (for multiple jumps) + curr_pos = jump_to + else: + break + + #Searching for moves that can go right + curr_pos = (row, col) + #Is move within scope of the board? + while ((curr_pos[1] - left) > 0): + jump_to = (row, curr_pos[1] - left) + #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 + possible_moves.append((row, col, jump_to[0], jump_to[1])) + #Update how far it'll jump next time (for multiple jumps) + curr_pos = jump_to + else: + break + return possible_moves