예제 #1
0
def right_click(x, y, game):
    """ Set/increment/clear the flag in this cell. """
    flag_map = DBSession.query(models.PlayerMap).filter_by(
        game_id=game.id, map_type=const.PlayerMapType.FLAG).first()

    # Get the FlagMapData, if it exists
    flag_data = DBSession.query(models.PlayerMapData).filter_by(
        player_map_id=flag_map.id, row_num=x, col_num=y).first()

    if flag_data is None:
        # UNCLICKED --> FLAG
        recorded_flag = models.PlayerMapData(
            player_map_id=flag_map.id, row_num=x, col_num=y,
            value=const.PlayerMapDataValue.FLAG)
        DBSession.add(recorded_flag)
        value = const.PlayerMapDataValue.FLAG
    else:
        if flag_data.value == const.PlayerMapDataValue.FLAG:
            # FLAG --> UNSURE
            value = const.PlayerMapDataValue.UNSURE
            flag_data.value = value
            DBSession.add(flag_data)
        else:
            # UNSURE --> UNCLICKED
            value = const.CellStates.UNCLICKED
            DBSession.delete(flag_data)
            DBSession.flush()
    return (value, game)
예제 #2
0
    def post(self):
        """ Creates a new Game, MineMap, and PlayerMaps. """
        # Create a MineMap
        mine_matrix = MineMatrix(const.DEFAULT_HEIGHT,
                                 width=const.DEFAULT_WIDTH,
                                 mine_number=const.DEFAULT_MINES)
        mine_map = mine_matrix.to_model()
        DBSession.add(mine_map)
        DBSession.flush()

        # Create a Game and PlayerMaps for it
        game = models.Game(mine_map=mine_map.id)
        game.player_maps = [
            models.PlayerMap(map_type=const.PlayerMapType.CLICK),
            models.PlayerMap(map_type=const.PlayerMapType.FLAG),
        ]
        DBSession.add(game)
        DBSession.flush()
        return HTTPFound(location=self.request.route_url('view_game',
                                                         game_id=game.id))
예제 #3
0
def left_click(x, y, click_map, game):
    reveal = []

    # Record a successful click on the Click Map
    recorded_click = models.PlayerMapData(
        player_map_id=click_map.id, row_num=x, col_num=y,
        value=const.PlayerMapDataValue.CLICKED)
    DBSession.add(recorded_click)

    mine_map = DBSession.query(models.MineMap).get(game.mine_map)
    mine_matrix = mine_map.to_matrix()
    value = mine_matrix[x][y]

    # If the cell contains a mine, game over
    if value is const.MineMapDataValue.MINE:
        # Update game state
        game.state = const.GameState.LOSE
        DBSession.add(game)
        DBSession.flush()
        return (value, reveal, game)

    # If a blank space got revealed, time for a cascade reveal
    if value is const.MineMapDataValue.CLUE[0]:
        reveal = cascade_reveal(x, y, mine_matrix, click_map.id)
    DBSession.flush()

    # Compare the Click matrix to the Win matrix. If the same, the game is won
    click_map = DBSession.query(models.PlayerMap).filter_by(
        game_id=game.id, map_type=const.PlayerMapType.CLICK).first()
    click_matrix = click_map.to_matrix()
    win_matrix = derive_win_matrix(game)
    if click_matrix == win_matrix:
        # Update game state
        game.state = const.GameState.WIN
        DBSession.add(game)
        DBSession.flush()
    return (value, reveal, game)