Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. print('Welcome to the Master Mind game! Try using invalid entries to test the error handling')
  2. print('Please note that C means an exact match in the exact location and B means a match but not in the correct spot.')
  3. play = 'YES'
  4. while play == 'YES':
  5.     #Generate Rondom Code
  6.     ClrOptions = ['R','G','B','W']
  7.     import random
  8.     Clr1 = random.choice(ClrOptions)
  9.     Clr2 = random.choice(ClrOptions)
  10.     Clr3 = random.choice(ClrOptions)
  11.     Clr4 = random.choice(ClrOptions)
  12.     Code = [Clr1, Clr2, Clr3, Clr4]
  13.     CodeRestore = list(Code)
  14.  
  15.     # Initialize
  16.     Result = 'place holder'
  17.     NumTries = 0
  18.     while Result != 'CCCC':
  19.         CodeRestore = list(Code)
  20.        
  21.         #Get User Guess
  22.         myGuess = input('Please choose a 4 digit combination of R(Red), G(Green), B(Blue), and W(White): ')
  23.         myGuess = myGuess.upper()
  24.         myGuess = list(myGuess)
  25.        
  26.         # Check for valid entry
  27.         def ValidEntry(a, b, c): #a=len(myGuess) b=myGuess c=ClrOptions
  28.             counter1 = 0
  29.             for i in range(a):
  30.                 if b[i] not in c:
  31.                     counter1 = counter1 + 1
  32.             if a != 4:
  33.                 counter1 = counter1 + 1
  34.             if counter1 > 0:
  35.                 return False
  36.         while ValidEntry(len(myGuess), myGuess, ClrOptions) == False:
  37.             print('Oops, not a valid entry. This entry is either the wrong length or uses invalid letters.')
  38.             myGuess = input('Please choose a 4 digit combination of R(Red), G(Green), B(Blue), and W(White): ')
  39.             myGuess = myGuess.upper()
  40.             myGuess = list(myGuess)
  41.  
  42.         # Compare - Look for exact matches
  43.         count = 0
  44.         for i in range(4):
  45.             if Code[i] == myGuess[i]:
  46.                 count = count + 1
  47.                 Code[i] = 'X'
  48.                 myGuess[i] = 'Z'
  49.         Result = 'C' * count
  50.  
  51.         # Compare - Look for semi-matches
  52.         count = 0
  53.         for i in range(4):
  54.             if Code[i] in myGuess[0]:
  55.                 count = count + 1
  56.                 Code[i] = 'X'
  57.                 myGuess[0] = 'Z'
  58.             if Code[i] in myGuess[1]:
  59.                 count = count + 1
  60.                 Code[i] = 'X'
  61.                 myGuess[1] = 'Z'
  62.             if Code[i] in myGuess[2]:
  63.                 count = count + 1
  64.                 Code[i] = 'X'
  65.                 myGuess[2] = 'Z'
  66.             if Code[i] in myGuess[3]:
  67.                 count = count + 1
  68.                 Code[i] = 'X'
  69.                 myGuess[3] = 'Z'
  70.         Result = Result + 'B' * count
  71.         Result = list(Result)
  72.  
  73.         # Report Result
  74.         def scrambled(orig):
  75.             dest = orig[:]
  76.             random.shuffle(dest)
  77.             return dest
  78.         Result = scrambled(Result)
  79.         Result = ''.join(Result)
  80.         print(Result)
  81.         Code = CodeRestore
  82.         NumTries = NumTries + 1
  83.         if Result == 'CCCC':
  84.             print('YOU WON! It took you',NumTries,'tries to guess the code.')
  85.             play = input("Would you like to play again? Type 'yes' to play and any other key to exit: ")
  86.             play = play.upper()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement