def negaMaxEval(self, arrRep, agent_number, opponent_number, depth, validRowValue, alpha, beta): import hashlib #hashValue=hash(frozenset[arrRep]) hashValue = hashlib.sha256(str(arrRep).encode('utf-8', 'ignore')).hexdigest() count = 0 if hashValue in self.stateScore: return None, self.stateScore[hashValue] if depth == self.max_depth: score = self.evaluation_func(arrRep, validRowValue, agent_number, opponent_number) self.stateScore[hashValue] = score return None, score best_score = -99999999 best_action = None for col in range(0, 7, 1): current_column = col isAllowed, validRowValue = game.check_move(current_column, arrRep, validRowValue, agent_number) if not isAllowed: continue winner, game_over = game.game_over(arrRep, agent_number, opponent_number, current_column, validRowValue) if game_over: if winner == agent_number: best_currscore = 9999 elif winner == opponent_number: best_currscore = -9999 else: best_currscore = 0 else: best_submove, best_currscore = self.negaMaxEval( arrRep, opponent_number, agent_number, depth + 1, validRowValue, -beta, -alpha) best_currscore *= -1 #Previous Arrayrep is restored. rowNumber = validRowValue[current_column] arrRep[rowNumber - 1][current_column] = 0 validRowValue[current_column] -= 1 if best_currscore > best_score: best_score = best_currscore best_action = col if alpha < best_score: alpha = best_score if alpha >= beta: break if best_action is None: print "I was here" best_score = self.evaluation_func(arrRep, validRowValue, agent_number, opponent_number) self.stateScore[hashValue] = best_score return best_action, best_score
def main(): pygame.init() pygame.display.set_caption("Fanorona v0.9") gfx.init() game.init() while not game.finished: game.clock.tick(60) gfx.update() for event in pygame.event.get(): if event.type is pygame.QUIT: # QUIT # game.finished = True break if game.current == game.player: # player's turn if event.type is pygame.MOUSEBUTTONDOWN and event.button == 1: game.player_move( game.grid.collision(pygame.mouse.get_pos())) if event.type is pygame.KEYDOWN and event.key == pygame.K_ESCAPE: game.finished = True if game.current == game.cpu and not game.game_over(): # computer's turn game.cpu_move() if not game.moving and game.game_over(): game.finished = True if game.winner == game.player: gfx.text = "YOU WIN!" else: gfx.text = "YOU LOSE!" quitting = False while not quitting: game.clock.tick(60) gfx.update() for event in pygame.event.get(): if event.type is pygame.QUIT: quitting = True
def play_battleships(player1, player2): """ Play battleships :param player1, player2: Select players from agents 'human', 'random', 'MC', 'MC2' """ import game game = game.Game(size=10, ships=[5, 4, 3, 2, 2, 1, 1], player1=player1, player2=player2) # game = game.Game(size = 10, ships = [5]) game.initialize_game() game.print_gamestate() while not game.game_over(): game.one_turn() return game.winner
def die(): game.output("You die.") game.game_over()
"""Index""" import game game = game.Game() while game.running: game.start() game.run(-1) if game.g_o: game.game_over() game.quit_game()