Results 1 to 6 of 6
  1. #1
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Counter not working?

    I have separated characters into an array that can be accessed from any method, however, when I try to compare the inputed guess to the computers random sequence, the counter always comes back as 0 (the comparison occurs in the checkColour method). Can someone help me?

    Java Code:
    package mastermind;
    import java.util.Scanner;
    /**
     * Name: Ben Hughes
     * Date: Saturday, May 24th, 2014   
     * Teacher's Name: Mr. Montgomery
     * Course Code: ICS3U
     * Program Description: play a game of mastermind versus the computer
     */
    public class Mastermind {
        private static int block1, block2, block3;
        private static String blockColour1, blockColour2, blockColour3;
        private static char inputChar[] = new char[3]; 
        private static char computerChar[] = new char[3];
        
        public static void main(String[] args) {
            
    //Output information        
            System.out.println("Guess the Blocks\n================");
            System.out.println("In this game, there are 4 different coloured blocks (red, yellow, green, blue)."
                    + "\nThe computer hides 3 coloured blocks from you. You then try to"
                    + "\nguess the colour and the order of the blocks.");
            
    //Call method to get random computer guess        
            String computerGuess = newGame();
            
    //Get user input        
            System.out.println("The computer has created the sequence!\n\nEnter your first guess: ");
            Scanner myInput = new Scanner(System.in);
            String input = myInput.nextLine();
            
    //Seperate into different characters        
            
            for(int i = 0; i < 2; i++){
                inputChar[i] = input.charAt(i);
            }
            
    //Seperate computer guess into different charcters
            
            for(int a = 0; a < 2; a++){
                computerChar[a] = computerGuess.charAt(a);
            }
            
    //Call method to check colours        
            int colourCorrect = checkColour(computerChar, inputChar);
            System.out.println(computerGuess);
            System.out.println(colourCorrect);
    //Call method to check position
            checkPosition(computerChar, inputChar, colourCorrect);
            
        }
    
        public static String newGame(){
            block1 = randomWholeNumber();
            block2 = randomWholeNumber();
            block3 = randomWholeNumber();
            
            if(block1 == 1){
                blockColour1 = "R";
            }
            if(block1 == 2){
                blockColour1 = "Y";
            }
            if(block1 == 3){
                blockColour1 = "G";
            }
            if(block1 == 4){
                blockColour1 = "B";
            }
            if(block2 == 1){
                blockColour2 = "R";
            }
            if(block2 == 2){
                blockColour2 = "Y";
            }
            if(block2 == 3){
                blockColour2 = "G";
            }
            if(block2 == 4){
                blockColour2 = "B";
            }
            if(block3 == 1){
                blockColour3 = "R";
            }
            if(block3 == 2){
                blockColour3 = "Y";
            }
            if(block3 == 3){
                blockColour3 = "G";
            }
            if(block3 == 4){
                blockColour3 = "B";
            }
            
            String computerGuess = blockColour1 + blockColour2 + blockColour3;
            return computerGuess;
        }
    
        public static int randomWholeNumber(){
            int randomNumber = (int)((Math.random() * 4) + 1);
            return randomNumber;
        }    
    
        public static int checkColour(char computerChar[], char inputChar[]){
            
            int colourCorrect = 0;
            int i = 0;
            for(i = 0; i == 2; i++){
                if(inputChar[0] == computerChar[i]){
                    colourCorrect++;
                }
            }
            for(i = 0; i == 2; i++){
                if(inputChar[1] == computerChar[i]){
                    colourCorrect++;
                }
            }
            for(i = 0; i == 2; i++){
                if(inputChar[2] == computerChar[i]){
                    colourCorrect++;
                }
            }
            return colourCorrect;
        }
    
        public static void checkPosition(char computerChar[], char inputChar[], int colourCorrect){
    
            int positionCorrect = 0;
            int i = 0;
    
        }
    }
    The counter is called colourCorrect.. But I think the problem lies more in me converting the strings (input and computerGuess) into char arrays, as if I output the values at position in the array, I get numbers (but the numbers are not character values from the ascii table).
    Last edited by madtaffy; 05-27-2014 at 05:15 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Counter not working?

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    What is the variable name of the counter?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Counter not working?

    Try debugging the code by printing out the values that are being compared in the if statements to see what the computer sees when the code is executed. The print out should help you understand what the program is doing.

    The Arrays class's toString() method is useful for formatting arrays for printing:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayNameHere));
    Last edited by Norm; 05-27-2014 at 05:24 AM.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    madtaffy is offline Member
    Join Date
    May 2014
    Posts
    16
    Rep Power
    0

    Default Re: Counter not working?

    If I input a dummy guess, like RYB, and check the values in the array:

    System.out.println(inputChar[0] + inputerChar[1] + inputChar[2]);

    I get 171... I can enter the same guess and the values will be constant, but I have no idea where they are coming from.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Counter not working?

    The Arrays class's toString() method is useful for formatting arrays for printing:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayNameHere));


    Why are theere hardcoded values 2 in the for statements? The for statements should use the length of the arrays to control their looping? The length of the Strings should be tested to be sure they are the same length as the array.

    Why pull the char from the Strings into arrays? The charAt() method allows the code to get at each of the char the same as when it is in the array.

    I get 171
    Copy this into your code and execute it:
    System.out.println('a' + 'b' + " " +(int)'a' + " " +(int)'b');
    Look at what is printed: the first number is the sum of 'a' and 'b', the next two show the values of 'a' and 'b'
    char are converted to int and added.
    Last edited by Norm; 05-27-2014 at 05:32 AM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Counter not working?

    Hi,
    Your loops does not make sense to me. They should not be executed:
    for(i = 0; i == 2; i++)
    So i is set to 0
    Then it id checked: i==2 which is false.
    So the loops are never executed.

Similar Threads

  1. Counter problem
    By Razpet22 in forum NetBeans
    Replies: 5
    Last Post: 06-14-2011, 09:27 PM
  2. switch (counter) example please
    By erin.ctm in forum New To Java
    Replies: 1
    Last Post: 12-04-2010, 12:21 AM
  3. Increment Counter for ID
    By blkshrk81 in forum New To Java
    Replies: 6
    Last Post: 12-01-2010, 02:25 AM
  4. Counter
    By ks1615 in forum New To Java
    Replies: 6
    Last Post: 02-20-2009, 04:02 AM

Posting Permissions

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