Chapter 7: Creating Multiplayer Games
Chapter 6, “Using Polymorphism via Interfaces,” discussed a basic framework for the creation of two-player games, but it did so with the assumption that one player would be human controlled and the other computer controlled. This chapter introduces a two-player game in which the first player is always human and the second player is either a human or a computer. Furthermore, the second human player can be sitting at the same computer as the first player, or she can be on another computer connected to the first one via a network.If you put your object-oriented-design hat on, you might quickly conclude that the concept of a “player” in this a two-player game might be a useful construct to abstract through one or more classes. After all, a player is a thing with responsibilities. A computer-controlled player must select the best move to make. A human-controlled player object needs to determine what piece a player clicked on and move a piece there. It should also disallow movement if it’s currently not that player’s turn. With two human players, the player object might be responsible for telling the other computer what move was made. Clearly, many of these responsibilities are simply not present when designing two-player games for one human player and one computer player. I smell a refactor!
Understanding Network Communication
The .NET language has a number of features that allow program components to communicate over a network (in fact, the name .NET implies some type of intranet-based software). Many of these features imply some form of server that acts as a data source to which programs can connect. An Extensible Markup Language (XML) Web Service, for example, is a .NET class that resides on a server; a remote program can access it over a regular Hypertext Transfer Protocol (HTTP) channel. Another feature known as remoting allows programs to use objects on the other side of a network that’s being served by another program.Much of the .NET method of program communication is new; if you’d like to study remoting in an in-depth way, pick up Advanced .NET Remoting (C# Edition) by Ingo Rammer (Apress, 2002). For this chapter, you’ll explore the VB .NET version of a C# example program written by student Nguyen Kinh Luan that contains a good example of peer-to-peer communication. By coincidence, this example was also a two-player game—the game of Gomoku (kind of a Connect Four without the gravity for you Americans). After looking at this game, I was impressed with the way Kinh Luan organized his classes within the game program. With his permission, this chapter uses his basic class layout and a similar method of his program-to-program communication in a two-player version of Reversi.