Example #1
0
def game_new():
    """
    Creates a new Quagen game in the backend

    Post:
        ai_count (int): Number of AI players
        ai_strength (int): AI difficulty level
        dimension_x (int): Width of the game board
        dimension_y (int): Height of the game board
        player_count (int): Total number of players in a game
        power (int): Amount of power acquired for a spot to turn solid

    Returns:
        (Response) JSON game state
    """
    posted = request.get_json()

    # Parse out the settings from the post
    settings = {}
    possible_settings = Game.DEFAULT_SETTINGS.keys()
    for setting in possible_settings:
        if setting in posted:
            settings[setting] = int(posted[setting])

    # Create and start the game -- the start is called immediately for now
    # as we do not have a way to change settings or add players in the UI
    game = Game({"settings": settings})
    game.start()

    # Save the game to the database
    queries.insert_game(game)
    queries.insert_game_event(game.game_id, {"type": "start"})

    response = json.jsonify(game=game.get_game_state())
    return response
Example #2
0
def test_game_start(mock_generate):
    """
    Assert game has started
    """
    a_game = Game()
    assert a_game.time_started is None
    mock_generate.assert_not_called()

    a_game.start()
    assert a_game.time_started is not None
    mock_generate.assert_called_once()
Example #3
0
def test_game_in_progress():
    """
    Assert game is in progress between start and end
    """
    a_game = Game()
    assert not a_game.is_in_progress()

    a_game.start()
    assert a_game.is_in_progress()

    a_game.end()
    assert not a_game.is_in_progress()
Example #4
0
def test_game_add_move_duplicate():
    """
    Player cannot move twice in one turn
    """
    a_game = Game()
    a_game.start()
    a_game.settings["player_count"] = 2
    a_game.add_player("player1")
    a_game.add_player("player2")

    assert a_game.add_move("player1", 3, 4)
    assert not a_game.add_move("player1", 5, 6)
Example #5
0
def test_game_is_leading_alt_field():
    """
    Assert player is projected to lead the game
    """
    a_game = Game()

    # Manually hack the scores for test
    a_game._scores = [
        {"controlled": 2, "projected": 1},
        {"controlled": 15, "projected": 1},
        {"controlled": 2, "projected": 17},
        {"controlled": 12, "projected": 2},
    ]

    assert not a_game.is_leading(1, field="projected")
    assert a_game.is_leading(2, field="projected")
Example #6
0
def test_game_is_leading():
    """
    Assert player is leading the game
    """
    a_game = Game()

    # Manually hack the scores for test
    a_game._scores = [
        {"controlled": 2, "projected": 1},
        {"controlled": 15, "projected": 1},
        {"controlled": 2, "projected": 17},
        {"controlled": 12, "projected": 2},
    ]

    assert a_game.is_leading(1)
    assert not a_game.is_leading(3)
Example #7
0
def test_game_process_turn_winner(mock_apply_moves, mock_check_for_winners, mock_end):
    """
    End the game when a winner has been found
    """
    mock_check_for_winners.return_value = True

    a_game = Game()
    a_game.settings["player_count"] = 2
    a_game.add_player("player1")
    a_game.add_player("player2")
    a_game.start()

    a_game.add_move("player1", 1, 1)
    a_game.add_move("player2", 3, 3)
    assert a_game.process_turn()
    mock_apply_moves.assert_called_once()
    mock_end.assert_called_once()
Example #8
0
def test_get_sensitive_state():
    """
    Sensitive state made available
    """
    params = {
        "game_id": "1245",
        "turn_completed": 5,
        "turn_moves": {"0": "234", "1": "234"},
        "settings": {"player_count": 3, "power": 6},
    }

    a_game = Game(params)
    state = a_game.get_sensitive_state()
    assert state["game_id"] == "1245"
    assert state["turn_completed"] == 5
    assert state["settings"]["player_count"] == 3
    assert state["settings"]["power"] == 6
    assert {"0": "234", "1": "234"} == state["turn_moves"]
Example #9
0
def test_game_get_leaders():
    """
    Grab the correct leader based on current score
    """
    a_game = Game()

    # Manually hack the scores for test
    a_game._scores = [
        {"controlled": 2},
        {"controlled": 15},
        {"controlled": 2},
        {"controlled": 4},
    ]

    leaders = a_game.get_leaders()
    assert len(leaders) == 1
    assert leaders[0][0] == 1
    assert leaders[0][1] == 15
Example #10
0
def test_game_get_leaders_alt_field():
    """
    Grab the correct leader based on projected score
    """
    a_game = Game()

    # Manually hack the scores for test
    a_game._scores = [
        {"controlled": 2, "projected": 1},
        {"controlled": 15, "projected": 1},
        {"controlled": 2, "projected": 17},
        {"controlled": 15, "projected": 2},
    ]

    leaders = a_game.get_leaders("projected")
    assert len(leaders) == 1
    assert leaders[0][0] == 2
    assert leaders[0][1] == 17
