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 10, 2019
1 parent 9ec9bb9 commit 4ce75a1
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions state.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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):
Expand All @@ -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")
Expand Down Expand Up @@ -138,17 +142,20 @@ 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

#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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 4ce75a1

Please sign in to comment.