C # socket game programming (1 server, multiple clients)

Source: Internet
Author: User

 

There have been few blog posts in the last two days .. This example is too long.

The following is a reference to guide this example:

The rules are as follows:

(1) players compete with each other at the same table over the internet. One player chooses the black side and the other selects the white side.

(2) After the game starts, the computer automatically generates black or white pawns at random at a fixed interval within the 15x15 checkerboard.

(3) The goal of a player is to quickly click the color of the selected chess piece that automatically appears on the board, so that the pieces can be moved from the board to an hour, so as not to appear in the adjacent position.

(4) Each time a piece is taken from the board for an hour, a player of the corresponding color scores 1 point. Note: If the player clicks the opponent's pawn, the opponent scores 1 point.

(5) If two or more adjacent same-color pawns appear on the board, the game is over. Players who change the color are losers.

 

The game features are as follows:

(1) The server can serve multiple tables at the same time. Each table allows two players to compete over the internet.

(2) players are allowed to freely choose which table to sit on. If two players are sitting at the same table, both sides should be able to see each other's status. After both Players click the start button, the game starts.

(3) After a game starts, the server randomly sends the black and white pawns to the game at a fixed interval of 15x15 in the checkerboard, after the client receives the position and color of the pawns sent by the server, it displays the pawns at the corresponding position of the 15x15 board.

(4) After a player is seated at the game table, the player can adjust the time interval at which the server sends the pawns at any time no matter whether the game starts or not.

(5) After the game starts, the client program responds to the mouse click and calculates the score of the player according to the game rules.

(6) If two pawns of the same color are adjacent in the horizontal or vertical direction, the two pawns are considered to be adjacent, and the adjacent diagonal lines are not considered here.

(7) If pawns of the same color appear in adjacent positions, the game ends.

(8) Two gamers at the same table can chat.

 

Although this game is relatively small, it is "small but dirty ". If you really understand the writing method, you can easily write other TCP-based applications.

 

(I'm tired of typing. I typed the words one by one .)

------------------------------------------

After the above introduction, you can directly upload the code below ..

Another point is that the Program (client and server) has been tested.

It is not perfect, but it does not affect the study of socket TCP programming. A good example.

First, don't worry.

 

 

The figure shows the running status of two clients and one server.

 

Full download: http://www.bkjia.com/uploadfile/2011/1210/20111210023130590.rar

Note: I will not discuss the code perfection here. Only used to learn socket programming

Andrew's Blog

Related Article

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.