Example #11
0
def test_get_game_state():
    """
    Share only information we want to share
    """
    params = {
        "game_id": "1245",
        "turn_completed": 5,
        "turn_moves": {"0": "234", "1": "234"},
        "settings": {"player_count": 3, "power": 6},
    }

    a_game = Game(params)
    state = a_game.get_game_state()
    assert "turn_moves" not in state
    assert state["game_id"] == "1245"
    assert state["turn_completed"] == 5
    assert state["settings"]["player_count"] == 3
    assert state["settings"]["power"] == 6
Example #12
0
def test_game_add_move_not_joined():
    """
    Player cannot make move when not part of game
    """
    a_game = Game()
    a_game.start()
    a_game.settings["player_count"] = 2
    a_game.add_player("player1")
    a_game.add_player("player2")

    assert not a_game.add_move("player3", 3, 4)
Example #13
0
def test_game_end_once():
    """
    Assert game only ends once
    """
    a_game = Game()
    a_game.start()
    a_game.end()
    time_completed = a_game.time_completed

    a_game.end()
    assert a_game.time_completed == time_completed
Example #14
0
def test_process_turn(mock_calculate_scores, mock_apply_power, mock_apply_moves):
    """
    Process a turn after all players made a move
    """
    a_game = Game()
    a_game.start()
    a_game.settings["player_count"] = 2
    a_game.add_player("player1")
    a_game.add_player("player2")
    a_game.add_move("player1", 1, 1)
    a_game.add_move("player2", 3, 3)

    assert a_game.process_turn()
    assert a_game._turn_completed == 1
    assert {} == a_game._turn_moves
    assert [[[1, 1, 1], [3, 3, 2]]] == a_game._history
    mock_apply_moves.assert_called_once()
    mock_apply_power.assert_called_once()
    mock_calculate_scores.assert_called()
Example #15
0
def test_game_check_winner_no_availability(mock_get_movable_spots):
    """
    Assert a winner exists when no spots are available
    """
    mock_get_movable_spots.return_value = []

    a_game = Game()
    a_game.settings["player_count"] = 2
    a_game.add_player("player1")
    a_game.add_player("player2")
    a_game.start()

    assert a_game._check_for_winners()
Example #16
0
def test_game_is_leading_tied():
    """
    Assert tied players leading the game
    """
    a_game = Game()

    # Manually hack the scores for test
    a_game._scores = [
        {"controlled": 2, "projected": 1},
        {"controlled": 15, "projected": 1},
        {"controlled": 2, "projected": 17},
        {"controlled": 15, "projected": 2},
    ]

    # Not an outright lead when tied
    assert not a_game.is_leading(1)
    assert not a_game.is_leading(3)

    assert a_game.is_leading(1, outright=False)
    assert a_game.is_leading(3, outright=False)
Example #17
0
def test_game_add_move_invalid(mock_validate_move):
    """
    Player cannot add a invalid move
    """
    mock_validate_move.return_value = False

    a_game = Game()
    a_game.settings["player_count"] = 2
    a_game.add_player("player1")
    a_game.add_player("player2")
    a_game.start()

    assert not a_game.add_move("player1", -1, -1)
    mock_validate_move.assert_called_once()
Example #18
0
def test_game_is_player():
    """
    Assert verifying players in game
    """
    a_game = Game()
    a_game.add_player("player1")
    a_game.add_player("player2")

    assert a_game.is_player("player1")
    assert a_game.is_player("player2")
    assert not a_game.is_player("player3")
    assert not a_game.is_player("player12")
Example #19
0
def test_game_add_player_not_exists():
    """
    Same player can't be added twice
    """
    a_game = Game()
    a_game.settings["player_count"] = 4

    assert a_game.add_player("duplicate")
    assert not a_game.add_player("duplicate")
    assert a_game.add_player("allgood")
Example #20
0
def test_game_add_move_not_started():
    """
    Player cannot make move when game has not started
    """
    a_game = Game()
    a_game.settings["player_count"] = 2
    a_game.add_player("player1")
    a_game.add_player("player2")

    assert not a_game.add_move("player1", 3, 4)
Example #21
0
class Simulation:
    """
    A single game simulation
    """

    def __init__(self):
        self._game = Game()
        self._ai_players = []

    @property
    def game(self):
        """The game being simulated"""
        return self._game

    def add_ai_player(self, ai_instance):
        """
        Add an AI player to to the simulation

        Args:
            ai_instance (AI): Instance of an AI class

        """
        ai_name = ai_instance.__class__.__name__ + "_" + str(len(self._ai_players))
        self._ai_players.append((ai_name, ai_instance))
        self._game.add_player(ai_name)

    def run(self):
        """
        Simulate the game until completion
        """
        self._game.start()
        while self._game.is_in_progress():
            for ai_player in self._ai_players:
                ai_name = ai_player[0]
                ai_instance = ai_player[1]

                x, y = ai_instance.choose_move()
                self._game.add_move(ai_name, x, y)

            self._game.process_turn()
