def main(): # Create a new game game = TicTacToe() # Game loop while game.running: if game.turn == CROSS: human_player(game) else: computer_player(game)
def user_interface(): # user inputs characters as options and different functions are called # under the hood. print(" ** MENU ** ") print("Input a letter corresponding to the following options\n") print("i ---- instructions") print("r ---- play against random player") print("t ---- 2 player human game") print("g ---- play against an agent that uses Binary XOR Evaluation") print("b ---- CHALLENGE! Can you beat the binary XOR evaluation Agent") print("d ---- Play human vs dynamic agent") print("m ---- Mine Solutions") #print("a ---- Play versus a winning state") print("q ---- exit\n") p = input() #initialize the players if (p == 'r'): playerA = players.human_player() playerB = players.random_player() elif (p == 't'): playerA = players.human_player() playerB = players.human_player() elif (p == 'g'): playerA = players.human_player() playerB = players.binary_player() elif (p == 'i'): instructions() exit() elif (p == 'b'): binary_challenge() exit() elif (p == 'd'): dynamic_game() exit() elif (p == "m"): mine_solutions() exit() elif (p == 'q'): print("Goodbye!") exit() else: print("Error: Invalid Input") exit() #initialize game my_game = nim_game.nim_game(random_board(6, 9)) #play the game my_game.play(playerA, playerB) return
def dynamic_game(): playerA = players.human_player() playerB = players.dynamic_player("init_cache.txt") game = nim_game.nim_game([1, 2, 3, 4, 5, 6]) game.play(playerA, playerB) playerB.save_cache("dynamic_game_cache.txt")
def binary_challenge(): print("BINARY: Finally a worthy opponent!") print("BINARY: Would you like to move first or second? [1/2]") while True: t = int(input()) if t == 1 or t == 2: break print("Invalid input, try again") board = random.choice([0, 1, 2, 3, 4, 5]) #player moves first if t == 1: playerA = players.human_player() playerB = players.binary_player() game = nim_game.nim_game(challenge_boards1[board]) #grundy moves first else: playerA = players.binary_player() playerB = players.human_player() game = nim_game.nim_game(challenge_boards2[board]) game.play(playerA, playerB) exit()
def run(self): self.game_running = True self.mainlopp() #para o loop principal fechando o jogo def close_game(self): self.game_running = False class player_control(): def get_current_player(self): return self.players[self.index_current_player % len(self.players)] def set_current_player(self): player = self.get_current_player() if (player.complet_move == True): player.complet_move = False self.index_current_player = self.index_current_player + 1 if (self.index_current_player > len(self.players) - 1): self.index_current_player = 0 def __init__(self, game, players): self.game = game self.players = players self.index_current_player = 0 if __name__ == "__main__": players = (human_player(0), human_player(1)) app = game(players) app.run()
def get_move(player_name): if player_name == 'human': return players.human_player()
print("ilegal move..try again") def play(self): while True: # assert if game has ended on previous loop endGame = self.endGame() if endGame: self.showEndGameMessage(False) exit(0) self.view() print("\nPlayer {}: ".format(self.turn)) # wait for player input pl = self.getPlayerOfTurn() movement = pl.play(self.board) print("") # process move legalMove = self.assertIfMoveIsLegal(movement) if legalMove: self.processLegalMove(movement) else: self.processIlegalMove(movement) P1 = human_player("P1", 'X') P2 = rand_player('P2', "O") game = tictactoe(player1=P1, player2=P2) game.play()