Flex online game development-Landlords game: (1) core logic

Source: Internet
Author: User
Tags server website

This is the first card game I want to develop. The landlords themselves are a limited-user turn-based game. The core logic is not very different from that of chess games, however, because there is not much chess algorithm, the location is not that important. So I decided to develop the game using the flexchessapi developed by the four-nation military games. If flexchessapi is not competent, I would consider forming a general flexcardapi to develop the game. In this way, I can use flexcardapi to develop more games, such as fraud.

Results After core logic development

Now, let's design some general logic thinking about card categories and put them in a static class such as CardHelp. ..

1) Design a card

We should give a value to each card, each of which includes the color and size. In this way, we represent a card with a three-digit value, for example, 303 represents safflower 3,414 represents Black peach A. In this way, we can design a complete array of cards. Different cards require different numbers of complete cards. For example, Zarn is not king, the landlords are king. Now, we design a card.

/**
* A complete card with landlords leading the king
* In landlords, 2 is A card bigger than A. We cannot call it 102, so it is called 115.
*/
Public static const CARDS_WITH_JOKER: Array = [115,103,104,105,106,107,108,109,110,111,112,113,114,215,203,204,205,206,207,208,209,210,211,212,213,214,315,303,304,305,306,307,308,309,310,311,312,313,314,415,403,404,405,406,407,408,409,410,411,412,413,414,518,519];

/**
* A complete card with no king in Zarn
*/
Public static const CARDS_NO_JOKER: Array = [102,103,104,105,106,107,108,109,110,111,112,113,114,202,203,204,205,206,207,208,209,210,211,212,213,214,302,303,304,305,306,307,308,309,310,311,312,313,314,402,403,404,405,406,407,408,409,410,411,412,413,414];

Design two help methods

/**
* Obtain a specific color
* @ Param card
* @ Return
*
*/
Public static function getHuaShe (value: int): int {
Return Math. floor (value/100 );
}
/**
* Obtain the original value of a specific card
* @ Param card
* @ Return
*
*/
Public static function getPureValue (value: int): int {
Return value % 100;
}

2) When the card is ready, we start to design a licensing function.

2.1) if you send a card, the licensing function is relatively simple. You can just take one card from the card array. Note that you should delete the card after the card is obtained, to avoid sending the same card again next time.

/**
* Obtain a random card value in the card group
* @ Param cards
* @ Return
*
*/
Public static function getRadomCard (cards: ArrayCollection): int {
If (cards. length = 0) return-1;
Var key: int = Math. round (Math. random () * (cards. length-1 ));
Return cards. removeItemAt (key) as int;

}

2.2) in the case of landlords, 17 cards are in the first hand and the last three cards are in the back. During licensing, we consider sorting cards. In this way, after the cards are issued, they are automatically arranged in ascending order.

/**
* Get a random hand card.
* @ Param cards all cards
* @ Param num: number of cards
* @ Param sort: whether to sort
* @ Return
*
*/
Public static function getRadomCards (cards: ArrayCollection, num: int, sort: Boolean = true): ArrayCollection {
Var temp: ArrayCollection = new ArrayCollection ();
Do {
Var cardvalue: int = getRadomCard (cards );
Trace ("generate card:" + getDisplayName (cardvalue ));
If (! Sort)
Temp. addItem (cardvalue );
Else
{
Var added: Boolean = false;
For (var I: int = 0; I <temp. length; I ++)
{

Var value: int = temp. getItemAt (I) as int;
If (cardvalue % 100> value % 100)
{
Temp. addItemAt (cardvalue, I );
Added = true;
Break;
}

}
If (! Added)
{
Temp. addItemAt (cardvalue, I );
}


}

Num --;
} While (num> 0 );
Trace ("sorted:" + temp. toString ());
Return temp;
}

3) Go to the logic of the landlords. Now we design a common class named DoudizhuHelp..

There are many card models and different algorithms in the landlords game, but I always think some common algorithms on the Internet are a bit overwhelming. So I decided to rethink the algorithm myself. In general, no matter what you are, three-to-two, blow-up, and four-to-one, you always have a primary card. You only recognize the number of cards and the size does not matter, I should have checked out the main card only the sub-, three, bombs, dual kings, and shunzi. So after licensing or getting the license card, we can simply clear all the main card types.

Now, write a function to clear the main card type.

/**
* Obtain all the cards of the current card
* @ Return
*
*/
Public function initType (): void {

Var temp: ArrayCollection = new ArrayCollection ();

For (var I: int = 0; I <_ cardAC. length-1; I ++)
{

Var card1: ICard = _ cardAC. getItemAt (I) as ICard;
Var card2: ICard = _ cardAC. getItemAt (I + 1) as ICard;

// Add to the double king
If (card1.pureValue = 18 | card1.pureValue = 19)
{
If (! ShuangwangAC. contains (card1.pureValue ))
ShuangwangAC. addItem (card1.pureValue );
}
// For the same part, we only need to judge it once
If (card1.pureValue = card2.pureValue)
{

// Add to bomb
If (this. santiaoAC. contains (card1.pureValue ))
{
If (! This. zhadanAC. contains (card1.pureValue ))
This. zhadanAC. addItem (card1.pureValue );
}


// Add to three
Else if (this. duizhiAC. contains (card1.pureValue ))
{
This. santiaoAC. addItem (card1.pureValue );
}
// Add to child
Else
{
This. duizhiAC. addItem (card1.pureValue );
}



}
// Add to shunzi
Else if (card1.pureValue = card2.pureValue + 1 & card2.pureValue! = 3)
{
// 2 cannot be added to shunzi
If (card1.pureValue = 15) continue;

If (! Temp. contains (card1.pureValue ))
Temp. addItem (card1.pureValue );
If (! Temp. contains (card2.pureValue ))
Temp. addItem (card2.pureValue );
}
// The number of digits is not equal to or has reached the end
Else
{
If (card2.pureValue = 3) temp. addItem (card2.pureValue );
If (temp. length> = 5)
{
This. sunzhiAC. addAll (temp );
}
Temp = new ArrayCollection ();

}



}
// If there is no dual king, the dual king set is cleared.
If (shuangwangAC. length <2)
{
ShuangwangAC. removeAll ();

}


Trace ("double king..." + this. shuangwangAC. toString ());
Trace ("child..." + this. duizhiAC. toString ());
Trace ("Three..." + this. santiaoAC. toString ());
Trace ("shunzi..." + this. sunzhiAC. toString ());
Trace ("bomb..." + this. zhadanAC. toString ());

}

Analyze the meaning of each sentence. The function should be easy to understand. I haven't found any bugs yet, but I always don't feel relieved :). Experts can help me find bugs. Thank you...

Now, I tried to analyze the cards.

Double King ..
Child..., 3
3...
Shunzi ...,
Bomb ....

In the past month, I have already opened too many headers. I wrote three in four military games, two in five games, and one in Chinese chess, so I decided to complete the logic analysis of the online game server website www.boyiju.com and develop the APIs of the online game client, then, all the games that have completed the logic are updated in a unified manner. Today, with the rapid development of third-party applications, my goal is to build a public open-source platform that allows all programmers to easily develop and test third-party game applications. The platform combines a complete sns website testing environment with the following APIs, interested friends can keep an eye on the progress of project development at any time. If you are extremely interested, or you are still living with the ambition of the work part. You can also add QQ 2 1 0 7 3 4 5 2 to learn about this project or participate in the development of this project. You don't have to worry about me. You only have to worry about it, developers with no bad habits.

Related Article

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.