COMPLETE HILO APPLET PROGRAM



This section of the tutorial assumes that either you have completed the easier sections or are an accomplished windows programmer. This section will display the code from the full Hilo game. It will not discuss individual sections of the code not related to the features of the Java language. This section assumes you have programmed before and can figure out the general programming techniques used.

PLAY THE GAME

Click here to play hilo.
Click on source to view the source.

LOOKING AT THE CODE

In this part of the tutorial, I will step through each of the 5 classes used in the final Hilo program.



The import statements
import java.awt.*;
import java.util.Random;
import java.lang.Math;


These import statements will load the necessary classes for the window toolkit, for the random number generator, and for the rounding math functions, respectively.

LOOKING AT THE HILO CLASS

The init() method remains mostly the same as discussed previously in the introduction to windowing section of the tutorial; therefore we will not discuss it in greater detail here.

action() METHOD

The action() method is disussed in the section on handling events.

LOOKING AT THE PLAYER CLASS

This class will emulate the features of a player playing the Hilo game. The person will have accumulated earnings for the entire length of the game. Since this class definition is a standard class in any OO-language, I will leave it to you to figure it out.

LOOKING AT THE GAME CLASS

This class handles the interaction between the game and the window interface. The game class will deal two cards, draw the middle card, and test to see if the player has won or lost.

LOOKING AT THE DECK CLASS

This class handles the concept of a deck of cards. The class will initially put the cards in order. Then the class can shuffle the deck and deal a card. Let's look at the shuffle() method to examine Java's random number generator.

1:     void shuffle(int num_times) {
2:     /* This method will shuffle the deck */
3:         double first_rand, second_rand;
4:         Random rand_num = new Random();
5:         int first_number;
6:         int second_number;
7:         int temp;
8:             
9:         cards_remaining = 52;
10:        for (int i = 0; i < num_times; i++) {
11:            first_rand = rand_num.nextDouble();
12:            second_rand = rand_num.nextDouble();
13: 
14:            first_number = (int) Math.round((51*first_rand));
15:            second_number = (int) Math.round((51*second_rand));
16: 
17:            temp = index_array[first_number];
18:            index_array[first_number] = index_array[second_number];
19:            index_array[second_number] = temp;
20:        }
21:    }   


Look at line 4. In this line, we are asking Java to create a random number instance by using the new operator on the Random class contained in java.util.Random. This line will initialize the seed for the random number generator.

Now look at lines 11 and 12. Lines 11 and 12 contain the code to get the random double values from the random number generator, which are values between 0.0 and 1.0. The next line (14), converts the first_rand number to an integer between 0 and 51. We want 52 numbers to represent the 52 cards in a deck. The Math.round() method is called to round the double value to an integer value. The right hand side also has to be cast to an int.

THE CARD CLASS

The card class represents the concept of a single card. It contains the integer value, the suit, and the type of card.

You should now have a fully functioning Hilo window game that you can put on your web page.