Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Mastermind Rules This is a two person game. One person creates a hidden colour combination or code - consisting of 4 colour pegs chosen from

Mastermind Rules

This is a two person game. One person creates a hidden colour combination or code - consisting of 4 colour pegs chosen from 6 colours. The colours may be repeated. For example, the code might be Blue, Red, Red, Yellow. Your program will play the role of the first person and create a hidden code.

The other player (the user) is the code-breaker and tries to figure out the code. He or she does this by guessing the code. The first player (ie, your program) gives feedback on the correctness of the guess by telling the second player how many correctly positioned colour pegs the guess has and how many correct colour but incorrectly positioned pegs the guess has. This feedback is in the form of a white peg for a correct colour but incorrect position and black peg for a correctly positioned colour. See the online game. The game usually goes for a maximum of 10 tries before the guesser either wins or looses.

In our version of the game, we will use 6 colours, code length 4 and maximum of 10 guesses. These are set as constants. Constants (fixed values) are variables whose values do not change once assigned. A different naming convention (uppercase pothole) is used for constants so that programmers know to not change their values. The constants VALID_CHARS, SIZE and TRIES in the starter file game.py have been set for you. These constants must remain unchanged.

In 1977, the mathematician Donald Knuth demonstrated that the code-breaker can solve the pattern in five moves or less, using an algorithm that progressively reduced the number of possible patterns. Can you figure out the algorithm?

Mastermind Game Functions

In this part you will write the functions that do most of the work in your game of Mastermind. You will write these functions in a file called mastermind.py. Make sure you really understand the rules of Mastermind as described above.

For each function name and description below, fill in the docstring and code for the Python function of the same name in the starter file mastermind.py. You will need to remove the Python command pass from the starter code function definitions. Notice that now you are responsible for writing the docstring types, description and examples - please include at least two examples for each function except for create_code and display_game. You may find that some functions need more to fully check their correctness. Remember that for assignments you will be graded on the quality of your docstrings.

Function Name

Description

create_code(str, int)

Given a str and a size (an int) return a list of length size of single character strs comprised of the characters in the given str. Use the module random and the method choice. For example, given the string 'YGBOPR' and int 4, one possible list is ['G', 'B', 'R', 'B'].

valid(list, str, int)

Given a list of single character strs, a str and an int, return True if every character is in the given str and the list of length equal to the given int.

find_fully_correct(list, list)

Given the answer code (a list) and the guess (a list) return a list containing a 'b' for each correctly positioned colour in the guess. For example, if the answer code is ['P', 'G', 'R', 'O'] and the guess is ['Y', 'G', 'O', 'O'] then return ['b', 'b'].

remove_fully_correct(list, list)

Given two lists of single character strs return a new list that is the result of removing from the first list all chars that are the same and in the same position in the second list. Assume that both lists have the same length. For example, if the lists are ['A','B','C','D'] and['D','B','A','D'] then ['A', 'C'] is returned.

find_colour_correct(list, list)

Given two lists of single character strs return a list of 'w's where the number of 'w's is equal to the number of str in the second list that have the same value as str in the first list but different position. Note that only one 'w' is returned for each str in the first list. For example if the two lists are ['Y','P','G','G'] and ['G','P','O','R']return ['w'].

display_game(list, list)

Given two lists of lists of single character strs return a string (that could be printed to the display the current state of the game. The string should begin with the headers Guess and Clues separated by a tab and ended with a new line. The next line should consist of 16 '*'characters followed by a new line. Next, each corresponding sublists of the given lists. Each character in the sublists should be separated by spaces. Each pair of sublists should be separated by a tab and on a separate line (see the sample output described in the game section for clarification). Your function should work no matter what size the guesses are and no matter how many guesses there are. Note that in order for the automarker to correctly accept your output you must follow these instructions exactly. For example, if the two lists were [['Y', 'P', 'G', 'G'], ['O', 'O', 'G', 'G']]and [['b', 'b'], ['b','b', 'b', 'b']] then the function would return '''Guess\tClues **************** Y P G G\tb b O O G G\tb b b b '''.

The Game

In this section you will write a main program that calls your functions to create the game. ALL code for this section MUST be in the if '__name__' == '__main__': section of the game.py starter file. In this section, your code will consist primarily of a while loop, interactions with the user and calls to functions from mastermind.py.

The program order is as follows:

Create the secret colour code. Hint: use one of your functions to help you!

Get the user's first guess by asking the user to

"Please enter your guess of length X using the letters Y:"

where X is the size of the code size specified in the starter file and Y refers to the string 'GRBYOP' (Green, Red, Blue, Yellow, Orange, Purple) also specified in the starter file.

Check that the guess is valid (uses the correct characters and is the correct length). If the guess is not valid, your program should ask for a new guess using the statement:

"Please enter your guess again of length X using the letters Y:"

where X is the size of the code size specified in the starter file and Y refers to the string 'GRBYOP' (Green, Red, Blue, Yellow, Orange, Purple) also specified in the starter file.

Determine the appropriate clue for the guess. Keep track of all the guesses and clues along the way - this way you can display the state of the game.

Display the current state of the game including all guesses and clues. For example, after two guesses have been made, an output may look like:

Guess Clues **************** O O O O b R R R R b b

The formatting of your output must look exactly like that above with each letter separated by a space and a tab separating the guesses from the clues, one guess, clue pair per line.

The game ends when either the player reaches the maximum number of guesses or the player guesses the code. If the player reaches the maximum number of guesses without guessing the code the message:

"I'm sorry you lose. The correct code was Z."

where Z is the answer code. If the player guesses the code correctly your program should output:

"Congratulations! It took you N guesses to find the code."

where N is the number of guesses used.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 22nd International Conference Dexa 2011 Toulouse France August/September 2011 Proceedings Part 1 Lncs 6860

Authors: Abdelkader Hameurlain ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2011th Edition

3642230873, 978-3642230875

More Books

Students also viewed these Databases questions