Mini Project: Number Guessing Game
ဒီ Course မှာ သင်ယူခဲ့တဲ့ Variables, Loops, If/Else တွေနဲ့ Modules တွေကို ပေါင်းစပ်ပြီး “ဂဏန်းခန့်မှန်းတဲ့ ဂိမ်း (Number Guessing Game)” ကို ဆောက်ကြည့်ကြရအောင်။
ဂိမ်းရဲ့ စည်းမျဉ်းများ
Section titled “ဂိမ်းရဲ့ စည်းမျဉ်းများ”- ကွန်ပျူတာက 1 ကနေ 100 ကြား ဂဏန်းတစ်ခုကို ကျပန်း ရွေးထားပါမယ်
- ကစားသမားက မှန်းပြီး ရိုက်ထည့်ရပါမယ်
- မှန်းတဲ့ ဂဏန်းက ကြီးနေရင် “ကြီးနေတယ်”၊ ငယ်နေရင် “ငယ်နေတယ်” ဆိုပြီး Hint ပေးပါမယ်
- အဖြေမှန်ရင် ဘယ်နှစ်ကြိမ်မြောက်မှာ မှန်သလဲဆိုတာ ပြပေးပါမယ်
Version 1 — အခြေခံ Game
Section titled “Version 1 — အခြေခံ Game”import random
def play_game(): print("=====================================") print("ဂဏန်းခန့်မှန်း ဂိမ်းမှ ကြိုဆိုပါတယ်!") print("ကွန်ပျူတာက 1 ကနေ 100 ကြား ဂဏန်းတစ်ခုကို ကျပန်း ရွေးထားပါတယ်") print("=====================================\n")
secret_number = random.randint(1, 100) # Line 9: ကျပန်း ဂဏန်းရွေးမယ် attempts = 0
while True: # Line 12: အဖြေမမှန်မချင်း Loop ဆက်ပတ်မယ် try: guess = int(input("ခင်ဗျား ခန့်မှန်းမယ့် ဂဏန်းကို ထည့်ပါ: ")) # Line 14: Input ယူမယ် attempts += 1 # Line 15: ကြိမ်ရေ တစ်ခုတိုးမယ်
if guess < secret_number: # Line 17: ငယ်နေသလား စစ်ဆေးမယ် print("📉 နည်းနည်း ငယ်နေသေးတယ်! ထပ်ကြိုးစားကြည့်ပါဦး\n") elif guess > secret_number: # Line 19: ကြီးနေသလား စစ်ဆေးမယ် print("📈 နည်းနည်း ကြီးနေတယ်! ထပ်ကြိုးစားကြည့်ပါဦး\n") else: # Line 21: မှန်သွားပြီ (တူညီတယ်) print(f"\n🎉 မှန်ပါတယ်! ဂဏန်းက {secret_number} ဖြစ်ပါတယ်") print(f"ခင်ဗျား {attempts} ကြိမ်မြောက်မှာ မှန်ကြောင်း ရှာတွေ့သွားပါပြီ!") break # Line 24: Loop ရပ်မယ်
except ValueError: # Line 26: ဂဏန်းမဟုတ်ရင် Error ပြမယ် print("❌ ကျေးဇူးပြု၍ ဂဏန်းသာ ရိုက်ထည့်ပါ\n")
play_game()Code တစ်ကြောင်းချင်း ရှင်းလင်းချက်
Section titled “Code တစ်ကြောင်းချင်း ရှင်းလင်းချက်”| Line | Code | ရှင်းလင်းချက် |
|---|---|---|
| 1 | import random | Modules ယူသုံးခြင်း (Module 7) |
| 5 | def play_game(): | Function ကြေညာခြင်း (Module 3) |
| 9 | secret_number = random.randint(1, 100) | random Module နဲ့ Variable တွဲသုံးခြင်း |
| 10 | attempts = 0 | ကြိမ်ရေ မှတ်သားမယ့် Counter Variable |
| 12 | while True: | အဖြေမမှန်မချင်း အလုပ်လုပ်မယ့် While Loop (Module 2) |
| 14 | int(input(...)) | Input ယူခြင်းနဲ့ Type ပြောင်းလဲခြင်း (Module 1) |
| 17-21 | if / elif / else | အခြေအနေ စစ်ဆေးတဲ့ Control Flow (Module 2) |
| 24 | break | မှန်သွားတဲ့အခါ Loop ကနေ ထွက်ဖို့ |
| 26 | except ValueError: | မှားယွင်းတဲ့ Input ကို Error Handling လုပ်ခြင်း (Module 6) |
Version 2 — ပိုကောင်းသော Game (Features အသစ်များ ထည့်ပြီး)
Section titled “Version 2 — ပိုကောင်းသော Game (Features အသစ်များ ထည့်ပြီး)”import random
def get_difficulty(): """Difficulty Level ရွေးချယ်ခြင်း""" print("Difficulty Level ကို ရွေးပါ:") print(" [1] လွယ် — 1 ကနေ 50 ထိ, Chances 10 ကြိမ်") print(" [2] ပုံမှန် — 1 ကနေ 100 ထိ, Chances 7 ကြိမ်") print(" [3] ခက် — 1 ကနေ 200 ထိ, Chances 5 ကြိမ်")
while True: choice = input("ရွေးချယ်ပါ (1/2/3): ").strip() if choice == "1": return 50, 10, "လွယ်" elif choice == "2": return 100, 7, "ပုံမှန်" elif choice == "3": return 200, 5, "ခက်" else: print("1, 2, 3 ထဲက တစ်ခုကိုသာ ရွေးပေးပါ")
def evaluate_performance(attempts, max_attempts): """ကစားသူရဲ့ စွမ်းဆောင်ရည်ကို Rating ပေးခြင်း""" ratio = attempts / max_attempts if ratio <= 0.3: return "⭐⭐⭐ Excellent!" elif ratio <= 0.6: return "⭐⭐ Good!" else: return "⭐ You can do better!"
def play_game(): print("=" * 45) print(" ဂဏန်းခန့်မှန်း ဂိမ်းမှ ကြိုဆိုပါတယ်!") print("=" * 45 + "\n")
max_number, max_attempts, level = get_difficulty() secret_number = random.randint(1, max_number) attempts = 0 history = [] # ခန့်မှန်းခဲ့တဲ့ ဂဏန်းတွေကို သိမ်းထားမယ်
print(f"\n{level} Level — 1 မှ {max_number} ကြား ဂဏန်း ({max_attempts} ကြိမ် Chance ရပါမယ်)")
while attempts < max_attempts: remaining = max_attempts - attempts print(f"\nကျန်ရှိတဲ့ Chance: {remaining}")
try: guess = int(input("ဂဏန်း ခန့်မှန်းပါ: "))
if not 1 <= guess <= max_number: print(f"❌ 1 မှ {max_number} ကြား ဂဏန်းသာ ထည့်ပေးပါ") continue
attempts += 1 history.append(guess)
if guess < secret_number: diff = secret_number - guess hint = "အနည်းငယ် ငယ်" if diff <= 10 else "အများကြီး ငယ်" print(f"📉 {hint}နေသေးတယ်!") elif guess > secret_number: diff = guess - secret_number hint = "အနည်းငယ် ကြီး" if diff <= 10 else "အများကြီး ကြီး" print(f"📈 {hint}နေသေးတယ်!") else: rating = evaluate_performance(attempts, max_attempts) print(f"\n{'=' * 45}") print(f"🎉 မှန်ပါတယ်! ဂဏန်းက {secret_number} ဖြစ်ပါတယ်") print(f"ကြိမ်အရေအတွက်: {attempts} / {max_attempts}") print(f"Rating: {rating}") print(f"Guess History: {history}") print(f"{'=' * 45}") return True
except ValueError: print("❌ ကျေးဇူးပြု၍ ဂဏန်းသာ ရိုက်ထည့်ပါ")
print(f"\n😢 Chances ကုန်သွားပါပြီ! အဖြေမှန်က {secret_number} ဖြစ်ပါတယ်") return False
def main(): while True: won = play_game()
print("\nနောက်ထပ် ထပ်ကစားဦးမလား?") again = input("(yes/no): ").strip().lower() if again not in ("yes", "y", "ဟုတ်"): print("\n👋 ကစားပေးတဲ့အတွက် ကျေးဇူးတင်ပါတယ်!") break
main()Challenge Extensions — ကိုယ်တိုင် ထပ်မံဖြည့်စွက်ကြည့်ပါ
Section titled “Challenge Extensions — ကိုယ်တိုင် ထပ်မံဖြည့်စွက်ကြည့်ပါ”ဒီ Game ကို ပိုကောင်းအောင် ဆက်လက် ကြိုးစားကြည့်ပါ:
- High Score — အနည်းဆုံး ကြိမ်နဲ့ အဖြေမှန်တဲ့ Record ကို File ထဲမှာ သိမ်းပါ
- Timer —
timeModule သုံးပြီး အချိန် ဘယ်လောက်ကြာသလဲ တွက်ပြပါ - Hint System — “ဒုတိယ Hint ဝယ်မလား?” ဆိုတဲ့ Option မျိုး ထည့်ပါ
- Multiplayer — ကစားသမား ၂ ယောက် အလှည့်ကျ ကစားတဲ့ Mode ထည့်ကြည့်ပါ
Module 8 ပြီးဆုံးပါပြီ — မဆုတ်မနစ် ဆက်လက် လေ့လာကြပါစို့! 💪