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)
def __init__(self, gid: str, red: Agent, blue: Agent, board: GameBoard = None, state: GameState = None, seed: int = 42, useLoggers: bool = True): """ Initialize the state-machine. :param gid: Unique identifier. :param board: Static descriptor of the game. :param state: Dynamic descriptor of the game. :param red: String value of the player to use. Check module agent.interactive for a list. :param blue: String value of the player to use. Check module agent.interactive for a list. :param seed: Random seed value (default: 42) """ if useLoggers: self.logger = logging.getLogger(__name__) else: self.logger = logging.getLogger("internal") self.gid: str = gid self.seed: int = seed self.actions_history: List[Action] = [] self.states_history: List[GameState] = [] self.outcome: List[Outcome] = [] self.winner: str = '' self.end: bool = False self.update: bool = False self.board: GameBoard = board self.state: GameState = state self.origin: GameState = deepcopy(state) self.gm: GameManager = GameManager() self.red: Agent = red self.blue: Agent = blue self.humans: bool = isinstance(self.red, Human) or isinstance( self.blue, Human) self.first = None self.second = None self.step = self._goInit
def __init__(self, name: str, team: str, seed: int = 0): """ :param name: name of this agent :param team: color of the team :param seed: random seed to use internally """ self.name: str = name self.team: str = team self.seed: int = seed self.count: int = 0 # history of registered actions used to build the dataframe of actions performed self.history: list = [] # internal game manager self.gm: GameManager = GameManager()
import unittest from core.actions import Move from core.const import RED, BLUE from core.figures import buildFigure from core.game import GameBoard, GameState, GoalMaxTurn, GoalReachPoint, GoalEliminateOpponent, GameManager from core.utils.coordinates import Hex GM: GameManager = GameManager() class TestGoals(unittest.TestCase): 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) def testEliminateOpponent(self): g = GoalEliminateOpponent(RED, BLUE) self.assertFalse(g.check(self.state), 'before attack, blue unit is still alive!') self.blue_tank.killed = True