backlinks to this page:
Github:
Everything you need to know about TicTacToe (the board game) is very well explained in this Wikipedia Page: https://en.wikipedia.org/wiki/Tic-tac-toe
The goal is now to make a computer version out of this board game.
Because creating a challenging artifical intelligence (AI) is a bit difficult, it will explained in later steps. The first step is to create a computer version of the board where 2 human(!) players can play against each other. The computer program will display the board, update the (text) display, handle the user-input and determine if one player has won. A sub-task of handling the user-input is checking if a move of a player is allowed or not.
To summarize, the game is defined as:
It is not strictly necessary for this game to have a graphical representation of the board. (Super)human players could imagine the board in their minds and play against each other (see Blind chess).
However…. at least a text representation of the board makes the game experience far more comfortable. For humans .
The first sub-task is therefore to write a python code that can display a 3×3 board. There exist as many variants as there exist python programmers, so here are just 2 versions:
Python does allow multi-line strings (everything inside triple-quotes). The script below creates one such multi-line variable and prints it.
code: line numbers are never part of the python code!
board = """ [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]""" print(board)
board
is created and a string value is assigned to the variable. [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
This is done by the =
operator (the equal sign): left of the equal sign is the variable, right of it is the value. Think of the equal sign as an arrow pointing from right to left. Where is the string value that is assigned to the variable board
? Well, everything between the triple-quotes (line 1 unitl line 4). See the paragraph naming variables below for more information.
prints
the value of the variable board
to the screen. Choosing a name for a variable: You are free to name variables in python as you want as long as you do not conflict with a python command and follow a few rules. Basically:
Another variant, without triple-quotes, is to *multiply* a string! In Python, if you multiply a string with a number (an integer), the string is simply repeated. To create a new line, use the special Escape-Sequence \n
for a new line. This allows this very cool One-liner:
print("[ ] [ ] [ ]\n" * 3) # all in one-line
\n
Escape Sequence to force a new line. The whole string is multiplied by 3, including the line line break.[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
Not so bad! However, there is a practical problem. Assuming the computer has no touch-display, it is not straight-forward for human players to indicate in what field they want to place their symbol. Expressions like “Top-left corner” or “bottom middle field” are understandable for humans, but their exist many many variants of how to describe a field.
Humans tend to be very creative, and writing a computer program that accurately understands descriptions like “…well, this empty field left of the field you used” is beyond the scope of this tutorial.
Therefore, let us display the board with an easy-to-use coordiante system, like an index for rows and another index for columns. The player then must just enter 2 numbers to indicate a field, like “row 1, field 2”.
To make this example easier to code (at the cost of a bit inconvenience for the user) we will let the index start with zero instead of with one. The user should first enter the row (0, 1 or 2) and then the column (0, 1 or 2).
The board should display the indexes on top of each column and left of each row.
The output should be like that, the r\c in the topleft corner means “rows\columns”:
r\c 0: 1: 2: 0: [ ] [ ] [ ] 1: [ ] [ ] [ ] 2: [ ] [ ] [ ]
Go to the next step to see how to do it!