11g. Review: An Othello Game#
A review of Chapters 1 to 11 (with the additions pages): one project, a program in which two people play the board game Othello. This page explains the game and shows example runs. It has no solution: it is an exercise. “11f. Review: A Tic-Tac-Toe Game” shows how to develop such a program one step at a time.
Run the setup cell below first. It downloads thinkpython.py, the helper module the chapters use.
from os.path import basename, exists
def download(url):
filename = basename(url)
if not exists(filename):
from urllib.request import urlretrieve
local, _ = urlretrieve(url, filename)
print("Downloaded " + str(local))
return filename
download('https://github.com/AllenDowney/ThinkPython/raw/v3/thinkpython.py');
import thinkpython
The game#
Othello is a game for two players, Black and White, on a board of 8 rows and 8 columns. The columns
have the letters a to h, and the rows have the numbers 1 to 8, so each square has a name such as d3
(column d, row 3). Each player puts discs of their color on the board: B for Black and W for White.
The start. The game starts with 4 discs in the middle: White on d4 and e5, Black on e4 and d5.
Black moves first. Then the players take turns.
A move. A player puts one disc of their color on an empty square. The move is legal only if it outflanks at least one disc of the other color: in at least one of the 8 directions from the new disc (up, down, left, right, and the 4 diagonals), there is a line of one or more discs of the other color, and right after them a disc of the player’s color. All the discs of the other color in each such line flip: they get the player’s color. A move can flip discs in several directions at once.
For example, at the start, Black can play d3. Down from d3 there is the white disc on d4, and right after
it the black disc on d5. So the move outflanks d4, and d4 flips to black (see the first example run below).
Black cannot play a1: no line of white discs goes from a1 to a black disc.
Passing. If a player has no legal move, they must pass, and the other player moves again. If a player has a legal move, they must move.
The end. The game ends when neither player has a legal move (usually when the board is full). The player with more discs on the board wins. With the same number of discs, the game is a tie.
The problem#
Question 1 (very hard): write an Othello game for two players#
Write a program in which two people play Othello at the same keyboard. The program must do these things:
Show the board as in the example runs: a line with the letters of the columns, then each row with its number. An empty square is
., a black discB, a white discW. An empty line follows the board.Before each move, show the board, and the number of discs of each color:
Black: 2 White: 2.Ask the player whose turn it is for a move:
Black (B), your move:. A move is the name of a square, such asd3.If the reply is not a letter
atohfollowed by a number1to8, ask again withType a column letter and a row number, for example d3:. If the square is not a legal move, ask again witha1 is not a legal move. Try again:(with the square that the player typed).Put the disc on the board, and flip the outflanked discs in every direction.
If the player whose turn it is has no legal move, display
Black has no legal move and must pass.(orWhite ...), and go on with the other player.When neither player has a legal move, show the board, then
Game over. Black: 26 White: 38(with the numbers of discs), thenBlack wins.,White wins.orIt is a tie.
Example run: the start of a game. The text after each prompt is what the players typed. White first types
z9 (not a square) and then a1 (not a legal move): the program asks again each time.
a b c d e f g h
1 . . . . . . . .
2 . . . . . . . .
3 . . . . . . . .
4 . . . W B . . .
5 . . . B W . . .
6 . . . . . . . .
7 . . . . . . . .
8 . . . . . . . .
Black: 2 White: 2
Black (B), your move: d3
a b c d e f g h
1 . . . . . . . .
2 . . . . . . . .
3 . . . B . . . .
4 . . . B B . . .
5 . . . B W . . .
6 . . . . . . . .
7 . . . . . . . .
8 . . . . . . . .
Black: 4 White: 1
White (W), your move: z9
Type a column letter and a row number, for example d3: a1
a1 is not a legal move. Try again: c5
a b c d e f g h
1 . . . . . . . .
2 . . . . . . . .
3 . . . B . . . .
4 . . . B B . . .
5 . . W W W . . .
6 . . . . . . . .
7 . . . . . . . .
8 . . . . . . . .
Black: 3 White: 3
Black (B), your move: e6
a b c d e f g h
1 . . . . . . . .
2 . . . . . . . .
3 . . . B . . . .
4 . . . B B . . .
5 . . W W B . . .
6 . . . . B . . .
7 . . . . . . . .
8 . . . . . . . .
Black: 5 White: 2
White (W), your move: f5
a b c d e f g h
1 . . . . . . . .
2 . . . . . . . .
3 . . . B . . . .
4 . . . B B . . .
5 . . W W W W . .
6 . . . . B . . .
7 . . . . . . . .
8 . . . . . . . .
Black: 4 White: 4
(The game goes on.)
Example run: the end of another game. Black has no legal move two times, and must pass. White’s last move,
b1, fills the board and flips discs in two directions: to the right (c1 to f1) and down and to the right
(c2).
... (the start of the game)
a b c d e f g h
1 . . B B B B W B
2 B B B B B W W B
3 B W B W W W W B
4 B W B W W W W B
5 B W W W B W W B
6 B W B W W B W B
7 B W W W W W B B
8 B W W B B B B B
Black: 33 White: 29
Black has no legal move and must pass.
a b c d e f g h
1 . . B B B B W B
2 B B B B B W W B
3 B W B W W W W B
4 B W B W W W W B
5 B W W W B W W B
6 B W B W W B W B
7 B W W W W W B B
8 B W W B B B B B
Black: 33 White: 29
White (W), your move: a1
a b c d e f g h
1 W . B B B B W B
2 B W B B B W W B
3 B W W W W W W B
4 B W B W W W W B
5 B W W W B W W B
6 B W B W W B W B
7 B W W W W W B B
8 B W W B B B B B
Black: 31 White: 32
Black has no legal move and must pass.
a b c d e f g h
1 W . B B B B W B
2 B W B B B W W B
3 B W W W W W W B
4 B W B W W W W B
5 B W W W B W W B
6 B W B W W B W B
7 B W W W W W B B
8 B W W B B B B B
Black: 31 White: 32
White (W), your move: b1
a b c d e f g h
1 W W W W W W W B
2 B W W B B W W B
3 B W W W W W W B
4 B W B W W W W B
5 B W W W B W W B
6 B W B W W B W B
7 B W W W W W B B
8 B W W B B B B B
Game over. Black: 26 White: 38
White wins.
Hints.
Store the board as a list of 8 lists, each with 8 strings:
'.','B'or'W'. Change a move such asd3into two indexes: the row isint(move[1]) - 1, and the column is the position ofmove[0]in'abcdefgh'(find: “8b. Additions: String Methods”).Write a function that takes the board, a player and a square, and returns the list of the squares that a disc there would flip. For each of the 8 directions, a pair of steps such as
(-1, 0)(up) or(1, 1)(down and to the right), walk from the square while the squares belong to the other player. A move is legal when the list is not empty. The same function then tells you which discs to flip.Develop the program one step at a time, and test each function on its own with boards that you make, as in “11f. Review: A Tic-Tac-Toe Game”. The Thonny debugger helps (“11c. Additions: The Thonny Debugger”).
Write the program in the cell below, or in Thonny.
Credits#
Additions by Prof. Y. Rosenthal, for Think Python: 3rd Edition by Allen B. Downey. Text license: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International