Flex online game development-Chinese chess games: (1) core logic

Source: Internet
Author: User
Tags findone switch case

In the development of four-nation military games, use flex online game development-four-nation military games (5)-refine the chess development api, we have extracted the first api for chess game development-FlexChessAPI. This api design principle is based on the state machine and event-driven flex mechanism to make the development work simple and easy. Now, we use this api for the first time to develop a Chinese chess game. For a mature developer, I believe that you only need half a day to make the game work.

Now, we can use the logic of Chinese chess.

1) Chinese chess sets 9x10 squares. Unlike the four-nation military chess, the chess pieces themselves have no way to play,

That is to say, we only need to simply expand ipostion to form the posiitonVO class.

2) The Chinese chess piece itself has no size, but each sub-player has his own chess line. The way to eat sub-players is that the target sub-player is on the sub-chess line. In addition, the behavior of other sub-accounts may affect the ro route of the target sub-account.

That is to say, we cannot immediately obtain the mobility route of the sub-account, whether it is moving or eating sub-accounts. We can determine the ro route only after the other party finishes playing the game, therefore, the method we choose to calculate the ro route should be positioned when selecting the source pawn.

Next, I will design a ChinaChessHelp class. All the methods and variables in the class are static classes.

2.1 list all the pawns. (Note that the red and black pictures show different names and different chess routes. In this way, I will process them into two different pawns ), as for the value of a piece, it is not important, but a difference.

// Pawns
Public static const PIECE_RED_CHE: int = 56;
Public static const QIZHI_RED_MA: int = 57;
Public static const QIZHI_RED_XIAN: int = 58;
Public static const QIZHI_RED_SHI: int = 59;
Public static const QIZHI_RED_JIAN: int = 60;
Public static const QIZHI_RED_BING: int = 55;
Public static const QIZHI_RED_PAO: int = 54;

Public static const PIECE_BLACK_CHE: int = 156;
Public static const QIZHI_BLACK_MA: int = 157;
Public static const QIZHI_BLACK_XIAN: int = 158;
Public static const QIZHI_BLACK_SHI: int = 159;
Public static const QIZHI_BLACK_SUAN: int = 160;
Public static const QIZHI_BLACK_ZHU: int = 155;
Public static const QIZHI_BLACK_PAO: int = 154;

2.2 design the movement route of chess pieces, which is the core logic of Chinese chess. Please take part in this figure. I have designed each sub-game route. For some chess moves with limited and transparent routes, I have directly indexed the values (such as Shi, Xiang, and Shi ), for some pieces of chess lines that are uncertain, we can calculate their ro routes, such as cars, horses, and guns. Refer to the image below for all the indexes (as I will discuss in the next section ).

2.2.1 function initMoveRedJian (): ArrayCollection

The following describes the ro route using switch case:

/**
* The Defender is playing the game.
* @ Param posindex
* @ Return
*
*/
Public static function initMoveRedJian (posindex: int): ArrayCollection {
Var temp: ArrayCollection;
Switch (posindex ){
Case 3:
Temp = new ArrayCollection ([4, 12]);
Break;
Case 4:
Temp = new ArrayCollection ([3, 5, 13]);
Break;
Case 5:
Temp = new ArrayCollection ([4, 14]);
Break;
Case 12:
Temp = new ArrayCollection ([3, 13, 21]);
Break;
Case 13:
Temp = new ArrayCollection ([4, 12, 14, 22]);
Break;
Case 14:
Temp = new ArrayCollection ([5, 13, 23]);
Break;
Case 21:
Temp = new ArrayCollection ([12, 22]);
Break;
Case 22:
Temp = new ArrayCollection ([13, 21, 23]);
Break;
Case 23:
Temp = new ArrayCollection ([14,22]);
Break;
Default:
Break;
}
Return temp;
}

 

2.2.2 function initMoveRedShi (): ArrayCollection

Shi's ro route is described as follows using switch case:

