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.