Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mastermind Python coding

Tags:

python

Ok I have a feeling that this is a simple simple issue but I have been staring at this code for about 10 hours now. The issue I am having is in mastermind is that once I get it to recognize that I have the correct colors in the right spot I can get it to display the right spots with X and the wrong spots with O. I need to be able to convert that so instead of X and O I need it to tell the user that he/she has 2 blacks and one white For example: The secret code is RGYB The user enters RGOY so then Python relays "You have 2 blacks(The R and G spots) and one 1 White (The Y because it's the right color just in the wrong index) As of right now I got it to display X for the right color in the right spot and anything else it is an O I will post what I have been working with now but today I am at my wit's end https://pastebin.com/HKK0T7bQ

    if correctColor != "XXXX":
    for i in range(4):
        if guess[i] == tempCode[i]:
           correctColor += "X"
        if  guess[i] != tempCode[i] in tempCode:
           correctColor += "O"
    print (correctColor + "\n")    

if correctColor == "XXXX":
    if attempts == 1:
        print ("You think you are sweet because you got it right on the first try? Play me again!")
    else:
        print ("Well done... You needed " + str(attempts) + " attempts to guess.")
    game = False
like image 316
Willam Hollinger Avatar asked Nov 22 '25 22:11

Willam Hollinger


1 Answers

A few comments

X and O

you use X and 0 to denote the success, it will be easier and faster to use a list or tuple or booleans for this, that way you can use sum() to count how many colors and locations were correct. Then whether you represent that with X and O or red and white pins is a matter for later

compartmentalization

Your game logic (guess input, input validation, do you want to continue, etc) is mixed with the comparison logic, so it would be best to separate the different functions of your program into different methods.

This is an fineexample to introduce object oriented programming, but is so simple it doesn't need OO, but it can help. What you need is a method which takes a series of colours and compares it to another series of colours

Standard library

Python has a very extended standard library, so a lot of stuff you want to do probably already exists

Correct colours

to count the number of letters which occur in 2 strings, you can use collections.Counter

guess = "RGOY "
solution = "RGYB"
a = collections.Counter(guess)
b = collections.Counter(solution)

a & b
Counter({'G': 1, 'R': 1, 'Y': 1})
correct_colours = sum((a & b).values())
3

So the user guessed 3 colours correctly

Correct locations

can be solved with an easy list comprehension

[g == s for g, s in zip(guess, solution)]
[True, True, False, False]
sum(g == s for g, s in zip(guess, solution))
2

so the used put 2 colours on the correct location

like image 166
Maarten Fabré Avatar answered Nov 28 '25 15:11

Maarten Fabré



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!