/**
* The ro path of Fang Shi
* @ Param posindex
* @ Return
*
*/
Public static function initMoveRedShi (posindex: int): ArrayCollection {
Var temp: ArrayCollection;
Switch (posindex ){
Case 3:
Temp = new ArrayCollection ([13]);
Break;
Case 5:
Temp = new ArrayCollection ([13]);
Break;
Case 13:
Temp = new ArrayCollection ([3, 5, 21,23]);
Break;
Case 21:
Temp = new ArrayCollection ([13]);
Break;
Case 23:
Temp = new ArrayCollection ([13]);
Break;
Default:
Break;
}
Return temp;
}

The logic is like this, but the above method is incorrect. In flex, when the array has only one element, it is best not to initialize it directly with arraycollection. You can modify it as follows :)

2.2.3 The ro line of the Red Square elephant, initMoveRedXian

/**
* The ro path of Fang Xiang
* Pay attention to poor routes.
* @ Param posindex
* @ Return
*
*/

Public static function initMoveRedXian (posindex: int, pieceMap: HashMap): ArrayCollection {
Var temp: ArrayCollection = new ArrayCollection ();
Switch (posindex ){
Case 2:
If (pieceMap. get (10) as PieceVO). Then Pos =-1)
Temp. addItem (18 );
If (pieceMap. get (12) as PieceVO). Then Pos =-1)
Temp. addItem (22 );
Break;
Case 6:
If (pieceMap. get (14) as PieceVO). Then Pos =-1)
Temp. addItem (22 );
If (pieceMap. get (16) as PieceVO). Then Pos =-1)
Temp. addItem (26 );
Break;
Case 18:
If (pieceMap. get (10) as PieceVO). Then Pos =-1)
Temp. addItem (2 );
If (pieceMap. get (28) as PieceVO). Then Pos =-1)
Temp. addItem (38 );
Break;
Case 22:
If (pieceMap. get (12) as PieceVO). Then Pos =-1)
Temp. addItem (2 );
If (pieceMap. get (14) as PieceVO). Then Pos =-1)
Temp. addItem (6 );
If (pieceMap. get (30) as PieceVO). Then Pos =-1)
Temp. addItem (38 );
If (pieceMap. get (32) as PieceVO). Then Pos =-1)
Temp. addItem (42 );
Break;
Case 26:
If (pieceMap. get (16) as PieceVO). Then Pos =-1)
Temp. addItem (6 );
If (pieceMap. get (34) as PieceVO). Then Pos =-1)
Temp. addItem (42 );

Break;
Case 38:
If (pieceMap. get (28) as PieceVO). Then Pos =-1)
Temp. addItem (18 );
If (pieceMap. get (30) as PieceVO). Then Pos =-1)
Temp. addItem (22 );
Break;
Case 42:
If (pieceMap. get (32) as PieceVO). Then Pos =-1)
Temp. addItem (22 );
If (pieceMap. get (34) as PieceVO). Then Pos =-1)
Temp. addItem (26 );
Break;
Default:
Break;
}
Return temp;
}

2.2.4 hongfangbing's chess line public static function initMoveRedBing (): ArrayCollection

Note that when the river is not crossed, you can only move forward. After the river is crossed, you can go in three places.

Use the value of posindex for determination.

/**
* The ro of yunqi
* @ Param posindex
* @ Return
*
*/
Public static function initMoveRedBing (posindex: int, pieceMap: HashMap): ArrayCollection {
Var temp: ArrayCollection = new ArrayCollection ();
If (posindex % 9-1> = 0 & posindex> = 45) temp. addItem (posindex-1 );
If (posindex % 9 + 1 <9 & posindex> = 45) temp. addItem (posindex + 1 );
If (posindex + 9 <90) temp. addItem (posindex + 9 );
Return temp;
}

2.2.5 horse's ro Route public static function initMoveRedBing (): ArrayCollection

Cars, horses, and guns belong to free-moving pawns, which are equivalent to the red and black sides. Therefore, public functions can be used for processing.

The main logic of the horse game is to determine whether the target point is on the board or whether there is a poor point.

