Online Demo address: Silverlight + WCF novice instance chess Online Demo
In the Silverlight + WCF novice instance, the chess main interface-chess spectrum-playback (), we implemented the playback of the user's chess spectrum. Below, we left two questions:
1: Do the player play back the game while playing the game? If so, what do you need to pay attention?
2: During the playback process, the audience suddenly encountered another chess step. What do you need to pay attention?
Before answering these two questions, let's first answer the questions found in the previous article:
I wonder if anyone has found out? There are three "Guns" or three "horses" in the figure. The cause of the accident is,
It is because when we Reset chess. Reset each time, we did not clear the pawns list, resulting in repeated pawns. After finding the cause, the solution is quite simple,
As long as the Reset method in the chess class is indeed added, the. clear method can be used to clear a row of chess pieces. Only the first line is added:
Public void Reset ()
{
ChessmanList. Clear (); // Add a pawn list to Clear
Container. Children. Clear (); // the chessboard and the pawns are cleared together.
InitBoard (); // you have to re-initialize the board.
InitChessman (); // you have to re-initialize the pawn.
IsCanMove = false; // The status cannot be moved.
IsGaming = false; // I almost forgot this
}
Okay, one line of code solves the bug that we hidden in the image in the previous section.
Next, we will answer the two questions left in the previous section:
Let's sort down the questions and answer the second question first: What do you need to pay attention to when the audience suddenly goes into a game during playback?
In the previous section, we defined a global variable:
Public static bool chessManualPlaying = false; // The score is being played back.
In the previous section, I only mentioned that [very good] will be used later, so we will use this section, because this variable can be used to answer the question:
Let's assume that when the audience is playing back the game, the players are constantly playing the game and passing new game moves;
At this time, we only need to deal with it, but do not perform automatic playback. Just write the text in the chess and music area and add it to the chess and music list as usual.
Therefore, the task is to set App. chessManualPlaying to true when playing the video. After playing the video, set it to false;
So we added the following code when returning to play:
Private void PlayStep ()
{
If (App. stepList. Count> 0)
{
// Reset the pawns before playing
App. chess. Reset ();
App. chessManualPlaying = true; // Add a row
Timer. Interval = TimeSpan. FromSeconds (slPlayerInternal. Value );
Timer. Start ();
}
}
So we will go back to the code added at the end of playback:
Void timer_Tick (object sender, EventArgs e)
{
//... N rows omitted...
If (moveStepIndex = App. stepList. Count) // The chess step ends.
{
MoveStepIndex = 0; // reset the index
Timer. Stop (); // Stop timer
App. chessManualPlaying = false; // Add a row
}
}
After the identification space is added, let's go back to the place where auto-move the game is set and add judgment. If the game is playing, cut off the automatic movement of the game:
The Chess. xaml. cs app receives the Chess step notifications. We only need to add a half line of code [below! App. chessManualPlaying]:
Void client_NotifyMoveStepReceived (object sender, NotifyMoveStepReceivedEventArgs e)
{
App. stepList. Add (e. player. Step); // Add a chess Step
If (! App. chessManualPlaying & App. player. ID! = E. player. ID) // non-player or self-player
{
//... Omitting the Code related to chess and mobile games...
}
HelpSetChessManualEvent (e. player. Step); // write the score Area
}
The second problem is solved now. A total of 2.5 lines of code are added.
Now, the first question is: Do you want to enable the "playback" function when the player is playing the game? If so, what should you pay attention?
Not open: you only need to set the Enable status of the "playback" button to false;
Openness: it is not difficult: we only need to limit the playing process to prevent players from playing chess,
At the same time, the player playing process will produce the first problem: however, we have solved the problem just now, and the added code is equally effective for the player.
How to restrict players from playing chess is actually very simple. The chess. IsCanMove attribute can be limited, as long as it is set to false during Playing, after playing, set back to the original value;
Does it feel too similar to the first question? Start and end the playing, but add a variable to save the player's playing status:
Add a global variable line:
Public partial class ChessManual: UserControl
{
//... Omit 2 lines of code...
Bool tempIsCanMove; // Save the status before the player.
Public ChessManual ()
{
//... N lines of code are omitted...
}
//... N lines of code are omitted...
}
When playing the video, first take the status, and then set IsCanMove = false [this sentence is already in Reset, which can be saved];
Private void PlayStep ()
{
If (App. stepList. Count> 0)
{
// Reset the pawns before playing
TempIsCanMove = App. chess. IsCanMove; // status before saving
App. chess. Reset (); // IsCanMove = false is set when the status is Reset, so no more than one row is required.
App. chessManualPlaying = true;
Timer. Interval = TimeSpan. FromSeconds (slPlayerInternal. Value );
Timer. Start ();
}
}
When the playback ends, the status is set back to [last line of code]:
Void timer_Tick (object sender, EventArgs e)
{
//... N lines of code are omitted...
If (moveStepIndex = App. stepList. Count) // determines whether the game is completed.
{
MoveStepIndex = 0; // reset the index
Timer. Stop (); // Stop timer
App. chessManualPlaying = false;
App. chess. IsCanMove = tempIsCanMove; // Add a row
}
}
OK. Now, we have used the same method to solve the two problems in the previous section. Next we will go to the showtime of F5:
Normal. Now the playback of the game is normal:
1: players playing chess soon:
2: when the audience comes in, they play the game and locate the real-time status:
3: The audience wants to watch the game and start playing back the game:
4: Play back to Step 3:
5: Play back to Step 5:
OK. Now, I wrote 40 articles in this series accidentally. In the 40 articles in this series, all functions are fully open source code and explained!
Interested readers are welcome to follow this series of articles, and the company will move again tomorrow. Is there any follow-up articles in this series? The current situation is unknown ......
Next, update the index: Silverlight + WCF beginner instance chess topic Index