4
\$\begingroup\$

I have written some Java code for a school assignment. It is a game, and it works as follows:

The game makes 4 random numbers, all different, as the users ticket. It then generates 4 other random numbers, again all different, to compare it to. If they match, you win. If they don't match, then you lose.

A = random 0-9
B = random 0-9
C = random 0-9
D = random 0-9

A Cannot be equal to B / C / D
B Cannot be equal to A / C / D
C Cannot be equal to A / B / D
D Cannot be equal to A / B / C

Here is the code:

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

public class Lottery {

   public static void main(String[] args) {

      Scanner scan = new Scanner(System.in);

      System.out.println("Lottery Game");



      Random rand = new Random();
      // Creating the four individual balls
      int ball1 = 0;
      int ball2 = 0;
      int ball3 = 0; 
      int ball4 = 0;
      int guess1 = 0;
      int guess2 = 0;
      int guess3 = 0;
      int guess4 = 0;

      // THIS IS THE USERS TICKET NUMBER

      System.out.println("Generating Ticket");



      guess1 = rand.nextInt(10);
      guess2 = rand.nextInt(10);
      guess3 = rand.nextInt(10);
      guess4 = rand.nextInt(10);

      do {    

          guess1 = rand.nextInt(10);
          guess2 = rand.nextInt(10);
          guess3 = rand.nextInt(10);
          guess4 = rand.nextInt(10); 

      if ( guess1 == guess2 || guess1 == guess3 || guess1 == guess4 ) {
          guess1 = rand.nextInt(10);
      }
      if ( guess2 == guess3 || guess2 == guess4 ) {
          guess2 = rand.nextInt(10);
      }
      if ( guess3 == guess4 ) {
          guess3 = rand.nextInt(10);
      }

      } while (guess1 == guess2 || guess2 == guess3 || guess3 == guess4 || guess4 == ball1);

      System.out.println(guess1 + " " + guess2 + " " + guess3 + " " + guess4);

      System.out.println("Are you happy with the selected numbers?");

      int confirmation = 0;

      System.out.println("Please Type '1' For 'YES' Or '2' For 'NO");

      confirmation = scan.nextInt();

      System.out.println(confirmation);


      if (confirmation == 1) {

          ball1 = rand.nextInt(10);
          ball2 = rand.nextInt(10);
          ball3 = rand.nextInt(10);
          ball4 = rand.nextInt(10);

          do {    

              ball1 = rand.nextInt(10);
              ball2 = rand.nextInt(10);
              ball3 = rand.nextInt(10);
              ball4 = rand.nextInt(10); 

          if ( ball1 == ball2 || ball1 == ball3 || ball1 == ball4 ) {
              ball1 = rand.nextInt(10);
          }
          if ( ball2 == ball3 || ball2 == ball4 ) {
              ball2 = rand.nextInt(10);
          }
          if ( ball3 == ball4 ) {
              ball3 = rand.nextInt(10);
          }

          } while (ball1 == ball2 || ball2 == ball3 || ball3 == ball4 || ball4 == ball1);

          System.out.println("The Winning Numbers Are As Follows...");
          System.out.println(ball1 + " " + ball2 + " " + ball3 + " " + ball4);

          if ( ball1 == guess1 &&  ball2 == guess2 &&  ball3 == guess3 &&  ball4 == guess4) {
              System.out.println("Congratulations! You've Won £1,000,000!");
              System.exit(0);
          }
          else {
              System.out.println("Unfortunately, You Did Not Win. Better Luck Next Time!");
              System.exit(0);
          }


      }
      else if (confirmation == 2) {
          System.out.println("Exiting Game, Thanks for playing!");
      }
      else {
          System.out.println("ERROR");
      }

      }

   }

Specifically, I would like to improve the algorithm that I've used here. I don't think it is very elegant, even though it does work.

I'm also interested in other ways that my code can be improved. I would appreciate any suggestions.

\$\endgroup\$

migrated from stackoverflow.com Sep 13 '17 at 19:46

This question came from our site for professional and enthusiast programmers.

2
\$\begingroup\$

Besides minor improvements like consistent formatting and proper indentation I would like to target your random number generation:

int guess1 = 0;
int guess2 = 0;
int guess3 = 0;
int guess4 = 0;

guess1 = rand.nextInt(10);
guess2 = rand.nextInt(10);
guess3 = rand.nextInt(10);
guess4 = rand.nextInt(10);

do {    

    guess1 = rand.nextInt(10);
    guess2 = rand.nextInt(10);
    guess3 = rand.nextInt(10);
    guess4 = rand.nextInt(10); 

if ( guess1 == guess2 || guess1 == guess3 || guess1 == guess4 ) {
    guess1 = rand.nextInt(10);
}
if ( guess2 == guess3 || guess2 == guess4 ) {
    guess2 = rand.nextInt(10);
}
if ( guess3 == guess4 ) {
    guess3 = rand.nextInt(10);
}

} while (guess1 == guess2 || guess2 == guess3 || guess3 == guess4 || guess4 == ball1);

While not incorrect, that certainly looks a bit clumsy.

Since you want four unique random numbers, a better choice would be to create a list of numbers from 0-9, shuffle it and then simply take the first four elements. And while you're at it, convert your four individual variables guess1 to guess4 to a single int[] array of guesses.

This allows you to replace the above code with simply:

List<Integer> numbers = IntStream.range(0, 9).boxed()
        .collect(Collectors.toList());
Collections.shuffle(numbers);
int[] guesses = new int[4];
for (int i = 0; i < 4; i++) {
    guesses[i] = numbers.get(i);
}

(Refactor that into a separate method and you can re-use it for your balls generation as well.)

When guesses and balls are both arrays, the comparison could also be simplified to:

Arrays.equals(guesses, balls);

Arrays.equals:

Returns true if the two specified arrays of ints are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order.

(Emphasis by me)

\$\endgroup\$
  • \$\begingroup\$ Good comments, the presence of unit test would allow these improvements (refactoring) to take place while ensuring the behaviour remains correct. \$\endgroup\$ – Martin Spamer Sep 14 '17 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy