Beispiel #1
0
    def test_play_1000_games_nnqVSrandom(self):
        TFSessionManager.get_session().run(tf.global_variables_initializer())

        player = RandomPlayer()
        match = Match()

        self.nnq_player1.new_game(CROSS)
        player.new_game(NAUGHT)

        num_games = 10000

        cross_count, naught_count, draw_count = match.play(
            self.nnq_player1, player, num_games)

        print(
            "After {} game we have draws: {}, Player 1 wins: {}, and Player 2 wins: {}."
            .format(num_games, draw_count, cross_count, naught_count))
        print(
            "Which gives percentages of draws: {:.2%}, Player 1 wins: {:.2%}, and Player 2 wins:  {:.2%}"
            .format(draw_count / num_games, cross_count / num_games,
                    naught_count / num_games))

        self.assertGreaterEqual(cross_count, 0)
        self.assertGreaterEqual(naught_count, 0)
        self.assertGreaterEqual(draw_count, 0)
Beispiel #2
0
    def test_one_game_randomVSrandom(self):

        player1 = RandomPlayer()
        player2 = RandomPlayer()

        match = Match()
        game_result = match.game(player1, player2, self.board)

        self.assertNotIn(game_result, [GameResult.NOT_FINISHED])
Beispiel #3
0
def test_update_board():
    renderer = ConsoleRenderer()
    match = Match()
    match.board = make_board()
    renderer._update_board_for_print(match.board.columns)
    assert (len(renderer.board_for_print[0]) == const.BOARD_TOTAL_COLUMNS)
    assert (len(renderer.board_for_print) == const.BOARD_TOTAL_ROWS_PER_COLUMN)
    assert (renderer.board_for_print[0][0] == renderer.char_cell_red)
    assert (renderer.board_for_print[0][1] == renderer.char_cell_yellow)
    assert (renderer.board_for_print[const.BOARD_TOTAL_ROWS_PER_COLUMN -
                                     1][0] == renderer.char_cell_empty)
Beispiel #4
0
    def unserializeMatch(self, match_serialized):
        player_1 = self.unserializePlayer(match_serialized['player_1'])
        player_2 = self.unserializePlayer(match_serialized['player_2'])

        match = Match(player_1, player_2)

        match.player_1_point = match_serialized['player_1_point']
        match.player_2_point = match_serialized['player_2_point']

        match.state = match_serialized['state']
        return match
Beispiel #5
0
    def test_play_10_games_randomVSrandom(self):

        board = Board()
        player1 = RandomPlayer()
        player2 = RandomPlayer()
        match = Match()

        cross_count, naught_count, draw_count = match.play(
            player1, player2, 10)

        self.assertGreaterEqual(cross_count, 0)
        self.assertGreaterEqual(naught_count, 0)
        self.assertGreaterEqual(draw_count, 0)
Beispiel #6
0
def test_update_column_selection():
    renderer = ConsoleRenderer()
    match = Match()
    renderer._update_column_selection_for_print(match)
    assert (len(renderer.col_selection) == const.BOARD_TOTAL_COLUMNS)
    assert (renderer.col_selection[0] == renderer.char_cell_red)
    for s in renderer.col_selection[1:]:
        assert (s == "  ")

    match.player_turn = const.PlayerTurn.YELLOW
    renderer._update_column_selection_for_print(match)
    assert (len(renderer.col_selection) == const.BOARD_TOTAL_COLUMNS)
    assert (renderer.col_selection[0] == renderer.char_cell_yellow)
    for s in renderer.col_selection[1:]:
        assert (s == "  ")
Beispiel #7
0
class Game():
    """Logic of connect4 game."""
    def __init__(self) -> None:
        """Constructor."""
        self.match = Match()

    def process_input(self, input):
        """Process user keyboard input after it was read and identified."""
        if input == UserInput.KEY_R:
            self._reset_match()
        elif self._is_match_ongoing():
            self._process_input_match(input)

    def _reset_match(self):
        """Create a new match in default state."""
        self.match = Match()

    def _is_match_ongoing(self):
        """Return if match is ongoing or stopped (player won or they tied)."""
        return self.match.is_being_played()

    def _process_input_match(self, input):
        """Process user input with ongoing match."""
        switcher = {
            UserInput.ARROW_LEFT: self.match.column_previous,
            UserInput.ARROW_RIGHT: self.match.column_next,
            UserInput.ENTER: self.match.add_checker
        }
        switcher.get(input, lambda: None)()
