bwukki

Untitled

Oct 25th, 2017
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. from random import *
  2.  
  3. def generatecode():
  4.     codeLength = 4
  5.     code = ['null','null','null','null']
  6.     for i in range(codeLength) :      
  7.         x = randint(1,4)
  8.         if x == 1:
  9.             code[i] = 'R'
  10.         if x == 2:
  11.             code[i] = 'G'
  12.         if x == 3:
  13.             code[i] = 'B'
  14.         if x == 4:
  15.             code[i] = 'Y'
  16.     return code;
  17.  
  18. def getuserinput():
  19.     while True:
  20.         guess = input('Type your guess: ')
  21.         guess = guess.upper()
  22.         validity = '0'
  23.         #check if guess is valid
  24.         if len(guess) != 4:
  25.             print('Invalid guess length. Please guess only 4 colors.')
  26.         else:
  27.             for i in range(4):
  28.                 if (guess[i] != 'R') and (guess[i] != 'G') and (guess[i] != 'B') and (guess[i] != 'Y'):
  29.                     validity = 'invalid'
  30.             if (validity == 'invalid'):
  31.                 print('Please only use the characters RGBY')
  32.             else:
  33.                 break;
  34.     return guess;
  35.  
  36. def comparefeedback(code,guess):
  37.     guesscopy = list(guess.upper())
  38.     codecopy = list(code)
  39.     x = 0
  40.     for i in range(4):
  41.         if code[i] == guess[i]:
  42.             del guesscopy[i-x]
  43.             del codecopy[i-x]
  44.             x = x+1
  45.             #look for exact matches, counts them
  46.     if x == 4:
  47.         print('You win! You guessed all colors correctly!')
  48.         return;
  49.     else:
  50.         feedback = 'B' * x
  51.         a = 0
  52.         b = 0
  53.         c = 0
  54.         #look for matches in wrong spot
  55.         while True:
  56.             if c == len(codecopy)*len(guesscopy):
  57.                 break;
  58.             if (c % len(guesscopy) == 0) and (c != 0):
  59.                 a = a+1
  60.                 b = 0
  61.             if codecopy[a] == guesscopy[b]:
  62.                 #invalidates the spots with matches so it doesn't see them again
  63.                 guesscopy[b] = 'X'
  64.                 codecopy[a] = 'z'
  65.                 feedback = feedback + 'W'
  66.             b = b+1
  67.             c = c+1      
  68.         print(feedback)
  69.         replay()
  70.     return;
  71.  
  72. def replay():
  73.     guess = getuserinput()
  74.     comparefeedback(code,guess)
  75.    
  76. code = generatecode()
  77. guess = getuserinput()
  78. comparefeedback(code,guess)
Advertisement
Add Comment
Please, Sign In to add comment