In multiplayer games, one of the most complex issues is to keep all player's state in sync with the server state. There are a few good articles around this topic on the internet. However, some details are missing here and there, which may be confusing for beginners in the field of game programming. I hope I can clear things up in this article.
I'll present a few techniques commonly used in this problem space.
Before we jump into the problem, let's have an overview on how multiplayer game generally works.
Typically, a game's program needs to simulate the following:
the changes in an environment with respect of time and players input
Game is stateful program, so it depends on time (be it real or logical time). For example, PACMAN is simulating an environment where ghosts will continuously move around.
A multiplayer game is no exception, however the complexity is higher due to the interaction between multipler players.
Let's use the classic Snake Game as an example:
Assume we use a server-client settings. The core game logic works like this