Example #22
0
def test_game_start_once(mock_generate):
    """
    Assert game can not start more than once
    """
    a_game = Game()
    a_game.start()
    time_started = a_game.time_started

    a_game.start()
    assert a_game.time_started == time_started
    mock_generate.assert_called_once()
Example #23
0
def test_game_end():
    """
    Assert game ends in right state
    """
    a_game = Game()
    a_game.start()
    assert not a_game.completed
    assert a_game.time_completed is None

    a_game.end()
    assert a_game.completed
    assert a_game.time_completed is not None
Example #24
0
def get_game(game_id):
    """
    Retrieves a Game from the database

    Attr:
        game_id (str): Id of the game to retrieve

    Returns:
        Game object
    """
    game = None
    row = db.query("SELECT game_id, data  FROM game WHERE game_id = %s",
                   [game_id], True)

    if row is not None:
        data = json.loads(row["data"])
        game = Game(data)

    return game
Example #25
0
def test_game_check_winner_no_hope(mock_get_movable_spots):
    """
    Assert a winner when no players can catch leader
    """
    mock_get_movable_spots.return_value = [(0, 0), (0, 1), (0, 2)]

    a_game = Game()
    a_game.settings["player_count"] = 3
    a_game.add_player("player1")
    a_game.add_player("player2")
    a_game.add_player("player3")
    a_game.start()

    # Fudge the scores so there are just enough available spots
    a_game._scores = [
        {"controlled": 0},
        {"controlled": 10},
        {"controlled": 8},
        {"controlled": 3},
    ]
    assert not a_game._check_for_winners()

    # Fudge the scores so there are not enough available spots
    a_game._scores = [
        {"controlled": 0},
        {"controlled": 10},
        {"controlled": 4},
        {"controlled": 3},
    ]
    assert a_game._check_for_winners()
Example #26
0
def test_game_check_winner_majority():
    """
    Assert a winner exists when a player controls the majority of spots
    """
    a_game = Game()
    a_game.settings["player_count"] = 2
    a_game.add_player("player1")
    a_game.add_player("player2")
    a_game.start()

    # Fudge the scores so we have just under majority
    a_game._scores = [{"controlled": 0}, {"controlled": 199}, {"controlled": 53}]
    assert not a_game._check_for_winners()

    # Fudge the scores so we now have a majority
    a_game._scores = [{"controlled": 0}, {"controlled": 201}, {"controlled": 53}]
    assert a_game._check_for_winners()
Example #27
0
def test_game_process_turn_missing_players(mock_apply_moves):
    """
    Can't process turn if all players have not moved
    """
    a_game = Game()
    a_game.start()
    a_game.settings["player_count"] = 3
    a_game.add_player("player1")
    a_game.add_player("player2")
    a_game.add_player("player3")

    a_game.add_move("player1", 1, 1)
    a_game.add_move("player3", 3, 3)
    assert not a_game.process_turn()
    assert a_game._turn_completed == 0
    mock_apply_moves.assert_not_called()

    a_game.add_move("player2", 5, 5)
    assert a_game.process_turn()
    assert a_game._turn_completed == 1
    mock_apply_moves.assert_called_once()
Example #28
0
def test_game_process_turn_not_in_progress(mock_apply_moves):
    """
    Can't process if game not in progress
    """

    # Not started
    a_game = Game()
    a_game.settings["player_count"] = 3
    a_game.add_player("player1")
    a_game.add_player("player2")
    a_game.add_player("player3")

    a_game.add_move("player1", 1, 1)
    a_game.add_move("player3", 3, 3)
    assert not a_game.process_turn()
    assert a_game._turn_completed == 0
    mock_apply_moves.assert_not_called()

    # Already ended
    a_game = Game()
    a_game.start()
    a_game.settings["player_count"] = 3
    a_game.add_player("player1")
    a_game.add_player("player2")
    a_game.add_player("player3")
    a_game.add_move("player1", 1, 1)
    a_game.add_move("player3", 3, 3)
    a_game.end()

    assert not a_game.process_turn()
    assert a_game._turn_completed == 0
    mock_apply_moves.assert_not_called()
Example #29
0
 def __init__(self):
     self._game = Game()
     self._ai_players = []
Example #30
0
def test_game_process_turn_multiple(
    mock_calculate_scores, mock_apply_power, mock_apply_moves
):
    """
    Process consecutive turns
    """
    a_game = Game()
    a_game.settings["player_count"] = 2
    a_game.add_player("player1")
    a_game.add_player("player2")
    a_game.start()

    # First move
    a_game.add_move("player1", 1, 1)
    a_game.add_move("player2", 3, 3)
    assert a_game.process_turn()

    # Second move
    a_game.add_move("player2", 3, 4)
    a_game.add_move("player1", 5, 9)
    assert a_game.process_turn()
    assert a_game._turn_completed == 2
    assert {} == a_game._turn_moves
    assert [[[1, 1, 1], [3, 3, 2]], [[3, 4, 2], [5, 9, 1]]] == a_game._history
    assert mock_apply_moves.call_count == 2
    assert mock_apply_power.call_count == 2
    mock_calculate_scores.assert_called()