Hello, i am having with my mastermind game. I try to display black and white pegs but it does not work. Right now i gave up on black pegs and i am trying whitepegs.

import java.io.*;
  
 import java.io.*;
  
  public class Mastermind
{
  int  a = 0;
  int p = 0;
  int x = 0;
  int gen1 = 0;
  int gen2 = 0;
  int gen3 = 0;
  int gen4 = 0;
  int gen  = 0;
  int black = 0;
  String s = "";
   int[] getinput = new int[4];
   int[] getnumbers = new int[4];
  InputStreamReader isr = new InputStreamReader(System.in);
   BufferedReader br = new BufferedReader(isr);
   // the generated code
   public void generate()
   {
   for (int counter = 0; counter < 4; counter ++)
   {
     double number = 0;
    number = Math.random();
    System.out.println();
    int dice = 0;
    dice = (int)(Math.random() * 4) + 1;
    System.out.print(dice);
    getnumbers[p] = dice;
    
   }
   }
   //end of generated code
   public void input() 
   {
     try
    {   
       String s = "";
            System.out.println("Enter 4 colours:");
            //get names from user
            //store in array 'getnames'
            do{
                InputStreamReader isr = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(isr);
                s = br.readLine();               
                 Integer.parseInt(s); //takes away the quotations
                getinput[x] = x;                
                x++;
               
            }while(x < 4);    
    }
   catch (IOException ex)
     {
       ex.printStackTrace();
     }
   }
  
   
   public int pegs()
  {
     if(getinput[x]==getnumbers[x])
    {
      black++;
 
     }         
    return black;
   }
   
  
  public void print()
  {
    System.out.println(black);
  }
  }

And my runner

public class Mastermindrunner
{
  public static void main (String[] args) 
  {
   Mastermind black = new Mastermind();
  black.generate();
    black.input(); 
  black.pegs();
black.pegs();



  
  }
  
  
}

Recommended Answers

All 2 Replies

does anyone see my problem? or do i have to somehow improve my question so it you guys can help me? Im not familiar with the forums :/

Ouf.. a lot of useless variables
a lot of useless temporary variables
use a Scanner a lot easier then a InputReader that you re-initialize for nothing
Random object is better for integer value
And please indent your code correctly, it is a pain

After a few cleanup.... I guess it should work

import java.util.*; // 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.

	Random ran = new Random();
	Scanner scan = new Scanner(System.in);

	// 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
		{
			getnumbers[counter] = ran.nextInt(4) + 1;
			System.out.println("getnumbers[" + counter + "] = " + getnumbers[counter]);
			//			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;       <---- surely not [x] but counter

		}
	}
	//end of generated code
	public void input() 
	{
		System.out.println();
		for(int i = 0; i < getinput.length; ++i) {
			System.out.print("Enter number " + (i+1) + ": ");
			getinput[i] = scan.nextInt();
		}

		//		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 void pegs()
	{
		for(int counter = 0; counter < 4 ; counter ++)  {
			if(getinput[counter] == getnumbers[counter])	{	
				black++;
				System.out.println("getinput[ " + counter + "] == getnumbers[" + counter + "] so black = " + black);
			}

		}         
	}
	public void print() // the method that prints out the black and white pegs
	{
		System.out.println("number of black pegs :" + black);
	}

	public static void main (String[] args) 
	{
		Mastermind black = new Mastermind();
		black.generate();
		black.input(); 
		black.pegs();
		black.print();	  
	}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.