mariang_09

mastermind™

Dec 16th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.29 KB | None | 0 0
  1. import random, time
  2.  
  3. def menu():
  4.     global guessNumber, statDict
  5.  
  6.     #Pretty straight forward stuff, nothing to be explained here
  7.     var = False
  8.     tries = 0
  9.     print("Welcome to Mastermind!\
  10.         \nThe rules are:\
  11.         \n1. If you're playing on Easy mode, you'll have to guess a 4 digit number, and will be given what digits you guessed.\
  12.         \n2. If you're playing on Hard mode, the number you'll have to guess will have 5 digits, you won't know the places of the digits you guessed.\
  13.         \n3. You have 10 tries to guess the number.")
  14.  
  15.     userName = input("What is your name? ")
  16.     print("What difficulty do you want to play on?\ Easy/Hard")
  17.  
  18.     statDict = {
  19.                 "Player name:": userName,
  20.                 "Number of tries:": tries
  21.     }
  22.  
  23.     while var == False:
  24.         userDiff = input("Choice: ")
  25.         #Program takes the user's input and turns it into a lower case string that will be compared to other words
  26.  
  27.         if userDiff.lower() == "easy":
  28.             confirmation = input("You have chosen Easy mode. Are you sure about it? y/n ")
  29.  
  30.             if confirmation.lower() == "y" or confirmation.lower() == "yes":
  31.                 print("Ok.")
  32.                 guessNumber = random.randint(1000, 9999)
  33.                 easyMode()
  34.  
  35.  
  36.             elif confirmation.lower() == "n" or confirmation.lower() == "no":
  37.                 print("You'll be redirected to change your difficulty")
  38.                 pass
  39.  
  40.             else:
  41.                 print("Sorry. I didn't get that...")
  42.                 pass
  43.  
  44.         elif userDiff.lower() == "hard":
  45.             confirmation = input("You have chosen Hard mode. Are you sure about it? y/n")
  46.  
  47.             if confirmation.lower() == "y" or confirmation.lower() == "yes":
  48.                 print("Ok.")
  49.                 guessNumber = random.randint(10000, 99999)
  50.                 hardMode()
  51.                 #Program goes thru a 1 sec delay after which it calls the function hardMode, same thing goes for lines 20-23
  52.  
  53.             elif confirmation.lower() == "n" or confirmation.lower() == "no":
  54.                 print("You'll be redirected to change your difficulty")
  55.                
  56.             else:
  57.                 print("Sorry. I didn't get that...")
  58.  
  59. # this function is used when the user plays on Easy Mode, nothing special so far, nothing to be explained
  60.  
  61. def easyMode():
  62.     global guessNumber, guessedDigits, tries, statDict
  63.  
  64.     guessNumber = str(guessNumber)
  65.     print(guessNumber)
  66.  
  67.     #movingNumbers is the digits of the guessNumber, put into a list, the items in this list will be compared to the user input
  68.     #if the input is the same as any item in movingNumbers, the input will occupy the same index from movingNumbers into guessedDigits
  69.  
  70.     movingNumbers = list(guessNumber)
  71.     guessedDigits = ["_"] * len(movingNumbers)
  72.     whileVariable = False
  73.  
  74.     while whileVariable == False:
  75.         userGuess = input("Your guess is...")
  76.  
  77.     #what this for loop does is that it goes thru the whole list, the if statements check if the user input is the same as
  78.     #one of the items in the list, if the item appears twice, both items will be get the same places in guessedDigits
  79.  
  80.         if userGuess in movingNumbers:
  81.             itmIndx = guessNumber.index(userGuess)
  82.  
  83.             for i, x in enumerate(movingNumbers):
  84.                 if x == userGuess:
  85.                     guessedDigits[i] = userGuess
  86.                 else:
  87.                     guessedDigits[itmIndx] = userGuess
  88.             tries += 1
  89.  
  90.             print("You're correct.")
  91.             print (" ".join(guessedDigits))
  92.  
  93.         elif userGuess not in guessNumber:
  94.             tries += 1
  95.             print("You're not correct.")
  96.  
  97.         if "_" not in guessedDigits:
  98.             print("You Won!",\
  99.                 "\nThere's your stats", statDict["Player name:"], ":",\
  100.                 "\nTries: ", statDict["Number of tries:"])
  101.             exit()
  102.  
  103.         else:
  104.             pass
  105.  
  106. def hardMode():
  107.     global guessNumber, guessedDigits, tries, statDict
  108.  
  109.     guessNumber = str(guessNumber)
  110.     print(guessNumber)
  111.     movingNumbers = list(guessNumber)
  112.  
  113.     guessedDigits = ["_"] * len(movingNumbers)
  114.     whileVariable = False
  115.  
  116.     while whileVariable == False:
  117.         userGuess = input("Your guess is...")
  118.  
  119.         if userGuess in movingNumbers:
  120.             itmIndx = guessNumber.index(userGuess)
  121.  
  122.             for i, x in enumerate(movingNumbers):
  123.                 if x == userGuess:
  124.                     guessedDigits[i] = userGuess
  125.                 else:
  126.                     guessedDigits[itmIndx] = userGuess
  127.             tries += 1
  128.  
  129.             print("You're correct.")
  130.             print (" ".join(guessedDigits))
  131.  
  132.         elif userGuess not in guessNumber:
  133.             tries += 1
  134.             print("You're not correct.")
  135.  
  136.         if "_" not in guessedDigits:
  137.             print("You Won!",\
  138.                 "\nThere's your stats", statDict["Player name:"], ":",\
  139.                 "\nTries: ", statDict["Number of tries:"])
  140.             exit()
  141.            
  142.         else:
  143.             pass
  144.  
  145. menu()
Add Comment
Please, Sign In to add comment