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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Please Please Please Please help with my Program!!!!!!!!!

    Hello, i am desprate for help. I need help with my Java mastermind game. When a user enters 4 numbers my program checks if he got it right. If he got 1 right then my program displays " 1 black peg" if he got 3 right my program says " blacks pegs" i need to make this using methods and arrays.

    This is my code:

    Code:
    import java.io.*; // imports java package
      
      public class Mastermind // the class mastermind
    {
      int p = 0; // the variable that the user enter his data in
      int x = 0; 
      int z = 0;
      int n = 0; // a counter variable
      int black = 0; // the variable that holds the number of black pegs
      String s = ""; // user input
       int[] getinput = new int[4]; // the size of the array and decleration.
       int[] getnumbers = new int[4];  // the size of the array and decleration.
      InputStreamReader isr = new InputStreamReader(System.in);
       BufferedReader br = new BufferedReader(isr);
       // the generated code
       public void generate() // the method generates the random numbers
       {
       for (int counter = 0; counter < 4; counter ++) // this method will generate until four numbers have been generated
       {
         double number = 0;
        number = Math.random();
        System.out.println(); 
        int dice = 0;
        dice = (int)(Math.random() * 4) + 1;
        System.out.print(dice);
        getnumbers[x] = dice;
        
       }
       }
       //end of generated code
       public void input() 
       {
         try
        {   
           String s = "";
                System.out.println("Enter 4 numbers:");
                //get numbers from user
                //store in array 'getinput'
                do{
                    InputStreamReader isr = new InputStreamReader(System.in);
                    BufferedReader br = new BufferedReader(isr);
                    s = br.readLine();               
                       int p = Integer.parseInt(s); //takes away the quotations
                    getinput[z] = p;                
                    n++;
                   
                }while(n < 4);    
        }
       catch (IOException ex)
         {
           ex.printStackTrace();
         }
       }
      
       public int pegs()
      {
         for(int counter = 0; counter < 4 ; counter ++)  
         if(getinput[z] == getnumbers[x])
        {
          black++;
           
           }         
        return black; 
       }
                
      public void print() // the method that prints out the black and white pegs
      {
         System.out.println("number of black pegs :" + black);
      }
      }
    And my runner:
    Code:
    public class Mastermindrunner
    {
      public static void main (String[] args) 
      {
       Mastermind black = new Mastermind();
      black.generate();
        black.input(); 
      black.pegs();
      black.print();
    
      
      }
        
    }
    Last edited by kiddies; 12-17-2010 at 02:37 AM.

  • #2
    Regular Coder
    Join Date
    Feb 2008
    Location
    Edinburgh - Scotland
    Posts
    107
    Thanks
    0
    Thanked 12 Times in 12 Posts
    So You have described what you need to do - dumped some code and left...

    You need to explain what it IS doing and what you WANT it to do


  •  

    Posting Permissions

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