fork download
  1. import tkinter as tk
  2. import random
  3. from datetime import datetime, timedelta
  4.  
  5. # กำหนดราคาห้องตามสี
  6. ROOM_PRICES = {
  7. "Green": 50,
  8. "Yellow": 100,
  9. "Blue": 200,
  10. "Red": 500
  11. }
  12.  
  13. # กำหนดห้องทั้งหมดในโรงแรม (9 ชั้น x 4 ห้อง)
  14. FLOORS = 9
  15. ROOMS_PER_FLOOR = 4
  16.  
  17. class HotelManagement:
  18. def __init__(self, root):
  19. self.root = root
  20. self.root.title("Hotel Management Game")
  21.  
  22. self.money = 1000 # เงินเริ่มต้นของผู้เล่น
  23. self.current_day = 1 # ระบบเวลาในเกม (เริ่มจากวันที่ 1)
  24. self.rooms = {} # เก็บข้อมูลห้องพัก
  25.  
  26. # สร้างห้องพัก
  27. colors = ["Green", "Yellow", "Blue", "Red"]
  28. for floor in range(1, FLOORS + 1):
  29. for room in range(1, ROOMS_PER_FLOOR + 1):
  30. room_number = f"{floor}-{room}"
  31. self.rooms[room_number] = {
  32. "color": random.choice(colors),
  33. "occupied": False,
  34. "customer": None,
  35. "checkout_day": None
  36. }
  37.  
  38. # UI
  39. self.money_label = tk.Label(root, text=f"Money: ${self.money}", font=("Arial", 14))
  40. self.money_label.pack()
  41.  
  42. self.day_label = tk.Label(root, text=f"Day: {self.current_day}", font=("Arial", 14))
  43. self.day_label.pack()
  44.  
  45. self.room_listbox = tk.Listbox(root, width=50, height=15)
  46. self.room_listbox.pack()
  47. self.update_room_list()
  48.  
  49. self.new_customer_button = tk.Button(root, text="New Customer", command=self.new_customer)
  50. self.new_customer_button.pack()
  51.  
  52. self.checkin_button = tk.Button(root, text="Check-in", command=self.check_in)
  53. self.checkin_button.pack()
  54.  
  55. self.checkout_button = tk.Button(root, text="Check-out", command=self.check_out)
  56. self.checkout_button.pack()
  57.  
  58. self.next_day_button = tk.Button(root, text="Next Day", command=self.next_day)
  59. self.next_day_button.pack()
  60.  
  61. self.customer = None # ลูกค้าที่มาขอจองห้อง
  62.  
  63. def update_room_list(self):
  64. """อัปเดตรายการห้องใน Listbox"""
  65. self.room_listbox.delete(0, tk.END)
  66. for room, info in self.rooms.items():
  67. status = "Available" if not info["occupied"] else f"Occupied by {info['customer']['name']}"
  68. self.room_listbox.insert(tk.END, f"Room {room} - {info['color']} - {status}")
  69.  
  70. def new_customer(self):
  71. """สร้างลูกค้าใหม่ที่ต้องการเข้าพัก"""
  72. names = ["Alice", "Bob", "Charlie", "David", "Emma", "Frank", "Grace", "Hannah"]
  73. name = random.choice(names)
  74. budget = random.randint(50, 600) # งบประมาณลูกค้า
  75. nights = random.randint(1, 5) # จำนวนคืนที่ต้องการพัก
  76. self.customer = {"name": name, "budget": budget, "nights": nights}
  77. tk.messagebox.showinfo("New Customer", f"{name} wants to stay for {nights} nights with a budget of ${budget}")
  78.  
  79. def check_in(self):
  80. """เช็กอินลูกค้าเข้าห้อง"""
  81. if not self.customer:
  82. tk.messagebox.showwarning("Warning", "No customer waiting!")
  83. return
  84.  
  85. selected_index = self.room_listbox.curselection()
  86. if not selected_index:
  87. tk.messagebox.showwarning("Warning", "Select a room!")
  88. return
  89.  
  90. room_number = self.room_listbox.get(selected_index[0]).split()[1]
  91. room_info = self.rooms[room_number]
  92.  
  93. if room_info["occupied"]:
  94. tk.messagebox.showwarning("Warning", "Room is already occupied!")
  95. return
  96.  
  97. room_price = ROOM_PRICES[room_info["color"]]
  98.  
  99. if self.customer["budget"] < room_price:
  100. tk.messagebox.showwarning("Warning", "Customer cannot afford this room!")
  101. return
  102.  
  103. # ดำเนินการเช็กอิน
  104. total_price = room_price * self.customer["nights"]
  105. self.money += total_price
  106. self.rooms[room_number]["occupied"] = True
  107. self.rooms[room_number]["customer"] = self.customer
  108. self.rooms[room_number]["checkout_day"] = self.current_day + self.customer["nights"]
  109. self.customer = None # ล้างลูกค้าปัจจุบัน
  110.  
  111. self.money_label.config(text=f"Money: ${self.money}")
  112. self.update_room_list()
  113.  
  114. def check_out(self):
  115. """เช็กเอาต์ลูกค้าจากห้อง"""
  116. selected_index = self.room_listbox.curselection()
  117. if not selected_index:
  118. tk.messagebox.showwarning("Warning", "Select a room!")
  119. return
  120.  
  121. room_number = self.room_listbox.get(selected_index[0]).split()[1]
  122. room_info = self.rooms[room_number]
  123.  
  124. if not room_info["occupied"]:
  125. tk.messagebox.showwarning("Warning", "Room is not occupied!")
  126. return
  127.  
  128. self.rooms[room_number]["occupied"] = False
  129. self.rooms[room_number]["customer"] = None
  130. self.rooms[room_number]["checkout_day"] = None
  131. self.update_room_list()
  132.  
  133. def next_day(self):
  134. """ข้ามไปวันถัดไป"""
  135. self.current_day += 1
  136. self.day_label.config(text=f"Day: {self.current_day}")
  137.  
  138. # เช็กเอาต์ลูกค้าที่ถึงวันออก
  139. for room, info in self.rooms.items():
  140. if info["occupied"] and info["checkout_day"] == self.current_day:
  141. self.rooms[room]["occupied"] = False
  142. self.rooms[room]["customer"] = None
  143. self.rooms[room]["checkout_day"] = None
  144.  
  145. self.update_room_list()
  146.  
  147.  
  148. if __name__ == "__main__":
  149. root = tk.Tk()
  150. game = HotelManagement(root)
  151. root.mainloop()
