1

I'm trying to write a java code with(bingo game),(bullseye game) the rules are simple:

  • computer picks 4 numbers

  • user input 4 numbers

  • must check the user input is between 1 to 10

  • If the user input exists in the computer randomized numbers it will be 1 bulls

  • If a number exist in the same location of the computer randomized number it will show 1 "eye"

Max limit is 20 tries until the user is considered "failed"; I need to print each round how many bulls were and how many eye were by the user input;

Example:

if the pc randomizing 1 4 6 7
and the user type 3 4 1 7 
the output will be 3 bulls and 2 eyes.

my code prints 0 and 0 at the end.

Thanks for the help!

Here is my code:

import java.util.Random;
import java.util.Scanner;

public class ArraysEx1 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Random r = new Random();
        int[] pcGuess = new int[4];
        int[] playerGuess = new int[4];
        int countGuess = 0, bulls = 0, eye = 0;
        final int maxGuess = 20;
        System.out.println("Please press enter to begin");
        in.nextLine();
        boolean areNumbersCorrect = true; // a boolean value to define if the user input are correct (values between 1 to 10)
        for (; countGuess < maxGuess; countGuess++) {
            System.out.println("Please enter 4 numbers between 1-10");
            for (int i = 0; i < playerGuess.length; i++) {
                playerGuess[i] = in.nextInt();
                pcGuess[i] = r.nextInt(10)+1;
                if (playerGuess[i] < 0 || playerGuess[i] > 10) { // an if statement to check if the user input are correct
                    areNumbersCorrect = false;
                    do {                                            // do while loop if the boolean is not true. (force the user to enter correct values)
                        System.out.println("Please try again");
                        for (int j = 0; j < playerGuess.length; j++) {
                            playerGuess[j] = in.nextInt();
                            if (playerGuess[j] > 0 && playerGuess[j] < 10) {
                                areNumbersCorrect = true;
                                continue;
                            }
                        }
                    } while (!areNumbersCorrect); // end of do while loop
                }
                for (int j=pcGuess.length; j>0; j--) { // for loop to check each number from the user and computer.
                    if (playerGuess[i] == pcGuess[i]) {
                        eye++;                             // if the user number exist in the same location evaluate eye++
                        if (playerGuess[i]%pcGuess[j]== 0) {
                            bulls++;                        // if the user number exist in the randomized number but not in the same location evaluate bulls++
                        }
                    }
                }
                System.out.println(
                        eye+" Hits!(same pc number and location)"+" And: "+bulls+" Numbers exist");
            } if(eye==4&&bulls==4) {
                break;
            }

        }
        System.out.println("It took you: "+countGuess+" Times to guess the numbers");
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tal Ouzan
  • 75
  • 1
  • 5

1 Answers1

0
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ArraysEx1 {
    public static void main(String[] args) {
        ArrayList<Integer> randNumbers = new ArrayList<>();
        Random random = new Random();
        String input;
        int number;
        Scanner sc = new Scanner(System.in);
        do {
            System.out.println("Please press enter to begin");
            input = sc.nextLine();
        }while (!input.equals(""));//loop while user doesn't press ENTER

        for (int i = 0; i < 4; i++){
            randNumbers.add(random.nextInt(10) + 1);//loop to fill the randNumbers arraylist with random numbers
        }
        /*
        randNumbers.add(3);
        randNumbers.add(2);
        randNumbers.add(9);
        randNumbers.add(9);

         */
        System.out.println("My random numbers: " + randNumbers.toString());

        int counter = 0;
        int bulls = 0;
        int eyes = 0;
        do {
            System.out.println("Please enter 4 numbers between 1-10");
            number = sc.nextInt();
            if (number > 0 && number <= 10){
                //System.out.println("index of rand: " + randNumbers.indexOf(number));
                //System.out.println("count: " + counter);
                if (randNumbers.indexOf(number) == counter){
                    eyes++;
                    System.out.println("eyes++");
                }else if (randNumbers.contains(number)){
                    bulls++;
                    System.out.println("bulls++");
                }
                counter++;
                System.out.println("Number " + counter + " introduced. " + (4 - counter) + " more to go.");
            }else {
                System.out.println("Wrong number.");
            }
        }while (counter < 4);//loop for user to introduce valid numbers

        System.out.println("You scored " + bulls + " bulls and " + eyes + " eyes.");
    }
}

Try this piece of code. Note that I used ArrayList rather than an array, as it offers methods such as .contains() and .indexof().

WARNING: Code will fail if the randNumbers arraylist contains two numbers that are equals, such as the 3-2-9-9 array that is commented when you input 9-9-9-9 as your number guesses. This is because .indexof() method:

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Meaning the code fails to account the last 9 as it will compare the count index (3) to the first occurrence of 9 in the randNumbers, which is 2, making it false and not increasing eyes count.

Since I'm short on time, and this is an assigment you have and just copying it won't teach you much, I'll leave it to you to solve this specific case (I already told you what's wrong, won't be difficult to solve).

afarre
  • 512
  • 6
  • 18