CSE 231 - University of Arizona



Programming Project: mindNumber

Collaboration: Solo! Work on these project by yourself with help from section leaders or Rick only

Due: Monday 24-Oct by 9:00 pm

Turn in: Turn in one file name mindNumber.py to the ISTA 130 D2L drop box named Project 6: mindNumber

Grading: 100 points. See Grading Criteria at the end of this file. You should use the 75 assertions in mindNumberTests.py

Goals: In this project, you are going to implement a number guessing game similar to the game of Mastermind. This assignment will give you more experience with

1. string processing

2. user input

3. decision statements

4. indeterminate while loops

5. determinate for loops

6. top down design

7. problem solving a more complex problem

8. writing a program with several related functions

Preview of dialog in main()

To give you an overall feeling for the game, we first present a dialog to show how the game will be played. After getting the first three functions (see next page) working, the code that run this game should go in the function main(). Your dialog should match this pretty closely.

I have a randomly generated number that is five digits long where

all digits are unique. Possible secret numbers include 01234, 59120,

and 67891. Invalid secret numbers include 123456, 11234, and 1234.

Use deductive reasoning to determine this random secret number.

You have a maximum of 32 tries.

Attempt 1: 123456

Need 5 digit inputs only

Attempt 2: 00000

Digits found: 1

Correct position: 1

Attempt 3: 09999

Digits found: 1

Correct position: 1

...

Attempt 14: 01234

Characters found: 5

Correct position: 5

Congratulations! You deduced the secret number in 14 attempts.

The game rules insist that you give the player feedback:

1) If the player attempt does not have a length of 5.

2) the number of digits in the attempt that are in the secret number and

3) the number of digits found in the same position as the secret number.

The player wins when all 5 digits are in the correct position. Based on the feedback, the player enters more attempts. Guessing continues until the “secret” number is deduced or until the maximum number of attempts (32) is reached. In this case, your program must print this:

You only get 32 attempts, try again.

Top Down Design

Before implementing the main() function that plays the actual game, you are asked to write three well-tested functions that will make writing the actual game itself much easier with far fewer bugs. These three functions are first to ensure you have the same exact function names and parameters. We also include a Python module mindNumberTest.py which are the same test we will be run to check your functions for correctness. You should have these three methods plus main in mindNumber.py.

# Developer: [Your name]

#

# SL: [Your SL's name here]

#

# File: mindNumber.py

#

# Due: Monday 24-Oct 9:00 pm

#

# Purpose: Play the game of mindNumber that was designed using

# top-down design

from random import randrange

# Returns a 5 digit, valid secret number as a string. A secret number is valid

# if it contains no duplicates and all five characters are the 10 digits '0'..'9'

#

# Valid secret numbers include '01234' '56789' '62730'

def generateSecretNumber():

# TODO: Complete this method

print('generateSecretNumber')

# Returns the number of matching digits common the guess and the secret

# number. For example when secret is "12345" guess is "12675"

# returns 3 as the 1, 2 and 5 all have the same value at the same location.

# Precondtion: secret is valid and guess has a length of exactly 5

def digitsInCorrectPosition(secret, guess):

# TODO: Complete this method

print('digitsInCorrectPosition')

# Return the number of digits that are contained in both the secret number and

# the guess. For example when secretNumber is '12345' and guess '67821', the two

# share two digits: 1 and 2.

#

# secret: the secret number the user is trying to guess.

# guess: the number the user is comparing to the secret number

def uniqueDigitsFound(secret, guess):

# TODO: Complete this method

print('uniqueDigitsFound')

# Explain program with prints

def printInformation():

# TODO: Complete this method

print('printInformaton')

# Play one game of mindNumber until the player deduces the randomly generated

# secret number or the player has made 32 unsuccessful attempts.

def main():

# TODO: Complete this method

printInformation()

# Allow this module to be run as a program and the mindNumberTest.py

# to import this module without running this module as a program.

if __name__ == '__main__':

main()

Tips/Hints:

1) The console game cannot allow more than 32 attempts to guess. At the point, the player loses.

2) Manipulation of the guess (the users input) is best done as a string (don’t convert it to an int)

3) The user may enter anything as a guess such as what? as long as it does not affect the game or terminate the program. Count this as one of the 32 allowed attempts.

4) Report an error if the attempt is not a length of 5. That still counts as an attempt.

5) A useful operation is use of the in operator to see if a character is in a string.

self.assertTrue('1' in '32190')

self.assertFalse('5' in '32190')

6) Here are a few of the given assertions to show the behavior. The first function is a helper function added to the tests in order to help test whether you are generating valid random secret numbers.

# Return True if secret has length 5, all chars are digits 0..9, and all 5 are unique.

def isValid(self, secret):

if len(secret) != 5:

return False

for i in range(5):

if not secret[i] in '0123456789':

return False

for i in range(5):

sub = secret[:i] + secret[i + 1:]

if secret[i] in sub:

return False

return True

def testOneAssertionOnAll3Functions(self):

self.assertTrue(self.isValid(generateSecretNumber()))

self.assertEqual(3, digitsInCorrectPosition('12345', '92349'))

self.assertEqual(5, uniqueDigitsFound('12345', '54321'))

Grading Criteria 100 pts max, subject to change

____/ 30 mindNumber.py has the code to run the game using standard IO and dialogs are very close to what

you see at the beginning of this document.

+10 Dialog matches (includes instructions, win output, loss output, two feedbacks)

+2 Player notified the input was not exactly length 5 (can be any 5 characters)

+2 Inputs like these count as an attempt, but can possibly match: '123456' '1234' 'ABCDE'

+4 Program terminates after 32 tries with this output You only get 32 attempts, try again.

+12 Can play to a win with Congratulations! You deduced the secret number in ...

____/ 10 generateRandomNumber returns secret numbers that are random

____/ 60 Correctness of 3 functions used in main(). All of Rick's tests pass.

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

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

Google Online Preview   Download