Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mastermind
#1
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.
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks! I did that and it worked perfectly. Now I'll just have to check that it always works in less than 9 tries.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help (mastermind game) sseansssean 2 2,865 Jul-06-2020, 12:47 PM
Last Post: deanhystad
  Mastermind game Staren 6 4,694 Feb-03-2018, 03:12 PM
Last Post: Staren

Forum Jump:

User Panel Messages

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