Concepts / TicTacToe Game State Management

TicTacToe Game State Management

The full Poker game state, including each player's hand and the full deck, is known only to the PokerHub.

  • Programming

Why Two Windows Must Agree

Imagine two people playing TicTacToe on two different computers, each looking at their own window. Both windows must show the same board, and both players must agree on whose turn it is. If one window showed an X in the top-left corner while the other showed an empty square, the game would stop being a game. This chapter asks one question: when a program is spread across several machines, where does the game's knowledge live, and how does every player come to see the same thing?

Before reading on, pause and consider a simple plan. The server holds the complete state of the game, and each time something changes, it sends that complete state to every connected player. Each player's screen then displays whatever it needs. This feels simple and symmetrical, and it works well for a game where nothing is hidden. TicTacToe is such a game, since both players can see the entire board at all times. Keep that plan in mind, because the rest of this article tests where it holds and where it breaks.

What Makes Up a TicTacToe State

Definition

The state of a TicTacToe game is the board contents, whose turn it is, and the outcome when the game is over. Those three pieces of information together describe everything about one game at one moment.

Notice that the state is more than the picture on the screen. The picture is something the program draws from the state. If the state says the board holds an O in the center and it is now X's turn, the window can be redrawn to show exactly that. The state is the underlying information; the window is one way of presenting it.

Board cellsnine squares, each empty,X, or OCurrent turnwhose move comes next, X orOOutcomein progress, a winner, or adraw
Which separate pieces of data together make up the complete state of one game?

In a non-networked version of the game, all three of these values would live in instance variables inside one program. The program would consult those variables to decide how to draw the board and how to respond when a user clicks the mouse. Keeping the state in one place inside one program is simple. The difficulty begins when the pieces of the program no longer share a single memory.

Three Objects and One Shared Memory

The networked version of the game involves three objects. Two of them belong to a client class, and each provides the interface for one of the two players. The third is the hub object, which manages the connections to the clients. These objects are not on the same computer, so they cannot simply read and write the same instance variables. A client on one machine cannot reach into the variables of a client on another machine.

Key point

Networked objects cannot share instance variables because they run on different computers. Any design for a networked game must answer the question of who owns the official copy of the state.

Tracing One Move Through the Hub

A player's action begins as a request, not as a change to the game. When a player places a piece on the board, that action is packaged as a message and sent to the hub. Only the hub decides what the action does to the state. The hub then updates the official state to reflect the result and sends the new state to both players. Each player's window is redrawn to match what the hub sent.

Worked example

X places a piece in the top-left corner

X clicks the top-left square during X's turn. Trace where the move goes, what the hub does with it, and what both windows show afterward.

  1. 1
    Step 1: the click becomes a request

    X's client packages the choice of the top-left square as a message and sends it to the hub. X's window does not yet show an X there, because the window does not change the game by itself.

  2. 2
    Step 2: the hub applies the move to the official state

    The hub is the only component that decides what the action does to the state. It records the mark in the top-left cell of the official board and moves the turn on to O, then checks the outcome of the game.

  3. 3
    Step 3: the hub sends the new state to both players

    After each action, the hub sends the new state to both players. Both X's window and O's window receive the same updated board and turn, and each redraws itself from that state.

  4. 4
    Step 4: both windows agree

    Because every view is rebuilt from the same official copy, the board looks the same in both windows. O now sees an X in the top-left corner and knows it is O's turn.

Result

The change started as a request from X, was applied only by the hub, and reached both windows as the same new state.

Player X windowsends the actionHubowns the official statePlayer O windowredraws from new state1. move: top-left2. new state3. new state
What happens step by step when X makes a move: which component receives it, which component changes the state, and where the updated results go?

The diagram shows the flow in one direction at a time. Actions travel inward to the hub, and the state travels outward to both players. The hub does not change anything on its own; every change to the state begins with a player's action.

Why the Hub Owns the Truth

The design resolves the tension between separate machines by choosing one owner for the truth. The official game state is stored in the hub. Each player receives a copy of that state, and the copy is refreshed every time the state changes. The players never hold the authority to alter the state directly. A player's window is a copy the player looks at, and it is not the game itself.

Key point

The hub holds the official state; players send actions to it and never change the state directly.

The payoff is a guarantee stated in the source: because every view is rebuilt from the same official copy, the game always looks the same to both players. The clients are display-and-input devices, and the hub is the arbiter of what is true.

Why Trusting the Client Is Not Enough

A tempting shortcut in a networked game is to let the hub send everything and rely on the client program to behave. In TicTacToe this worry is small, because both players are allowed to see the whole board. The same reasoning matters more in games with hidden information, where the client could simply choose not to show certain data. The source makes the key point: hiding information in the client is not sufficient, because a player can substitute their own client program to enable cheating.

