Professional Documents
Culture Documents
Tictactoe Documentation: Release 0.1
Tictactoe Documentation: Release 0.1
Release 0.1
deterralba
1 Forewords 1
3 Contents: 5
3.1 The main module, where you should start! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2 List of all the packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.3 The trash module, just a random test file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
i
ii
CHAPTER 1
Forewords
Dear reader,
This is the cherish documentation of my TicTacToe program. I made this program with love and patience to learn how
the use of a bunch of new technologies.
For instance, the doc you are currently reading is compiled with sphinx, a very powerful and noob-unfriendly software.
1
TicTacToe Documentation, Release 0.1
2 Chapter 1. Forewords
CHAPTER 2
If you are in a hurry, please go the the main module page. Everything is explained there if you just want to run a
quick simulation.
3
TicTacToe Documentation, Release 0.1
Contents:
The project is divided in several packages. They are all called from the main module located at the root of the project.
The core package provides... the core classes of the program, which represent a game, the board, the manager of a
simulation, basic players etc.
The players package contains more sophisticated players that have either simple strategies or are able to learn from
the past game! It is in this package that you should create your own players.
The misc package (aka miscellaneous) contains other useful packages, for instance the one used to plot graphs of
simulations’ results.
This is the entry point of the program, the first module executed.
Here you can parameter all the program, no need to read other source files.
5
TicTacToe Documentation, Release 0.1
Submodules
core.BoardAndRules module
class BoardAndRules(game)
Represents the rules of the game, saves the present state of the game and checks that the players follow the rules.
Variables
• game (Game) – A reference to the game.
• boardS (BoardState) – Represents the present physical board.
extractLines()
Extracts the 8 lines than can be completed:
•3 vertical: left -> right,
•3 horizontal: up -> down,
•2 diagonal: 11->33 & 31->13.
getBoard()
Returns a copy of the self.board that will not be updated when a new movement is made
Return type BoardState
play(mvt)
Verifies that the movement of the player follows the rules and writes it on the board.
Also sets Movement.turn and registers the movement in the list Game.movements Registers boardS
in Game.states
Parameters mvt (Movement) – The Movement the player wants to play
Returns True if the movement is possible, else False
Return type bool
reset()
Resets the state for a new game
thereIsAWinner()
Sets Game.winner if there is one.
Returns True if there is a winner (ie if 3 dots are aligned), else False
Return type bool
6 Chapter 3. Contents:
TicTacToe Documentation, Release 0.1
core.BoardState module
class BoardState(hash=None)
Represents the physical board.
Variables state (3-list (line) of 3-list (column)) – The state of the board: initialised with
“0”: [[0, 0, 0], [0, 0, 0], [0, 0, 0]] uses player.order to put “1” or “2”
where the players play.
Parameters hash (int, optional) – If hash is in **kwargs, it will be used to fill the board, default
is None.
Examples
Examples
__len__()
Returns the turn of the board: counts the 0 and deduces the turn T = 9 - nb(0)
Return type int
__repr__()
Returns 3 lines representing the board (plus one line above and one under) with X and O instead
of 1 and 2
Return type string
copy()
Returns A new identical copy of BoardState (useful for storage)
Return type BoardState
reset()
Reset self.state for a new game: to [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
unhash(theHash)
core.Game module
class Game
Main object, registers the Board and the Players.
Variables
• boardAR (BoardAndRules) – Reference to the main BoardAndRules instance
• and player2 (player1) – References to the players
• and nextPlayer (lastPlayer) – References to player1 and player2, are exchanged be-
tween the turns
• movements (list of Movement) – the chronological list of Movement() played
• states (list of BoardStates) – the chronological list of BoardStates()
• turn (int) – The present turn of the game, initialised at 0, first turn must be 1 (modified in
start() )
• movements – the list of all the Movements made
• states – the list of the different BoardState of the game
• winner (Player or None) – Defined by BoardAndRules.thereIsAWinner(), stays
None is there is none
• interactionLevel (InteractionLevel) – used to define the level of printed outputs
reset()
Resets the game for a new game
Resets turn, movements, sates, winner, next and last player Calls boardAR.reset()
start()
Plays a game until it is over, i.e. there is a winner or the game is even
How it is working:
As long as the game is not over (ie self.turn < 9 and thereIsAWinner == False) there is
a loop where:
•turn is incremented,
•nextPlayer.play() is called and then next and last player are inverted,
•if wanted, the board is printed.
When it is over, meth:.Player.endOfGame() of player1 and player2 is called - if wanted, the result is
printed.
core.InteractionLevel module
class InteractionLevel
This object is used to store the parameters of the simulation.
Variables
• showEveryMovement (bool) – Print the state of the game after every movement
8 Chapter 3. Contents:
TicTacToe Documentation, Release 0.1
• showEveryMovementAndWait (bool) – Print the state of the game after every move-
ment and wait that the user press enter to continue
• showFinalBoard (bool) – Print the final state of the game
• showElapsedTime (bool) – Print the total time that the simulation took
• showPlayerDebug (bool) – Print the debug messages of each player
core.Movement module
core.Player module
endOfGame()
play()
Calls randomPlay()
randomPlay()
Play a random movement (stupid: tries until a movement is not refused)
core.PlayerStatistic module
class PlayerStatistic(player)
newResult(game)
core.Simulation module
class Simulation(game)
start()
Module contents
This is the Core package of the Game, it contains the primary classes of the game that are all needed to start a series
of games between two random players.
Importation
This may seem like a bad idea but the importation process in controlled in the core/__init__.py file. All the
following classes will be imported:
• BoardAndRules
• BoardState
• Game
• InteractionLevel
• Movement
• Player
• PlayerStatistic
• Simulation
Use
10 Chapter 3. Contents:
TicTacToe Documentation, Release 0.1
Submodules
players.HAL1Player module
players.HumanPlayer module
players.LinePlayer module
play()
Plays a first random movement and then tries to complete lines
players.Memory module
class Memory
addGame(game)
Add a game in the dictionary pastGames :
•key = hash of the state before the wining movement
•value = the Movement.place (couple [x,y]) that linked to the victory
Module contents
This is the Players package: it contains Player’s subclasses that try to develop some kind of intelligence...
Importation
Use
Submodules
misc.Tools module
class Analyze
12 Chapter 3. Contents:
TicTacToe Documentation, Release 0.1
Module contents
This is a test file used as a Python interpreter.. Chunks of more or less useful pices of code!
14 Chapter 3. Contents:
CHAPTER 4
• genindex
• modindex
• search
15
TicTacToe Documentation, Release 0.1
c
core, 10
core.BoardAndRules, 6
core.BoardState, 7
core.Game, 8
core.InteractionLevel, 8
core.Movement, 9
core.Player, 9
core.PlayerStatistic, 10
core.Simulation, 10
m
main, 5
misc, 13
misc.Tools, 12
p
players, 12
players.HAL1Player, 11
players.HumanPlayer, 11
players.LinePlayer, 11
players.Memory, 12
t
trash, 13
17
TicTacToe Documentation, Release 0.1
19
TicTacToe Documentation, Release 0.1
R
randomPlay() (Player method), 9
replaceInList() (Analyze static method), 12
reset() (BoardAndRules method), 6
reset() (BoardState method), 7
reset() (Game method), 8
S
saveTraining() (HAL1Player method), 11
Simulation (class in core.Simulation), 10
start() (Game method), 8
start() (Simulation method), 10
T
thereIsAWinner() (BoardAndRules method), 6
trash (module), 13
U
unhash() (BoardState method), 7
W
writeResultsInConsole() (Plot static method), 13
20 Index