-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
141 lines (125 loc) · 4.28 KB
/
game.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# from codegen import getCode
import random
colors = ['R','O','Y','G','P','W']
from clear import clear
def getCode():
# orange, red, white, green, yellow, purple
# ORWGYP
# ROYGPW
# goal is to generate random code of 4 colors
i = 0
code = []
while i < 4:
x = random.randint(0,5)
color = colors[x]
# print(color)
code.append(color)
i += 1
# print(code)
return code
def startGame():
print("Welcome to Mastermind!")
print()
print("Adapted and coded by McKennah and Gavin")
print()
go = input("Are you ready to play? [Y/N]").upper()
if go != 'Y':
print("Ok, maybe another time then.")
return
else:
replay = 'play'
while replay == 'play':
clear()
print("Let's play!")
code = getCode()
print("The secret code has been generated!\nLet's see if you can figure it out!\n")
replay = play(code)
def play(code):
# initialize variables
win = False
guesses = []
# codelist = list(code)
# build game board
for i in range(0,10):
guesses.append([i+1,'----',0,0])
# start turns
print("Time to guess! You must choose from the following colors. R,O,Y,G,W,P")
for i in range(0,10):
# assign guess to list position for this turn
guesses[i][1] = getGuess()
r = [] # red pegs
w = 0 # white pegs
g = list(guesses[i][1].upper()) # guess
wgl = list(g) # white guess list to be used for white pegs
wcl = list(code) # white code list
# check for 'Red' pegs = where color and location are correct
for j in range(3,-1,-1):
# print(j, g[j], code[j])
if g[j] == code[j]:
r.append(1)
wgl.pop(j)
wcl.pop(j)
else:
r.append(0)
guesses[i][2] = sum(r)
# white pegs - look at those not matched exactly and check to see if they exist and how many.
if len(wgl) > 0:
for k in set(wgl):
w += "".join(wcl).count(k)
guesses[i][3] = w
# print game board
clear()
#print("Turn x")
print('-----------------------------------------')
print('Colors: R,O,Y,G,W,P',)
print('Red: Correct color, correct position.')
print('White: Correct color, incorrect position.')
print('-----------------------------------------')
print('Turn\tGuess\tRed\tWhite')
for x in guesses[::-1]:
print("%i\t%s\t%i\t%i" % (x[0], x[1], x[2], x[3]))
# if guess is correct
if sum(r) == 4:
win = True
break
# print("this is the play function returning the valid guess: " +guess)
if win:
print("Congratulations!\nYou correctly guess the secret code in round %i!" % (i+1))
print("Code:", "".join(code))
else:
print("Too bad!\nYou did not correctly guess the secret code.\nCode:", "".join(code))
replay = input("Would you like to play again? [Y/N]").upper()
if replay == 'Y':
return 'play'
else:
print("Ok, Bye!")
return 'stop'
def getGuess():
i = 0
while i < 2:
# print("\tstart loop")
i = 0
charcount = 0
# print("\tstart i = " , str(i))
guess = input("Enter your guess: ").upper()
# Check the length
while len(guess) != 4:
guess = input("Your guess must be exactly 4 characters!\nTry again: ").upper()
i += 1 # guess is the right length
# print("\tlength check i = " , str(i))
# Check the characters
chars = list(guess)
for char in chars:
if char in colors:
charcount += 1
else:
print(char + " is not a valid color!")
# print("\tpre-char check i = " , str(i))
if charcount == 4:
i += 1 # characters are valid
# print("\tchar check i = " , str(i))
continue
# print("Your guess is " + guess)
return guess
if __name__ == '__main__':
startGame()