Ejemplo n.º 1
0
 def setUp(self):
     self.image_repo = ImageRepositoryStub()
     self.repository = GameRepository(database_connection, self.image_repo)
     self.repository.clear()
     round1 = Round([("Koira", "[koira]"), ("Kissa", "[kissa]")])
     round2 = Round([("Pöytä", "[pöytä]")])
     self.game = Game("Peli", [round1, round2])
Ejemplo n.º 2
0
def main():
    teams = read_teams_names()
    players = read_player_names(teams[0], teams[1])

    player1 = Player(players[0])
    player2 = Player(players[1])
    team1 = Team(teams[0], player1, player2)

    player3 = Player(players[2])
    player4 = Player(players[3])
    team2 = Team(teams[1], player3, player4)

    write_to_txt = WriteToTxt(team1, team2)
    write_to_json = WriteToJSON()

    game_id = 1
    while not is_match_won(team1.games_won, team2.games_won):
        g = Game(game_id=game_id, team1=team1, team2=team2)
        g.play_game(write_to_txt, write_to_json)
        game_id += 1
Ejemplo n.º 3
0
    def new_game(name):
        """
        Create a new game.

        Args:
            name: The initial name of the game.
        Returns:
            New Game entity.
        """

        return Game(name, [])
    def test_if_constructor_sets_attributes_correctly(self):
        player1 = Player(name='Marto')
        player2 = Player(name='Rado')
        team1 = Team(team_name='Mechetata', player1=player1, player2=player2)

        player3 = Player(name='Gosho')
        player4 = Player(name='Pesho')
        team2 = Team(team_name='Kotetata', player1=player3, player2=player4)
        g = Game(game_id=1, team1=team1, team2=team2)

        self.assertEqual(g.game_id, 'game 1')
        self.assertEqual(g.team1, team1)
        self.assertEqual(g.team2, team2)
Ejemplo n.º 5
0
def resolve_hands(p, g):
    """Check who has strongest hand and divide the pot accordingly

    :param p: List of players to resolve the winner
    :param g: Game object
    :return:
    """
    # take five additional cards from the deck
    # add them to users hand
    for c in range(5):
        new_card = g.deck.draw_card()
        for player in p:
            player.hand.add_card(new_card)

    # sort the hands
    for pl in p:
        pl.hand.sort()

    print("Resolving player hands")
    g.render_game()

    # get the absolute score of the hand and the best five cards
    results = []
    for player in p:
        results.append(Game.score(player.hand))
    for i in range(g.p.__len__()):
        print(g.p[i].name, "has", g.name_of_hand(results[i][0]))

    # select the winner
    winners = Game.determine_winner(results)
    # award the pot to the winner
    if winners.__len__() > 1:
        # split the pot
        print("No winner - split the pot")
        g.split_the_pot()
    else:
        print(p[winners[0]].name, "has taken the pot")
        print(p[winners[0]].name, "gained", min(p[winners[0]].bet * 2, g.pot))
        g.player_won(p[winners[0]])
Ejemplo n.º 6
0
def load_test_game():
    """
    Load a hardcoded test game

    Returns:
        A `Game`.
    """
    koira = Image.load_from_file("data/koira.jpg")
    kissa = Image.load_from_file("data/kissa.jpg")
    aurinko = Image.load_from_file("data/aurinko.jpg")
    return Game("Esimerkkipeli", [
        Round([("Koira", koira), ("Kissa", kissa)]),
        Round([("Aurinko", aurinko)]),
    ])
Ejemplo n.º 7
0
    def all(self):
        """
        Get all games stored in the repository.

        Returns:
            Array of Game entities, sorted by creation time.
        """

        games = {}
        cursor = self._db.cursor()
        cursor.execute("SELECT id, name FROM game ORDER BY id;")
        for row in cursor.fetchall():
            game = Game(row["name"], [])
            game.id = row["id"]
            games[game.id] = game

        rounds = {}
        cursor.execute(
            "SELECT id, game_id FROM round ORDER BY game_id, position;")
        for row in cursor.fetchall():
            g_round = Round([])
            g_round.id = row["id"]
            games[row["game_id"]].rounds.append(g_round)
            rounds[g_round.id] = g_round

        images = []
        cursor.execute("SELECT round_id, caption, image_id FROM image_pair;")
        for row in cursor.fetchall():
            image = self._image_repository.get_lazy(row["image_id"])
            images.append(image)
            pair = (row["caption"], image)
            rounds[row["round_id"]].pairs.append(pair)

        # self._image_repository.load_thumbnails(images)

        return list(games.values())
