Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Method Calling Issues/Compiler Issues

    My program will prompt the user to enter N or Y, this determines if the user wants to create a Mastermind 4 color code, or if they would like to solve a computer generated code. If the user inputs "Y" then I have called the Input method which asks the user to input the code. After that i have print lines which displays the code that has been taken by the program then my program abruptly stops there. Now my compiler problem.. If the user were to enter "N" then my randomize method is called, and in my screen output I have a weird error only when I run it but it compiles fine. If anyone would be able to help me out here and work over it/give me some tips I'd be greatly appreciated. Here is the code:

    MAIN:

    Code:
    import java.io.*;
    
    public class MastermindRunTest
    {
      public static void main (String[] args)//main kickstart method
      {
        Mastermind callMethod = new Mastermind();//method caller declared, named Mastermind 
        
        int loop = 0;//loop declared as an int, sets it to a value of 0
        int input = 0;//handles an input from the user, sets it to 0
        int inputMore = 0;//runs the integer method, sets it to 0
        String ynInput = "";//declares a yes/no string
        String userinput1 = "";//declares a string as userinput2
        int userinput = 0;//declares integer as userinput, sets it to 0
        try
        {
        InputStreamReader isr = new InputStreamReader(System.in);//detects keyboard strokes that the user inputs
        BufferedReader br = new BufferedReader(isr);//declares a buffered reader which tells the program which key strokes that the user inputs
    
        do
        {
          System.out.println("Would you like to play against the CPU or have the CPU play against you? Enter 'Y' to play against the CPU, enter 'N' for the CPU to play against you.");//intro message to user
          ynInput = br.readLine();
          
          if((ynInput.equals("Y")))
          {   
            callMethod.input();//calls the input method if the user chooses Y     
          }
          else
          {
            callMethod.randomize();//calls the randomize method if the user chooses N
                  }
        }
        while(loop == 1);//infinite loop
        }
        catch(IOException ex)
        {
          ex.printStackTrace();
        }
      }
    }


    MAIN ENDS





    Code:
    import java.io.*;
    
    public class Mastermind//name of the class
    {
      private String[] colour = {"", "", "", "", "", ""};//declares an empty array for each different colour
      //p = purple, w = white, g = green, y = yellow, b = blue, 0 = orange     
      
      public int randomize()//randomize method
        //------------------------------------------MAKER-(UserMustGuessCode)--------------------------------------------
        
      {
        String stringRandom = "";//creates a string callsed stringrandom, makes it empty
        
        int counterStringRandom = 0;//declares counterStringRandom as an int, sets it to 0 
        int counterRandom = 0;//declares counterRandom as an int, sets it to 0
        int counter = 0;//declares counter as an int, sets it to a value of 0
        int counter2 = 0;//declares a second counter as an int, sets it to a value of 0
        int stringRandom1 = 0;//declares an integer, assigns it to a value of 0
        int randomnumber = 0;//declares an integer, random number and assigns it a value of 0
        
        
        do//do the following
        {
          randomnumber = (int)(Math.random() * 6) + 1;//creates a random number, gives it a maximum of 6 and a minimum of 1
          counter++;//adds 1 to the counter
          
          
          do//do the following
          {
            if(randomnumber == 1)//if the random number generated is equal to 1
            {
              stringRandom.equals("P");//make the stringRandom equal P
              counterStringRandom++;//adds one to the counter
            }
            else if(randomnumber == 2)//if the random number generated is equal to 2
            {
              stringRandom.equals("W");//make the stringRandom equal W
              counterStringRandom++;//adds one to the counter
            }
            else if(randomnumber == 3)//if the random number generated is equal to 3
            {
              stringRandom.equals("G");//make the stringRandom equal G
              counterStringRandom++;//adds one to the counter
            }      
            else if(randomnumber == 4)//if the random number generated is equal to 4
            {
              stringRandom.equals("Y");//make the stringRandom equal Y
              counterStringRandom++;//adds one to the counter
            }
            else if(randomnumber == 5)//if the random number generated is equal to 5
            {
              stringRandom.equals("B");//make the stringRandom equal B
              counterStringRandom++;//adds one to the counter
            }
            else//if all the above is false
            {
              stringRandom.equals("O");//make the stringRandom equal O
              counterStringRandom++;//adds one to the counter
            }
          }
          while(counterStringRandom < 5);//condition for do/while loop
          
        }
        while(counter < 4);//condition; while the counter is smaller than 5
        
        do
        {
          if(counter == 1)//if the counter is equal to 1
          {
            colour[0] = stringRandom;//array at position 0 is equal to the number generated by randomizer
            counterRandom++;
          }
          else if(counter == 2)//if the counter is equal to 2
          {
            colour[1] = stringRandom;//array at position 1 is equal to the number generated by randomizer
            counterRandom++;
          }
          else if(counter == 3)//if the coutner is equal to 3
          {
            colour[2] = stringRandom;//array at position 2 is equal to the number generated by randomizer
            counterRandom++;
          }
          else//if all of the above is false
          {
            colour[3] = stringRandom;//array at position 3 is equal to the number generated by randomizer
            counterRandom++;//adds 1 to counterRandom
          }
        }
        while(counterRandom < 4);//do the following while the counter is less than 5
        stringRandom1 = Integer.parseInt(stringRandom);
        return stringRandom1;
      }
      
      public int input()//method used to collect the user input  
        //----------------------------------------BREAKER-(ComputerMustGuessCode)---------------------------------------
        
      {    
        int userCounter = 0;    
        String input2 = "";
        int input = 0;
        String[] getColours = new String[4];
        int x = 0;
        
        System.out.println("Welcome to the Mastermind game!");
        System.out.println("----------------------------------------------");
        System.out.println("RULES OF MASTERMIND:");
        System.out.println("Please enter a colour in each of the four inputs listed below.");
        System.out.println("Enter 'P' for Purple, 'W' for White, 'G' for Green, 'Y' for Yellow, 'B' for Blue, and 'O' for Orange.");
        System.out.println("A WHITE peg will  be displayed if you have a right colour guessed.");
        System.out.println("A RED peg will be displayed if you do not have the colour correct.");
        System.out.println("You will only have 12 chances to guess the code generated by the computer.");
        System.out.println("----------------------------------------------");
        System.out.println("GOOD LUCK!");
        
        
        InputStreamReader reading = new InputStreamReader(System.in);//declares input stream reader whoch detects the user keystrokes
        BufferedReader read = new BufferedReader(reading);//buffered reader used to read the input stream reader
        
        try
        {
          
          do
          {
            input2 = read.readLine();//reads the input
            getColours[x] = input2;//places the 4 values into int x
            x++;//adds one to the counter
          }
          while(x < 4);
          
          System.out.print("\nThe colour code you have chosen is: " + colour[0] + " " + colour[1] + " " + colour[2] + " " + colour[3]);
          
          for(int y = 0; y < 4; y++)
          {
            System.out.println(getColours[y]);
          }
          
          
          
          
        }
        catch(IOException ex)//incase program crashes
        {
          ex.printStackTrace();
        }
        return input;//returns the input value back to it's original
      }
    }

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    You've stumbled into the javascript forum by mistake. PM a mod for a move to the Java forum.

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts

    Java is not JavaScript

    Java and JavaScript are entirely different languages. Java questions belong in the Java and JSP forum.

    Moving thread there.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    Could you provide some additional information to help make things easier for us to help you.
    1. The exact run-time error message that you receive
    2. If applicable, the line number (and the code on that line) associated with the error.

    A couple of quick notes based on my (very) quick scan of your code.
    In your main method you have a do-while-loop with the following condition: while(loop == 1);//infinite loop. However, I noticed that you declare loop = 0, and I don't see any code that changes it to 1. This may be the source of you program halting when you weren't expecting it to.

    In your mastermind class you do the following in a few places:
    stringRandom.equals("W");//make the stringRandom equal W
    etc.

    It appears (but I may be wrong) that you are trying to set the string "stringRandom" to W with this code. However, the code you use will only test if stringRandom equals W. If you want to assign it to W you'll need to do:
    stringRandom = "W";

    This may be a cause of your run-time error...

  • #5
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Gox View Post
    Could you provide some additional information to help make things easier for us to help you.
    1. The exact run-time error message that you receive
    2. If applicable, the line number (and the code on that line) associated with the error.

    A couple of quick notes based on my (very) quick scan of your code.
    In your main method you have a do-while-loop with the following condition: while(loop == 1);//infinite loop. However, I noticed that you declare loop = 0, and I don't see any code that changes it to 1. This may be the source of you program halting when you weren't expecting it to.

    In your mastermind class you do the following in a few places:
    stringRandom.equals("W");//make the stringRandom equal W
    etc.

    It appears (but I may be wrong) that you are trying to set the string "stringRandom" to W with this code. However, the code you use will only test if stringRandom equals W. If you want to assign it to W you'll need to do:
    stringRandom = "W";

    This may be a cause of your run-time error...
    Should i create a completely separate check method for my if statements?

  • #6
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My main objective here is when i'm trying to break the user generated code, i built the randomizer in order to have 6 different outcomes, which each outcome would be equal to the first letter of the colour.

    How will I be able to assign each randomly generated number to a different colour?

    In my code I have "randomnumber = "P", then "randomnumber = "O", but all that is doing is assigning the random number to P then to O. I want each colour chosen to be stored into a different string each time. Must I use arrays?

    I'm very stuck at this point as to what to do.

    If I have to use a different array for each randomly selected colour, what will my code be looking like. Please, I need some assistance.


  •  

    Posting Permissions

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