def __init__(self, num_of_rows=7, num_of_cols=7): self.current_player = PLAYER self.num_of_rows = num_of_rows self.num_of_cols = num_of_cols self.board_fields = [] self.player_positions = [-1, -1] self.best_direction_to_go = -1 self.best_num_of_steps = -1 ai_player_x = 0 ai_player_y = self.num_of_cols // 2 player_x = self.num_of_rows - 1 player_y = self.num_of_cols // 2 print(ai_player_x, ai_player_y, player_x, player_y) for i in range(num_of_rows): for j in range(num_of_cols): if i == ai_player_x and j == ai_player_y: self.player_positions[0] = len(self.board_fields) self.board_fields.append( Field(ai_player_x, ai_player_y, AI_PLAYER, True)) elif i == player_x and j == player_y: self.player_positions[1] = len(self.board_fields) self.board_fields.append( Field(player_x, player_y, PLAYER, True)) else: self.board_fields.append(Field(i, j))
def __init__(self): self.time_per_move = -1 self.timebank = -1 self.last_update = None self.max_rounds = -1 self.round = 0 self.player_names = [] self.players = {} self.me = None self.opponent = None self.field = Field()
def read_field(lines, symbol_by_cell): # read size x_size, y_size = map(int, lines[0].split()) # read field field = [[None] * y_size for _ in range(x_size)] read_cells(field, lines, symbol_by_cell, x_size, y_size) walls = read_walls(lines, x_size, y_size) return Field(field, walls, has_key=symbol_by_cell.get(Key().to_symbol(), None) is not None)
def UCTPlayGame(nn, nn2=None): """ Self-play using MCTS, returns s_t's, pi_t's, and z to use for training. """ width = config.board_width height = config.board_height cells_each_player = config.cells_each_player field = Field() field.width = width field.height = height field.parse(init_cells(field.width, field.width, cells_each_player)) state = GOLADState(field) data = {} data['s'] = [] data['pi'] = [] c = None current_nn = nn # use nn for first player while (state.GetMoves() != []): c, pi = UCT(rootstate=state, itermax=config.mcts_itermax, nn=current_nn, verbose=config.verbose, rootnode=c) m = c.move data['s'].append(state.Convert()) data['pi'].append(pi) if config.verbose: print ("\nTurn {}, Player {}, Best Move: {}" \ .format(state.timestep, state.current_player, str(m))) state.field.pprint() state.DoMove(m) if nn2 is not None: if current_nn == nn: current_nn = nn2 else: current_nn = nn if config.verbose: print('Result: {}'.format(state.GetResult(0))) state.field.pprint() data['z'] = [[state.GetResult(0)]] * len( data['s']) # get result from perspective of first player (ie rootnode) return data
def generate_field(x_size, y_size): cells = [[Empty() for _ in range(y_size)] for _ in range(x_size)] vertical_walls = [[True for _ in range(y_size)] for _ in range(x_size)] horizontal_walls = [[True for _ in range(y_size)] for _ in range(x_size)] field = Field(cells, (vertical_walls, horizontal_walls), has_key=False) game_field = GameField(field) generate_walls(game_field) config = GenerationConfig(x_size, y_size) exit_direction = calc_random_direction() for exit_count in range(config.max_exits_on_map): random_put_cell_on(field, Exit(exit_direction), wall_required_at=exit_direction) random_put_cell_on(field, Key()) teleport_counter = 0 for _ in range(config.random_cell_count): random_cell_type = calc_random_cell_type() if teleport_counter == config.max_teleport_count: while random_cell_type is Teleport: random_cell_type = calc_random_cell_type() if random_cell_type is Stun: random_put_cell_on(field, Stun(config.default_stun_duration)) elif random_cell_type is RubberRoom: random_direction = calc_random_direction() random_put_cell_on(field, RubberRoom(random_direction), without_wall_at_direction=random_direction) elif random_cell_type is Teleport: teleport_counter += 1 random_put_cell_on( field, Teleport(calc_random_position_on(field).as_tuple())) while fix_bad_rubber_room(game_field): pass return game_field.field
def test_Field_getUnknownTile(self): field = Field(100,100) tile = field.getTile(1,2) self.assertIsNotNone(tile)
def test_Field_setTile(self): field = Field(100, 100) tile = Tile() field.setTile(3,4, tile) self.assertEqual(tile, field.getTile(3, 4))
arguments, setting = gf.create_arguments(arguments, sys.argv[1:], setting) itteration = 1 map = np.array([[0 for _ in range(setting.width // setting.pixel_size)] for _ in range(setting.height // setting.pixel_size)]) trees = gf.create_trees(arguments, setting, map) if not arguments['non_gui']: delay = 0 pygame.font.init() pygame.init() screen = pygame.display.set_mode( (setting.width, setting.height)) # создание экрана field = Field(screen, setting) dashboard = Dashboard(screen, setting) dashboard.switches[0].pressed = arguments['step_mode'] next_step = False while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() for button in dashboard.buttons: # отработка нажатий кнопок и выключателей button.button_down(event) for switcher in dashboard.switches: switcher.press(event) if event.type == pygame.KEYDOWN: # следующий шаг в step mode при нажатии пробела или стрелки