Network programming asynchronous Gobang game __ Programming

Source: Internet
Author: User

A Experimental requirements

Reference materials Fifth Chapter 2nd game procedure, according to the following requirements for adaptation, requirements are as follows:

1 The game to the two sides, rather than the system of automatic chess;

2 Modify the rules of the game, such as Gobang game rules;

3 synchronous change asynchronous;

Two Design ideas

1. First of all, this experiment is mainly modified experiment, so the first step of thinking is to understand the structure of the original code, and the structure of the new Code analysis, analysis as follows

(1) The server is responsible for providing a number of game table services, can choose the number limit (1~200), Game Table restrictions (1~100)

(2) The server has the "Start service" and "Stop service" function, only after starting the service to allow the client to connect the game

(3) After the client login can be clicked in the Game room table available tables into the table, each game table can accommodate two players, the same table after the two sides are ready to click, the game began.

(4) If one side first let five children alignment situation (row, column, diagonal) appear, it is a victory, the game is over.

(5) Players at the same table can chat.

2. Then, formally start coding, the first is synchronous asynchronous conversion:

Server-side asynchronous receive client connection:

<summary> Receive client Connections </summary>

private void Listenclientconnect ()

{

TcpClient newclient = null;

while (true)

{

Listenclientdelegate d = new Listenclientdelegate (listenclient);

IAsyncResult result = D.begininvoke (out newclient, NULL, NULL);

while (result. IsCompleted = = False)

{

if (normalexit)

{

Break

}

Thread.Sleep (50);

}

D.endinvoke (out of newclient, result);

Each time a client connection is accepted, a corresponding thread is created to iterate over the information sent by the client

if (newclient!= null)

{

Parameterizedthreadstart pts = new Parameterizedthreadstart (receivedata);//Use Parameterizedthreadstart to create a thread that can pass parameters

Thread threadreceive = new Thread (pts);

User user = new user (newclient);

Threadreceive.start (user);

Userlist.add (user);

Service. AddItem (String. Format ("{0} entry", NewClient.Client.RemoteEndPoint));

Service. AddItem (String. Format ("Current number of connected users: {0}", Userlist.count));

}

}

}

Private delegate void Listenclientdelegate (out tcpclient newclient);

private void Listenclient (out tcpclient newclient)

{

Try

{

Wait for user to enter

Newclient = Mylistener.accepttcpclient ();

}

Catch

{

AcceptTcpClient () generates an exception when you click Stop Listening or when you exit this form

So you can use this exception to exit the loop

Newclient = null;

}

}

The client sends the message asynchronously to the server:

public void Sendtoserver (String str)

{

Sendmessagedelegate d = new Sendmessagedelegate (SendMessage);

IAsyncResult result = D.begininvoke (str, NULL, NULL);

while (result. IsCompleted = = False)

{

Thread.Sleep (50);

}

D.endinvoke (result);

}

delegate void Sendmessagedelegate (String message);

public void SendMessage (String message)

{

Try

{

Sw. WriteLine (message);

Sw. Flush ();

}

Catch

{

Additemtolistbox ("Send data Failed");

}

}

3. Then remove the AI and set the order of the shot, that is, when one side of the action, must wait for the other side also action completed, in order to proceed.

if (Gametable[tableindex].ifstart = = True && Gametable[tableindex]. Cansetdot[side] = = True)

{

Gametable[tableindex]. Cansetdot[side] = false;

Gametable[tableindex]. cansetdot[(side + 1)% 2] = true;

Gametable[tableindex]. Setdot (xi, XJ, color);

}

This paragraph originally wanted "both sides to determine the beginning, the game begins", the function, I added a paragraph in the back, to determine who shot, immediately set it to false, the other side set to true, to determine who shot.

4. The next step is to judge the conditions of victory, here I use is nested loops, from the horizontal, vertical, right oblique, left oblique, one by one check, if the 5, then return the winner of the Color (-1, no one wins, 0 black side wins, 1 white wins)

private int Checkx (int y)//Detect Landscape

{

for (int x = 0; <= x + +)

{

int color = grid[x, y];

if (color = = 1)

Continue

if (grid[x + 1, y] = = Color &&

Grid[x+2,y] = = Color &&

Grid[x+3,y] = = Color &&

Grid[x+4,y] = = color)

{

return color;

}

}

return-1;

}

private int Checky (int x)//Detect Portrait

{

for (int y = 0; y <= y++)

{

int color = grid[x, y];

if (color = = 1)

Continue

if (grid[x, y+1] = = Color &&

Grid[x, y+2] = = Color &&

Grid[x, y+3] = = Color &&

Grid[x, y+4] = = color)

{

return color;

}

}

return-1;

}

private int checkaddx (int y)//Check right skew

{

for (int x = 0; <= x + +)

{

int color = grid[x, y];

if (color = = 1)

Continue

if (grid[x+1, y + 1] = = Color &&

grid[x+2, y + 2] = = Color &&

Grid[x+3, y + 3] = = Color &&

Grid[x+4, Y + 4] = = color)

{

return color;

}

}

return-1;

}

private int checksubx (int y)//check left skew

{

for (int x = 4; <= x + +)

{

int color = grid[x, y];

if (color = = 1)

Continue

if (grid[x-1, y + 1] = = Color &&

Grid[x-2, y + 2] = = Color &&

Grid[x-3, y + 3] = = Color &&

Grid[x-4, Y + 4] = = color)

{

return color;

}

}

return-1;

}

Three Program Operation Effect Chart

1. Server Startup

2. Client-initiated

3. Game interface between the two sides

4. Winning interface


The download link to the code below is attached http://download.csdn.net/detail/u010669765/7500297

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.