The create method will initialize all variables at the start of a game. The first line should be:
Random.randomize();
Why do we need that line? Computers have a big problem with random numbers. They’re a bit too exact for that. We solve this with the Random object, but this object internally has a fixed list of numbers which it will use one by one. Every time you ask for a random number, you just get the next number from the list. Should you start at the beginning of that list every time you run your application, you get the same ‘random’ numbers every time. This will make your game rather predictable over time. The function randomize allows you to start at a random place in the table. This way, another part of the list is used every time.
The following statements provide two random blocks:
currentBlock.create(STARTPOS, (BLOCK_TYPE)Random(BT_NUM));
nextBlock .create(WAITPOS , (BLOCK_TYPE)Random(BT_NUM));
Notice that we use STARTPOS and WAITPOS to set the positions. The second argument is the block type. We want a random block every time, so the random function is used. The argument of Random determines the highest number. But that value is not inclusive: for example, when you write Random(3), the result can be 0, 1 or 2. Not 3. So why do we write BT_NUM? For this, you have to go back and look in the file ‘enumerations’. BT_NUM is the eighth item in the list. The first element is equal to 0, so the eighth element is equal to 7. Therefore, the Random function will return a number from 0 to 6. And because an enum is nothing but a name for a number, we can easily convert that number to a BLOCK_TYPE. Because that is the type the create function expects.
After these statements, we must assign forceDownCounter a value of 0, and SLIDE_TIME must be assigned to slideCounter. You are surely able to write those statements yourself. Finally, call the Pile and Score classes’ init method.