The source concludes that the server must not give a player more than they should have. Preventing cheating depends on what data leaves the hub, not only on how the official client behaves. This is the reason the hub, not the client, decides what counts as an allowed change to the state.

Hiding Information in Poker

Poker is more involved than TicTacToe, and its game state is correspondingly more involved. The important difference is not only how many values the state contains. Poker has information that belongs to one player and not to the others. A player's hand is private to that player, and the deck from which cards are dealt is not something players should be able to inspect. Once these facts are taken into account, the question of what to send over the network stops being a simple broadcast decision.

The full Poker game state, including each player's hand and the full deck, is known only to the PokerHub. A player never needs to hold the complete picture in order to play, and the design deliberately keeps it away from them. The PokerHub holds the one complete picture, while each player receives a limited picture shaped for that player alone.

When the state of a Poker game changes, the PokerHub does not send one object to everyone. It creates a separate PokerGameState object for each player, one representing the game from that player's point of view, and sends each object to the appropriate player. A PokerGameState represents the game from the point of view of one player, and each player receives only their own.

PokerHub full stateevery hand and the fulldeckPokerGameState forPlayer Aonly what A may knowPokerGameState forPlayer Bonly what B may knowfiltered for Afiltered for B
How does one full authoritative state in the PokerHub become different filtered views, and which fields does each view contain or omit?

There is also a practical reason to avoid sending a fresh copy of the complete state after every minor change. Each update that carries the whole state repeats information that a player often does not need. The real need, however, is to decide what each player should receive, not merely to reduce traffic.

Why Full-State Broadcast Works Here

The source notes a design choice. Instead of sending a complete copy of the state each time it changes, the program could send only the change. That would reduce what travels over the network, but it would require a way to encode each change into a message that both ends can read. Someone would have to define that encoding and make both sides agree on it.

The author chose the simpler option: send the entire state as the message. The reasoning is specific to this game. The state of TicTacToe is small, consisting of a board, a turn, and an outcome, so resending all of it each time costs little and keeps the message format straightforward. A larger game with much more state would weigh the trade-off differently.

Mistakes About Shared State

  • Common mistake

    Treating each player's window as the real game

    What it looks like

    A player believes the X on their screen is the game's record of the move, so they assume the other window must match it automatically.

    Why it fails

    The window is a drawing made from a copy of the state. The official state lives in the hub, and a window only shows what the hub last sent.

    Do this instead

    Ask where the authoritative copy is stored, and remember that windows are refreshed from it.

  • Common mistake

    Assuming the client's rules are enforced by the client

    What it looks like

    A designer hides the opponent's hand in the client and considers the game secure.

    Why it fails

    A player can substitute their own client program, so hiding data in the client does not prevent cheating.

    Do this instead

    Decide what data leaves the hub, and enforce legal moves and visibility on the hub side.

  • Common mistake

    Thinking the players can change the state directly

    What it looks like

    A player's click is treated as if it has already changed the board in the official state.

    Why it fails

    Players send actions to the hub and never change the state directly. The hub decides what an action does.

    Do this instead

    Describe every click as a request that the hub may apply and then broadcast.

  • Common mistake

    Assuming the same state object is shared by all machines

    What it looks like

    Two client objects on different computers are expected to read the same instance variables.

    Why it fails

    Networked objects cannot share instance variables because they run on different computers.

    Do this instead

    Pass state through messages, and let one owner, the hub, hold the official copy.

  • Common mistake

    Sending everything to everyone is always the right default, regardless of hidden information

Practice: Trace a Move Through the Hub

What do you think happens?

In a networked TicTacToe game, should O's window change the board before the hub confirms X's move?

  • Yes, the window updates immediately

  • No, the window waits for the hub's new state

Reveal answer
Answer:

No, the window waits for the hub's new state

Players send actions to the hub and never change the state directly. The hub applies the action to the official state and sends the new state to both players, and each window is redrawn from what the hub sent.

MEDIUM

O clicks the center square on a turn that the official state says belongs to X. Trace what the hub should do with this action, which state the hub sends afterward, and what the two windows should show. Then explain why the hub, not O's client, is the right place to reject this move.

Hints
  • Ask which component is the only one allowed to decide what an action does to the state.
  • Consider what the official state says about whose turn it is.
  • Think about whether a modified client could skip a check that lives only in the client.

Key Takeaways

  • A TicTacToe game state is the board contents, whose turn it is, and the outcome when the game is over.
  • A networked game stores its official state in the hub, because networked objects cannot share instance variables and players must not change the state directly.
  • Each player's action is sent to the hub as a request, the hub applies it to the official state, and the new state is sent to both players so their windows agree.
  • Hiding information in the client is not sufficient, since a player can substitute their own client program, so the hub must control what data leaves it.
  • In Poker, the PokerHub creates a separate PokerGameState for each player, so each player receives only the view they are entitled to.