Python Forum
Need help (mastermind game)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help (mastermind game)
#1
Here is a part of my code for one of my assignment in programming.
When I try the program I run into a certain problem, when the answer contains (for example) black, and the user enters black twice in the input, the program sees it as 2 correct colour.
How do I improve my program to prevent this from happening!

colour =['red','blue','green','white','orange','grey','yellow','black','pink']
#selecting colour from the pre set list
import random
c1=random.choice(colour)
c2=random.choice(colour)
c3=random.choice(colour)
c4=random.choice(colour)
answer = [c1,c2,c3,c4]
print(answer)
#explaining the game to the user
print('Welcome to the game of Mastermind, in this game 4 colours are choosen randomily, but the sequence of the colours matter.')
print('The goal of the game is to guess those colours along with their respective positions')
print('The list of colurs are', colour, 'please write in lower case ONLY')
print('After every try, you will be prompted about the number of correct colours and the number of colours in the right position')
print('Your code is ready now try your best to figure it out with at little attempts as you can!')
attempt=1
#asking user to make a guess
g1=input('please guess the first colour: ')
while g1 not in colour:
print ('please input colour from the list of given colours!')
g1=input('please guess the first colour: ')
g2=input('please guess the second colour: ')
while g2 not in colour:
print ('please input colour from the list of given colours!')
g2=input('please guess the second colour: ')
g3=input('please guess the third colour: ')
while g3 not in colour:
print ('please input colour from the list of given colours!')
g3=input('please guess the third colour: ')
g4=input('please guess the fourth colour: ')
while g4 not in colour:
print ('please input colour from the list of given colours!')
g4=input('please guess the fourth colour: ')
#comparing the users input to the pre determined answer
col=0
if g1 in answer:
col=col+1
if g2 in answer:
col=col+1
if g3 in answer:
col=col+1
if g4 in answer:
col=col+1
Reply
#2
Pls. put your code in python tags, so it becomes readable.
As for the problem: as long as "black" is in the list, they can select it.

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
Unless I am mistaken, your scoring is incorrect even if it you got the count right. In Mastermind you mark pins in the correct position using a black marker and pins that are the correct color but in the wrong position with a white pin. Your game should print a count of pins that are correct (color and position) and a count of pins that are the correct color but in the wrong position.

Counting correct pins is easy. In pseudo-code:
correct_count = 0
for i in number_pins
    if answer[i] = guess[i]
        correct_count += 1
As you have already discovered, counting correct color in the wrong position is more difficult. You have to make sure that you don't count any of the pins twice. Using "in" is not going to work, at least not by itself. To get a correct count, you will have to remove pins that are already counted. For example, in pin[2] is correct, this pin should not be used when looking for matching colors, or if marked pin[3] as the correct color but wrong position, it cannot be used to match any remaining colors. You will have to think of a way to make sure each pin gets counted only once.

As an aside, your code for entering the guess will result in finger cramps and frustrated players. I would much rather enter "BRPK" instead of answering Blue, Red, Pink blacK. Not only is it shorter to type, but I can easily read my previous guesses and use them to compose my next guess. Using your entry method I would have to write down my guesses on a separate sheet of paper. Since it may be confusing remembering letters for all the colors (Is B Blue or Black?) I would dump the colors and go with letters.

Whatever method you use for entering the guess, write it as a function that you can call over and over. Do the same for reporting the feedback. The body of the program should only be a few lines of code that calls a function to get the input, than calls a function to display the results. Since you don't know how many guesses are required to complete a game, you will need something that lets you repeat this code an arbitrary number of times.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mastermind game Staren 6 4,735 Feb-03-2018, 03:12 PM
Last Post: Staren
  Mastermind Davidlit95 2 3,366 Oct-16-2017, 10:17 PM
Last Post: Davidlit95

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020