Example #1
0
def play():
    success = 0
    saver = HistorySaver()
    while success < 5000:
        white_player = RandomPlayer()
        black_player = RandomPlayer()
        game = Game(white_player, black_player)
        try:
            game.start()
            success += 1
            print("{0} Победитель:{1}".format(success, game.winner.name))
            saver.saveHistoryToFile(game.history, "games")
        except Exception as e:
            # print(str(e))
            pass
Example #2
0
    def test(self, episodes=100, draw=False):
        white_player = TDAgent(self)
        black_player = RandomPlayer()
        winners = [0, 0]
        historySaver = HistorySaver()
        for episode in range(episodes):
            game = Game(white_player, black_player)

            try:
                game.start()
            except Exception as e:
                historySaver.saveHistoryToFile(game.history, "excepttests")
                print("ой")
                continue

            winner = game.winner
            winners[winner.value] += 1
            if game.steps < 150:
                historySaver.saveHistoryToFile(game.history, "games5")
            winners_total = sum(winners)
            out = "[Episode %d] %s vs %s  %d:%d of %d games (%.2f%%)" % (
                episode, white_player.name, black_player.name, winners[0],
                winners[1], winners_total,
                (winners[0] / winners_total) * 100.0)
            print(out)
            file_name = "test_{0}".format(self.layer_size_hidden)
            with open("./tests/" + file_name, 'a') as f:
                f.write(out)
                f.write(".\n")
Example #3
0
    def __init__(self):
        # initialize AI's here
        self.playerA1 = RandomPlayer.SimpleStat("Tyler")
        self.playerA2 = RandomPlayer.RandomPlay("Hudson")
        self.playerB1 = RandomPlayer.RandomPlay("Shawn")
        self.playerB2 = RandomPlayer.RandomPlay("Matt")

        # make a list of the players for rotations
        self.players = [
            self.playerA1, self.playerB1, self.playerA2, self.playerB2
        ]

        self.playerA1.setRelations(self.playerA2, self.playerB1, self.playerB2)

        self.deck = list(
            allcards[:])  # get a deep copy of all_cards for dealing
Example #4
0
    def __init__(self,
                 coin_type,
                 player_type,
                 sequence_length,
                 mode,
                 opponent=None):
        # Initialize an AI with the proper type which are one of Random and
        # Q-learner currently

        ## In fact the agent can be used both for single player mode and AgentsLearn mode
        self.mode = mode

        if player_type == "random":
            self.player = randomPlaeyer.RandomPlayer(coin_type)
        if player_type == "qlearner":
            self.player = qlearningPlayer.QLearningPlayer(
                coin_type, sequence_length, mode)
        else:
            self.player = qlearningPlayer.QLearningPlayer(
                coin_type, sequence_length, mode)

        self.last_action = None
        self.coin_type = coin_type
        self.player_type = player_type
        self.table_learning = None
Example #5
0
def get_players():
    players = []
    # get the user input which contains the players type values and
    # create the player objects accordingly.
    input_list = sys.argv[1].split(',')
    for index, player in enumerate(input_list):
        if player == GOOBI_PLAYER_INDEX:
            goobi_player = GoobiPlayer.GoobiPlayer(index,
                                                   GOOBI_PLAYER_STRING_REP)
            players.append(goobi_player)
        elif player == LEARNING_PLAYER_INDEX:
            learning_player = learningAgent.ValueIterationPlayer(
                index, LEARNING_PLAYER_STRING_REP)
            players.append(learning_player)
        elif player == HEURISTIC_PLAYER_INDEX:
            heuristic_player = HeuristicPlayer.HeuristicPlayer(
                index, HEURISTIC_PLAYER_STRING_REP)
            players.append(heuristic_player)
        elif player == AZULAY_PLAYER_INDEX:
            azulay_player = AzulayPlayer.AzulayPlayer(
                index, AZULAY_PLAYER_STRING_REP)
            players.append(azulay_player)
        elif player == OFIR_PLAYER_INDEX:
            ofir_player = OfirPlayer.OfirPlayer(index, OFIR_PLAYER_STRING_REP)
            players.append(ofir_player)
        elif player == HILLEL_PLAYER_INDEX:
            hillel_player = HillelPlayer.HillelPlayer(
                index, HILLEL_PLAYER_STRING_REP)
            players.append(hillel_player)
        elif player == HUMAN_PLAYER_INDEX:
            human_player = HumanPlayer.HumanPlayer(index,
                                                   HUMAN_PLAYER_STRING_REP)
            players.append(human_player)
        elif player == RANDOM_PLAYER_INDEX:
            random_player = RandomPlayer.RandomPlayer(
                index, RANDOM_PLAYER_STRING_REP)
            players.append(random_player)
        elif player == EXPECTI_PLAYER_INDEX:
            expecti_player = ExpectiPlayer.ExpectiPlayer(
                index, EXPECTI_PLAYER_STRING_REP)
            players.append(expecti_player)
        elif player == TSCORING_PLAYER_INDEX:
            tscoring_player = TScoringPlayer.TScoringPlayer(
                index, TSCORING_PLAYER_STRING_REP)
            players.append(tscoring_player)
        elif player == END_RACE_PLAYER_INDEX:
            end_race_player = EndRacePlayer.EndRacePlayer(
                index, END_RACE_PLAYER_STRING_REP)
            players.append(end_race_player)
        elif player == QLEARNING_PLAYER_INDEX:
            q_player = QLearningAgent.QLearningAgent(
                index, QLEARNING_PLAYER_STRING_REP)
            players.append(q_player)
        elif player == APPROX_QLEARNING_PLAYER_INDEX:
            approx_q_player = ApproxQLearningAgent.ApproxQLearningAgent(
                index, APPROX_QLEARNING_STRING_REP)
            players.append(approx_q_player)

    return players
