pytron-book/src/Game.md

2.2 KiB

The Game module

The Game class is probably the most important class in this architecture, since it is the class you will get as a parameter in your AI. Your AI will need to analyse the game in order to take a decision that will make it win.

It contains the size of the game, the Maps of each step of the game, the players, and can run.

Before detailling the content of the Game class, we first need to describe some other classes in the same module.

The PositionPlayer class

Yes, this is a really poor name, but I had no imagination for that one...

Basically, this class contains the things that the game needs to know about the player:

  • the player attribute is the one corresponding to the player described in the player section (which serves the purpose of the AI)
  • the position attribute is the current position of the player
  • the id is a number (between 1 and 2) that allows the game to know which player we are talking about
  • the alive attribute is a boolean which allows the game to know if the player is still alive so it can know if the game ended and which player has won

It also contains two methods, head and body which returns the tile the corresponding to the head or the body of the given player.

The HistoryElement class

This class desribes the elements you will find in the game history. It contains the directions that were given by the players, and the state of the map after the turn.

And finally, the Game class

This class contains:

  • the width and height of the game, which are integers
  • the history of the states of the game, which is an array of HistoryElements that we described in the previous section
  • the PositionPlayers of the game (in the pps attribute), which is an array of PositionPlayer described previously
  • the winner which is initialized to None, at the begining of the game, but will be 1 or 2 if player 1 or 2 won the game

It also contains a few methods:

  • map, which returns the current map of the game
  • next_frame which advances one step in the game and updates the history
  • main_loop, that runs the game until it's finished