Ejemplo n.º 8
0
 def test_remove_game_removes_game_from_repository(self):
     game = Game("Empty game", [("Kameli", "[Kameli]")])
     self.game_repository.store(game)
     self.service.remove_game(game)
     self.assertEqual(self.game_repository.all(), [])
Ejemplo n.º 9
0
 def test_save_game_stores_game_in_repository(self):
     game = Game("Empty game", [("Kameli", "[Kameli]")])
     self.service.save_game(game)
     self.assertEqual(self.game_repository.all(), [game])
Ejemplo n.º 10
0
 def test_add_round_changes_game(self):
     game = Game("Empty game", [])
     self.service.add_round(game, [("Kameli", "[Kameli]"), ("Lepakko", "[Lepakko]")])
     self.assertEqual(game.rounds[0].pairs[1], ("Lepakko", "[Lepakko]"))
Ejemplo n.º 11
0
def test_game_factory():
    return Game("Testipeli", [
        Round([("Koira", "[Koira]"), ("Kissa", "[Kissa]")]),
        Round([("Aurinko", "[Aurinko]")]),
    ])
Ejemplo n.º 12
0
 def setUp(self):
     seeded_random = random.Random(12)
     self.service = PlayService(shuffle=seeded_random.shuffle)
     round1 = Round([("Koira", "[koira]"), ("Kissa", "[kissa]")])
     round2 = Round([("Pöytä", "[pöytä]")])
     self.game = Game("Peli", [round1, round2])
