I'm trying to build a program that has a user guess 4 random pegs in order. it needs to use arrays and methods and i'm struggling mightly... here are some parameters


The structures must be declared as variables in the main() method.

You must write your program so that the number of digits in the random number can be changed. When you submit your answer, there should be 4 digits, but this should be easily changed in the program. Similarly, the number of guesses that the user can make should be defined and be easily changed.

You must not declare any variables outside of your methods, only constant values.

Your program must contain a method called chooseRandomNumber(). The method takes the structure holding the random number as its only argument. The purpose of the method is to fill the number with random digits in the range 0 to 9.

Your program must contain a method called getGuess(). The method takes two arguments, the structure holding the user's guess and the current guess number (i.e. 1st guess, 2nd guess etc). The method will prompt the user to enter a guess and read in the digits of the guess. If the user enters an invalid input, the method will print out an error method and loop back to allow the user to enter the guess again. The method must only return when the structure has been filled with digits.

Your program must contain a method called countBlackPegs(). This takes two arguments, the structure holding the random number and the structure holding the user's guess. The method will return an integer which is the count of the number of digits in the guess that are in the same position as the random number.

Your program must contain a method called countWhitePegs(). This takes two arguments, the structure holding the random number and the structure holding the user's guess. The method will return an integer which is the count of the number of digits in the guess that are in the random number but not in the correct position.

Your main() method will call chooseRandomNumber() to initialise the number chosen by the computer. Then the method will loop. Inside the loop, main() will:

call getGuess() to get the user's move,
call countBlackPegs() and countWhitePegs() to find out how close the user's guess was to the random number.
print out the number of black and white pegs that the user scored with the guess.

The looping will continue until the human guesses the number within the allowed number of guesses, or until the allowed number of guesses has finished. The computer will
print out if the user guessed the number, or not.

Only the getGuess() method can get input from the user. Only the main() and getGuess()
methods can output to the user.

I'm trying to build a program that has a user guess 4 random pegs in order. it needs to use arrays and methods and i'm struggling mightly... here are some parameters


The structures must be declared as variables in the main() method.

You must write your program so that the number of digits in the random number can be changed. When you submit your answer, there should be 4 digits, but this should be easily changed in the program. Similarly, the number of guesses that the user can make should be defined and be easily changed.

You must not declare any variables outside of your methods, only constant values.

Your program must contain a method called chooseRandomNumber(). The method takes the structure holding the random number as its only argument. The purpose of the method is to fill the number with random digits in the range 0 to 9.

Your program must contain a method called getGuess(). The method takes two arguments, the structure holding the user's guess and the current guess number (i.e. 1st guess, 2nd guess etc). The method will prompt the user to enter a guess and read in the digits of the guess. If the user enters an invalid input, the method will print out an error method and loop back to allow the user to enter the guess again. The method must only return when the structure has been filled with digits.

Your program must contain a method called countBlackPegs(). This takes two arguments, the structure holding the random number and the structure holding the user's guess. The method will return an integer which is the count of the number of digits in the guess that are in the same position as the random number.

Your program must contain a method called countWhitePegs(). This takes two arguments, the structure holding the random number and the structure holding the user's guess. The method will return an integer which is the count of the number of digits in the guess that are in the random number but not in the correct position.

Your main() method will call chooseRandomNumber() to initialise the number chosen by the computer. Then the method will loop. Inside the loop, main() will:

call getGuess() to get the user's move,
call countBlackPegs() and countWhitePegs() to find out how close the user's guess was to the random number.
print out the number of black and white pegs that the user scored with the guess.

The looping will continue until the human guesses the number within the allowed number of guesses, or until the allowed number of guesses has finished. The computer will
print out if the user guessed the number, or not.

Only the getGuess() method can get input from the user. Only the main() and getGuess()
methods can output to the user.

what have you coded so far?

import java.util.*;
public class Pegs
{

    static final int HIGH=9;
    static final int LOW= 0;
    static Scanner scan= new Scanner(System.in);
    public static void main()
    {
        boolean userwins= false;
        int [] peglist= new int[4];

        System.out.print("Welcome to the game of Mastermind. You need to guess\n");
        System.out.print("a 4-digit number, but you only get 10 guesses. After\n");
        System.out.print("each guess, I will tell you the number of black and\n");
        System.out.println("white pegs you scored.");
        chooseRandomNumber(peglist);
        
        for(int guess=1; guess<11; guess++)
        {
            System.out.println("Guess #" + guess+ ": " );
        }

    }
    
    public static int randomIntBetween(int low, int high)
    {
        return( (int) Math.round (Math.random()));
    }
    public static int chooseRandomNumber(int [] peglist)
    {
       for(int i=0; i<peglist.length; i++)
       peglist[i]=(randomIntBetween(LOW, HIGH));
    }
This article has been dead for over six months. Start a new discussion instead.