My Experience Creating a Simple Multiplayer Game Using Sockets
It’s the start of a new semester and for me that means a ton of new programming assignments to play with. The topic of this post covers this weeks assignment in my C# programming class. Without leaving out too much of the details, the assignment involves developing a Windows form application with numerous buttons and controls with the purpose of creating a way for two users to play a game of Tic-Tac-Toe sharing the same keyboard. Bear in mind the assignment is meant to be fairly low on the difficulty scale as a way to ease students in to the idea of programming with the Visual Studio designer. However, Professor Forman encourages his students to go above and beyond the necessary requirements, especially if her or she has previous programming experience, and often awards a huge amount of extra credit for exemplary work. As soon as I read the specifications, I immediately began thinking of ways I could push the limit on this simple assignment.
My main philosophy has always been to produce great quality software, regardless of what the assignment is. That is to say, if someone else had created my program and shown it to me, I want to be able to say that I enjoyed it. This can be a challenge when it comes to class projects since you’re being told what to program, but luckily there’s no one telling me how to program. Tic-Tac-Toe isn’t the most exciting of games, but I decided I would still try to create something impressive. That’s why, for the first time, I would try to implement a networked game-play mode into my project.
I really had no idea what I was doing at first. I searched around online looking for ways I could accomplish getting two computers to play the same game and I quickly discovered the Windows Socket class. By reading online tutorials, I was pretty quickly able to figure out how a program can connect two users over a network IP address using the TcpListener class. Without pouring over too much of the technical details, here’s a glimpse of some of that code that I include in my program courtesy of the Binus University tutorial on sockets.
The next challenge was figuring out how to send the tic-tac-toe board as a packet of bytes over the connection. What I ended up doing was storing the whole board in a two dimensional array that could easily be converted into bytes and sent over the network stream.
Eventually, I was able to string together a very basic version of my final product. At this point, I had no menus or assets or anything that really resembled a game, just the ability to send and receive data. I knew I had the workings of something cool already, what came next was just filling in the blanks.
From the previous examples shown in class, I learned that many students like to add a lot of creativity to this assignment. One of the ones that stood out to me was a student who did a Street Fighter themed tic-tac-toe game who went on to be a student at UCSD. I decided to go down that same route by choosing a theme based on one of my favorite games right now, Counter-Strike: Global Offensive.
Icons, player portraits, and background images were all fairly easy to find online. I created the menu panels and the UI layout myself which I tried to emulate to be as close as possibly to the actual design of the CSGO.
Sooner or later things began to look just how I wanted them to and I ended up completing the basic assignment, a simple shared keyboard version of tic-tac-toe, but there still loomed a serious problem. The actual game-play was riddled with bugs that I didn’t easily know how to fix. With time running short and exams in other classes approaching, I began hacking away to try and solve certain issues. How do both players know who’s turn it is? How does a player know if he or she won? How can one player’s application changed based on an action from the other? These were tough questions that took up the bulk of my time on this project. At one point, I almost gave up entirely, but as these seemingly insurmountable problems began to be solved one-by-one, my energy was renewed to keep on going. The hours I spent debugging while hovering over my laptop and desktop simultaneously ended up paying off and I breathed the biggest sigh of relief in my life.
As everything began coming together, the last thing on my plate was adding in sound effects. I spent a while doing my usual routine: finding good quality .wav files to download that sound close enough to work. Then, out of the blue, I stumble across a post from a user on the Steam forums writing about a program called GCFScape that can be used to extract files from CSGO’s install directory.
When I opened up the “pak01_dir.vpk” file to find all of the sound files I would ever need to complete my program I was really pleased. One of the most tiresome and frustrating aspects of polishing a game for someone without natural artistic talent has to be scouring the internet for game assets to “borrow”, so much so that I put it off until everything else was in place. Now, I could finish the game with extra time over the weekend to spare for my other classes.
After all was said and done and my final commit was pushed, I tallied about twenty hours of work on this game. I took this simple assignment into my own hands and decided to challenge myself by doing something I’ve never attempted before and in the end it worked out great. Nothing feels better than accomplishing your goals, especially when that involves producing something you’re really proud of.
Tic-Tac: Global Offensive can be downloaded here:
https://www.dropbox.com/s/ls8kjbhkyoguzdo/Tic-Tac%20Global%20Offensive.rar?dl=0