# 引入 Point 定義
from exercise1810 import Point
# 引入 Animal 定義
from exercise1811 import Animal
# 引入 Lion 定義
from exercise1812 import Lion
# 引入 copy
import copy
# 檢查參數是否為 Animal 串列
def examine_animal_list(p: list) -> bool:
try:
assert type(p) is list
for a in p:
if not issubclass(type(a), Animal):
return False
return True
except AssertionError:
return False
# 在地圖上印出動物的函數
def print_world(animals: list, world: list):
# 檢查參數是否正確的型態
if examine_animal_list(animals):
# 深度拷貝原始地圖
real_world = copy.deepcopy(world)
# 在新地圖上加入動物
for animal in animals:
# 取得動物座標
x = animal.point.x
y = animal.point.y
# 在地圖上置換成動物名稱
i = 0
while i < len(real_world):
j = 0
while j < len(real_world[i]):
if i == y and j == x:
real_world[i][j] = animal.name
j += 1
i += 1
# 印出新地圖
for row in real_world:
for column in row:
print(column, end=" ")
print()
else:
raise ParameterError
# 執行部分
if __name__ == '__main__':
# 建立動物串列
animals = [Lion(Point(1,1), "Red"), Lion(Point(2,2), "Blue")]
# 設定地圖
world = [["口", "口", "口", "口"],
["口", "口", "口", "口"],
["口", "口", "口", "口"],
["口", "口", "口", "口"]]
try:
# 印出地圖
print_world(animals, world)
except:
print("遊戲建立過程中遭遇問題")
# 檔名: exercise2808.py
# 說明:《Python入門指南》的練習
# 網站: http://kaiching.org
# 作者: 張凱慶
# 時間: 2023 年 11 月