def play_round(self, round):
        self.rounds.append(round)
        self.current_round = round

        pairings = itertools.combinations(self.lobby.values(), 2)

        for pairing in pairings:
            player_1 = pairing[0]
            player_2 = pairing[1]

            # TODO: since X always has the advantage, would it be better
            # to generate two games for each pairing?  To give them an
            # equal chance at the advantage?

            new_game = Game("{} vs {}".format(player_1.name, player_2.name),
                            uuid4(),
                            deepcopy(player_1),
                            deepcopy(player_2),
                            size_x=round.x_size,
                            size_y=round.y_size,
                            winning_length=round.winning_length)
            new_game.state = GAME_INPROGRESS

            round.games.append(new_game)
            game_thread(new_game)

        self.process_completed_game()
    def test__mark_cell_by_coordinates__adds_mark_to_cells(self):
        game = Game('test-grid', uuid.uuid4(), PLAYER_X, PLAYER_O)
        game.mark_cell_by_coordinates(1, 1, X_MARK)

        actual_mark = game.get_cell_by_coordinates(1, 1)

        assert actual_mark.value == X_MARK
    def test__get_cell_by_coordinates__returns_none_if_coordinates_are_empty(self):
        existing_mark_1 = Mark(0, 0, X_MARK)
        game = Game('test-grid', uuid.uuid4(), PLAYER_X, PLAYER_O)
        game.cells.append(existing_mark_1)

        actual_mark = game.get_cell_by_coordinates(1, 1)

        assert not actual_mark
    def test__mark_causes_win__should_return_true_if_horizontal_win(self):
        game = Game('test-grid', uuid.uuid4(), PLAYER_X, PLAYER_O)
        game.cells = [
            Mark(0, 0, X_MARK),
            Mark(2, 0, X_MARK)
        ]

        win = game.mark_causes_win(Mark(1, 0, X_MARK))

        assert win
    def test__get_cell_by_coordinates__returns_mark_at_coordinates(self):
        existing_mark_1 = Mark(0, 0, X_MARK)
        existing_mark_2 = Mark(1, 1, O_MARK)
        game = Game('test-game', uuid.uuid4(), PLAYER_X, PLAYER_O)
        game.cells.append(existing_mark_1)
        game.cells.append(existing_mark_2)

        actual_mark = game.get_cell_by_coordinates(1, 1)

        assert actual_mark == existing_mark_2
    def test__mark_causes_win__should_return_true_if_positive_slope_diagonal_win(self):
        game = Game('test-grid', uuid.uuid4(), PLAYER_X, PLAYER_O)
        game.cells = [
            Mark(0, 2, X_MARK),
            Mark(2, 0, X_MARK)
        ]

        win = game.mark_causes_win(Mark(1, 1, X_MARK))

        assert win
Example #7
0
    def setUp(self):
        self.player_1 = Player(uuid4(), "player 1", "update_url1")
        self.player_2 = Player(uuid4(), "player 2", "update_url2")
        self.player_3 = Player(uuid4(), "player 3", "update_url3")

        self.game_1 = Game("Test Game 1", uuid4(), deepcopy(self.player_1),
                           deepcopy(self.player_2))
        self.game_2 = Game("Test Game 2", uuid4(), deepcopy(self.player_1),
                           deepcopy(self.player_3))
        self.game_3 = Game("Test Game 3", uuid4(), deepcopy(self.player_2),
                           deepcopy(self.player_3))
    def create_game(self,
                    game_name,
                    player_name,
                    update_url,
                    grid_size_x=3,
                    grid_size_y=3,
                    winning_length=3):
        print ""
        print "Player {} has created game {}".format(player_name, game_name)
        game_key = uuid.uuid4()
        player_x = Player(uuid.uuid4(), player_name, update_url)
        player_o = None

        new_game = Game(game_name,
                        game_key,
                        player_x,
                        player_o,
                        size_x=grid_size_x,
                        size_y=grid_size_y,
                        winning_length=winning_length)

        self.games[game_key] = new_game

        dumped_game, errors = GameSchema().dump(new_game)

        return dumped_game
    def test__mark_cell_by_coordinates_raises_exception_when_mark_outside(self):
        game = Game('test-grid',
                    uuid.uuid4(),
                    PLAYER_X,
                    PLAYER_O)
        with pytest.raises(OutOfBoundsException):
            game.mark_cell_by_coordinates(-1, 0, X_MARK)

        with pytest.raises(OutOfBoundsException):
            game.mark_cell_by_coordinates(0, -1, X_MARK)

        with pytest.raises(OutOfBoundsException):
            game.mark_cell_by_coordinates(3, 0, X_MARK)

        with pytest.raises(OutOfBoundsException):
            game.mark_cell_by_coordinates(0, 3, X_MARK)
Example #10
0
    def setUp(self):
        self.player_x = Player(uuid4(), "player x", "http://testx/update")

        self.player_o = Player(uuid4(), "player o", "http://testo/update")
        self.game_key = uuid4()

        self.game = Game("Test Game", self.game_key, self.player_x,
                         self.player_o)

        self.game.state = GAME_INPROGRESS
 def test__init__should_start_in_created_waiting_state(self):
     game = Game('test-game', uuid.uuid4(), PLAYER_X, PLAYER_O)
     assert game.state == GAME_CREATED_WAITING
    def test__init__should_raise_exception_when_name_is_empty(self):
        with pytest.raises(NoNameException):
            Game('', uuid.uuid4(), PLAYER_X, PLAYER_O)

        with pytest.raises(NoNameException):
            Game(None, uuid.uuid4(), PLAYER_X, PLAYER_O)
 def test__init__should_raise_exception_when_winning_length_greater_than_shortest_side(self):
     with pytest.raises(WinningLengthTooLongException):
         Game('test-grid', uuid.uuid4(), PLAYER_X, PLAYER_O, 3, 4, 4, 2)
 def test__init__should_raise_exception_when_winning_length_too_short(self):
     with pytest.raises(WinningLengthTooShortException):
         Game('test-grid', uuid.uuid4(), PLAYER_X, PLAYER_O, 3, 3, 0, 2)
 def test__init__should_raise_exception_when_dimensions_greater_than_two(self):
     with pytest.raises(GridTooLargeException):
         Game('test-grid', uuid.uuid4(), PLAYER_X, PLAYER_O, 3, 3, 3, 3)
 def test__init__should_raise_exception_when_dimensions_less_than_two(self):
     with pytest.raises(GridTooSmallException):
         Game('test-grid', uuid.uuid4(), PLAYER_X, PLAYER_O, 3, 3, 3, 1)