-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuessColorSequence
78 lines (56 loc) · 2.14 KB
/
GuessColorSequence
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import random
def validateInput():
inputGuess = input("Enter your guess as 4 letters e.g. XXXX:")
while True:
if len(inputGuess) != 4:
inputGuess = input("Enter your guess as 4 letters e.g. XXXX:")
else:
wordList = list( inputGuess.upper() )
invalidLetters = False
for letter in wordList:
if letter not in ['R','G','Y','B','W']:
invalidLetters = True
if invalidLetters == True:
print("Possible colours are R G Y B W")
inputGuess = input("Enter your guess as 4 letters e.g. XXXX:")
else:
return wordList
guessesRemaining = 12
code = []
guess = []
correctPosition = 0
correctColour = 0
for i in range(4):
code.append(random.choice(['R','G','Y','B','W']))
print("Guess my sequence of four colours, in the correct order.")
print("\nPossible colours are R G Y B W")
while guessesRemaining > 0:
correctPosition = 0
correctColour = 0
lettersChecked = []
guess = validateInput()
guessesRemaining -= 1 # Deduct one guess
tempGuess = list(guess)
tempCode = list(code)
for i in range(4):
if guess[i] == code[i]:
correctPosition += 1
tempGuess[i] = "X"
tempCode[i] = "X"
for j in range(4):
if tempGuess[j] in tempCode and tempGuess[j] != "X" and tempGuess[j] not in lettersChecked:
if tempCode.count(guess[j]) > tempGuess.count(tempGuess[j]):
correctColour += tempGuess.count(tempGuess[j])
else:
correctColour += tempCode.count(tempGuess[j])
lettersChecked.append(tempGuess[j])
if correctPosition > 0:
print("You had",correctPosition,"correct colours in the correct place")
if correctColour > 0:
print("You had",correctColour,"correct colours in the wrong place")
if correctPosition == 0 and correctColour == 0:
print("No correct colours")
if correctPosition == 4:
print("You won in",12-guessesRemaining,"guesses, congratulations!")
guessesRemaining = 0
print("Thanks for playing")