Example #1
0
def play(q_agent, human=0, helper=False, helper_depth=20):
    """
    Play human game against the QAgent.
    `human` can be set to 0 or 1 to specify whether
    human moves first or second.
    """

    connect4 = Connect4()
    helper = Minimax(max_depth=helper_depth) if helper else None

    while True:

        for l in range(0, 42, 7):
            row = ''.join([f"{connect4.board[l + i]}|" for i in range(7)])
            print(row[:13])
            print('-+-+-+-+-+-+-')

        actions = connect4.available_actions(connect4.board)

        if connect4.player == human:
            print("Your Move.")

            while True:
                column = int(input().strip()) - 1

                if not column in actions:
                    print(
                        'That place is already filled or invalid. Still your move.'
                    )
                else:
                    break

            # column, values = helper.get_move(connect4.board)
        else:
            print("QAgent's Move.")

            if helper:
                action, values = helper.get_move(connect4.board)
                if values.count(1000) >= 1 or values.count(-1000) >= 1:
                    column = action
                else:
                    column = q_agent.choose_action(connect4.board,
                                                   epsilon=False)
            else:
                column = q_agent.choose_action(connect4.board, epsilon=False)

            print(f"QAgent put a chip in column {column + 1}.")

        connect4.move(column)

        if connect4.result is not None:
            print("\nGAME OVER\n")

            winner = "Human" if connect4.result == human else "QAgent"
            print(f"Winner is {winner}")

            break

    if input("Play again?\n").lower() == "y":
        play(q_agent)
Example #2
0
 def __compare_bots(self, iterations):
     if self.state.playerToMove == self.player1:
         assert self.state.playerToMove.algorithm == "ISMCTS"
         return self.smart_ismcts.get_move(rootstate=self.state,
                                           itermax=iterations,
                                           verbose=False)
     else:
         assert self.state.playerToMove.algorithm == "Minimax"
         return Minimax.get_move(self.state)
Example #3
0
	def ai(self,event):
		print "AI"
		self.new_game()
		ai = Minimax()
		while not self.board.has_lost():
			ai.state = self.board
			move = ai.get_move()
			self.board.move(move)
			if self.board.valid_move:
				new_tile = Tile(None)
				new_tile.set_start_value()
				self.board.set_new_tile(new_tile)
				self.update_board()
			else:
				self.update_board()
				break
		self.game_ended()