diff --git a/README.md b/README.md index a67deeb..75f19b1 100644 --- a/README.md +++ b/README.md @@ -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! + + + + diff --git a/mastermind_code.py b/mastermind_code.py new file mode 100644 index 0000000..ae0fef8 --- /dev/null +++ b/mastermind_code.py @@ -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() diff --git a/nf.py b/nf.py new file mode 100644 index 0000000..b8257ab --- /dev/null +++ b/nf.py @@ -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: ")