예제 #1
0
def vectorBoard(board: GameBoard, state: GameState, action: Action = None, params: GoalParams = None) -> tuple:
    # TODO: add features that are an interaction of board and state:
    #       - distance from goals
    #       - distance to cover (forest, building)

    data = []

    # LOS/LOF check
    for teams in [(RED, BLUE), (BLUE, RED)]:
        team, other = teams
        for i in range(MAX_UNITS_PER_TEAM):
            for j in range(MAX_UNITS_PER_TEAM):
                if i != j:
                    if i < len(state.figures[team]) and j < len(state.figures[team]):
                        line: list = state.figuresDistance.get(team)[j][i]
                        data.append(GameManager.checkLine(board, state, line))
                    else:
                        data.append(None)

    # goals parameter (static for the whole game) TODO: and if we made them dynamic?!
    if params:
        data += [
            params.unit_team_lost,
            params.unit_team_alive,
            params.unit_enemy_killed,
            params.unit_enemy_alive,
            params.reach_team_near,
            params.defend_team_near,
            params.defend_enemy_near,
            params.wait_for_turn
        ]
    else:
        params = GoalParams()
        data += [None] * 8

    # info on the goals
    for team in [RED, BLUE]:
        objectives = board.objectives[team]

        for goal in GOAL_KEY_LIST:
            if goal in objectives:
                obj = objectives[goal]
                data.append(obj.score(state, params))
                data.append(obj.check(state))
            else:
                data.append(None)
                data.append(None)

    # extra info from action
    if action and isinstance(action, Move):
        data.append(board.getProtectionLevel(action.destination))
        data.append(board.getMovementCost(action.destination, FigureType.INFANTRY))
        data.append(board.getMovementCost(action.destination, FigureType.VEHICLE))
    else:
        data.append(-1)
        data.append(-1)
        data.append(-1)

    return tuple(data)
예제 #2
0
    def setUp(self):
        shape = (8, 8)
        self.board = GameBoard(shape)
        self.state = GameState(shape)

        self.blue_tank = buildFigure('Tank', (6, 4), BLUE)
        self.red_tank = buildFigure('Tank', (1, 4), RED)
        self.red_inf = buildFigure('Infantry', (2, 4), RED)

        self.state.addFigure(self.red_tank, self.red_inf, self.blue_tank)
예제 #3
0
    def search(self, board: GameBoard, state: GameState) -> Tuple[float, Action]:
        """
        Initialize some internal values after that it continues with the basic search() method.

        :param board:   board of the game
        :param state:   starting state
        :return: a tuple composed by the score and the best Action found
        """
        if not self.boardValues:
            # this is done only once for the first search since we assume that the board does not change over time
            self.boardValues = GoalReachPoint(self.team, board.shape, board.getObjectiveMark())

        return super().search(board, state)
예제 #4
0
def stateScore(team: str, params: GoalParams, board: GameBoard,
               state: GameState) -> float:
    """
    Calculates the score of a state based on the objectives assigned to the given team. The given "params" are used to
    assign a score to each goal in the GameBoard based on the given GameState.

    :param team:        team that performs the action
    :param params:      GoalParams given to the agent
    :param board:       board of the game
    :param state:       current state to evaluate
    :return: the score associate to the current state based on the given params
    """
    goals = board.getObjectives(team)
    return sum([goal.score(state, params) for goal in goals])
예제 #5
0
    def setUp(self):
        shape = (16, 16)
        self.board = GameBoard(shape)
        self.state = GameState(shape)

        self.inf_1 = buildFigure('Infantry', (5, 11), RED)
        self.inf_2 = buildFigure('Infantry', (7, 12), RED)

        self.target_1 = buildFigure('Infantry', (3, 13), BLUE)
        self.target_2 = buildFigure('Infantry', (5, 13), BLUE)
        self.target_3 = buildFigure('Infantry', (7, 13), BLUE)
        self.target_4 = buildFigure('Infantry', (9, 13), BLUE)

        self.state.addFigure(
            self.inf_1,
            self.inf_2,
            self.target_1,
            self.target_2,
            self.target_3,
            self.target_4,
        )
예제 #6
0
    def setUp(self):
        shape = (16, 16)
        self.board = GameBoard(shape)
        self.state = GameState(shape)

        self.inf_1 = buildFigure('Infantry', (4, 0), RED)
        self.inf_2 = buildFigure('Infantry', (8, 0), BLUE)

        self.state.addFigure(
            self.inf_1,
            self.inf_2,
        )

        self.red = Puppet(RED)
        self.red.action = GM.actionMove(self.board, self.state, self.inf_1, destination=self.inf_1.position)
        self.red.response = GM.actionNoResponse(RED)

        self.blue = Puppet(BLUE)
        self.blue.action = GM.actionMove(self.board, self.state, self.inf_2, destination=self.inf_2.position)
        self.blue.response = GM.actionNoResponse(BLUE)

        self.mm = MatchManager('', self.red, self.blue)
예제 #7
0
def blank(shape) -> (GameBoard, GameState):
    return GameBoard(shape), GameState(shape)