Question
My program doesn't play chess automatically, how can I fix it. from othello.OthelloUtil import getValidMoves, makeMove, isEndGame import time class BOT(): def __init__(self, max_depth=3, max_time=180):
My program doesn't play chess automatically, how can I fix it.
from othello.OthelloUtil import getValidMoves, makeMove, isEndGame
import time
class BOT():
def __init__(self, max_depth=3, max_time=180):
self.max_depth = max_depth
self.max_time = max_time
def getBestMove(self, game_state, player):
start_time = time.time()
best_move = (-1, -1)
best_score = float('-inf') if player == BLACK else float('inf')
for move in getValidMoves(game_state, player):
new_game_state = game_state.clone()
executeMove(new_game_state, player, move)
score = self.min_max(new_game_state, self.max_depth-1, -player, start_time)
if player == BLACK:
if score > best_score:
best_move = move
best_score = score
else:
if score < best_score:
best_move = move
best_score = score
return best_move
def min_max(self, game_state, depth, player, start_time):
if depth == 0 or isEndGame(game_state) != None:
return self.evaluate(game_state, player)
if time.time()-start_time > self.max_time:
raise TimeoutError
best_score = float('-inf') if player == BLACK else float('inf')
for move in getValidMoves(game_state, player):
new_game_state = game_state.clone()
executeMove(new_game_state, player, move)
score = self.min_max(new_game_state, depth-1, -player, start_time)
if player == BLACK:
best_score = max(best_score, score)
else:
best_score = min(best_score, score)
return best_score
def evaluate(self, game_state, player):
# Implement a function to evaluate the current game state
# and return a score for the given player.
pass
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started