# 引入 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
# 從標準程式庫中引入 Tk 的所有內容
from tkinter import *
# 執行部分
if __name__ == '__main__':
# 建立動物串列
animals = {"象":Elephant(Point(0,0), "象"), "獅":Lion(Point(0,3), "獅"), "鼠":Mouse(Point(3,0), "鼠"), "貓":Cat(Point(3,3), "貓")}
# 設定地圖
world = [["口", "口", "口", "口"],
["口", "口", "口", "口"],
["口", "口", "口", "口"],
["口", "口", "口", "口"]]
# 設定移動指令
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
# 建立最後結果字串
result = ""
winner = animals.popitem()[1]
x = winner.point.x
y = winner.point.y
i = 0
while i < len(world):
j = 0
while j < len(world[i]):
if i == y and j == x:
result += animal.name
else:
result += "口"
j += 1
i += 1
result += "\n"
# 印出結果
print(result)
# 建立 Tk 視窗
root = Tk()
# 設定視窗標題
root.title("鬥獸棋遊戲模擬")
# 加入視窗元件文字標籤
text = Label(root, text=result,
width="30", height="5",
bg="black", fg="white")
# 使用包裹版面管理員
text.pack()
# 呼叫維持視窗運作的 mainloop()
root.mainloop()
# 檔名: exercise2905.py
# 說明:《Python入門指南》的練習
# 網站: http://kaiching.org
# 作者: 張凱慶
# 時間: 2023 年 11 月