Python Forum
Mastermind - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Mastermind (/thread-5591.html)



Mastermind - Davidlit95 - Oct-12-2017

Im taking a basic python course in university and my class project is creating something like mastermind.

We have to create a program that guesses the 4-digit number you're thinking (digits from 1 to 6) in 9 tries or less. The only questions I can ask are: 'how many numbers are correct in the correct position?' and 'how many numbers are correct in the incorrect position?'.

Right now I have a program that loops 9 times through the 2 questions after guessing a number and stores the number and answers in a list. My problem is that my guesses are simply random numbers that do not take into account previous answers. I'd appreciate any tips on how to improve the guessed number every time. I can do it manually but I just can't figure out how to put it in Python.


RE: Mastermind - ichabod801 - Oct-12-2017

Start with a list of all the possible numbers. Your guess can be a random choice from that list. Each time you get the two answers, go through that list and remove any numbers that don't match the two answers and the number previously guessed. What will be really useful are two functions: one that returns True if number x has n digits of number y in the correct position, and another that returns True if number x has n digits of number y in the incorrect position. Only keep numbers in the list of possible guesses if they match both of those functions.


RE: Mastermind - Davidlit95 - Oct-16-2017

Thanks! I did that and it worked perfectly. Now I'll just have to check that it always works in less than 9 tries.