-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new edited code for mastermind Updated documentation of python-mastermind
- Loading branch information
1 parent
5284ddc
commit 77db835
Showing
3 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,25 @@ | ||
# python-mastermind | ||
Mastermind: A repository to get students familiar with the git environment. (python) | ||
# MASTERMIND | ||
##INTRODUCTION | ||
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. | ||
|
||
##RULES AND PROCEDURE | ||
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. | ||
|
||
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. | ||
|
||
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. | ||
|
||
##HOW TO RUN | ||
You can run the game after installing pygame through the command | ||
``` | ||
sudo apt-get install python-pygame | ||
``` | ||
and run it simply by, | ||
``` | ||
index.py | ||
``` | ||
######Happy gaming and happy coding! | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import random | ||
def mastermind(): | ||
colours = ["R", "G", "B", "Y", "W", "O"] #R-RED,G-GREEN,B-BLUE,Y-YELLOW,W-WHITE,O-ORANGE | ||
attempts = 0 | ||
game = True | ||
s=1 | ||
colour_code = random.sample(colours,4) | ||
print (colour_code) | ||
#player's guessing starts | ||
print ("START GUESSING") | ||
while game: | ||
correct_colour = "" | ||
guessed_colour = "" | ||
player_guess="" | ||
player_guess=raw_input().upper() | ||
attempts+= 1 | ||
if len(player_guess) != len(colour_code): | ||
print ("The secret code needs to have exactly four colors") | ||
continue | ||
for i in range(4): | ||
if player_guess[i] not in colours: | ||
print ("invalid input") | ||
continue | ||
if correct_colour != "bbbb": | ||
for i in range(4): | ||
if player_guess[i] == colour_code[i]: | ||
correct_colour += "b"#black peg for correct colour and position | ||
if player_guess[i] != colour_code[i] and player_guess[i] in colour_code: | ||
guessed_colour += "w"# white peg for correct colour and incorrect position | ||
print (correct_colour + guessed_colour + "\n") | ||
# matching player's input with original colour code | ||
if correct_colour == "bbbb": | ||
if attempts == 1: | ||
print ("You guessed at the first attempt!") | ||
else: | ||
print ("Well done... You needed " + str(attempts) + " attempts to guess.") | ||
game = False | ||
#player gets maximum of 5 attempts to guess the code | ||
if attempts >= 1 and attempts <6 and correct_colour != "bbbb": | ||
print ("Next attempt: ") | ||
elif attempts >= 6: | ||
print ("You didn't guess! The secret color code was: " + str(colour_code)) | ||
|
||
#To play or not to play | ||
while game == False: | ||
finish_game = raw_input("Do you want to play again (Y/N)?").upper() | ||
attempts = 0 | ||
if finish_game =="N": | ||
print ("Thanks for the game") | ||
break | ||
elif finish_game == "Y": | ||
game = True | ||
print ("Let's play again.Guess the secret code: ") | ||
mastermind() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import random | ||
def mastermind(): | ||
colours = ["R", "G", "B", "Y", "W", "O"] #R-RED,G-GREEN,B-BLUE,Y-YELLOW,W-WHITE,O-ORANGE | ||
attempts = 0 | ||
game = True | ||
s=1 | ||
colour_code = random.sample(colours,4) | ||
print ("START GUESSING") | ||
while game: | ||
correct_colour = "" | ||
guessed_colour = "" | ||
player_guess="" | ||
player_guess=raw_input().upper() | ||
attempts+= 1 | ||
if len(player_guess) != len(colour_code): | ||
print ("The secret code needs to have exactly four colors") | ||
continue | ||
for i in range(4): | ||
if player_guess[i] not in colours: | ||
print ("invalid input") | ||
continue | ||
if correct_colour != "bbbb": | ||
for i in range(4): | ||
if player_guess[i] == colour_code[i]: | ||
correct_colour += "b" | ||
if player_guess[i] != colour_code[i] and player_guess[i] in colour_code: | ||
guessed_colour += "w" | ||
print (correct_colour + guessed_colour + "\n") | ||
|
||
if correct_colour == "bbbb": | ||
if attempts == 1: | ||
print ("You guessed at the first attempt!") | ||
else: | ||
print ("Well done... You needed " + str(attempts) + " attempts to guess.") | ||
game = False | ||
|
||
if attempts >= 1 and attempts <6 and correct_colour != "bbbb": | ||
print ("Next attempt: ") | ||
elif attempts >= 6: | ||
print ("You didn't guess! The secret color code was: " + str(colour_code)) | ||
|
||
#To play or not to play | ||
while game == False: | ||
finish_game = input("Do you want to play again (Y/N)?").upper() | ||
attempts = 0 | ||
if finish_game =="N": | ||
print ("Thanks for the game") | ||
elif finish_game == "Y": | ||
game = True | ||
print ("Let's play again.Guess the secret code: ") |