/**
* Ma's ro Road
* Each horse has eight playing positions and four lame positions,
* @ Param posindex
* @ Return
*
*/
Public static function initMoveMa (posindex: int, pieceMap: HashMap): ArrayCollection {
Var temp: ArrayCollection = new ArrayCollection ();

// Left
If (posindex % 9-2> = 0 ){
If (pieceMap. get (posindex-1) as PieceVO). Adjust Pos =-1)
{
If (posindex-11)> = 0)
{
Temp. addItem (posindex-11 );
}
If (posindex + 7) <89)
{
Temp. addItem (posindex + 7 );
}

}
}
// Right
If (posindex % 9 + 2 <9 ){

If (pieceMap. get (posindex + 1) as PieceVO). Then Pos =-1)
{
If (posindex-7)> = 0)
{
Temp. addItem (posindex-7 );
}
If (posindex + 11) <89)
{
Temp. addItem (posindex + 11 );
}

}
}
// Forward
If (posindex-18> = 0 ){
If (pieceMap. get (posindex-9) as PieceVO). Adjust Pos =-1)
{
If (posindex-19) % 9 <posindex % 9)
{
Temp. addItem (posindex-19 );
}
If (posindex-17) % 9> posindex % 9)
{
Temp. addItem (posindex-17 );
}

}
}
// Bottom
If (posindex + 18 <90 ){
If (posindex + 19) % 9> posindex % 9)
{
Temp. addItem (posindex + 19 );
}
If (posindex + 17) % 9 <posindex % 9)
{
Temp. addItem (posindex + 17 );
}
}
Return temp;
}

 

2.2.6 ro routes

The playing logic of a car is to check whether there are any available positions in the four directions. If a piece is found, the search will end and the currently found piece will also be indexed.

/**
* Chelaile's ro Road
* @ Param posindex
* @ Return
*
*/
Public static function initMoveChe (posindex: int, pieceMap: HashMap): ArrayCollection {
Var temp: ArrayCollection = new ArrayCollection ();
// Module direction
For (var xr: int = posindex % 9 + 1; xr <9; xr ++)
{
If (pieceMap. get (posindex + xr-posindex % 9) as PieceVO). Then Pos =-1)
{
Temp. addItem (posindex + xr-posindex % 9 );
}
Else
{
// Add the location where the last child is located
Temp. addItem (posindex + xr-posindex % 9 );
Break;
}
}
For (var xl: int = posindex % 9-1; xl> = 0; xl --)
{
If (pieceMap. get (posindex + xl-posindex % 9) as PieceVO). Then Pos =-1)
{
Temp. addItem (posindex + xl-posindex % 9 );
}
Else
{
Temp. addItem (posindex + xl-posindex % 9 );
Break;
}
}
// Vertical direction
For (var yb: int = Math. floor (posindex/9) + 1; yb <10; yb ++)
{
If (pieceMap. get (posindex + 9 * (yb-Math.floor (posindex/9) as PieceVO). Adjust Pos =-1)
{
Temp. addItem (posindex + 9 * (yb-Math.floor (posindex/9 )));
}
Else
{
Temp. addItem (posindex + 9 * (yb-Math.floor (posindex/9 )));
Break;
}
}
For (var yt: int = Math. floor (posindex/9)-1; yt> = 0; yt --)
{
If (pieceMap. get (posindex + 9 * (yt-Math.floor (posindex/9) as PieceVO). Adjust Pos =-1)
{
Temp. addItem (posindex + 9 * (yt-Math.floor (posindex/9 )));
}
Else
{
Temp. addItem (posindex + 9 * (yt-Math.floor (posindex/9 )));
Break;
}
}
Return temp;
}

 

2.2.7 ro routes

The playing chess line of the gun is relatively complicated, but it is also well handled. The basic situation is the same as that of the car. However, when you find the first chess piece in a certain direction, you can set a judgment, after finding a pawn and no longer adding the remaining position, find the second pawn. If the second pawn is found, add the second one to the index.

/**
* Go chess
* @ Param posindex
* @ Return
*
*/
Public static function initMovePao (posindex: int, pieceMap: HashMap): ArrayCollection {
Var temp: ArrayCollection = new ArrayCollection ();

// Module direction
Var findOne: Boolean = false;
For (var xr: int = posindex % 9 + 1; xr <9; xr ++)
{
If (pieceMap. get (posindex + xr-posindex % 9) as PieceVO). Then Pos =-1)
{
If (! FindOne)
Temp. addItem (posindex + xr-posindex % 9 );
}
Else
{
If (! FindOne)
{
FindOne = true;
Continue;
}
Else
{
// Add the location of the next child that finds the child
Temp. addItem (posindex + xr-posindex % 9 );
Break;
}

}
}
FindOne = false;
For (var xl: int = posindex % 9-1; xl> = 0; xl --)
{
If (pieceMap. get (posindex + xl-posindex % 9) as PieceVO). Then Pos =-1)
{
If (! FindOne)
Temp. addItem (posindex + xl-posindex % 9 );
}
Else
{
If (! FindOne)
{
FindOne = true;
Continue;
}
Else
{
// Add the location of the next child that finds the child
Temp. addItem (posindex + xl-posindex % 9 );
Break;
}

}
}
// Vertical direction
FindOne = false;
For (var yb: int = Math. floor (posindex/9) + 1; yb <10; yb ++)
{
If (pieceMap. get (posindex + 9 * (yb-Math.floor (posindex/9) as PieceVO). Adjust Pos =-1)
{
If (! FindOne)
Temp. addItem (posindex + 9 * (yb-Math.floor (posindex/9 )));
}
Else
{
If (! FindOne)
{
FindOne = true;
Continue;
}
Else
{
// Add the location of the next child that finds the child
Temp. addItem (posindex + 9 * (yb-Math.floor (posindex/9 )));
Break;
}

}
}
FindOne = false;
For (var yt: int = Math. floor (posindex/9)-1; yt> = 0; yt --)
{
If (pieceMap. get (posindex + 9 * (yt-Math.floor (posindex/9) as PieceVO). Adjust Pos =-1)
{
If (! FindOne)
Temp. addItem (posindex + 9 * (yt-Math.floor (posindex/9 )));
}
Else
{
If (! FindOne)
{
FindOne = true;
Continue;
}
Else
{
// Add the location of the next child that finds the child
Temp. addItem (posindex + 9 * (yt-Math.floor (posindex/9 )));
Break;
}

}
}
Return temp;
}

3) There is no big difference between the game control engine of Chinese chess and the four-nation military chess. After we expand the BaseGameEngine, we only need to override the CheckWin () method to basically complete a lot of work. Of course, don't forget that the chessboard is not an initial layout. We only need to design a static layout and an initialization method.