Example #6
0
    def __init__(self):

        # Make teams of players across from one another
        self.team1 = Team("team1")
        self.team2 = Team("team2")

        # initialize AI's here
        self.playerA1 = RandomPlayer.RandomPlayer("Tyler", self.team1)
        self.playerA2 = RandomPlayer.RandomPlayer("Hudson", self.team1)
        self.playerB1 = RandomPlayer.RandomPlayer("Shawn", self.team2)
        self.playerB2 = RandomPlayer.RandomPlayer("Matt", self.team2)

        # make a list of the players for rotations
        self.players = [
            self.playerA1, self.playerB1, self.playerA2, self.playerB2
        ]

        # Player A1 is the default first player, it is then rotated each round
        game.dealer = self.playerA1
def main():

    while True:
        colors = read(str)
        colors = colors.split(' ')
        log = open('log/player_controller_' + colors[0] + '.log', "a")
        log.write("Yo soy el " + colors[0] + " y el es el " + colors[1] + "\n")

        board = read(str)
        board = board.split(' ')
        board = map(lambda x: int(x), board)
        log.write("Dimensiones: " + str(board[0]) + " x " + str(board[1]) +
                  '\n')
        log.write("c: " + str(board[2]) + " p: " + str(board[3]) + '\n')

        player = RandomPlayer(colors[0], colors[1], board[0], board[1],
                              board[2], board[3])

        go_first = read(str)
        if go_first == YOU_FIRST:
            log.write("empezas vos\n")
            move = player.move()
            send(move)
            log.write("movi: " + str(move))
        else:
            log.write("empieza el\n")

        while True:
            msg = read(str)
            if msg in [LOSE, WIN, TIE]:
                log.write(msg + '\n')
                sys.stdout.flush()
                break
            log.write("el movio: " + msg + '\n')
            move = player.move(int(msg))
            send(move)
            log.write("movi: " + str(move) + '\n')
            log.flush()
Example #8
0
 def __init__(self, epsilon_test=None, alpha_test=None, gamma_test=None):
     # Default hyperparameter value arrays for testing
     if gamma_test is None:
         gamma_test = [.1, .2, .3, .4, .5, .6, .7, .8, .9]
     if alpha_test is None:
         alpha_test = [.1, .2, .3, .4, .5, .6, .7, .8, .9]
     if epsilon_test is None:
         epsilon_test = [.1, .2, .3, .4, .5, .6, .7, .8, .9]
     self.qp1 = QPlayer.QPlayer(.999, .9, .9, (3 ** 9) + 1, 9, 0)  # Creates a QLearning Agent
     self.qp2 = QPlayer.QPlayer(.999, .9, .9, (3 ** 9) + 1, 9, 1)  # Creates a QLearning Agent
     self.rp = RandomPlayer.RandomPlayer('Random')  # Creates a Random Agent
     self.ttt = TTTRules.TTTRules()  # Applies the tic-tac-toe rules
     self.p = Player.Player('Player')  # Creates a real player
     # Creates hyperparameter test arrays
     self.e = epsilon_test
     self.a = alpha_test
     self.g = gamma_test
Example #9
0
class GameManager:
    player_won = 0
    for i in tqdm.tqdm(range(5)):
        firstPlayer, secondPlayer = MiniMaxPlayer(), RandomPlayer()
        firstPlayer.setTypeOfAssignedMark(O_MARK), secondPlayer.setTypeOfAssignedMark(X_MARK)
        empty_state = np.array([
                   [' ', ' ', ' ', ' ', ' '], 
                   [' ', ' ', ' ', ' ', ' '], 
                   [' ', ' ', ' ', ' ', ' '], 
                   [' ', ' ', ' ', ' ', ' '], 
                   [' ', ' ', ' ', ' ', ' ']])
        
        # empty_state = np.array([
        #     [' ', ' '],
        #     [' ', ' ']])
        # empty_state = np.array([
        #     [' ', ' ', ' '],
        #     [' ', ' ', ' '],
        #     [' ', ' ', ' ']])
        firstPlayer.setStateOfGameField(empty_state)
        currentPlayer = firstPlayer
        currentState = empty_state
        while currentPlayer.check_if_player_won(currentState, X_MARK) == False and currentPlayer.check_if_player_won(currentState, O_MARK) == False:
            move = currentPlayer.makeMove()
            new_state = currentPlayer.simulate_state_after_move(currentState, move)
            if currentPlayer == firstPlayer:
                currentPlayer = secondPlayer
            else:
                currentPlayer = firstPlayer
            #print (currentState)
            currentState = new_state
            currentPlayer.setStateOfGameField(currentState)
            #print (move)
            #print (currentState)
        #print (currentPlayer.gameField)
        if currentPlayer.check_if_player_won(currentState, O_MARK):
            player_won += 1
        print (currentState)
    print ("player_won", str(player_won))
        
