Jump to content

Mastermind computerguess, right guess doesn't show in console

doestilvoort

Hello,

I am trying to make a Mastermind game where i can play against the computer, but the piece of code doesn't keep track if the exact spot is right or not ( the blackpins ) it only keeps track of the ( white pins ) where that means 2 colours are right but in the wrong spot.

 

for example if i give a secretcode for the computer to guess like: AAAA

 

this is what it will show me in the console.

 

Computer guessed: ABDA
The computer 0 exactly right
The computer 2 but in the wrong place

 

It should say that the computer has guessed exactly 2 right but it doesn't and i can't understand why because this works great when i am the one who is trying to guess.

Link to comment
Share on other sites

Link to post
Share on other sites

Maybe it is because you're comparing computerGuess against secretCodeEnteredByUser when comes to blackpins, and when you compare for whitepins you compare computerGuess against secretCode. Check if secretCodeEntredByUser is same as secretCode. Also that if statement that checks if secretCodeEntredByUser is not empty is weird there, it is not like it changes inside loop so it could be checked outside just once, not every loop iteration, and if so I would check if its length i equal to the string you compare it against. But for now, figure out what you want to compare against, it may turn out that the check is checking wrong variable anyway.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Mr_KoKa said:

Maybe it is because you're comparing computerGuess against secretCodeEnteredByUser when comes to blackpins, and when you compare for whitepins you compare computerGuess against secretCode. Check if secretCodeEntredByUser is same as secretCode. Also that if statement that checks if secretCodeEntredByUser is not empty is weird there, it is not like it changes inside loop so it could be checked outside just once, not every loop iteration, and if so I would check if its length i equal to the string you compare it against. But for now, figure out what you want to compare against, it may turn out that the check is checking wrong variable anyway.

well that was akward that i didin't saw that, al i had to do in that if statement was to change secretCodeenterdByuser change to secretCode and now its working! thank you very much.

 

Also another question, is it possible too make the computer learn that when he has for example 2 exactly right he would guess from there, because now its just random, it doesn't remember that he had 1-2-3 right for example.

Link to comment
Share on other sites

Link to post
Share on other sites

It is possible, but it is complicated. When you receiver that 2 letters are correct  then you still don't know which one are correct, you only know that some of 4 are. So only when you change one of those to make it incorrect you will know that it was the one that was correct, or you will change other 2 that was incorrect anyway it would mean that you happened to change only two that was incorrect and the rest are correct. So you see it is not that easy. You would need to remember your last score and previous guesses to not make the same guess second time. Then the easiest way would be changing every letter when you don't have any of blackpins. When you have blackpin you can change only one letter until you know where the blackpin is. then you can go back to change every other but blackpinned letters, if you have white pins you can try to try every permutation of whitepins. I have described rough what is probably not the best way to solve it.

Link to comment
Share on other sites

Link to post
Share on other sites

On 1-4-2017 at 4:20 PM, Mr_KoKa said:

It is possible, but it is complicated. When you receiver that 2 letters are correct  then you still don't know which one are correct, you only know that some of 4 are. So only when you change one of those to make it incorrect you will know that it was the one that was correct, or you will change other 2 that was incorrect anyway it would mean that you happened to change only two that was incorrect and the rest are correct. So you see it is not that easy. You would need to remember your last score and previous guesses to not make the same guess second time. Then the easiest way would be changing every letter when you don't have any of blackpins. When you have blackpin you can change only one letter until you know where the blackpin is. then you can go back to change every other but blackpinned letters, if you have white pins you can try to try every permutation of whitepins. I have described rough what is probably not the best way to solve it.

ah oke, well guess thats too difficult, i changed the gameplay a bit in a way that now i have to tell the computer how many answers he has right but it doesn't save that example:

 

Enter a secret code, so the computer can guess it AAFA
Computer guessed: AABB
How many colours are exactly right? 2
How many colours are right but in the wrong place? 0


*** Mastermind Board ***
      1: AABB 0 0
************************

 

Link to comment
Share on other sites

Link to post
Share on other sites

It was done already, your blackpins number was how many of them was correct. When you play mastermind you don't know the password, so if your answer is "AABB" and you know that two colors are correct at correct place then you still don't know it they're AA or BB.

Link to comment
Share on other sites

Link to post
Share on other sites

20 hours ago, Mr_KoKa said:

It was done already, your blackpins number was how many of them was correct. When you play mastermind you don't know the password, so if your answer is "AABB" and you know that two colors are correct at correct place then you still don't know it they're AA or BB.

That is correct,  but i need some sort of algorithm so that the program locks 2 right letters in that place so it can uses the remaining 2 spots for others letters, if you know what i mean.

Link to comment
Share on other sites

Link to post
Share on other sites

I know, I just telling you that you're trying to do what you have already done. But I wont be too much help as I never played mastermind and I seem to be bad at this ;].

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×