Initialize game status data 2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.