Example #10
0
            game_over = True

    # Now that the game is over determine the winner
    winner = p1 if (board[26] == 15) else p2
    obj = {
        'winner': winner,
        'turns': i,
        'boards': board_tracker,
        'starting_player': starting_player
    }

    return obj


if __name__ == "__main__":
    p1 = RandomPlayer("player 1")
    p2 = RandomPlayer("player 2")

    N = 500
    turn_tracker = np.zeros(N)
    winner_tracker = [0, 0]

    for i in tqdm(range(N), leave=False):
        obj = play_game(p1, p2)
        turn_tracker[i] = obj['turns']
        if (obj['winner'] == p1):
            winner_tracker[0] += 1
        else:
            winner_tracker[1] += 1

    import matplotlib.pyplot as plt
Example #11
0
    def train(self):
        print("train")
        tf.train.write_graph(self.sess.graph,
                             self.model_path,
                             'hive.pb',
                             as_text=False)
        summary_writer = tf.summary.FileWriter(
            '{0}{1}'.format(self.summary_path, int(time.time())),
            self.sess.graph)

        # the agent plays against itself, making the best move for each player
        white_player = RandomPlayer()
        black_player = RandomPlayer()

        validation_interval = 1000
        games = 2000
        start = time.time()
        episodes = 2
        for episode in range(episodes):
            game = Game(white_player, black_player)

            x = game.extract_features()

            winner = 0
            block_num = int(random.random() * 100 % 6)
            file = "./saves/games{0}".format(block_num)
            with open(file, "r") as f:
                line = f.readline()
                n_game = 0
                while line and n_game < games:
                    if n_game != 0 and n_game % validation_interval == 0:
                        self.test(episodes=10)
                    board = Board()
                    game_step = 0
                    while line != ".\n":
                        # print(line)
                        board.doMoveFromString(line)

                        x_next = GameParser.getFeaturesForState(board)
                        V_next = self.get_output(x_next)
                        self.sess.run(self.train_op,
                                      feed_dict={
                                          self.x: x,
                                          self.V_next: V_next
                                      })

                        x = x_next
                        game_step += 1
                        # board.print()
                        line = f.readline()
                    n_game += 1
                    line = f.readline()

                    if board.isQueenSurrounded(Color.WHITE):
                        winner = Color.BLACK
                    else:
                        winner = Color.WHITE

                    _, global_step, summaries, _ = self.sess.run(
                        [
                            self.train_op, self.global_step, self.summaries_op,
                            self.reset_op
                        ],
                        feed_dict={
                            self.x:
                            x,
                            self.V_next:
                            np.array([[
                                0 if winner == Color.BLACK else earn(game_step)
                            ]],
                                     dtype='float')
                        })
                    summary_writer.add_summary(summaries,
                                               global_step=global_step)

                    print("Game %d/%d in %d turns" %
                          (n_game, games, game_step))
                    self.saver.save(self.sess,
                                    self.checkpoint_path + 'checkpoint',
                                    global_step=global_step)

        summary_writer.close()

        self.test(episodes=10)
        print("time:", time.time() - start)
Example #12
0
 def combat_random(model, iterations, player_config):
     tree = MCTS(model)
     player_1 = DeepPlayer(model, tree, player_config)
     player_2 = RandomPlayer()
     
     return Evaluator.combat(player_1, player_2, iterations, False)
 def generateModelPlayer(self):
     modelPlayer = RandomPlayer(self.playerNumber, self.possible_hand)
     modelPlayer.resetZeroCounter(self.zero_bid_count)
     modelPlayer.real_hand = [i for i in self.real_hand]
     return copy.deepcopy(modelPlayer)
Example #14
0
import RandomPlayer
import humanPlayer
import myPlayerv0
import time
from io import StringIO
import sys

b = Reversi.Board(10)
player1 = None
player2 = None
while True:
    plays = input("enter players (r, i, i2, z, or h)\n")
    plays2 = plays.split()
    if len(plays2) == 2:
        if plays2[0] == "r":
            player1 = RandomPlayer.RandomPlayer()
        elif plays2[0] == "i":
            player1 = myPlayer.myPlayer()
        elif plays2[0] == "h":
            player1 = humanPlayer.humanPlayer()
        elif plays2[0] == "i2":
            player1 = myPlayerv0.myPlayer()
        elif plays2[0] == "z":
            player1 = humanPlayer.clickerPlayer()
        if plays2[1] == "r":
            player2 = RandomPlayer.RandomPlayer()
        elif plays2[1] == "i":
            player2 = myPlayer.myPlayer()
        elif plays2[1] == "h":
            player2 = humanPlayer.humanPlayer()
        elif plays2[1] == "i2":