Skip to content

Commit

Permalink
Update state.py
Browse files Browse the repository at this point in the history
  • Loading branch information
hmh17002 authored Dec 6, 2019
1 parent 331c381 commit a5bec75
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a5bec75

Please sign in to comment.