Skip to content

Commit 5f28f50

Browse files
committed
new edited code for mastermind
1 parent c00b478 commit 5f28f50

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

mastermind_code.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import random
2+
def mastermind():
3+
colours = ["R", "G", "B", "Y", "W", "O"] #R-RED,G-GREEN,B-BLUE,Y-YELLOW,W-WHITE,O-ORANGE
4+
attempts = 0
5+
game = True
6+
s=1
7+
colour_code = random.sample(colours,4)
8+
print (colour_code)
9+
#player's guessing starts
10+
print ("START GUESSING")
11+
while game:
12+
correct_colour = ""
13+
guessed_colour = ""
14+
player_guess=""
15+
player_guess=raw_input().upper()
16+
attempts+= 1
17+
if len(player_guess) != len(colour_code):
18+
print ("The secret code needs to have exactly four colors")
19+
continue
20+
for i in range(4):
21+
if player_guess[i] not in colours:
22+
print ("invalid input")
23+
continue
24+
if correct_colour != "bbbb":
25+
for i in range(4):
26+
if player_guess[i] == colour_code[i]:
27+
correct_colour += "b"#black peg for correct colour and position
28+
if player_guess[i] != colour_code[i] and player_guess[i] in colour_code:
29+
guessed_colour += "w"# white peg for correct colour and incorrect position
30+
print (correct_colour + guessed_colour + "\n")
31+
# matching player's input with original colour code
32+
if correct_colour == "bbbb":
33+
if attempts == 1:
34+
print ("You guessed at the first attempt!")
35+
else:
36+
print ("Well done... You needed " + str(attempts) + " attempts to guess.")
37+
game = False
38+
#player gets maximum of 5 attempts to guess the code
39+
if attempts >= 1 and attempts <6 and correct_colour != "bbbb":
40+
print ("Next attempt: ")
41+
elif attempts >= 6:
42+
print ("You didn't guess! The secret color code was: " + str(colour_code))
43+
44+
#To play or not to play
45+
while game == False:
46+
finish_game = raw_input("Do you want to play again (Y/N)?").upper()
47+
attempts = 0
48+
if finish_game =="N":
49+
print ("Thanks for the game")
50+
break
51+
elif finish_game == "Y":
52+
game = True
53+
print ("Let's play again.Guess the secret code: ")
54+
mastermind()

0 commit comments

Comments
 (0)