Click here to Skip to main content
14,120,859 members
Rate this:
 
Please Sign up or sign in to vote.
I'm making a mastermind game (the one where you gotta guess the password and it tells you how many of the codes you got right when you tried). BUUUUUT, I'm doing mine with a single word, and not multiple words or colors

example:
answer = "this"
given = "that"

print(str(answer - given) + "/4 correct")
>>> "2/4 correct"

I need to find a way to find the number of characters the variables have this way based on the order they're in.

Here's the original program the code is trying to run in, in case it is needed:
https://repl.it/@BeardedPC/recreation

What I have tried:

I've tried using:

crct = len([i for i, j in zip(a,b) if i == j])
      gmtxt("> Entry denied \n> "+str(crct)+"/4 correct.")

but it either comes up as "0" or error.

I've also tried turning the variables into lists, and that seems to get me the closest, but it still doesn't work as I had hoped.
Posted
Updated 7-Dec-18 4:11am
Comments
Richard MacCutchan 7-Dec-18 10:46am
   
I just tried your first statement and it correctly reports the answer as 2. What is gmtxt that you are using to try to display the result?
BeardedPC 7-Dec-18 11:33am
   
please check the original code to if you have questions about variables
Richard MacCutchan 7-Dec-18 11:48am
   
I don't have questions about variables. Read my comment again: your code gives the answer 2, which is correct. I do not know what gmtxt is, or what it is supposed to do.
BeardedPC 7-Dec-18 11:50am
   
In the question, I posted a link to the original program, you can find the definition for all the variables there

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

First off, mastermind has two different scores: Black and White.
Black is where a letter is correct, and in the right place.
White is where a letter is correct, but in the wrong place.
So "this" and "that" would be 2 Black, 0 White.
And "this" and "here" would be 0 Black, 1 White.
So do this in two stages:
1) Match each input character by character.
1.1) If they are the same, add one to your Blacks count
1.2) Otherwise, copy the letters to output arrays.
1.3) After the loop, check the Blacks count. If it matches the length, he's won.
2) Sort both output arrays. (This makes life a lot easier)
2.1) Compare the two output arrays character by character, usine two indexes.
2.2) If they are the same, add one to your Whites count, and increment both indexes.
2.3) Otherwise, ignore the lowest, and increment that index.
2.4) After the loop, print the Black and White counts.

Try it - it should be pretty simple!
   
Comments
BeardedPC 7-Dec-18 10:16am
   
@OriginalGriff I am aware of this, but I'm only focusing on the correct letters in the correct place. This does not help me, I need a coding example of something to get me close to my goal.
OriginalGriff 7-Dec-18 10:31am
   
Then just use stage one ...
BeardedPC 7-Dec-18 10:50am
   
I need to know what the code for that would be!
OriginalGriff 7-Dec-18 10:56am
   
No, you'd like to be given the code, so you can hand it in as your own work.
What you *need* is to write it yourself - we don't do your homework for you: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190518.1 | Last Updated 7 Dec 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100