CS2321 – Lab 9



CS2321 – Lab MM1

Python MasterMind

MasterMind is a classic game licensed by Hasbro consisting of a pegboard 10 rows long and four rows wide, in addition to a hidden row of four holes, which are filled by the CodeMaker with exactly four pegs (the code), each of which can be one of six different colors.

The CodeBreaker player may then make up to 10 guesses using combinations of those six colored pegs.

Between each guess, the CodeMaker must accurately score the guess by awarding: one black point for each correct color in the correct spot, and one white point for each correct color in the incorrect spot. No pegs in the guess may be scored twice (i.e. cannot award both a white and a black point for the same peg in the guess, even if that color is featured twice in the code. Black points should be awarded first, then any white points determined.

Example – Code: 1,2,1,3 Guess: 4,3,1,2 Points: bww

The order of black and white points in no way indicates which peg generated which point. Points awarded for previous guesses can then be used to determine what subsequent guesses should be. If the code is not guessed in 10 guesses or less, the CodeMaker wins.

The Lab Assignment:

Your task is to implement MasterMind in Python. The computer will play the role of the CodeMaker, and the user will be Codebreaker. You will be provided a main() function and a list of functions to write in order to implement this game. The program should run through one game of MasterMind as the CodeMaker before exiting. You may change the main() function if you wish, and you may implement additional functions that you feel are necessary.

Main() function:

def main():

ans = welcome()

blnWon = False

if ans == 'y':

mypat = genPat()

for i in range(1,11):

guess = getPat()

blnWon = scorePat(guess, mypat)

if blnWon: break

if blnWon:

print("You won! My pattern was:", mypat)

else:

print("You lost. It was inevitable, really...")

Additional Functions:

from random import randint

def getPat():

# elicits four integers from the user in the range [1..6];

# PARAMETERS: NONE

# Returns: a bulletproof list of four integers (in the same order as entered)

def genPat():

# generates a pattern of four random integers from [1..6]

# PARAMETERS: None

# RETURNS: a list of four random integers from [1..6]

def scorePat(guess, code):

# generates a score for the given guess as compared to the code

# b = correct color, correct position

# w = correct color, incorrect position

# prints "Your score is: " + a string of b’s and w’s, or the string ""

# PARAMETERS: guess: the user’s 4-integer guess

code: the computer’s secret code

# RETURNS: True or False, to indicate if the game should end (i.e. score==’bbbb’ ==> TRUE )

def welcome():

# welcome screen and game explanation

# RETURNS: ‘y’ if player wants to play a game; returns any other character if not

>>> mm()

Welcome to Python Mastermind!

In this game, I pick four integers from [1..6] in a certain order,

and ask you to guess what they are -- in that same order!

You may guess up to ten (10) times

(Note: I may also pick the same integer multiple times!)

I will score your guess with either a black [b] or a white [w] stone

- [b] black means you have a correct digit in a correct location

- [w] white means you have a correct digit in an incorrect location

If you correctly guess my pattern before the eleventh guess, you win

Else I will have once again proven my undeniable superiority and

inevitable dominance over miserable organic life-forms.

So what do you say? Would you like to play a game? [y/n]

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download