Willam Hollinger
Willam Hollinger

Reputation: 31

Mastermind Python coding

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

Upvotes: 3

Views: 1398

Answers (2)

Nana Α.
Nana Α.

Reputation: 1

This is a MasterMind I made in Python. Hope you like it and it helped you! :)

import random                                         
import time  
from tkinter import *


def select_level():
    global level
    level = level_selector.get()
    root.destroy()

root = Tk()
level_selector = Scale(root, from_=1, to=3, tickinterval=1)
level_selector.set(0)
level_selector.pack()
Button(root, text="Select a difficulty level", command=select_level).pack()

mainloop()

cpc_1_digit = 0
cpc_2_digit = 0
cpc_3_digit = 0
cpc_4_digit = 0
p_1_digit = 0
p_2_digit = 0
p_3_digit = 0
p_4_digit = 0
correct_correct = 0
correct_wrong = 0
chances = 0

if level == 1:
    chances = 15
elif level == 2:
    chances = 10
else:
    chances = 7

cpc_1_digit = random.randint(0, 9)

while cpc_2_digit == cpc_1_digit or cpc_2_digit == cpc_3_digit or cpc_2_digit == 
cpc_4_digit:
    cpc_2_digit = random.randint(0, 9)

while cpc_3_digit == cpc_1_digit or cpc_3_digit == cpc_2_digit or cpc_3_digit == 
cpc_4_digit:
    cpc_3_digit = random.randint(0, 9)

while cpc_4_digit == cpc_1_digit or cpc_4_digit == cpc_2_digit or cpc_4_digit == 
cpc_3_digit:
    cpc_4_digit = random.randint(0, 9)

while chances > 0:
    correct_correct = 0
    correct_wrong = 0

    answer = input("Enter a four-digit number with different digits (e.g 1476): ")
    p_1_digit = int(answer[0])
    p_2_digit = int(answer[1])
    p_3_digit = int(answer[2])
    p_4_digit = int(answer[3])

    if p_1_digit == cpc_1_digit:
        correct_correct = int(correct_correct) + 1
    elif p_1_digit == cpc_2_digit or p_1_digit == cpc_3_digit or p_1_digit == 
cpc_4_digit:
        correct_wrong = int(correct_wrong) + 1
    else:
        pass

    if p_2_digit == cpc_2_digit:
        correct_correct = correct_correct + 1
    elif p_2_digit == cpc_1_digit or p_2_digit == cpc_3_digit or p_2_digit == 
cpc_4_digit:
        correct_wrong = int(correct_wrong) + 1
    else:
        pass

    if p_3_digit == cpc_3_digit:
        correct_correct = int(correct_correct) + 1
    elif p_3_digit == cpc_1_digit or p_3_digit == cpc_2_digit or p_3_digit == 
cpc_4_digit:
        correct_wrong = int(correct_wrong) + 1
    else:
        pass

    if p_4_digit == cpc_4_digit:
        correct_correct = int(correct_correct) + 1
    elif p_4_digit == cpc_1_digit or p_4_digit == cpc_3_digit or p_4_digit == 
cpc_2_digit:
        correct_wrong = int(correct_wrong) + 1
    else:
        pass

    print("")
    if int(correct_correct) == 4:
        print("Congratsulations! You found the computer's number!")
        break
    elif int(correct_wrong) > 0 or int(correct_correct) >= 1 and int(correct_correct) 
< 4:
        print("You got " + str(correct_correct) + " correct digit(s) in the correct 
place, and " + str(correct_wrong) + " correct digit(s) but in wrong place.")
    elif int(correct_correct) == 0 and int(correct_wrong) == 0:
        print("You didn't guess any number, try again!")
    else:
        raise Exception("CheckError: line 69, something went wrong with the 
comparings.")
        exit()
    print("")

    chances = chances - 1


if chances == 0:
    print("You lost... The secret number was " + str(cpc_1_digit) + str(cpc_2_digit) 
+ str(cpc_3_digit) + str(cpc_4_digit) + ". Try again by rerunning the program.")

time.sleep(4)

Upvotes: 0

Maarten Fabr&#233;
Maarten Fabr&#233;

Reputation: 7058

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

Upvotes: 3

Related Questions