Hi there .... Some months ago I decided to have a crack at Python. I
set myself the task of coding the 'Mastermind' game and got into great
problems where the random generated number contained duplicated digits.
I recently decided to get back to it as I have volunteered to introduce
the older kids at the local junior school to programming i initially
using 'Scratch' and then touching on Python.
I found some Mastermind example coding on the internet and took a look.
I'm playing with the attached:
It seemed to work OK so I re-wrote the input code to be (IMO) more
logical, and added more text to make it obvious what is happening. Then
I noticed it doesn't get the scoring right when there are duplicate
digits! I'm no expert, so I wonder if you guys can explain in simple
terms what is happening. I have to say I feel a bit stupid here. Below
is one of the 'mistakes':
$ python mastermind_2.py
I have chosen a random four digit number. You have to guess what it is.
Give me your guess at the four digit number ..... Enter four digits
between 1 and 6: 1234
line is: ['1', '2', '3', '4'] Length: 4
Random Code: (3, 4, 2, 3)
Result: 0 - you have a correct number in an incorrect position
Result: 0 - you have a correct number in an incorrect position
Result: 0 - you have a correct number in an incorrect position
Result: -1 - you have an incorrect number
I have chosen a random four digit number. You have to guess what it is.
Give me your guess at the four digit number ..... Enter four digits
between 1 and 6: 4215
line is: ['4', '2', '1', '5'] Length: 4
Random Code: (3, 4, 2, 3)
Result: 0 - you have a correct number in an incorrect position
Result: 0 - you have a correct number in an incorrect position
Result: -1 - you have an incorrect number
Result: -1 - you have an incorrect number
-- Barry Drake is a member of the the Ubuntu Advertising team.
http://ubuntu.com/
import random
# generate random code
code = (random.randrange(1, 6), random.randrange(1, 6),
random.randrange(1, 6), random.randrange(1, 6))
line = ['','','',''] # line from user
cc = [] # code count
cl = [] # line count
matched = 0 # number of matched items
result = [-1, -1, -1, -1] # result
user_own = False
max_attempts = 10 # XXX How to define a constant variable?
attempts = 0
while not user_own and attempts < max_attempts:
# line = raw_input(str(attempts + 1) + " You : ").strip().split(" ") # originally
# Barry
print "I have chosen a random four digit number. You have to guess what it is."
input_str = str(input("Give me your guess at the four digit number ..... Enter four digits between 1 and 6: "))
for i in range(len(input_str)):
line[i] = input_str[i]
print "line is: ", line, " Length: ", len(line) # debug hint
print "Random Code: ", code # for debugging only
# /Barry
if input_str == "1111":
# cheat code for debugging
print "Random Code: ", code
if len(line) != 4:
# won't be considered an attempt
print "Please enter only 4 digits "
continue
# convert list members in integer
line = [int(l) for l in line]
# TODO check for color in range 1 to 6????
# game has 6 colors
cc = [0, 0, 0, 0, 0, 0]
cl = [0, 0, 0, 0, 0, 0]
matched = 0
for i in range(len(line)):
if line[i] == code[i]:
# matched guess
matched += 1
else:
cc[code[i] - 1] += 1
cl[line[i] - 1] += 1
if matched == 4:
user_own = True
continue
# user is not own, evaluate user guesses
i = 0
result = [-1, -1, -1, -1]
while i < matched:
# color matched along with position
result[i] = 1
i += 1
ri = i
for i in range(len(cc)):
x = min(cc[i], cl[i])
for i in range(x):
# color matched, position incorrect
result[ri] = 0
ri += 1
if ri == 4:
break;
# XXX Note comma at the end to disable newline
# print "Result:",
for i in result:
print "Result:", i,
if (i == 1):
print " - you have a correct number in a correct position"
if (i == 0):
print " - you have a correct number in an incorrect position"
if (i == -1):
print " - you have an incorrect number"
attempts += 1
print
if user_own:
print "Congratulations - you have WON!!!"
else:
print "Sorry, YOU LOST!!!"
print "Random Code was:", code
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor