Results 1 to 6 of 6
- 01-14-2015, 08:14 PM #1
Member
- Join Date
- Jan 2015
- Posts
- 4
- Rep Power
- 0
Mastermind comparision event help
Hi all,
I'm not so new to java i know the basics. I want to make a mastermind game. Just for training purpose.
Now i am at the comparision phase where it is comparing your chosen "colors" with the actual "color code"
This is my code for that part:
guesChars is a char array of 4 of the acual code
readGuess is a string of 4 of which the players has filled in the console
goodGuess is a char at the right position with the right "color"
avgGues is a char with the right "color" at the wrong position
Java Code:for(int i = 0; i < guessChars.length; i++){ for(int ii = 0; ii < readGuess.length(); ii++){ if(readGuess.charAt(ii) == guessChars[i]){ if(ii == i){ goodGuess = goodGuess + 1; avgGuess = avgGuess - 1; } if( ii != i){ avgGuess = avgGuess -1; } } } } for(int ii = 0; ii < readGuess.length(); ii++){ if(String.valueOf(guessChars).toString().contains("" + readGuess.charAt(ii))){ avgGuess = avgGuess + 1; } }
ACDD
and the player guess is
ADDD
Result is
2 right spot right color
1 right color wrong spot
it have to be
2 right spot right color
0 right color wrong spot
- 01-14-2015, 08:50 PM #2
Re: Mastermind comparision event help
Can you make a small, complete program that compiles, executes and shows the problem?
Without user input. Just hardcode the values where the fails.If you don't understand my response, don't ignore it, ask a question.
- 01-14-2015, 09:45 PM #3
Member
- Join Date
- Jan 2015
- Posts
- 4
- Rep Power
- 0
Re: Mastermind comparision event help
I know where the fail is i explained it. I just don't know the solution i've tried various things to solve it.
i'll show you the result:
Java Code:Welcome to the game Mastermind I've got a secret code on my mind. Can you guess it DDBC <<-- this is the secret code DDDA << this is what the player types Enter your guess:DDDA You've got 2 letters exactly right You've got 1 letters right but in the wrong spot <<-- this should be 0 actually *** Mastermind Board *** 1: DDDA 2 1 ************************
- 01-14-2015, 09:56 PM #4
Re: Mastermind comparision event help
The point is that it's very hard for us to debug your program if we can't run the code. It's also very hard to debug your program if you don't narrow it down to the smallest example possible (in other words, not your whole program).
If you don't feel like posting an MCVE, then you'll have to debug your code yourself. You do this by running this code through a debugger, or at least adding print statements, or simply running through the program with a piece of paper and a pencil to record variables.
The goal is to figure out when the program's execution differs from your expectations. What exact line does it occur on? What are the values of every variable on that line?How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 01-15-2015, 10:28 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Mastermind comparision event help
You need two passes, which you have.
The first one finds all the entries in the guess that match exactly, and somehow marks those values that match as handled in both arrays (probably another array for each one, a boolean one).
The second pass looks at the remaining entries and sees if they are the right colour but wrong position against the remaining entries in the secret code.
At the moment your first pass makes no note of which ones are accurate guesses, and reuses those secret codes that have already been match exactly.Please do not ask for code as refusal often offends.
** This space for rent **
- 01-15-2015, 08:20 PM #6
Member
- Join Date
- Jan 2015
- Posts
- 4
- Rep Power
- 0
Re: Mastermind comparision event help
Thats not the problem because avgGuess gets minus one when its getting an exact match. The second loop will give it a plus 1 back again which makes it 0 for all exact matches at avgGuess.
I found the solution to it finally:
Java Code:for(int i = 0; i < guessChars.length; i++){ for(int ii = 0; ii < readGuess.length(); ii++){ if(readGuess.charAt(ii) == guessChars[i]){ if(ii == i){ goodGuess = goodGuess + 1; avgGuess = avgGuess -1; } } } } for(int i = 0; i < guessChars.length; i++){ for(int ii = 0; ii < readGuess.length(); ii++){ if(readGuess.charAt(ii) == guessChars[i]){ if(ii != i){ avgGuess = avgGuess + 1; break; } } } } if(avgGuess < 0){ avgGuess = 0; }
When avgGuess is under zero it will be zero.
Thanks for helping guys.
Similar Threads
-
Resultset comparision method
By EADS in forum Advanced JavaReplies: 0Last Post: 07-27-2011, 12:51 PM -
String Comparision
By vimalaranjan in forum New To JavaReplies: 6Last Post: 05-19-2011, 03:27 PM -
Secret of String references comparision.
By Anjaneyulu in forum Advanced JavaReplies: 15Last Post: 02-24-2010, 03:32 PM -
String comparision method problem
By rons_sacramental in forum New To JavaReplies: 7Last Post: 10-15-2009, 06:15 AM -
comparision between two lists
By suprabha in forum Advanced JavaReplies: 14Last Post: 08-01-2008, 03:49 PM
Bookmarks