Concepts / TicTacToe Game Design

TicTacToe Game Design

The two-player Poker program follows a design similar to TicTacToe, with a Main class run by both players.

  • Programming

Why Poker Needs a Different Kind of Reading

Most of the networked game examples in this course are simple enough to read in one sitting. The two-player Poker program is different. It implements a two-player version of the traditional five card draw game, and the source material is explicit that the application is complex. Reading every line of it would be slow and would hide the bigger picture, so this article looks at the shape of the design instead: which parts exist, how they relate to one another, and what the two players actually do when they sit down to play.

The design is also a good example of a pattern you may already know. The two-player Poker program follows a design similar to TicTacToe, with a Main class run by both players. As you read, keep one question in mind: if you already understand how a simpler networked game is organized, which parts of that organization can you recognize here, and which parts are new?

Two Players, Two Roles

Before looking at any class, start with the people using the program. Both players run the same Main class. Nothing in the setup treats one copy of the program as a server and the other as a client living in a separate codebase. The two players are distinguished by what they do, not by which program they run.

The first player starts a new game. The second player must join that existing game rather than creating a second one. The diagram below traces this order of events. It is an illustration built from the source's description, not a transcript of the program's actual code, so it shows the roles and their order rather than exact method calls.

First playerRuns MainShared gameManaged by PokerHubSecond playerRuns Mainstarts a new gamejoins existing game
What happens in what order when the first player starts a game and the second player joins, and how the two copies of the program connect through the shared game.
Worked example

Tracing the Two Roles Through a Session

Two people want to play one game of two-player Poker. Trace what each of them does and what the program's roles require.

  1. 1
    Both people launch the program

    Each person runs the same Main class. Neither copy is labeled as a server or a client in a separate codebase.

  2. 2
    The first player starts a new game

    The first player's choice creates the game that the second player will join. This is the role that begins the session.

  3. 3
    The second player joins the existing game

    The second player must join the game that already exists. Starting a second, separate game is not the second player's role.

  4. 4
    The game is shared

    Both players now take part in one game. PokerHub manages that game, and PokerGameState represents its state.

Result

The two players are distinguished by their actions: one starts the game and the other joins it. Both run the same Main class.

The Three Named Parts of the Design

The Poker program is built from three named parts. The Main class is the entry point that both players run. The PokerGameState class represents the state of a game. The PokerHub class is a subclass of Hub, and its job is to manage the game. The diagram below shows how these parts relate.

Each playerPerson at a keyboardMainEntry point both playersrunPokerHubManages the gameHubnetgame framework classPokerGameStateRepresents the game staterunmanages the gamesubclass of
Which piece contains or extends which, and where the game state lives relative to the hub that manages the game.

A useful way to separate responsibilities is to ask two questions about any piece of the program. Where does the information about the game live? And what keeps the game moving? PokerGameState answers the first question, because it represents the state of a game. PokerHub answers the second, because it manages the game. Main answers neither directly. It is the place where each player begins, so it is the connection between a person at a keyboard and the shared game.

PokerHub as a Subclass of Hub

The source names PokerHub as a subclass of Hub. This tells you that the Poker design builds on the netgame framework's existing Hub machinery instead of replacing it. The subclass relationship is the key idea: PokerHub inherits from Hub, and the Poker-specific behavior is what PokerHub adds on top of that foundation.

HubExisting netgame machineryPokerHubManages the poker gamesubclass
How PokerHub inherits from Hub, and where its poker-specific role sits in the hierarchy.

Mistakes Learners Make with This Design

  • Common mistake

    Assuming one copy of the program is a server and the other a client

    What it looks like

    Looking for a separate server program and a separate client program in the Poker package.

    Why it fails

    Both players run the same Main class. The players are distinguished by what they do, not by separate codebases.

    Do this instead

    Think of both players as running the same program, with the first starting a game and the second joining it.

  • Common mistake

    Thinking the second player creates a second game

    What it looks like

    Imagining that each player starts their own game and then the two games are matched.

    Why it fails

    The second player must join the game that already exists rather than creating a second one.

    Do this instead

    Trace the session as one game that the first player starts and the second player joins.

  • Common mistake

    Treating PokerHub as a replacement for Hub

    What it looks like

    Assuming PokerHub is written from scratch and has nothing to do with Hub.

    Why it fails

    The source names PokerHub as a subclass of Hub, so the design builds on the existing Hub machinery.

    Do this instead

    Read PokerHub as a specialization of Hub for poker, while remembering that the source does not describe Hub's internals.

  • Common mistake

    Giving PokerGameState the job of keeping the game moving

    What it looks like

    Believing that because the state lives in PokerGameState, that class must also drive the turns.

    Why it fails

    PokerGameState represents the state of a game. PokerHub is the class that manages the game.

    Do this instead

    Ask where the information lives (PokerGameState) and what keeps the game moving (PokerHub).

Why This Resembles the TicTacToe Design

The two-player Poker program follows a design similar to TicTacToe, and the most visible shared feature is a Main class run by both players. This is the common thread that lets a reader carry knowledge from a simpler game into this one. The source states the resemblance at this level and does not list every point of comparison, so use the shared Main class as your anchor and check the rest of the design against it rather than assuming more correspondences than the source gives.

The difference the source does make explicit is scale. Most networked examples in the course are simple enough to read in one sitting, while Poker implements a complex two-player version of five card draw. The design shape is the part you can carry across; the game rules are the part that require familiarity with five card draw poker.

Checking Your Reading of the Design

EASY

Without looking back, name the three parts of the Poker design and state, in one sentence each, what each one is responsible for. Then answer: if a player wants to know what the current state of the game is, which class is the source's place for that information, and which class is the one that manages the game?

Hints
  • Main is the entry point that both players run.
  • PokerGameState represents the state of a game.
  • PokerHub is a subclass of Hub and manages the game.

Key Takeaways

  • The two-player Poker program has three named parts: the Main class that both players run, the PokerGameState class that represents a game's state, and the PokerHub subclass of Hub that manages the game.
  • Both players run the same Main class. The first player starts a new game, and the second player joins that existing game rather than creating another one.
  • PokerHub builds on the netgame framework's Hub machinery through inheritance. The source does not describe Hub's internals or the exact methods PokerHub adds.
  • The design resembles TicTacToe, with a Main class run by both players. The full Poker source is in the package netgame.fivecarddraw and requires familiarity with five card draw poker.