Ejemplo n.º 13
0
def train(args):
    args_dict = vars(args)
    print('args: {}'.format(args_dict))

    with tf.Graph().as_default() as g:

        # rollout subgraph
        with tf.name_scope('rollout'):
            observations = tf.placeholder(shape=(None, OBSERVATION_DIM),
                                          dtype=tf.float32)

            logits = build_graph(observations)

            logits_for_sampling = tf.reshape(logits, shape=(1, len(ACTIONS)))

            # Sample the action to be played during rollout.
            sample_action = tf.multinomial(logits=logits_for_sampling,
                                           num_samples=1)
            sample_action = tf.squeeze(sample_action)

        optimizer = tf.train.RMSPropOptimizer(learning_rate=args.learning_rate,
                                              decay=args.decay)

        # dataset subgraph for experience replay
        with tf.name_scope('dataset'):
            # the dataset reads from MEMORY
            ds = tf.data.Dataset.from_generator(gen,
                                                output_types=(tf.float32,
                                                              tf.int32,
                                                              tf.float32))
            ds = ds.shuffle(MEMORY_CAPACITY).repeat().batch(args.batch_size)
            iterator = ds.make_one_shot_iterator()

        # training subgraph
        with tf.name_scope('train'):
            # the train_op includes getting a batch of data from the dataset, so we do not need to use a feed_dict when running the train_op.
            next_batch = iterator.get_next()
            train_observations, labels, processed_rewards = next_batch

            # This reuses the same weights in the rollout phase.
            train_observations.set_shape((args.batch_size, OBSERVATION_DIM))
            train_logits = build_graph(train_observations)

            cross_entropies = tf.nn.sparse_softmax_cross_entropy_with_logits(
                logits=train_logits, labels=labels)

            # Extra loss when the paddle is moved, to encourage more natural moves.
            probs = tf.nn.softmax(logits=train_logits)

            loss = tf.reduce_sum(processed_rewards * cross_entropies)

            global_step = tf.train.get_or_create_global_step()

            train_op = optimizer.minimize(loss, global_step=global_step)

        init = tf.global_variables_initializer()
        saver = tf.train.Saver(max_to_keep=args.max_to_keep)

        with tf.name_scope('summaries'):
            rollout_reward = tf.placeholder(shape=(), dtype=tf.float32)

            # # the weights to the hidden layer can be visualized
            # hidden_weights = tf.trainable_variables()[0]
            # for h in range(args.hidden_dim):
            #     slice_ = tf.slice(hidden_weights, [0, h], [-1, 1])
            #     image = tf.reshape(slice_, [1, 80, 80, 1])
            #     tf.summary.image('hidden_{:04d}'.format(h), image)

            # for var in tf.trainable_variables():
            #     tf.summary.histogram(var.op.name, var)
            #     tf.summary.scalar('{}_max'.format(var.op.name), tf.reduce_max(var))
            #     tf.summary.scalar('{}_min'.format(var.op.name), tf.reduce_min(var))

            tf.summary.scalar('rollout_reward', rollout_reward)
            tf.summary.scalar('loss', loss)

            merged = tf.summary.merge_all()

    game = Game()
    # tf.agents helper to more easily track consecutive pairs of frames
    # env = FrameHistory(inner_env, past_indices=[0, 1], flatten=False)
    # tf.agents helper to automatically reset the environment
    env = AutoReset(game)

    with tf.Session(graph=g) as sess:
        if args.restore:
            restore_path = tf.train.latest_checkpoint(args.output_dir)
            print('Restoring from {}'.format(restore_path))
            saver.restore(sess, restore_path)
        else:
            sess.run(init)

        summary_path = os.path.join(args.output_dir, 'summary')
        summary_writer = tf.summary.FileWriter(summary_path, sess.graph)

        # lowest possible score after an episode as the
        # starting value of the running reward
        _rollout_reward = game.lowest_reward

        for i in range(args.n_epoch):
            print('>>>>>>> epoch {}'.format(i + 1))

            print('>>> Rollout phase')
            epoch_memory = []
            episode_memory = []

            # The loop for actions/steps
            _observation = np.zeros(OBSERVATION_DIM)
            while True:
                # sample one action with the given probability distribution
                _label = sess.run(sample_action,
                                  feed_dict={observations: [_observation]})

                _action = ACTIONS[_label]

                _observation, _reward, _done, _ = env.step(_action)
                _observation = _observation.reshape(OBSERVATION_DIM)

                if args.render:
                    env.render()

                # record experience
                episode_memory.append((_observation, _label, _reward))

                if _done:
                    obs, lbl, rwd = zip(*episode_memory)

                    # processed rewards
                    prwd = discount_rewards(rwd, args.gamma)
                    prwd -= np.mean(prwd)
                    prwd /= np.std(prwd)

                    # store the processed experience to memory
                    epoch_memory.extend(zip(obs, lbl, prwd))

                    # calculate the running rollout reward
                    _rollout_reward = 0.9 * _rollout_reward + 0.1 * sum(rwd)

                    episode_memory = []

                    if args.render:
                        _ = input('episode done, press Enter to replay')
                        epoch_memory = []
                        continue

                if len(epoch_memory) >= ROLLOUT_SIZE:
                    break

            # add to the global memory
            MEMORY.extend(epoch_memory)

            print('>>> Train phase')
            print('rollout reward: {}'.format(_rollout_reward))

            # Here we train only once.
            _, _global_step = sess.run([train_op, global_step])

            if _global_step % args.save_checkpoint_steps == 0:
                print('Writing summary')

                feed_dict = {rollout_reward: _rollout_reward}
                summary = sess.run(merged, feed_dict=feed_dict)

                summary_writer.add_summary(summary, _global_step)

                save_path = os.path.join(args.output_dir, 'model.ckpt')
                save_path = saver.save(sess,
                                       save_path,
                                       global_step=_global_step)
                print('Model checkpoint saved: {}'.format(save_path))
