RPG Tutorial: Text-Based Yo-Kai Watch Lottery in Java

In this tutorial, we’ll discuss how to implement a text-based lottery system/game similar to the one in some of the Yo-kai Watch games. We will be doing the tutorial in Java. You’ll need some basic knowledge of Java, mainly arrays and loops. If you are inexperienced in Java and need a more detailed explanation, please let me know in the comments. You can also email contact@cloudyheavengames.com.

How the Lottery Works

Here’s the basic idea: the player collects lottery tickets from random drops after battles, and in random spots on the map.  The player can then go to a lottery, which has 45 prizes available, each with a number attached to it.  Lower numbers are the more common items, while the rare items have higher numbers.

Many of the available common prizes are repeat items, so you might see several prize slots filled with easily-obtained items such as chocolate bars.  For rarer items, there is usually only one of each item available, so the odds are higher that you will win the common items more often.

Each lottery ticket has a number that corresponds to one of the prizes.  If a particular prize number has already been pulled, then the ticket number will be reset to another prize number that is still available.

In the game, other in-game characters in the game world can also participate in the library, so if you don’t act quickly, someone else might snatch up those rare prizes you’re aiming at!  There’s also a question of whether it’s better to wait until some of the more common prizes have been taken, and then try to get the rarer prizes when the odds are better due to fewer available prizes.  However, that’s more of a gameplay strategy question, which we won’t really address in this tutorial.

You can learn a bit more about the lottery here.

Based on these guidelines, we will do a very basic version of this lottery system, in Java.  There are lots of different approaches and design decisions I could have taken (and likely will in future iterations).  But this simple version is a good start for helping you to understand how a system like this could work.

The complete code is here.

The Java Program

Setting the Prize List

The first thing we are going to do is set up the list of prizes.  For this simple version, this will just be a list of about 10 – 12 item names, stored as strings (text objects).  We will be using a Java array to store this list.

String[] prizes = {
"Bubble gum",
"Sandwich",
"Milk",
"Cola",
"Hamburger",
"Ramen cup",
"Chocolate bar",
"Rice ball",
"Lucky tag",
"Iron doll",
"Golden doll",
"Cursed sword"
};

Most of the item names are pulled from or variations on equipment and items from the actual Yo-Kai Watch games.  The rarest items are at the end of the list, with the last two being particularly valuable.  This is a shorter list than in the game, but hopefully you can see even here how adding more common items lowers the chance of getting a rare item.

Setting up the Tickets

So we have our list of possible prizes.  Next, let’s set up our tickets.  For now, let’s say we’ve got two tickets.  Let’s create a variable to keep track of the number of tickets:

int numTickets = 2;

Let’s also create a variable to keep track of the number of prizes available:

int prizeRange = prizes.length;

Next, we will create an array to store our tickets. Each item in the array will hold a number, which represents the number on the ticket. Initially, we’ll set each ticket number to -1. Then, we’ll go through and set each ticket to a valid number, based on the number of prizes.

The possible lottery numbers will range from 0 (which will represent the first prize in the list), up to the number of prizes, minus 1. We choose these numbers because Java arrays are zero-indexed (the position number of the first item is 0). That’s why we initially set each ticket number to -1. A negative number is an easy way to denote that the ticket number has not been set with its real number yet. If you are not comfortable with Java arrays, check the official Java array tutorial.

Here is how we create the array to store our ticket numbers:

int[] tickets = new int[numTickets];
Arrays.fill(tickets, -1);

In the first line, we’ve got an array of integers, the same length as the number of tickets. On the next line, we’re using a convenient function that Java provides for arrays, which will set the value of all the tickets to -1. Notice that at the beginning of the code, we have an import statement:

import java.util.Arrays;

You’ll need this import statement in order to use the Arrays.fill() method.

Assigning Ticket Number

Next, we’ll loop through each ticket and assign it a random number, in the prize number range. If another ticket already has the number, we’ll reassign a new number, until we get a unique one. This way, each ticket will have a distinct number.

We’ll use a for-loop to go through each ticket:

for(int i = 0; i < numTickets; i++)

For each ticket, we have a variable that will keep track of whether we need to give it a different number:

boolean repeat;

We’ll use a do-while loop to assign a number to each ticket. I chose a do-while loop because we will need to run through the loop at least once. (If you need to know more about Java do-while loops, here’s the official documentation.) If the number is a duplicate, we’ll run the loop again, until we get a unique number.

do {
  /* For each ticket, we will make sure that the ticket number has not already been assigned to another ticket.
  If we find that a ticket has a duplicate number, we will keep reassigning a new number until we get a unique one. 
  */
  repeat = false;
  int num = (int)(Math.random() * prizeRange);
  for(int j = 0; j < numTickets; j++)
    {
      // This loop goes through each of the tickets already in the list
      if(tickets[j] == num)
      {
        repeat = true;
        System.out.println("number already in array: " + num);
      }
    }
    if(!repeat){
      tickets[i] = num;
    }
}while(repeat);

As you can see, at the top of the loop, the repeat variable is false. This assumes that we won’t need to assign a different number.

In this line, we assign the random ticket number, an integer:

int num = (int)(Math.random() * prizeRange);

Then, we have a for-loop that goes through each ticket and checks if any of them have the number we just picked:

 for(int j = 0; j < numTickets; j++)
 {
   // This loop goes through each of the tickets already in the list
   if(tickets[j] == num)
   {
     repeat = true;
     System.out.println("number already in array: " + num);
   }
 }

If we find the number, we set repeat to true, to run the number assignment loop again.

Finally, if we don’t find a duplicate of the number, we assign it to the ticket:

if(!repeat){
  tickets[i] = num;
}

Printing the Results

After we assign the ticket numbers, we’ll output all of them, just to see our results:

  System.out.println("Tickets: " + Arrays.toString(tickets));

Finally, we print out the prize names that we won:

for(int i = 0; i < numTickets; i++){
  System.out.println("You won the prize: " + prizes[tickets[i]]);
}

We use the number on each ticket as the index for which item we won in the prize array. Again, if you need more clarification on how to access elements by index in an array, check the official tutorial.

Conclusion

Now that we’ve talked about randomness in RPGs, we’ve seen a concrete code example. This one was pretty simple, but I hope it was a helpful example, and there will be more.  I understand that readers might have a variety of experience levels with Java.  So if you are a newcomer and need more explanation, please feel free to ask questions.  And for more experienced readers, I will have more complex examples coming up.

As a reminder, I am putting together an RPG course.  If you’d like to provide some feedback about what you’d like to learn, and get updated when it’s ready, please fill out this form.  And don’t forget to check the page on developing RPGs for more learning.

JOIN OUR NEWSLETTER
Sign up to receive updates about new tutorials, game news, and educational opportunities.
We hate spam. Your email address will not be sold or shared with anyone else.

Share This:

Tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.