Silverlight + new WCF instance: who should play chess?-A: B: stopped (28th)

Source: Internet
Author: User

Online Demo address: Silverlight + WCF novice instance chess Online Demo

 

In the previous section, we implemented the "Start" game and notified each other to start the game. However, we did not restrict the pawns, and both sides were able to go down at any time;

Therefore, we need to limit the number of chess pieces. The number of chess pieces under A is B, and the number of pieces under B is A. At the same time, we need to transfer the chess step. when the other party receives the chess step, it needs to reverse the coordinates of the chess step, you have to move the pawns automatically.

 

Where can we start with this? In fact, I also thought about it for a long time ......

Let's return to the Chess class, so we add an attribute IsCanMove

// Chess by passing by autumn
Public class Chess
{
/// <Summary>
/// Whether the pawn can be moved
/// </Summary>
Public bool IsCanMove
{
Get;
Set;
}

//... Omitting N multiple rows...

}

 

OK, the attributes are available. We can use this attribute to control whether the pawns can be moved. Then, we return to the Chessman Click Event of the pawns class. With the restriction, the Chessman click event cannot be moved:

// Newly added event method, pawn Click Event
Void chessman_MouseLeftButtonDown (object sender, MouseButtonEventArgs e)
{
If (! Parent. IsCanMove)
{
Return;
}

//... N rows are omitted below...
}

 

With an attribute and a restriction, you can easily get a few lines of code. The next thing is to decide when to set IsCanMove to true or false.

OK. Let's go back to the [previous section] and click "start". Find the code that receives the "Reply to start". Now that we can start, set this attribute to true.

Let's go back to the EventButton. xaml. cs code:

Void client_policystartgamereceived (object sender, GameService. policystartgamereceivedeventargs e)
{
// What should I do if I receive the message?
Switch (e. player. AttachInfo)
{
//... N lines of code are omitted...
Case "11 ":
MessageBox. Show ("the other party agrees to start the game. Please start playing chess", "Game notification", MessageBoxButton. OK );
BtnGameStart. IsEnabled = false;
// Set Chess. IsCanMove = true here;
Break;
}
}

 

After the other party agrees to start the game, we need to set it to true so that we can play the game. However, our chess object is in another chess Zone Control, how to transmit messages?

We have talked about the control message transmission (26) in the main chess interface of the Silverlight + WCF novice instance. If you don't understand it, go back and have a look ......

 

However, this time we use another method to transmit messages. After all, this attribute or chess class is a very important class and may be used everywhere. We cannot accept messages everywhere, even if you entrust people once or twice, it will be annoying to delegate everything, and the code will be annoying.

 

So what method is used? Do you still remember our player and client objects? They can be used everywhere. So, go back to App. xmal. cs and add a global variable:

Have you found that I have become smarter recently and often omitted the previous code to make the key code more intuitive.

Public partial class App: Application
{
Grid root = new Grid ();
Public static GameService. ServiceClient client; // The callback client.
Public static GameService. Player player; // The current Player.
Public static ChessNewInstance. Chess chess; // Chess class, newly added
Public App ()
{
//... Omitted...
}
}

 

At this time, the sky in Guangzhou is constantly shining...

 

OK, the global is also available. Where can we assign a value to the global value? We can't take a Null object to the call.

Therefore, let's go back to Chess. xaml. cs and assign a global value to the Chess instance:

If the code is short, Copy it all in:

Public partial class Chess: UserControl
{
ChessNewInstance. Chess chess; // here we also mention it to the Global Object
Public Chess ()
{
InitializeComponent ();
Chess = new ChessNewInstance. Chess (canvasRoot );
Chess. InitBoard ();
Chess. InitChessman ();
CanvasRoot. Width = chess. Board. Width;
CanvasRoot. Height = chess. Board. Height;
App. chess = chess; // assign a value to a Global Object
}
}

 

Everything is ready. Now we can use App. chess objects everywhere, so we go back to EventButton. xaml. cs, just to add a line of code:

Void client_policystartgamereceived (object sender, GameService. policystartgamereceivedeventargs e)
{
// What should I do if I receive the message?
Switch (e. player. AttachInfo)
{
//... N rows are still omitted...

Case "11 ":
MessageBox. Show ("the other party agrees to start the game. Please start playing chess", "Game notification", MessageBoxButton. OK );
BtnGameStart. IsEnabled = false;
App. chess. IsCanMove = true; // set it here and add this line.
Break;
}
}

 

 

OK. Here, let's take a look at F5 and start again:

Let's take a closer look at the conversation:

The figure in the chess part area will not be cut off. Let's take A closer look at the dialog area. As we only implement A part, A can be down and B will stop. So it is always in this status. A can keep running and B can stop sending messages.

In this status, we will leave it to the next section to handle the situation. If we look at it carefully, we will also find that both sides have black pawns on their heads. That is to say, both sides use red games ???

Well, we also have a separate section to explain.

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.