Gomoku
Two players, X and O, five in a row on a 15 × 15 grid; arrows and space. Teaches a list as a grid, functions with return values and searching along a line.
Open in Tokbit Level: Expert. Free in the browser, nothing to install, no account needed.
Run the example in Tokbit and step through it: before each step say what will happen and check it in the Variables panel. Then change it your way.
Flowchart
Python program
The same program as Python code. In Tokbit you see it when you switch from Diagram to Python above the flowchart.
"""Five in a row for two players on a 15 × 15 grid. Teaches a list as a game board."""
# Tokbit screen functions, not in plain Python:
from tokbit import (
cls, at, color, background, border, key, sleep, beep, note, BLACK, WHITE,
CYAN, BLUE, YELLOW, LIGHT_RED, GREY, LIGHT_GREEN,
)
def line_length(board: list[str], x: int, y: int, dx: int, dy: int) -> int:
"""Counts equal stones in one direction dx, dy until the row breaks."""
ch: str = board[y * 15 + x]
n: int = 0
x = x + dx
y = y + dy
while x >= 0 and x < 15 and y >= 0 and y < 15 and board[y * 15 + x] == ch:
n = n + 1
x = x + dx
y = y + dy
return n
def draw_grid():
"""The title and an empty 15 × 15 grid of dots."""
background(BLACK)
border(BLUE)
cls()
color(WHITE)
at(0, 0)
print("GOMOKU: arrows and space, five in a row", end="")
color(GREY)
for y in range(15):
at(4, 2 + y)
print(" ." * 15, end="")
def empty_board() -> list[str]:
"""The board as a list of 225 squares (rows of 15), all dots at the start."""
board: list[str] = []
for i in range(225):
board.append(".")
return board
def show_cursor(cx: int, cy: int, player: str):
"""The > arrow at the chosen square and whose turn it is."""
color(WHITE)
at(4 + 2 * cx, 2 + cy)
print(">", end="")
at(2, 18)
print("Turn: " + player, end="")
def move_cursor(k: str, c: int, minus: str, plus: str) -> int:
"""The new cursor coordinate: the minus key goes down, plus up, only inside the grid."""
if k == minus and c > 0:
return c - 1
if k == plus and c < 14:
return c + 1
return c
def draw_stone(player: str, cx: int, cy: int):
"""Stone X in yellow, O in cyan, each with its own tone."""
if player == "X":
color(YELLOW)
beep(660, 30)
else:
color(CYAN)
beep(440, 30)
at(5 + 2 * (cx), 2 + (cy))
print(player, end="")
def message(text: str, shade: int):
"""A message below the grid in the given colour."""
color(shade)
at(2, 20)
print(text, end="")
def winning_line(board: list[str], x: int, y: int, dxs: list[int], dys: list[int]) -> int:
"""In which of the four directions there are five in a row through x, y (0–3), or -1."""
for d in range(4):
if 1 + line_length(board, x, y, dxs[d], dys[d]) + line_length(board, x, y, -dxs[d], -dys[d]) >= 5:
return d
return -1
def highlight_five(board: list[str], x: int, y: int, dx: int, dy: int, winner: str):
"""Recolours the winning five in green, from its start in direction dx, dy."""
sx: int = x - dx * line_length(board, x, y, -dx, -dy)
sy: int = y - dy * line_length(board, x, y, -dx, -dy)
color(LIGHT_GREEN)
for j in range(5):
at(5 + 2 * (sx + j * dx), 2 + sy + j * dy)
print(winner, end="")
def fanfare():
"""A short victory tune."""
note("C5", 120)
note("E5", 120)
note("G5", 120)
note("C6", 300)
playing: str = "a"
while True:
draw_grid()
board: list[str] = empty_board()
dxs: list[int] = [1, 0, 1, 1]
dys: list[int] = [0, 1, 1, -1]
cx: int = 7
cy: int = 7
player: str = "X"
k: str = ""
moves: int = 0
over: bool = False
winner: str = ""
wdx: int = 0
wdy: int = 0
# Game loop: a player's move, checking five in a row, switching players.
while not over:
show_cursor(cx, cy, player)
sleep(30)
k = key()
if k == "left" or k == "right" or k == "up" or k == "down":
at(4 + 2 * cx, 2 + cy)
print(" ", end="")
cx = move_cursor(k, cx, "left", "right")
cy = move_cursor(k, cy, "up", "down")
if k == " ":
i: int = cy * 15 + cx
if board[i] != ".":
message("That square is taken.", LIGHT_RED)
beep(110, 80)
else:
board[i] = player
draw_stone(player, cx, cy)
moves = moves + 1
at(2, 20)
print(" " * 30, end="")
win: int = winning_line(board, cx, cy, dxs, dys)
if win >= 0:
winner = player
wdx = dxs[win]
wdy = dys[win]
if winner != "" or moves == 225:
over = True
else:
if player == "X":
player = "O"
else:
player = "X"
at(2, 18)
print(" " * 30, end="")
if winner != "":
highlight_five(board, cx, cy, wdx, wdy, winner)
fanfare()
message("Winner: " + winner + "!", WHITE)
else:
message("Draw, the grid is full.", WHITE)
at(2, 22)
playing = input("Play again (y/n)?")
if not (playing == "a" or playing == "y"):
break