Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> Python

रॉक पेपर कैंची गेम को लागू करने के लिए पायथन कार्यक्रम

पायथन का उपयोग करके हम बहुत ही रोचक गेम भी विकसित कर सकते हैं। रॉक पेपर कैंची गेम उनमें से एक है। यहाँ हम रैंडिंट () फ़ंक्शन का उपयोग यादृच्छिक संख्याएँ उत्पन्न करने के लिए करते हैं।

इस खेल में खिलाड़ी आमतौर पर तीन की अनुमति देते हैं, या खेल का नाम बोलते हैं, हर बार या तो मुट्ठी में एक हाथ उठाते हैं और इसे गिनती पर नीचे घुमाते हैं या इसे पीछे रखते हैं।

उदाहरण कोड

# importing required random module
import random
print("The Rules of Rock paper scissor game will be follows: \n"
+"Rock vs paper --> paper wins \n"
+"Rock vs scissor --> Rock wins \n"
+"paper vs scissor --> scissor wins \n")
while True:
print("Now please enter your choice no. \n 1. Rock \n 2. paper \n 3. scissor \n")
# take the input from user
ch = int(input("Now Your turn: "))
while ch> 3 or ch< 1:
   ch = int(input("Enter your valid input here: "))
if ch == 1:
   choice_name = 'Rock'
elifch == 2:
   choice_name = 'paper'
else:
   choice_name = 'scissor'
# print user given choice
   print("Your choice is: " + choice_name)
print("\nNow its computer turn to initiate.......")
# Computer will select randomly any number
# among values 1, 2 and 3. Using randint method
# of random module
comp_choice = random.randint(1, 3)
# loopingwill continue until comp_choice value
# is equal to the choice value
while comp_choice == ch:
comp_choice = random.randint(1, 3)
# initialize value of the variable comp_choice_name
# variable corresponding to the choice value
if comp_choice == 1:
   comp_choice_name = 'Rock'
elifcomp_choice == 2:
   comp_choice_name = 'paper'
else:
   comp_choice_name = 'scissor'
   print("So computer choice is: " + comp_choice_name)
print(choice_name + " V/s " + comp_choice_name)
   # condition for winning the game
if((ch == 1 and comp_choice == 2) or
   (ch == 2 and comp_choice ==1 )):
print("paper wins => ", end = "")
   final_result = "paper"
elif((ch == 1 and comp_choice == 3) or
   (ch == 3 and comp_choice == 1)):
print("Rock wins =>", end = "")
   final_result = "Rock"
else:
   print("scissor wins =>", end = "")
   final_result = "scissor"
   # Printing either user or computer wins
if final_result == choice_name:
   print("<== You are the winner ==>")
else:
   print("<== Computer wins ==>")
      print("Do you want to play again? (Y/N)")
      ans = input()
      # if user input n or N then condition is True
if ans == 'n' or ans == 'N':
   break
   # after exiting from the while loop
print("\nThanks for sharing time with us...")

आउटपुट

The Rules of Rock paper scissor game will be follows:
Rock vs paper --> paper wins
Rock vs scissor --> Rock wins
paper vs scissor --> scissor wins

Now please enter your choice no.
1. Rock
2. paper
3. scissor

Now Your turn: 1
Your choice is: Rock

Now its computer turn to initiate.......
So computer choice is: paper
Rock V/s paper
paper wins =><== Computer wins ==>
Do you want to play again? (Y/N)
y
Now please enter your choice no.
1. Rock
2. paper
3. scissor

Now Your turn: 2
Your choice is: paper

Now its computer turn to initiate.......
So computer choice is: Rock
paper V/s Rock
paper wins =><== You are the winner ==>
Do you want to play again? (Y/N)
n
Thanks for sharing time with us...

  1. पायथन में न्यूनतम-अधिकतम गेम ट्री भरने का कार्यक्रम

    मान लीजिए कि हमारे पास एक द्विआधारी पेड़ है जो दो खिलाड़ियों के खेल की स्थिति का प्रतिनिधित्व करता है। प्रत्येक आंतरिक नोड 0 से भरा होता है और पत्तियां मान अंतिम स्कोर का प्रतिनिधित्व करती हैं। खिलाड़ी 1 अंतिम स्कोर को अधिकतम करना चाहता है जबकि खिलाड़ी 2 अंतिम स्कोर को कम करना चाहता है। खिलाड़ी 1 हम

  1. कॉल करने योग्य () पायथन प्रोग्राम में

    इस ट्यूटोरियल में, हम बिल्ट-इन मेथड callable() पर चर्चा करने जा रहे हैं। . यह एक तर्क लेता है और लौटाता है कि क्या तर्क कॉल करने योग्य . है या नहीं। यदि आप कोई फंक्शन या क्लास लेते हैं, तो वे कॉल करने योग्य होते हैं। पूर्णांक, फ्लोट्स, स्ट्रिंग्स इत्यादि जैसे स्थिरांक कॉल करने योग्य नहीं हैं। उदाहरण

  1. अजगर में जल्लाद खेल?

    जल्लाद एक क्लासिक शब्द का खेल है जिसमें प्रतिभागियों को समय समाप्त होने से पहले जितना हो सके उतने गुप्त शब्दों का अनुमान लगाने की आवश्यकता होती है! इसलिए, एक बार में एक अक्षर, नए शब्द सीखने के लिए यह एक अच्छा खेल है! इसलिए हम इस क्लासिक गेम जल्लाद के लिए पायथन लिपि लिखने जा रहे हैं। #importing the