Skip to content

Commit 77db835

Browse files
committed
The updated code and documentation
new edited code for mastermind Updated documentation of python-mastermind
1 parent 5284ddc commit 77db835

File tree

3 files changed

+129
-2
lines changed

3 files changed

+129
-2
lines changed

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
1-
# python-mastermind
2-
Mastermind: A repository to get students familiar with the git environment. (python)
1+
# MASTERMIND
2+
##INTRODUCTION
3+
Mastermind or Master Mind is a code-breaking game. The board game with pegs was invented in 1970 by Mordecai Meirowitz, an Israeli postmaster and telecommunications expert.It resembles an earlier pencil and paper game called Bulls and Cows that may date back a century or more. It has been recently implemented as an game application with similar rules as that of the board game, except the code is randomly generated.
4+
5+
##RULES AND PROCEDURE
6+
The game consists of a hidden randomly generated code of four colours. The player has to guess the code **both the correct colours and their correct order** out of possibility of eight colours, in the specified number of guesses.
7+
8+
The player chooses a pattern of four code pegs. Duplicates and blanks are allowed depending on player choice, so the player could even choose four code pegs of the same color or four blanks.The player tries to guess the pattern, in both order and color, within the specified turns. Each guess is made by placing a row of code pegs on the decoding board. Once placed, a feedback is provided by placing from zero to four key pegs in the small holes of the row with the guess. A colored or black key peg is placed for each code peg from the guess which is correct in both color and position. A white key peg indicates the existence of a correct color code peg placed in the wrong position.
9+
10+
Once feedback is provided, another guess is made; guesses and feedback continue to alternate until either the player guesses correctly, or the specified number of incorrect guesses are made.
11+
12+
##HOW TO RUN
13+
You can run the game after installing pygame through the command
14+
```
15+
sudo apt-get install python-pygame
16+
```
17+
and run it simply by,
18+
```
19+
index.py
20+
```
21+
######Happy gaming and happy coding!
22+
23+
24+
25+

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()

nf.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 ("START GUESSING")
9+
while game:
10+
correct_colour = ""
11+
guessed_colour = ""
12+
player_guess=""
13+
player_guess=raw_input().upper()
14+
attempts+= 1
15+
if len(player_guess) != len(colour_code):
16+
print ("The secret code needs to have exactly four colors")
17+
continue
18+
for i in range(4):
19+
if player_guess[i] not in colours:
20+
print ("invalid input")
21+
continue
22+
if correct_colour != "bbbb":
23+
for i in range(4):
24+
if player_guess[i] == colour_code[i]:
25+
correct_colour += "b"
26+
if player_guess[i] != colour_code[i] and player_guess[i] in colour_code:
27+
guessed_colour += "w"
28+
print (correct_colour + guessed_colour + "\n")
29+
30+
if correct_colour == "bbbb":
31+
if attempts == 1:
32+
print ("You guessed at the first attempt!")
33+
else:
34+
print ("Well done... You needed " + str(attempts) + " attempts to guess.")
35+
game = False
36+
37+
if attempts >= 1 and attempts <6 and correct_colour != "bbbb":
38+
print ("Next attempt: ")
39+
elif attempts >= 6:
40+
print ("You didn't guess! The secret color code was: " + str(colour_code))
41+
42+
#To play or not to play
43+
while game == False:
44+
finish_game = input("Do you want to play again (Y/N)?").upper()
45+
attempts = 0
46+
if finish_game =="N":
47+
print ("Thanks for the game")
48+
elif finish_game == "Y":
49+
game = True
50+
print ("Let's play again.Guess the secret code: ")

0 commit comments

Comments
 (0)