def test_get_chained_push_movements_empty(self): board = Board() board.get_tile(1, 3).place_piece(Piece(PLAYER_1_ID, PieceType.four)) movements = board.get_chained_push_movements(board.get_tile(1, 3), board.get_tile(1, 2)) assert movements == [ (Tile(1, 3), Tile(1, 2)), ]
def test_get_chained_push_movements_edge(self): board = Board() board.get_tile(3, 3).place_piece(Piece(PLAYER_1_ID, PieceType.four)) board.get_tile(3, 4).place_piece(Piece(PLAYER_1_ID, PieceType.one)) movements = board.get_chained_push_movements(board.get_tile(3, 3), board.get_tile(3, 4)) assert movements == [ (Tile(3, 3), Tile(3, 4)), (Tile(3, 4), None), ]
def init_tiles(self): """Initializes the tiles """ # initializes the tiles to a full rummikub game for num in range(1, 14): # 1-13 for color in TileColor: print("Created tile with num", num, "and color", color) self.tiles.append(Tile(num, color)) # add jokers, denoted by 0 self.tiles.append(Tile(0, TileColor.RED)) self.tiles.append(Tile(0, TileColor.BLACK))
def refresh_palette_tiles(self): self.all_palette_tile_sprites.empty() self.palette_tiles[:] = [] self.palette_ai_spawns[:] = [] x_pos = 40 y_pos = 40 sorted_tile_keys = sorted(self.tiled_level.all_tile_data.keys()) display_tile = self.palette_page * self.tiles_per_page max_tile = (self.palette_page * self.tiles_per_page) + self.tiles_per_page while display_tile < len( sorted_tile_keys ) + self.num_ai_spawns and display_tile < max_tile: if display_tile < len(sorted_tile_keys): tile_data = sorted_tile_keys[display_tile] self.palette_tiles.append( Tile([self.hud_rect[0] + x_pos, self.hud_rect[1] + y_pos], 0, self.tiled_level.all_tile_data[tile_data], self.editing_layer)) display_tile += 1 x_pos += 72 if x_pos > 904: x_pos = 40 y_pos += 72 for tile in self.palette_tiles: self.all_palette_tile_sprites.add(tile.sprite) for aiSpawn in self.palette_ai_spawns: self.all_palette_tile_sprites.add(aiSpawn.sprite)
def mark(self, row, col): """ Marks the tile at the row and column as a bomb and updates the remaining value of surrounding tiles """ self.board.mark(row, col) self.unmarkedBombs -= 1 tilesToReduce = self.getAllSurroundingTiles(Tile(0, row, col)) for t in tilesToReduce: if t is not None and t.remainingValue != 0 and t.value != BOMB: self.board.board[t.row][t.col].remainingValue -= 1
def load_tiles(self): if os.path.isfile(self.file_name): self.tiles[:] = [] self.collidable_tiles[:] = [] self.walkable_tiles[:] = [] with open(self.file_name, "r") as tileFile: reader = csv.reader(tileFile) for line in reader: line_type = line[0] if line_type == "tile": tile_id = line[1] tile_x_pos = int(line[2]) tile_y_pos = int(line[3]) tile_angle = int(line[4]) tile_layer = 0 if len(line) == 6: tile_layer = int(line[5]) loaded_tile = Tile([tile_x_pos, tile_y_pos], tile_angle, self.all_tile_data[tile_id], tile_layer, self.collision_grid) self.tiles.append(loaded_tile) if tile_layer == 0: tile_x = int( (tile_x_pos - (self.tile_size[0] / 2)) / self.tile_size[0]) tile_y = int( (tile_y_pos - (self.tile_size[1] / 2)) / self.tile_size[1]) self.tile_grid[tile_x][tile_y] = loaded_tile if loaded_tile.collidable: self.collidable_tiles.append(loaded_tile) else: self.walkable_tiles.append(loaded_tile) elif tile_layer == 1: tile_x = int( (tile_x_pos - (self.tile_size[0] / 2)) / self.tile_size[0]) tile_y = int( (tile_y_pos - (self.tile_size[1] / 2)) / self.tile_size[1]) self.top_tile_grid[tile_x][tile_y] = loaded_tile elif line_type == "aiSpawn": type_id = line[1] tile_x_pos = int(line[2]) tile_y_pos = int(line[3]) new_ai_spawn = AISpawn(self.guards_sprite_map[0][1], [tile_x_pos, tile_y_pos], type_id) self.ai_spawns.append(new_ai_spawn) else: self.clear_level_to_default_tile()
def clear_level_to_default_tile(self): for x in range(0, self.level_tile_size[0]): for y in range(0, self.level_tile_size[1]): x_centre = 32 + (x * 64) y_centre = 32 + (y * 64) default_tile = Tile([x_centre, y_centre], 0, self.default_tile, 0) self.tiles.append(default_tile) self.walkable_tiles.append(default_tile) self.tile_grid[x][y] = default_tile
def __init__(self, rows, cols, bombs): self.rows = rows self.cols = cols self.max_bombs = bombs self.firstMove = True self.correctMarks = 0 self.board = [[Tile(0, r, c) for c in range(self.cols)] for r in range(self.rows)] self.setupBoard() # FIXME: REMOVEME
def clear_level_to_default_tile(self): for x in range(0, self.level_tile_size[0]): for y in range(0, self.level_tile_size[1]): x_centre = self.tile_size[0] / 2 + (x * self.tile_size[0]) y_centre = self.tile_size[1] / 2 + (y * self.tile_size[1]) default_tile = Tile([x_centre, y_centre], 0, random.choice(self.default_tiles), 0, self.collision_grid) self.tiles.append(default_tile) self.walkable_tiles.append(default_tile) self.tile_grid[x][y] = default_tile
def _initialize_sprites(self, field_map): height = len(field_map) width = len(field_map[0]) for map_y in range(height): for map_x in range(width): value = field_map[map_y][map_x] norm_x = map_x * self.tile_size norm_y = map_y * self.tile_size + self.tile_size self.tiles.add(Tile(value, norm_x, norm_y))
class TestTile(unittest.TestCase): def setUp(self): self.tile = Tile(-1, 0, 0) def test_tile_can_be_clicked(self): self.tile.click() self.assertEqual(self.tile.style, "mine.png") def test_tile_can_be_flagged(self): self.tile.flag() self.assertEqual(self.tile.style, "flag.png") def test_tile_can_be_unflagged(self): self.tile.unflag() self.assertEqual(self.tile.style, "tile2.png") def test_tile_style_click_0(self): tile = Tile(0, 0, 0) self.assertEqual(tile.click(), 0) def test_tile_style_click_1(self): tile = Tile(1, 0, 0) self.assertEqual(tile.click(), 1) def test_tile_style_click_2(self): tile = Tile(2, 0, 0) self.assertEqual(tile.click(), 2) def test_tile_style_click_3(self): tile = Tile(3, 0, 0) self.assertEqual(tile.click(), 3) def test_tile_style_click_4(self): tile = Tile(4, 0, 0) self.assertEqual(tile.click(), 4) def test_tile_style_click_5(self): tile = Tile(5, 0, 0) self.assertEqual(tile.click(), 5) def test_tile_style_click_6(self): tile = Tile(6, 0, 0) self.assertEqual(tile.click(), 6) def test_tile_style_click_7(self): tile = Tile(7, 0, 0) self.assertEqual(tile.click(), 7) def test_tile_style_click_8(self): tile = Tile(8, 0, 0) self.assertEqual(tile.click(), 8) def test_tile_style_click_9(self): tile = Tile(9, 0, 0) self.assertEqual(tile.click(), 9) def test_tile_style_click_non_valid_value(self): tile = Tile("test", 0, 0) self.assertEqual(tile.click(), 0)
def setupBoard(self): for r in range(self.rows): for c in range(self.cols): self.board[r][c].value = 0 setBombs = 0 # NOTE: might be slow if bomb dense while (setBombs < self.max_bombs): row = randint(0, self.cols - 1) col = randint(0, self.rows - 1) if (self.board[row][col].value != BOMB): self.board[row][col] = Tile(BOMB, row, col) setBombs += 1 # TODO: improve? for r in range(self.cols): for c in range(self.rows): self.updateCounts(r, c)
def setup_tiles() -> Tuple[List[Tile], Dict[int, Dict[int, Tile]]]: tiles = list() coordinates_dict = dict() for x in range(PLAY_AREA_SIZE): for y in range(PLAY_AREA_SIZE): if x in PERIMETER_COORDINATES and y in PERIMETER_COORDINATES: # It's a corner, ignore continue tile = Tile(x, y) tiles.append(tile) # Add to coordinates dict if coordinates_dict.get(x) is None: coordinates_dict[x] = dict() coordinates_dict[x][y] = tile return tiles, coordinates_dict
def load_level_tiles(self): if os.path.isfile(self.file_name): self.create_empty_level() with open(self.file_name, "r") as tileFile: reader = csv.reader(tileFile) for line in reader: line_type = line[0] if line_type == "tile": tile_id = line[1] tile_x_pos = float(line[2]) tile_y_pos = float(line[3]) tile_angle = int(line[4]) layer = 0 if len(line) == 6: layer = int(line[5]) grid_layer = self.tile_grid_layers["layer_" + str(layer)] tile_x = int((tile_x_pos - (self.tile_size[0] / 2)) / self.tile_size[0]) tile_y = int((tile_y_pos - (self.tile_size[1] / 2)) / self.tile_size[1]) loaded_tile = Tile(grid_layer["sprite_group"], self.collision_grid, self.all_tile_data[tile_id], [tile_x_pos, tile_y_pos], layer, tile_angle) grid_layer["grid"][tile_x][tile_y] = loaded_tile self.update_tile_collision_normals( tile_x, tile_y, layer) elif line_type == "entity_placement": type_id = line[1] sub_type_id = line[2] tile_x_pos = float(line[3]) tile_y_pos = float(line[4]) if type_id == "ai_spawn": new_entity_placement = AISpawn( [tile_x_pos, tile_y_pos], type_id, sub_type_id, self.ai_spawn_data) self.entity_placements.append(new_entity_placement)
def generate_random_board(): available_types = { GRID_TYPES.STONE: 3, GRID_TYPES.WOOD: 4, GRID_TYPES.WHEAT: 4, GRID_TYPES.BRICKS: 3, GRID_TYPES.SHEEP: 4, } b = list(map(lambda item: [item[0]] * item[1], available_types.items())) types = [item for sublist in b for item in sublist] np.random.shuffle(types) values = [2, 3, 3, 4, 4, 5, 5, 6, 6, 8, 8, 9, 9, 10, 10, 11, 11, 12] np.random.shuffle(values) tileData = [[types[i], values[i]] for i in range(len(types))] tileData.append([GRID_TYPES.DESERT, None]) np.random.shuffle(tileData) return [Tile(i, d[0], d[1]) for i, d in enumerate(tileData)]
def test_get_chained_push_movements(self): board = Board() board.get_tile(0, 3).place_piece(Piece(PLAYER_1_ID, PieceType.four)) board.get_tile(1, 3).place_piece(Piece(PLAYER_1_ID, PieceType.two)) board.get_tile(2, 3).place_piece(Piece(PLAYER_2_ID, PieceType.three)) board.get_tile(4, 3).place_piece(Piece(PLAYER_2_ID, PieceType.one)) movements = board.get_chained_push_movements(board.get_tile(0, 3), board.get_tile(1, 3)) assert len(movements) == 3 assert movements == [ (Tile(0, 3), Tile(1, 3)), (Tile(1, 3), Tile(2, 3)), (Tile(2, 3), Tile(3, 3)), ]
def set_tile_at_screen_pos(self, camera, screen_position, tile_data_id, angle, layer): view_top_left_position = (camera.position[0] - camera.half_width, camera.position[1] - camera.half_height) world_click_position = [ screen_position[0] + view_top_left_position[0], screen_position[1] + view_top_left_position[1] ] half_tile_size = (self.tile_size[0] / 2, self.tile_size[1] / 2) tile_x = int((world_click_position[0]) // self.tile_size[0]) tile_y = int((world_click_position[1]) // self.tile_size[1]) tile_layer = self.tile_grid_layers["layer_" + str(layer)] tile_data = self.all_tile_data[tile_data_id] new_tile = Tile(tile_layer["sprite_group"], self.collision_grid, tile_data, [(tile_x * self.tile_size[0]) + half_tile_size[0], (tile_y * self.tile_size[1]) + half_tile_size[1]], layer, angle) tile_layer["grid"][tile_x][tile_y] = new_tile self.update_tile_collision_normals(tile_x, tile_y, layer)
def test_tile_style_click_2(self): tile = Tile(2, 0, 0) self.assertEqual(tile.click(), 2)
def set_tile_at_pos(self, click_pos, tile_id, tile_angle, layer): tile_to_set = None tile_click_x = 0 tile_click_y = 0 for tileX in range(self.zero_tile_x, self.end_tile_x): for tileY in range(self.zero_tile_y, self.end_tile_y): if layer == 0: tile = self.tile_grid[tileX][tileY] tile_screen_min = [(tileX*64) - self.position_offset[0], (tileY * 64) - self.position_offset[1]] tile_screen_max = [(tileX*64) + 64 - self.position_offset[0], (tileY * 64) + 64 - self.position_offset[1]] if tile_screen_min[0] <= click_pos[0] and tile_screen_min[1] <= click_pos[1]: if tile_screen_max[0] > click_pos[0] and tile_screen_max[1] > click_pos[1]: tile_to_set = tile tile_click_x = tileX tile_click_y = tileY break if layer == 1: tile = self.top_tile_grid[tileX][tileY] tile_screen_min = [(tileX*64) - self.position_offset[0], (tileY * 64) - self.position_offset[1]] tile_screen_max = [(tileX*64) + 64 - self.position_offset[0], (tileY * 64) + 64 - self.position_offset[1]] if tile_screen_min[0] <= click_pos[0] and tile_screen_min[1] <= click_pos[1]: if tile_screen_max[0] > click_pos[0] and tile_screen_max[1] > click_pos[1]: tile_to_set = tile tile_click_x = tileX tile_click_y = tileY break if tile_to_set is not None: if layer == 0: if tile_to_set.collidable: self.collidable_tiles.remove(tile_to_set) else: self.walkable_tiles.remove(tile_to_set) self.tiles.remove(tile_to_set) new_tile = Tile(tile_to_set.world_position, tile_angle, self.all_tile_data[tile_id], layer) self.tiles.append(new_tile) if layer == 0: x_grid_pos = int((new_tile.world_position[0] - 32) / 64) y_grid_pos = int((new_tile.world_position[1] - 32) / 64) self.tile_grid[x_grid_pos][y_grid_pos] = new_tile if new_tile.collidable: self.collidable_tiles.append(new_tile) else: self.walkable_tiles.append(new_tile) if layer == 1: x_grid_pos = int((new_tile.world_position[0] - 32) / 64) y_grid_pos = int((new_tile.world_position[1] - 32) / 64) self.top_tile_grid[x_grid_pos][y_grid_pos] = new_tile else: new_tile_world_position = [(tile_click_x*64)+32, (tile_click_y*64)+32] new_tile = Tile(new_tile_world_position, tile_angle, self.all_tile_data[tile_id], layer) self.tiles.append(new_tile) if layer == 0: x_grid_pos = int((new_tile.world_position[0] - 32) / 64) y_grid_pos = int((new_tile.world_position[1] - 32) / 64) self.tile_grid[x_grid_pos][y_grid_pos] = new_tile if new_tile.collidable: self.collidable_tiles.append(new_tile) else: self.walkable_tiles.append(new_tile) if layer == 1: x_grid_pos = int((new_tile.world_position[0] - 32) / 64) y_grid_pos = int((new_tile.world_position[1] - 32) / 64) self.top_tile_grid[x_grid_pos][y_grid_pos] = new_tile self.all_tile_sprites.empty() for tileX in range(self.zero_tile_x, self.end_tile_x): for tileY in range(self.zero_tile_y, self.end_tile_y): tile = self.tile_grid[tileX][tileY] if tile is None: print("No tile at grid: " + str(tileX) + ", " + str(tileY)) else: tile.update_offset_position(self.position_offset, self.screen_data) self.all_tile_sprites.add(tile) top_tile = self.top_tile_grid[tileX][tileY] if top_tile is not None: top_tile.update_offset_position(self.position_offset, self.screen_data) self.all_top_tile_sprites.add(top_tile)
def test_tile_style_click_3(self): tile = Tile(3, 0, 0) self.assertEqual(tile.click(), 3)
def main_game(genomes, config): gameSpeed = 5 gameOver = False nets = [] genomes_track = [] snakes = [] for _, genome in genomes: net = neat.nn.FeedForwardNetwork.create(genome, config) nets.append(net) snake_initial_coord = unique_coords(snakes) snakes.append(Snake(snake_initial_coord, gameSpeed)) genome.fitness = 0 genomes_track.append(genome) #snake = Snake((int(BOARD_ROWS / 2), int(BOARD_COLS / 2)), gameSpeed) tile_coord = unique_coords(snakes) tile = Tile(tile_coord) board = Board(screen) score = Score(screen) #last_obstacle = pygame.sprite.Group() while not gameOver: for event in pygame.event.get(): if event.type == QUIT: gameOver = True quit() if event.type == KEYDOWN: if event.key == K_UP: for snake in snakes: snake.move_up() if event.key == K_DOWN: for snake in snakes: snake.move_down() if event.key == K_RIGHT: for snake in snakes: snake.move_right() if event.key == K_LEFT: for snake in snakes: snake.move_left() for snake in snakes: snake.update() check_collide(snakes, tile, genomes_track, nets) if tile.is_dead(): score.update() #snake.eat_tile() tile_coord = unique_coords(snakes) tile = Tile(tile_coord) if len(snakes) == 0: gameOver = True quit() board.clean_board() for snake in snakes: board.display_snake(snake.get_body()) board.display_tile(tile.get_coord()) update_fitness(snakes, tile, genomes_track, nets) if pygame.display.get_surface() != None: screen.fill(BG_COLOR) board.draw() score.draw() pygame.display.update() clock.tick(FPS)
def test_tile_style_click_9(self): tile = Tile(9, 0, 0) self.assertEqual(tile.click(), 9)
def setUp(self): self.tile = Tile(-1, 0, 0)
def test_tile_style_click_5(self): tile = Tile(5, 0, 0) self.assertEqual(tile.click(), 5)
def test_tile_style_click_6(self): tile = Tile(6, 0, 0) self.assertEqual(tile.click(), 6)
def getAllSurroundingTiles(board, tile): """ Returns a list of the 5x5 grid of surrounding tiles any invalid tile is None """ surrounding = [] row = tile.row col = tile.col surrounding.append(board.left(row, col)) surrounding.append(board.right(row, col)) surrounding.append(board.up(row, col)) surrounding.append(board.down(row, col)) # Process 5 surrounding tiles of upLeft ul = board.upLeft(row, col) surrounding.append(ul) if ul is None: for _ in range(5): surrounding.append(None) else: surrounding.append(board.downLeft(ul.row, ul.col)) surrounding.append(board.left(ul.row, ul.col)) surrounding.append(board.upLeft(ul.row, ul.col)) surrounding.append(board.up(ul.row, ul.col)) surrounding.append(board.upRight(ul.row, ul.col)) # Process 3 surrounding tiles of upRight ur = board.upRight(row, col) surrounding.append(ur) if ur is None: for _ in range(3): surrounding.append(None) else: surrounding.append(board.up(ur.row, ur.col)) surrounding.append(board.upRight(ur.row, ur.col)) surrounding.append(board.right(ur.row, ur.col)) # Process 3 surrounding tiles of downLeft dl = board.downLeft(row, col) surrounding.append(dl) if dl is None: for _ in range(3): surrounding.append(None) else: surrounding.append(board.left(dl.row, dl.col)) surrounding.append(board.downLeft(dl.row, dl.col)) surrounding.append(board.down(dl.row, dl.col)) # Process 5 surrounding tiles of downRight dr = board.downRight(row, col) surrounding.append(dr) if dr is None: for _ in range(5): surrounding.append(None) else: surrounding.append(board.downLeft(dr.row, dr.col)) surrounding.append(board.down(dr.row, dr.col)) surrounding.append(board.downRight(dr.row, dr.col)) surrounding.append(board.right(dr.row, dr.col)) surrounding.append(board.upRight(dr.row, dr.col)) if MODEL == "nn": # Re-order stuff for clarity and so conv layer works good surrounding = [ surrounding[7], surrounding[8], surrounding[9], surrounding[11], surrounding[12], surrounding[6], surrounding[4], surrounding[2], surrounding[10], surrounding[13], surrounding[5], surrounding[0], surrounding[1], surrounding[23], surrounding[15], surrounding[14], surrounding[3], surrounding[18], surrounding[22], surrounding[16], surrounding[17], surrounding[19], surrounding[20], surrounding[21] ] elif MODEL == "2dnn" or MODEL == "2dnnNEW": surrounding = [ [ surrounding[7], surrounding[8], surrounding[9], surrounding[11], surrounding[12], ], [ surrounding[6], surrounding[4], surrounding[2], surrounding[10], surrounding[13], ], [ surrounding[5], surrounding[0], Tile(0, -1, -1), surrounding[1], surrounding[23], ], [ surrounding[15], surrounding[14], surrounding[3], surrounding[18], surrounding[22], ], [ surrounding[16], surrounding[17], surrounding[19], surrounding[20], surrounding[21] ], ] return surrounding
def test_tile_style_click_7(self): tile = Tile(7, 0, 0) self.assertEqual(tile.click(), 7)
def serialize_test(): tile = Tile("foo", 4, {}, {}) assert tile.as_json() == '"foo"'
def test_tile_style_click_4(self): tile = Tile(4, 0, 0) self.assertEqual(tile.click(), 4)
def test_tile_style_click_non_valid_value(self): tile = Tile("test", 0, 0) self.assertEqual(tile.click(), 0)
def test_tile_style_click_8(self): tile = Tile(8, 0, 0) self.assertEqual(tile.click(), 8)