[Original] Using C # writing three kingdoms in one step (3): Design Process

Source: Internet
Author: User

OriginalArticle, Reprinted, please keep the author's signature!

 

As we have already discussed the design of the card stack, we will now officially enter the process to meet our requirements described in (1.

 

As we have already mentioned in (2) that we want to maintain the extension, we need to define a selected extension package for the previously defined scene,CodeAs follows:

 

Expansion package

Private   Readonly Ipackage [] selectedpackages;

///   <Summary>
/// Initialize a new <See CREF = "scene"/> Class.
///   </Summary>
///   <Param name = "packages"> The package to be loaded. </Param>
Public Scene (ienumerable < Ipackage > Packages)
{
Players [currenttoken]. hastoken =   True ;
Selectedpackages = Packages. toarray ();
}

 

 

As players are rotated, a token is set and only players holding the token can act.

 

Private   Int Currenttoken;

 

 

Define a start method to start the game loop.

 

First, you must generate a game board heap based on the selected extension package. For game card heap, since we have defined the card heap base class, the implementation is relatively simple. Load and shuffles the game cards in the expansion package.

 

Game Board heap

///   <Summary>
/// Indicates the game card heap.
///   </Summary>
Public   Sealed   Class Gamecardheap: cardheap < Gamecard >
{
///   <Summary>
/// Initialize a new <See CREF = "gamecardheap"/> Class.
///   </Summary>
///   <Param name = "packages"> The extension package to be loaded. </Param>
Public Gamecardheap (ienumerable < Ipackage > Packages)
{
Foreach (VAR package In Packages)
{
(List < Gamecard > ) Items). addrange (package. gamecards );
}
Items. Shuffle ();
}
}

 

Looking back at the above start method, after the card heap is created, it enters the Process Loop. Let's go directly to the code. The comments are clearly written.

 

Process Loop

///   <Summary>
/// Start the game.
///   </Summary>
Public   Void Start ()
{
Player currentplayer;
Gamecardheap heap =   New Gamecardheap (selectedpackages ); // Create a card heap

While ( True )
{
Currentplayer = Players [currenttoken]; // Set players with tokens

//Card touch stage
Gamecard [] newcards=Heap. Pop (2,True);
Currentplayer. Draw (newcards );

// Playing stage
While (Currentplayer. hasplayablecard)
{
// Note: To make it simple, you can only kill the house and use kill, Flash, peach
Int Nexttoken = Currenttoken = Players. Length -   1   ?   0 : Currenttoken +   1 ;
Currentplayer. Play (players [nexttoken], currentplayer. firstplayablecard );

If(Isgameends ())
GotoLabel;
}

// Card discard stage
// Note: To make it simple, you can discard the cards from the beginning to the current physical strength.
Int Discardcount = Currentplayer. handcards. Length - Currentplayer. HP;
If (Discardcount >   0 )
{
Gamecard [] removecards = currentplayer. handcards. take (discardcount ). toarray ();
currentplayer. discard (removecards);
}

//Send the token to the next person
Givetokentonext ();
}
Label:
Console. writeline ("The game is over!");
}

 

 

An endless loop is used here, but the GOTO statement is used to exit the loop at the end of the game. The methods involved are as follows:

 

Auxiliary Methods

Private   Void Givetokentonext ()
{
If (Currenttoken = Players. Length -   1 )
Currenttoken =   0 ;
Else
Currenttoken ++ ;

players [currenttoken]. hastoken = true ;< BR >}

Private BoolIsgameends ()
{
ReturnPlayers. Any (P=>P. isdead);//If you select isdead player, the game ends.
}

 

 

note: In the process design, players are within the logic boundary of the game, so they adopt an active approach for design. In this case, for the Program , I only need to consider the changes caused by the player's touch, play, and discard.

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.