// Default board Layout
Private const DEFAULT_LAYOUT: Array = [56, 57, 58,59, 60, 59, 58,57, 56,-1,-1,-1,-1,-1,-1,-1, -1,-1,-,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,155,-1,155,-1,155,-1,155,-1,155,-1,154,-1,-1,-1, -1,-1,154,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,156,157,158,159,160,159,158,157,156];

 

Public function initLayout (): void {
For (var I: int = 0; I <this. DEFAULT_LAYOUT.length; I ++)
{
Var pieceVO: PieceVO = pieceMap. get (I) as PieceVO;
PieceVO. value = DEFAULT_LAYOUT [I];
If (int (DEFAULT_LAYOUT [I])> 0 & int (DEFAULT_LAYOUT [I]) <100)
{
PieceVO. deskpos = getNextPlayer (1). deskpos;
}
Else if (int (DEFAULT_LAYOUT [I])> = 100)
{
PieceVO. deskpos = currentDeskpos;
}
}
}

After completing the above logic work, the result is as follows. After clicking the gun, we can clearly see the ro route index.

 

 

This is basically the core logic. In this cold winter, I plan to use this time to develop and optimize some interesting card and board games. My next goal is zinjinhua game, a card game, I plan to write a flexcardapi. Of course, in the next section of this article, I will provide the design and display of the extension class of Chinese chess. If time permits, it will take a short time. Of course, if you are a flex Developer, you can send me a message to complete the following development, I will teach you the completed design work, and you can also learn how to use this api. I believe that you will find fun, and you can also develop some simpler or more complex games, for example, you can play three games, animal games, or checkers.

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.