Introduction
Dots game is a simple nice multiplayer game. Each player draws a line on a grid and gives the turn to other players. The aim is to get as much boxes as possible. The original game downloaded from the Internet lacked a lot of fun because it was playable on only one machine. There is a need for a way to play the game where each player uses his own computer and play his turn.
The project is a multiplayer game that can be played across LANs and Internet. The project intends to allow people to play across networks which solve the problem of taking turns. Also, making it playable via networks allow remote people to play together.
In order to develop the game, the following concepts were used:
- Serialization.
- Multithreading.
- Logic Synchronization.
The idea of the program is to create two sockets for each player. One handles the game messages and the other handles the chatting part. When the player first runs the program, he has two basic options. Either Host or Join a game. To join a game, the player must have the IP address of the person that he will play with.
Serialization is used to pass an object that has all of the latest board information. So, whenever a player makes a move, that move is passed to the server which then passes it to the other players. But since the current implementation is limited to two players, the server receives the information, updates, and then allows a move send that moves to the player which then repeats the process.
Multithreading was needed in many places since there is extensive use of GDI commands. But the main part where it is used is chatting. For the chatting, there is a thread to handle incoming messages and pass them to the GUI.
The most crucial and difficult part was the synchronization. It is very fortunate that TCP handles many issues. But there were still many things to handle. Like turns, blocking, winning, scores, etc. The messy code impacted a lot which highlighted the importance of organized design and OOP design methods, even if it was simple; as long as there is messaging and other OOP concepts.
The following are the limitations of the final product:
- Only two players can play.
- Cannot play if behind a firewall or a NAT.
- Players must agree before playing in order for them to know their IPs.
- Since it is implemented in C#, it will not run on machines without .NET runtime environment.
These are the lines for the serialization part:
IFormatter sender = new BinaryFormatter();
sender.Serialize(this.ns[n],o);
IFormatter getter = new BinaryFormatter();
if(ns[n].DataAvailable)
{
packet = (Packet) getter.Deserialize(ns[n]);
ns[n].Flush();
return packet;
}
References: