কম্পিউটার টিউটোরিয়াল

পাইথনে রক-পেপার-সিসর গেম ইমপ্লিমেন্টেশন


গেমের নিয়ম

  • রক (পাথর) ভাঙে সিসর (কাঁচি)
  • সিসর (কাঁচি) কাটে পেপার (কাগজ)
  • পেপার (কাগজ) মুছে ফেলে রক (পাথর)

সম্পূর্ণ কোড


import random

def get_computer_choice():
    return random.choice(['rock', 'paper', 'scissors'])

def get_user_choice():
    while True:
        choice = input("Enter your choice (rock/paper/scissors): ").lower().strip()
        if choice in ['rock', 'paper', 'scissors']:
            return choice
        print("Invalid choice! Please enter rock, paper, or scissors.")

def determine_winner(user, computer):
    if user == computer:
        return "tie"
    elif (user == 'rock' and computer == 'scissors') or \
         (user == 'scissors' and computer == 'paper') or \
         (user == 'paper' and computer == 'rock'):
        return "user"
    else:
        return "computer"

def play_game():
    print("=== Rock Paper Scissors ===")
    print("Rules: Rock beats Scissors, Scissors beats Paper, Paper beats Rock\n")
    
    user_score = 0
    computer_score = 0
    
    while True:
        user_choice = get_user_choice()
        computer_choice = get_computer_choice()
        
        print(f"\nYou chose: {user_choice}")
        print(f"Computer chose: {computer_choice}")
        
        result = determine_winner(user_choice, computer_choice)
        
        if result == "tie":
            print("It's a tie!")
        elif result == "user":
            print("You win this round!")
            user_score += 1
        else:
            print("Computer wins this round!")
            computer_score += 1
        
        print(f"Score - You: {user_score} | Computer: {computer_score}")
        
        play_again = input("\nPlay again? (y/n): ").lower().strip()
        if play_again not in ['y', 'yes']:
            break
        print()
    
    print(f"\nFinal Score - You: {user_score} | Computer: {computer_score}")
    print("Thanks for playing!")

if __name__ == "__main__":
    play_game()

নমুনা আউটপুট

=== Rock Paper Scissors ===
Rules: Rock beats Scissors, Scissors beats Paper, Paper beats Rock

Enter your choice (rock/paper/scissors): rock

You chose: rock
Computer chose: paper
Computer wins this round!
Score - You: 0 | Computer: 1

Play again? (y/n): y

Enter your choice (rock/paper/scissors): paper

You chose: paper
Computer chose: rock
You win this round!
Score - You: 1 | Computer: 1

Play again? (y/n): n

Final Score - You: 1 | Computer: 1
Thanks for playing!

কোড ব্যাখ্যা

  • random.choice() — কম্পিউটারের যাদুকরি বাছাই।
  • ইনপুট ভ্যালিডেশন লুপ — সঠিক ইনপুট না পাওয়া পর্যন্ত চায়।
  • determine_winner() — জয়-পরাজয়ের লজিক আলাদা ফাংশনে।
  • স্কোর ট্র্যাকিং ও রিপ্লে লুপ।

উন্নতির 아이ডিয়া

  • GUI (Tkinter/PyGame) ভার্সন বানানো।
  • বেস্ট-অফ-৩/৫ মোড।
  • কম্পিউটার AI (প্যাটার্ন রিকগনিশন) যোগ করা।
  • হাই স্কোর ফাইলে সংরক্ষণ।
  • রঙিন কনসোল আউটপুট (colorama)।
  1. পাইথনে মিন-ম্যাক্স গেম ট্রি পূরণ করার প্রোগ্রাম

  2. পাইথনে Implement strStr() (সাবস্ট্রিং সার্চ)

  3. পাইথন প্রোগ্রামে callable() ফাংশন

  4. পাইথনে হ্যাংম্যান গেম: ক্লাসিক ওয়ার্ড গেম তৈরি করা