Ejemplo n.º 14
0
def main(p1, p2, num_of_games, num_of_chips):
    import time
    start_time = time.time()
    # [0] player1 won accumulator, [1] player2 won accumulator
    stats = [0, 0]
    game_lengths_won_by_p1 = [0]
    game_lengths_won_by_p2 = [0]
    #  successful bluff is when p1 is when SB goes all-in, and opponent folds, but would have won if called
    bluffs_p1 = [0, 0]
    bluffs_p2 = [0, 0]
    if "n" in [p1, p2]:
        neural_npc = NeuralNetworkNPC()
    else:
        neural_npc = None
    if "q" in [p1, p2]:
        q_table_npc = QtableNPC()
    else:
        q_table_npc = None
    for games in range(num_of_games):
        # q - indicate q-table, indicate
        game = Game(player_types[p1],
                    player_types[p1],
                    player_types[p2],
                    player_types[p2],
                    bank=num_of_chips)
        if random() > 0.5:
            game.end_round()
        game_length = 0
        while not game.done:
            if game.a_player().bank <= 0 or game.na_player().bank <= 0:
                game.done = True
                break
            else:
                game.render_game()
                print(game.p[game.turn].name + " is small blind")
                print("Placing blinds")
                game.place_blinds()
                game.render_game()
            game.players_draw_cards()
            current_player_index = game.turn
            result = small_blind(game.p[game.turn],
                                 game,
                                 neural_model_npc=neural_npc,
                                 q_table_npc=q_table_npc)
            game.next_player()
            if result:
                result = big_blind(game.p[game.turn],
                                   game,
                                   neural_model_npc=neural_npc,
                                   q_table_npc=q_table_npc)
                if result:
                    resolve_hands(game.p, game)
                else:
                    # bluff?
                    results = []
                    community_cards = []
                    for i in range(5):
                        community_cards.append(game.deck.draw_card())
                    for ep in game.p:
                        hand = copy.deepcopy(ep.hand)
                        hand.cards.extend(community_cards)
                        hand.sort()
                        results.append(Game.score(hand))
                    # select the winner
                    winners = Game.determine_winner(results)
                    if winners[0] == current_player_index:
                        if current_player_index == 0:
                            bluffs_p1[0] += 1
                        else:
                            bluffs_p2[0] += 1
                    if current_player_index == 0:
                        bluffs_p1[1] += 1
                    else:
                        bluffs_p2[1] += 1
                    game.opponent_folded(game.na_player())
            else:
                game.opponent_folded(game.a_player())
            game.new_step()
            print()
            game_length += 1

        # region End game
        if game.done:
            if game.p[0].bank > game.p[1].bank:
                stats[0] += 1
                game_lengths_won_by_p1.append(game_length)
            else:
                stats[1] += 1
                game_lengths_won_by_p2.append(game_length)
            for pl in game.p:
                pl.bank += pl.bet
                pl.bet = 0
            if game.a_player().bank <= 0:
                pl = game.na_player()
            else:
                pl = game.a_player()
            s = pl.name + " has won the game with "
            s += str(pl.bank) + " coins"

            print(s)
        print('\n')
        # endregion
        print(stats)
    print("In total: Player1[" + player_types[p1] + "] has won " +
          str(stats[0]))
    print("In total: Player2[" + player_types[p2] + "] has won " +
          str(stats[1]))
    if len(game_lengths_won_by_p1) > 1:
        print("Average game length when Player1[" + player_types[p1] +
              "] has won " + str(
                  round(
                      sum(game_lengths_won_by_p1) /
                      len(game_lengths_won_by_p1), 2)))
    if len(game_lengths_won_by_p2) > 1:
        print("Average game length when Player2[" + player_types[p2] +
              "] has won " + str(
                  round(
                      sum(game_lengths_won_by_p2) /
                      len(game_lengths_won_by_p2), 2)))
    print("Successful bluffs by Player1[" + player_types[p1] + "] " +
          str(bluffs_p1[0]) + " / " + str(bluffs_p1[1]))
    print("Successful bluffs by Player2[" + player_types[p2] + "] " +
          str(bluffs_p2[0]) + " / " + str(bluffs_p2[1]))
    print("time elapsed: {:.2f}s".format(time.time() - start_time))
