Initialize game status data
The following three subclasses are implemented for the abstractboard.
1. squares in matrix arrangement
The square arranged in the matrix fills up every array element of the Two-dimensional array, leaving the surrounding area blank. The code of this subclass is as follows.
Program list: Codes \ 18 \ link \ SRC \ org \ crazyit \ link \ board \ impl \ fullboard. Java
Public class fullboard extendsabstractboard
{
@ Override
Protectedlist <piece> createpieces (gameconf config,
Piece [] [] pieces)
{
// Create a piece set that stores the piece objects required for game initialization.
List <piece> notnullpieces = new arraylist <piece> ();
For (INT I = 1; I <pieces. Length-1; I ++)
{
For (Int J = 1; j <pieces [I]. Length-1; j ++)
{
// First construct a piece object and set its index value in the piece [] [] Array
// The required pieceimage is set by its parent class.
Piecepiece = new piece (I, j );
// Add to piece collection
Notnullpieces. Add (piece );
}
}
Returnnotnullpieces;
}
}
The game interface 18.7 initialized by this subclass is shown.
2. vertically arranged squares
Vertical blocks are separated by vertical empty columns. The code for this subclass is as follows.
Program list: Codes \ 18 \ link \ SRC \ org \ crazyit \ link \ board \ impl \ verticalboard. Java
Public class verticalboard extendsabstractboard
{
Protectedlist <piece> createpieces (gameconf config,
Piece [] [] pieces)
{
// Create a piece set that stores the piece objects required for game initialization.
List <piece> notnullpieces = new arraylist <piece> ();
For (INT I = 0; I <pieces. length; I ++)
{
For (Int J = 0; j <pieces [I]. length; j ++)
{
// Add judgment to construct the piece object only when certain conditions are met and add it to the set
If (I % 2 = 0)
{
// If X can be divisible by 2, that is, a single sequence does not create blocks.
// First construct a piece object and set its index value in the piece [] [] Array
// The required pieceimage is set by its parent class.
Piece piece = new piece (I, j );
// Add to piece collection
Notnullpieces. Add (piece );
}
}
}
Returnnotnullpieces;
}
}
In the above program, the bold text code controls only the columns with I % 2 = 0, that is, only the columns with an even index are set. The game interface 18.8 initialized by this subclass is shown.
3. Horizontally arranged squares
Vertical blocks are separated by horizontal blank lines. The code for this subclass is as follows.
Program list: Codes \ 18 \ link \ SRC \ org \ crazyit \ link \ board \ impl \ horizontalboard. Java
Public class horizontalboard extendsabstractboard
{
Protectedlist <piece> createpieces (gameconf config,
Piece [] [] pieces)
{
// Create a piece set that stores the piece objects required for game initialization.
List <piece> notnullpieces = new arraylist <piece> ();
For (INT I = 0; I <pieces. length; I ++)
{
For (Int J = 0; j <pieces [I]. length; j ++)
{
// Add judgment to construct the piece object only when certain conditions are met and add it to the set
If (J % 2 = 0)
{
// If J can be divisible by 2, that is, no square is created for the singular row.
// First construct a piece object and set its index value in the piece [] [] Array
// The required pieceimage is set by its parent class.
Piece piece = new piece (I, j );
// Add to piece collection
Notnullpieces. Add (piece );
}
}
}
Returnnotnullpieces;
}
}
In the above program, the bold text code controls the rows with only J % 2 = 0, that is, only the rows with an even index are set. The game interface 18.9 initialized by this subclass is shown.
This article is excerpted from the book crazy android handouts (including one CD.
Book details: http://blog.csdn.net/broadview2006/article/details/6609027