Generating random numbers to deploy a sub

We need to deploy a sub' in a random position at the start of each game. There are, however, many possible uses for random numbers as we will see throughout this book. So, let's take a close look at the Random class and one of its methods nextInt.

The Random class and the nextInt method

Let's have a look at how we can create random numbers and later in the chapter we will put it to practical use to spawn our sub'. All the demanding work is done for us by the Random class.

Note

The Random class is part of the Java API which is why there is a slightly different import statement to get access to it. Here is the line we added in chapter two.

import java.util.Random;

Note that this is the only import statement (so far) that starts with java… instead of android….

First, we need to create and initialize an object of type Random. We can do so like this:

Random randGenerator = new Random();

Then we use our new object's nextInt method to generate a random number between a certain range.

This line of code generates the random number using our Random object and stores the result in the ourRandomNumber variable.

int ourRandomNumber = randGenerator.nextInt(10);

The number that we enter as the range starts from zero. So, the line above will generate a random number between 0 and 9. If we want a random number between 1 and 10 we just do this

int ourRandomNumber = randGenerator.nextInt(10) + 1;

We can also use the Random object to get other types of random number using nextLong, nextFloat and nextDouble methods.

Note

You can even get random Booleans or whole streams of random numbers. You can explore the Random class in detail here: https://developer.android.com/reference/java/util/Random.html

We are ready to spawn the sub'.

Adding Random based code to newGame

Add the following highlighted code in the newGame method.

/*
   This code will execute when a new
   game needs to be started. It will
   happen when the app is first started
   and after the player wins a game.
 */
public void newGame(){
   Random random = new Random();
   subHorizontalPosition = random.nextInt(gridWidth);
   subVerticalPosition = random.nextInt(gridHeight);
   shotsTaken = 0;
   
   Log.d("Debugging", "In newGame");
   
}

In the previous code, we first declared and initialized a new Random object called random with this line of code.

Random random = new Random();

Then we used the nextInt method to generate a random number and assigned it to subHorizontalPosition. Look closely at the line of code shown next, specifically look at the argument passed into nextInt.

subHorizontalPosition = random.nextInt(gridWidth);

The variable gridWidth is exactly the required value. It generates a number between 0 and gridWidth-1. When we handle collision detection between the sub' and the player's tap (shot) in Chapter 7, Making decisions with Java If, Else and Switch you will see this is just what we need.

Note

If we had needed non-zero numbers generated, we could have added the + 1 to the end of the previous line of code.

The next line of code is this:

subVerticalPosition = random.nextInt(gridHeight);

This works the same way as the previous line of code except that the random value is assigned to subVerticalPosition and the argument passed to nextInt is gridHeight.

Our sub position variables are now ready for use.

The final line of code that we added simply initializes shotsTaken to zero. It makes sense that when we start a new game we want to do this so that the number of shots taken displayed to the player is not accumulated over multiple games.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.53.166