# 引入 Point 定義
from exercise1810 import Point
# 引入 Lion 定義
from exercise1812 import Lion
# 引入 Elephant 定義
from exercise2901 import Elephant
# 引入 Cat 定義
from exercise2902 import Cat
# 引入 Mouse 定義
from exercise2903 import Mouse
# 引入 print_world() 定義
from exercise2808 import print_world
# 引入 clear_monitor() 定義
from exercise2604 import clear_monitor
# 引入 move() 定義
from exercise2807 import move
# 引入 bound_check() 定義
from exercise2605 import bound_check
# 引入 animal_check() 定義
from exercise2809 import animal_check
# 引入 time
import time
# 引入 os
import os
# 從 random 引入 randint
from random import randint
# 從 random 引入 choice
from random import choice
# 執行部分
if __name__ == '__main__':
# 建立動物串列
animals = {"象":Elephant(Point(0,0), "象"), "獅":Lion(Point(0,3), "獅"), "鼠":Mouse(Point(3,0), "鼠"), "貓":Cat(Point(3,3), "貓")}
# 設定地圖
world = [["口", "口", "口", "口"],
["口", "口", "口", "口"],
["口", "口", "口", "口"],
["口", "口", "口", "口"]]
# 清空螢幕
clear_monitor()
# 顯示初始位置
print_world(list(animals.values()), world)
# 暫停 0.4 秒
time.sleep(0.4)
# 設定移動指令
direction = ["up", "down", "right", "left"]
# 移動的迴圈
while len(animals) > 1:
animal = choice(list(animals.values()))
clear_monitor()
d = direction[randint(0, 3)]
if bound_check(animal, d, world):
other = animal_check(animal, list(animals.values()), d, world)
if other == "口":
move(animal, d)
else:
if other in animal.food:
animals.pop(other)
move(animal, d)
else:
animals.pop(animal.name)
else:
continue
print_world(list(animals.values()), world)
time.sleep(0.4)
print(f"贏家是{animals.popitem()[1].name}")
# 檔名: exercise2904.py
# 說明:《Python入門指南》的練習
# 網站: http://kaiching.org
# 作者: 張凱慶
# 時間: 2023 年 11 月