Initialization examples
We are now prepared to initialize the game state of our game model, no matter how complex it is. Below is the general procedure for initializing a game model. We assume each of your classes has a constructor.
- Create an instance of your game class. (This can be done in your game class's main() method but is not the only possible way.)
- In your game class's constructor, initialize each field of your game class.
Each field will need to be initialized in a way according to its type.
- If the field is a primitive type, an enumerated type, or a String, assign it a value of the same type.
- If the field is a class type, assign it a new instance of its class type. This will invoke that class's constructor. This constructor will initialize the new instance's fields in the same way we are describing here.
- If the field is an array type, create a new array of the array type, specifying the length of the array to be created. Then for each element of the array, assign to it a value according to its type in the same we are describing here.
Here's how it would work for the same examples we presented in Section 3.7 (Data Modeling—Examples).
Tic Tac Toe
Here is the class we developed for Tic Tac Toe, showing only the data-modeling parts.
class TicTacToeGame {
TicTacToeSquare[][] theBoard;
TicTacToePlayer whoseTurn;
}
enum TicTacToeSquare { Blank, X, O }
enum TicTacToePlayer { Player1, Player2 }
Here is the illustration we presented earlier of the state that we want to create:

We initialize our game state in the class's main() method and the class's constructor. Here is the entire class, with the main() method and constructor.
public class TicTacToeGame
{
TicTacToeSquare[][] theBoard;
TicTacToePlayer whoseTurn;
public static void main(String[] args)
{
// first we create an instance of the game
TicTacToeGame game = new TicTacToeGame();
// that's all---the TicTacToeGame constructor took care of
// initializing itself.
}
public TicTacToeGame() {
// we initialize each field of the class.
// the first is an array, so we create the array.
theBoard = new TicTacToeSquare[3][3];
// then we have to initialize each element of the array.
// we loop over all rows
for (int i = 0; i < 3; i++) {
// ...and for each row, we loop over the columns
for (int j = 0; j < 3; j++) {
theBoard[i][j] = TicTacToeSquare.Blank;
}
}
// now we initialze the second field
whoseTurn = TicTacToePlayer.Player1;
}
}
enum TicTacToeSquare { Blank, X, O }
enum TicTacToePlayer { Player1, Player2 }
Notice that we fill all the squares are filled with Blank, because we're starting a new game. The figure shows a game in progress.
Blackjack
We'll model the game of Blackjack as it would be played between a player and the computer. Our model here will then be a bit simpler than the model in Section 3.7, which assumed a variable number of players.
class BlackjackGame {
Card[] deck;
Card[] dealerHand;
Card[] playerHand;
BlackjackPlayer whoGetsNextCard;
}
class Card {
Suit suit;
Rank rank;
}
enum Suit { Spades, Hearts, Clubs, Diamonds }
enum Rank { Ace, R2, R3, R4, R5, R6, R7, R8, R9 , R10, Jack, Queen, King }
enum BlackjackPlayer { Dealer, Player }
This presents some different challenges than initializing the Tic Tac Toe game. We'll talk about them in the comments in our code.
public class BlackjackGame {
Card[] deck;
Card[] dealerHand;
Card[] playerHand;
BlackjackPlayer whoGetsNextCard;
public static void main(String[] args) {
// we create an instance of BlackjackGame, and the BlackjackGame
// constructor takes care of initializing the instance.
BlackjackGame game = new BlackjackGame();
// let's see how it did
game.printDeck();
}
// the constructor needs to know how many players are playing
public BlackjackGame() {
// the first field is the deck.
// we create the deck array, giving it room for a 52-card deck.
// (four suits, thirteen ranks per suit = 52)
deck = new Card[52];
// initializing the array with 52 unique Card objects is a big enough
// job to deserver a separate method.
initializeDeck();
// the deck should be shuffled before it's ready to use.
shuffleDeck();
// the next fields the dealer hand and the player hands.
// each hand is a Card array, and must be large enough to hold the
// maximum number of cards the dealer or the player could get.
// i think it's 11, but lets put it at a round 12.
dealerHand = new Card[12];
// same for the player's hand.
playerHand = new Card[12];
// the last field is whoGetsNextCard. player gets first deal.
whoGetsNextCard = BlackjackPlayer.Player;
}
// the initialize method fills the deck with 52 unique Card objects.
private void initializeDeck() {
// we need to create all 52 unique cards. we'll run through each suit.
// to store each card in the deck array, we need an index.
int i = 0;
for (Suit suit : Suit.values()) {
// for each suit, we'll run through each rank. the values() method
// on an enumerated type returns an array of the type's values.
for (Rank rank : Rank.values()) {
// i++ increments i but, as an expression, yields the original value of i
deck[i++] = new Card(suit, rank);
}
}
}
// the shuffleDeck method puts the cards in random order.
private void shuffleDeck() {
for (int i = 0; i < deck.length; i++) {
// use Math.random() to pick a random value between 0 and length of deck.
int rand = (int) (Math.random() * deck.length);
// get the card at rand
Card randCard = deck[rand];
// move the card at i to rand
deck[rand] = deck[i];
// now put the random card at i
deck[i] = randCard;
}
}
// so we can look at our deck
public void printDeck() {
for (Card card : deck) {
System.out.println(card);
}
}
}
class Card {
Suit suit;
Rank rank;
public Card(Suit suit, Rank rank) {
this.suit = suit;
this.rank = rank;
}
public String toString() {
return rank + " of " + suit;
}
}
enum Suit { Spades, Hearts, Clubs, Diamonds }
enum Rank { Ace, R2, R3, R4, R5, R6, R7, R8, R9 , R10, Jack, Queen, King }
enum BlackjackPlayer { Dealer, Player }
Rush Hour
Here's the data model for Rush Hour from Section 3.7.3:
class RushHour {
Vehicle[] vehicles;
int exitingVehicle;
}
class Vehicle {
Orientation orientation;
int length; // length = 2 or length = 3
Position position;
}
enum Orientation { Horizontal, Vertical }
class Position {
int row; // 0-5 (6 rows)
int col; // 0-5 (6 cols)
}
Now we add initialization code:
public class RushHour
{
Vehicle[] vehicles;
int exitingVehicle;
public static void main(String[] args)
{
// Below we need to imagine some code that
// "reads" a Rush Hour setup card.
Orientation[] orientations = ...
int[] lengths = ...
Position[] positions = ...
int redCar = ...
RushHour rushHour = new RushHour(
orientations, lengths, positions, redCar);
}
public RushHour(Orientation[] orientations,
int[] lengths,
Position[] positions,
int redCar) {
vehicles = new Vehicle[orientations.length];
for (int i = 0; i < vehicles.length; i++) {
}
}
}
class Vehicle {
Orientation orientation;
int length; // length = 2 or length = 3
Position position;
public Vehicle(Orientation orientation,
int length,
Position position) {
this.orientation = orientation;
this.length = length;
this.position = position;
}
}
enum Orientation { Horizontal, Vertical }
class Position {
int row; // 0-5 (6 rows)
int col; // 0-5 (6 cols)
}