?
2011-10-20 23:38:59 UTC
Here’s the idea of the game. The computer picks a random 5-digit number. That number is treated as a string of characters, not really as a number. The player then tries to guess the number, getting all the digits in the right place, with clues from the computer based on how many digits are correct, and how many are in the right place.
Here are separate pieces you need to write. Make each of these a function, returning a value. These are independent of one another, so you can tackle them in any order.
· Generate a random string of five digits. There are lots of ways to do this. It is easier to write the rest of the game if the digits are distinct, but harder to generate this string. One way is to put the ten digits as characters into a list, shuffle the list, then put the first five into a string. Do this as a function; it should return a string of five digits. For example, it might return “32516’, but not ‘22345’ or ‘5678’. There are lots of other ways. You could generate a random number as a number, then convert it to a string and check for repeated digits. If you find repetition, reject and try again, until you get an acceptable string.
· Compare two strings of digits, to see how many of the digits in the first string (the computer’s selection) appear in the second string (the player’s guess,) irrespective of where in the strings those digits are. If the two strings are ‘12345’ and ‘13579’ respectively, this should return 3, since ‘1’, ‘3’, and ‘5’ appear in the first string and in the second.
· Compare two strings of digits, to see how many of the digits in the first string (the computer’s selection) appear in exact same spot in the second string (the player’s guess.) If the two strings are ‘12345’ and ‘15379’ respectively, this should return 2, since ‘1’and ‘3’appear in the first string and in the second in exactly the same places.
· Get input from the user. You need not error-check the input (but see the extra-credit below). If the user puts in a 4 or 6 digit number, or something that isn’t a number, the program may terminate with an error message.
With those four pieces written and tested, you can assemble the game. Make this another function, with a single parameter, which is the maximum number of guesses allowed. Inside: first, get a random string from the computer, as above. Then, you need a loop; a for loop with a maximum number of guesses as the number of iterations of the loop. In this loop, you need to:
§ Get input from the user
§ Score the input by comparing the string from the user with the string from the computer. There are two numbers to report: the number of digits in the right place, and the number of digits that are correct. Print the results.
§ If all the digits are in the right place, print an appropriate message and then return – that will exit the function and the game.
§ The only way you can get through the loop without returning is to never guess all the digits correctly. So, past the end of the loop, print a message consoling the user on their loss.
Tip: debugging may be easier if you ‘cheat’, printing out the computer selection. That makes it easier to tell if the computer is scoring correctly. Remove this ‘feature’ in the final product.
Other thoughts: Even though you are playing with digits, you aren’t treating things as numbers, but strings. You could do this with letters or, for that matter, arbitrary symbols. If you need to move from numbers to strings, the str function will convert. You can go the other way with the eval function. For the record, I did not use either of these, but you can use them if you need to with your approach.
Here is one play of the game as I wrote it. Your precise form may vary.
Let's play DigitalMind!
Try to guess my five-digit number.
All the digits are distinct.
Please type in a 5 number. 12345
You got 2 digits right, and 0 in the right place.
Please type in a 5 number. 13579
You got 3 digits right, and 1 in the right place.
Please type in a 5 number. 13580
You got 3 digits right, and 1 in the right place.
Please type in a 5 number. 13578
You got 4 digits right, and 2 in the right place.
Please type in a 5 number. 13568
You got 4 digits right, and 3 in the right place.
etc...
I'm not even sure how to start writing this...any help?