Beispiel #8
0
    def initNextRound(self):
        self.currRound += 1
        self.updateScoreboard()

        playing = []
        for index in range(0, int(len(self.player_list) / 2)):
            # Choix du joueur 1
            player_1 = None
            if len(playing) == 0:
                player_1 = self.scoreboard[0]
            else:
                for player in self.scoreboard:

                    if playing.count(player.id) == 0:
                        player_1 = player
                        break
            # Choix du joueur 2
            player_2 = None
            opponent = self.getOpponentOfPlayer(player_1.id)
            index_player = self.scoreboard.index(player_1) + 1
            while index_player < len(self.scoreboard):
                if opponent.count(self.scoreboard[index_player].id) == 0:
                    player_2 = self.scoreboard[index_player]
                    index_player = 100
                index_player += 1
            self.rounds[self.currRound - 1].match_list.append(
                Match(player_1, player_2))
            playing.append(player_1.id)
            playing.append(player_2.id)
Beispiel #9
0
def test_set_board():
    renderer = ConsoleRenderer()
    match = Match()
    renderer._set_board(match)
    assert (len(renderer.print_buffer) == const.BOARD_TOTAL_ROWS_PER_COLUMN)
    for i, row in enumerate(renderer.print_buffer):
        assert (renderer.char_cell_empty in row)
        assert not (renderer.char_cell_red in row)
        assert not (renderer.char_cell_yellow in row)
Beispiel #10
0
def test_set_current_infos():
    renderer = ConsoleRenderer()
    match = Match()
    renderer._set_current_infos(match)
    assert (" " == renderer.print_buffer[0])
    assert ("Turn for" in renderer.print_buffer[1])
    assert (" " == renderer.print_buffer[2])

    renderer._clear_buffer()
    match.state = const.MatchState.TIE
    renderer._set_current_infos(match)
    assert ("tie" in renderer.print_buffer[1])

    renderer._clear_buffer()
    match.state = const.MatchState.RED_WON
    renderer._set_current_infos(match)
    assert ("Victory" in renderer.print_buffer[1])

    renderer._clear_buffer()
    match.state = const.MatchState.YELLOW_WON
    renderer._set_current_infos(match)
    assert ("Victory" in renderer.print_buffer[1])
Beispiel #11
0
def test_render(capsys):
    renderer = ConsoleRenderer()
    match = Match()
    renderer.render(match)
    assert (len(renderer.print_buffer) >= 10)

    # Capture stdout output
    captured = capsys.readouterr()
    assert ("\n" in captured.out)
    assert ("Connect4" in captured.out)

    m = re.search(r"\](.)\[", renderer.char_cell_empty)
    assert (m.group(1) in captured.out)
Beispiel #12
0
    def run(self):

        for task in self.tasks:

            sampleImage, sampleSize, originalSize, threshold, topOffset, leftOffset, resizedWidth, resizedHeight = task

            is_face_probabilty = self.is_face(sampleImage)

            if is_face_probabilty > threshold:
                topOffsetOriginalSized = round(originalSize[1] * topOffset /
                                               resizedHeight)
                leftOffsetOriginalSized = round(originalSize[0] * leftOffset /
                                                resizedWidth)

                sampleHeightOriginalSized = round(
                    originalSize[1] * sampleSize[1] / resizedHeight)
                sampleWidthOriginalSized = round(originalSize[0] *
                                                 sampleSize[0] / resizedWidth)

                self.matches.append(
                    Match(leftOffsetOriginalSized, topOffsetOriginalSized,
                          sampleWidthOriginalSized, sampleHeightOriginalSized,
                          is_face_probabilty))
Beispiel #13
0
    def initFirstRound(self):
        sorted_player_list = []
        # tri des joueurs en fonction de leur elo
        for player in self.player_list:
            index = 0
            if len(sorted_player_list) == 0:
                sorted_player_list.append(player)
            else:
                while index < len(sorted_player_list):
                    if player.elo > sorted_player_list[index].elo:
                        sorted_player_list.insert(index, player)
                        index = 100
                    index += 1
                if index == len(sorted_player_list):
                    sorted_player_list.append(player)

        length = len(sorted_player_list)
        # creation des match

        for index in range(int(length / 2)):
            self.rounds[0].match_list.append(
                Match(sorted_player_list[index],
                      sorted_player_list[int(length / 2) + index]))
Beispiel #14
0
 def __init__(self) -> None:
     """Constructor."""
     self.match = Match()
Beispiel #15
0
 def _reset_match(self):
     """Create a new match in default state."""
     self.match = Match()
Beispiel #16
0
def test_set_column_selector():
    renderer = ConsoleRenderer()
    match = Match()
    renderer._set_column_selector(match)
    assert (renderer.char_cell_red in renderer.print_buffer[0])
    assert (" " == renderer.print_buffer[-1])