Success #stdin #stdout 0.02s 26028KB
stdin
Standard input is empty
stdout
import tkinter as tk
import random
from datetime import datetime, timedelta

# กำหนดราคาห้องตามสี
ROOM_PRICES = {
    "Green": 50,
    "Yellow": 100,
    "Blue": 200,
    "Red": 500
}

# กำหนดห้องทั้งหมดในโรงแรม (9 ชั้น x 4 ห้อง)
FLOORS = 9
ROOMS_PER_FLOOR = 4

class HotelManagement:
    def __init__(self, root):
        self.root = root
        self.root.title("Hotel Management Game")

        self.money = 1000  # เงินเริ่มต้นของผู้เล่น
        self.current_day = 1  # ระบบเวลาในเกม (เริ่มจากวันที่ 1)
        self.rooms = {}  # เก็บข้อมูลห้องพัก

        # สร้างห้องพัก
        colors = ["Green", "Yellow", "Blue", "Red"]
        for floor in range(1, FLOORS + 1):
            for room in range(1, ROOMS_PER_FLOOR + 1):
                room_number = f"{floor}-{room}"
                self.rooms[room_number] = {
                    "color": random.choice(colors),
                    "occupied": False,
                    "customer": None,
                    "checkout_day": None
                }

        # UI
        self.money_label = tk.Label(root, text=f"Money: ${self.money}", font=("Arial", 14))
        self.money_label.pack()

        self.day_label = tk.Label(root, text=f"Day: {self.current_day}", font=("Arial", 14))
        self.day_label.pack()

        self.room_listbox = tk.Listbox(root, width=50, height=15)
        self.room_listbox.pack()
        self.update_room_list()

        self.new_customer_button = tk.Button(root, text="New Customer", command=self.new_customer)
        self.new_customer_button.pack()

        self.checkin_button = tk.Button(root, text="Check-in", command=self.check_in)
        self.checkin_button.pack()

        self.checkout_button = tk.Button(root, text="Check-out", command=self.check_out)
        self.checkout_button.pack()

        self.next_day_button = tk.Button(root, text="Next Day", command=self.next_day)
        self.next_day_button.pack()

        self.customer = None  # ลูกค้าที่มาขอจองห้อง

    def update_room_list(self):
        """อัปเดตรายการห้องใน Listbox"""
        self.room_listbox.delete(0, tk.END)
        for room, info in self.rooms.items():
            status = "Available" if not info["occupied"] else f"Occupied by {info['customer']['name']}"
            self.room_listbox.insert(tk.END, f"Room {room} - {info['color']} - {status}")

    def new_customer(self):
        """สร้างลูกค้าใหม่ที่ต้องการเข้าพัก"""
        names = ["Alice", "Bob", "Charlie", "David", "Emma", "Frank", "Grace", "Hannah"]
        name = random.choice(names)
        budget = random.randint(50, 600)  # งบประมาณลูกค้า
        nights = random.randint(1, 5)  # จำนวนคืนที่ต้องการพัก
        self.customer = {"name": name, "budget": budget, "nights": nights}
        tk.messagebox.showinfo("New Customer", f"{name} wants to stay for {nights} nights with a budget of ${budget}")

    def check_in(self):
        """เช็กอินลูกค้าเข้าห้อง"""
        if not self.customer:
            tk.messagebox.showwarning("Warning", "No customer waiting!")
            return

        selected_index = self.room_listbox.curselection()
        if not selected_index:
            tk.messagebox.showwarning("Warning", "Select a room!")
            return

        room_number = self.room_listbox.get(selected_index[0]).split()[1]
        room_info = self.rooms[room_number]

        if room_info["occupied"]:
            tk.messagebox.showwarning("Warning", "Room is already occupied!")
            return

        room_price = ROOM_PRICES[room_info["color"]]

        if self.customer["budget"] < room_price:
            tk.messagebox.showwarning("Warning", "Customer cannot afford this room!")
            return

        # ดำเนินการเช็กอิน
        total_price = room_price * self.customer["nights"]
        self.money += total_price
        self.rooms[room_number]["occupied"] = True
        self.rooms[room_number]["customer"] = self.customer
        self.rooms[room_number]["checkout_day"] = self.current_day + self.customer["nights"]
        self.customer = None  # ล้างลูกค้าปัจจุบัน

        self.money_label.config(text=f"Money: ${self.money}")
        self.update_room_list()

    def check_out(self):
        """เช็กเอาต์ลูกค้าจากห้อง"""
        selected_index = self.room_listbox.curselection()
        if not selected_index:
            tk.messagebox.showwarning("Warning", "Select a room!")
            return

        room_number = self.room_listbox.get(selected_index[0]).split()[1]
        room_info = self.rooms[room_number]

        if not room_info["occupied"]:
            tk.messagebox.showwarning("Warning", "Room is not occupied!")
            return

        self.rooms[room_number]["occupied"] = False
        self.rooms[room_number]["customer"] = None
        self.rooms[room_number]["checkout_day"] = None
        self.update_room_list()

    def next_day(self):
        """ข้ามไปวันถัดไป"""
        self.current_day += 1
        self.day_label.config(text=f"Day: {self.current_day}")

        # เช็กเอาต์ลูกค้าที่ถึงวันออก
        for room, info in self.rooms.items():
            if info["occupied"] and info["checkout_day"] == self.current_day:
                self.rooms[room]["occupied"] = False
                self.rooms[room]["customer"] = None
                self.rooms[room]["checkout_day"] = None

        self.update_room_list()


if __name__ == "__main__":
    root = tk.Tk()
    game = HotelManagement(root)
    root.mainloop()