# Understanding game logic

The game logic is code running on the backend that dictates how the game will run. Understanding the game logic is not strictly necessary for building bots, but it can be helpful for users who want to understand the nuances of how the game works when it is not adequetly described in the game description.

Game logic consists of these components

* game initialization
* what happens when an action is performed?
* when does the game end and who is the winner?

## Game initialization

The `init_game(game_settings)` method is called at the beginning of a game and returns a newly initialized game state. It determines who makes the first move, by specifying the `player_turn_idx`. Some games support different settings, such as board size or player count, and the initialization can take those settings into account.

## Performing the action

Bots return an action object on their turns. The action object is the input for these game logic methods. Given the current game state, the game logic calculates what the new game state is when the action is performed.

Games with multiple actions need to implement a method for each action.

### Example

Tictactoe has a single action: placing a piece at some index. The Tic-Tac-Toe game logic has a method `place_piece(state, index)` that returns a new state after the piece has been places in that index.

## Determining the winner

After each action, the game logic checks if the game has finished given the new game state. For multi-player games, the supported game results are&#x20;

* Someone has won
* Game ended with a draw
* Game is on-going

Single player game results also include things like the `score` of the game.&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.botpot.ai/games/understanding-game-logic.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
