|
|
发表于 2026-2-6 08:31:37
|
显示全部楼层
这是6*6的:
- # 定义草地状态常量
- GRASSLAND = 'grassland'
- SOIL = 'soil'
- class Map:
- def __init__(self, width=6, height=6):
- self.width = width
- self.height = height
- # 初始化地图,所有单元格为未开垦状态(grassland)
- self.grid = [[GRASSLAND for _ in range(width)] for _ in range(height)]
-
- def get_cell_state(self, x, y):
- # 获取指定坐标的单元格状态(x为列索引,y为行索引)
- return self.grid[y][x]
-
- def set_cell_state(self, x, y, state):
- # 设置指定坐标的单元格状态
- self.grid[y][x] = state
-
- def toggle_cell_state(self, x, y):
- # 切换指定坐标的单元格状态:grassland <-> soil
- current_state = self.get_cell_state(x, y)
- if current_state == GRASSLAND:
- self.set_cell_state(x, y, SOIL)
- else:
- self.set_cell_state(x, y, GRASSLAND)
-
- def display(self):
- # 打印当前地图状态,显示每个单元格的状态
- for row in self.grid:
- print(' '.join(row))
- print() # 空行用于输出分隔
- class Robot:
- def __init__(self, map, start_x=0, start_y=0):
- self.map = map # 机器人关联的地图对象
- self.x = start_x # 当前列索引(东西方向)
- self.y = start_y # 当前行索引(南北方向)
-
- def move(self, direction):
- # 根据方向移动机器人,处理边界循环
- # 方向参数为字符串,如 'east', 'north', 等
- if direction == 'east':
- dx, dy = 1, 0 # 向东移动:列索引增加
- elif direction == 'west':
- dx, dy = -1, 0 # 向西移动:列索引减少
- elif direction == 'north':
- dx, dy = 0, -1 # 向北移动:行索引减少(假设地图左上角为(0,0))
- elif direction == 'south':
- dx, dy = 0, 1 # 向南移动:行索引增加
- else:
- print("错误:无效方向!请使用 'east', 'west', 'north', 或 'south'。")
- return
-
- # 计算新位置
- new_x = self.x + dx
- new_y = self.y + dy
-
- # 处理边界循环:如果超出边界,则从对面重新进入
- if new_x >= self.map.width:
- new_x = 0
- elif new_x < 0:
- new_x = self.map.width - 1
-
- if new_y >= self.map.height:
- new_y = 0
- elif new_y < 0:
- new_y = self.map.height - 1
-
- # 更新机器人位置
- self.x = new_x
- self.y = new_y
- print(f"机器人移动方向: {direction}, 新位置: ({self.x}, {self.y})")
-
- def till(self):
- # 在当前单元格执行耕种操作,切换状态
- self.map.toggle_cell_state(self.x, self.y)
- current_state = self.map.get_cell_state(self.x, self.y)
- print(f"在位置 ({self.x}, {self.y}) 耕种,当前状态: {current_state}")
- # 示例使用:初始化6*6地图和机器人
- if __name__ == "__main__":
- # 创建6x6地图
- game_map = Map(6, 6)
- robot = Robot(game_map) # 机器人默认从(0,0)开始
-
- # 显示初始地图
- print("初始地图状态:")
- game_map.display()
-
- # 示例操作:移动和耕种
- robot.move('east') # 向东移动
- robot.till() # 耕种当前单元格
- robot.move('north') # 向北移动
- robot.till() # 耕种当前单元格
-
- # 显示更新后的地图
- print("操作后地图状态:")
- game_map.display()
复制代码
这是8*8的:
- # 定义草地状态常量
- GRASSLAND = 'grassland'
- SOIL = 'soil'
- class Map:
- def __init__(self, width=8, height=8):
- self.width = width
- self.height = height
- # 初始化地图,所有单元格为未开垦状态(grassland)
- self.grid = [[GRASSLAND for _ in range(width)] for _ in range(height)]
-
- def get_cell_state(self, x, y):
- # 获取指定坐标的单元格状态(x为列索引,y为行索引)
- return self.grid[y][x]
-
- def set_cell_state(self, x, y, state):
- # 设置指定坐标的单元格状态
- self.grid[y][x] = state
-
- def toggle_cell_state(self, x, y):
- # 切换指定坐标的单元格状态:grassland <-> soil
- current_state = self.get_cell_state(x, y)
- if current_state == GRASSLAND:
- self.set_cell_state(x, y, SOIL)
- else:
- self.set_cell_state(x, y, GRASSLAND)
-
- def display(self):
- # 打印当前地图状态,显示每个单元格的状态
- for row in self.grid:
- print(' '.join(row))
- print() # 空行用于输出分隔
- class Robot:
- def __init__(self, map, start_x=0, start_y=0):
- self.map = map # 机器人关联的地图对象
- self.x = start_x # 当前列索引(东西方向)
- self.y = start_y # 当前行索引(南北方向)
-
- def move(self, direction):
- # 根据方向移动机器人,处理边界循环
- # 方向参数为字符串,如 'east', 'north', 等
- if direction == 'east':
- dx, dy = 1, 0 # 向东移动:列索引增加
- elif direction == 'west':
- dx, dy = -1, 0 # 向西移动:列索引减少
- elif direction == 'north':
- dx, dy = 0, -1 # 向北移动:行索引减少(假设地图左上角为(0,0))
- elif direction == 'south':
- dx, dy = 0, 1 # 向南移动:行索引增加
- else:
- print("错误:无效方向!请使用 'east', 'west', 'north', 或 'south'。")
- return
-
- # 计算新位置
- new_x = self.x + dx
- new_y = self.y + dy
-
- # 处理边界循环:如果超出边界,则从对面重新进入
- if new_x >= self.map.width:
- new_x = 0
- elif new_x < 0:
- new_x = self.map.width - 1
-
- if new_y >= self.map.height:
- new_y = 0
- elif new_y < 0:
- new_y = self.map.height - 1
-
- # 更新机器人位置
- self.x = new_x
- self.y = new_y
- print(f"机器人移动方向: {direction}, 新位置: ({self.x}, {self.y})")
-
- def till(self):
- # 在当前单元格执行耕种操作,切换状态
- self.map.toggle_cell_state(self.x, self.y)
- current_state = self.map.get_cell_state(self.x, self.y)
- print(f"在位置 ({self.x}, {self.y}) 耕种,当前状态: {current_state}")
- # 示例使用:初始化6*6地图和机器人
- if __name__ == "__main__":
- # 创建6x6地图
- game_map = Map(6, 6)
- robot = Robot(game_map) # 机器人默认从(0,0)开始
-
- # 显示初始地图
- print("初始地图状态:")
- game_map.display()
-
- # 示例操作:移动和耕种
- robot.move('east') # 向东移动
- robot.till() # 耕种当前单元格
- robot.move('north') # 向北移动
- robot.till() # 耕种当前单元格
-
- # 显示更新后的地图
- print("操作后地图状态:")
- game_map.display()
复制代码 |
|