Ejemplo n.º 15
0
def main():
    run = True
    clock = pygame.time.Clock()
    game = Game(WIN)
    input_level = int(input("Select the difficulty:"))
    # input_level = 3
    while run:
        clock.tick(FPS)

        if input_level in range(1, 4):
            new_board = None
            if game.turn == GREY:
                if input_level == 1:
                    value, new_board = minimax(game.get_board(), 2, True)
                if input_level == 2:
                    value, new_board = minimax_alpha_beta(
                        game.get_board(), 3, float('-inf'), float('inf'), True)
                if input_level == 3:
                    value, new_board = custom_ai_move(game.get_board(), 4,
                                                      True)
                if new_board is not None:
                    game.ai_move(new_board)

        if game.winner() != "False":
            print(game.winner())
            WIN.fill(GREEN)
            text_surface = FONT.render(game.winner(), False, (0, 0, 0))
            WIN.blit(text_surface, (WIDTH // 2 - 120, HEIGHT // 2 - 120))
            pygame.display.flip()
            run = False
            time.sleep(5)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                row, col = get_row_col_from_mouse(pos)
                if col in range(COLS) and row in range(ROWS):
                    game.select(row, col)
        game.update()
Ejemplo n.º 16
0
 def test_get_all_fetches_from_repository(self):
     game = Game("Empty game", [("Kameli", "[Kameli]")])
     self.game_repository.store(game)
     result = self.service.get_all_games()
     self.assertEqual(result, [game])
Ejemplo n.º 17
0
 def test_dont_load_test_game_when_not_empty(self):
     game = Game("Empty game", [("Kameli", "[Kameli]")])
     self.game_repository.store(game)
     self.service.load_test_game_if_empty()
     self.assertEqual(len(self.game_repository.all()), 1)
     self.assertEqual(self.game_repository.all()[0].name, "Empty game")
Ejemplo n.º 18
0
def callback(update: Update, context: CallbackContext):
    link = context.bot.link
    query = update.callback_query
    user = query.from_user
    data = query.data
    not_found_alert = callback_strings.not_found_alert

    def alert(text: str):
        context.bot.answer_callback_query(callback_query_id=query.id,
                                          text=text,
                                          show_alert=True)

    def edit_message(text: str, reply_markup: Optional[ReplyMarkup] = None):
        context.bot.editMessageText(text,
                                    inline_message_id=query.inline_message_id,
                                    reply_markup=reply_markup)

    print("received: ", data)
    data_type, payloads = restore_callback_data(data)
    if CallbackDataType.HELP.value == data_type:
        alert(not_found_alert)
    elif CallbackDataType.GET_IN.value == data_type:
        [game_id] = payloads
        game = Game.get_instance(game_id)

        def edit_game_inline():
            edit_message(callback_strings.before_start(game),
                         create_inline_markup(game))

        game.get_in(MyUser.new(user.id, user.first_name), alert,
                    edit_game_inline)
    elif CallbackDataType.START.value == data_type:
        [game_id] = payloads
        starter_id = user.id
        game = Game.get_instance(game_id)

        def edit_game_inline():
            edit_message(callback_strings.choose_type.text(game),
                         create_choice_markup(game_id, link))

        game.start(starter_id, alert, edit_game_inline)
    elif CallbackDataType.CHOOSE.value == data_type:
        [game_id, q_type] = payloads
        user_id = user.id
        game = Game.get_instance(game_id)

        def edit_question():
            edit_message(callback_strings.question.text(game),
                         create_question_markup(game_id, link))

        game.choose(user_id, q_type, alert, edit_question)
    elif CallbackDataType.ANSWER.value == data_type:
        [game_id] = payloads
        user_id = user.id
        game = Game.get_instance(game_id)

        def edit_vote():
            edit_message(callback_strings.vote.text(game),
                         create_vote_markup(game_id, link))

        game.answer(user_id, alert, edit_vote)
    elif CallbackDataType.VOTE.value == data_type:
        [game_id, result] = payloads
        user_id = user.id
        game = Game.get_instance(game_id)

        def edit(is_voting_finish: bool, is_repeated: bool):
            if is_repeated:
                text = callback_strings.question.text(game, is_repeated)
                markup = create_question_markup(game_id, link)
            elif is_voting_finish:
                text = callback_strings.choose_type.text(game)
                markup = create_choice_markup(game_id, link)
            else:
                text = callback_strings.vote.text(game)
                markup = create_vote_markup(game_id, link)
            edit_message(text, markup)

        game.vote(user_id, result, alert, edit)
    else:
        alert(callback_strings.not_recognized)