Results 1 to 6 of 6
  1. #1
    predricted is offline Member
    Join Date
    Jan 2015
    Posts
    4
    Rep Power
    0

    Default 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;
    				}
    			}
    The problem is when the code is for example

    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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.

  3. #3
    predricted is offline Member
    Join Date
    Jan 2015
    Posts
    4
    Rep Power
    0

    Default Re: Mastermind comparision event help

    Quote Originally Posted by Norm View Post
    Can you make a small, complete program that compiles, executes and shows the problem?
    Without user input. Just hardcode the values where the fails.
    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
    
    ************************

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default 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!

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default 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 **

  6. #6
    predricted is offline Member
    Join Date
    Jan 2015
    Posts
    4
    Rep Power
    0

    Default Re: Mastermind comparision event help

    Quote Originally Posted by Tolls View Post
    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.
    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;
    			}
    just needed to make a second loop with a break when it finds a match where the positions are not equal.
    When avgGuess is under zero it will be zero.


    Thanks for helping guys.

Similar Threads

  1. Resultset comparision method
    By EADS in forum Advanced Java
    Replies: 0
    Last Post: 07-27-2011, 11:51 AM
  2. String Comparision
    By vimalaranjan in forum New To Java
    Replies: 6
    Last Post: 05-19-2011, 02:27 PM
  3. Secret of String references comparision.
    By Anjaneyulu in forum Advanced Java
    Replies: 15
    Last Post: 02-24-2010, 02:32 PM
  4. String comparision method problem
    By rons_sacramental in forum New To Java
    Replies: 7
    Last Post: 10-15-2009, 05:15 AM
  5. comparision between two lists
    By suprabha in forum Advanced Java
    Replies: 14
    Last Post: 08-01-2008, 02:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •