Creating Tic-tac-toe in Swift: Gameplay and data model

This blog post is an update on my work-in-progress Tic-tac-toe game, being written in Swift. The source code is available on GitHub:

I’ve reached the first major milestone. The program is capable of playing Tic-tac-toe against itself, although there is not yet an intelligent agent deciding where to put each mark on the game board.  Instead, the program has what I call a “scripted strategy” which unit test methods use to advance a game to completion.

Before we get into how that works, here’s a mile-high view of the classes involved so far.


GameBoard is the data model which stores each mark a player puts on the board.

Game orchestrates gameplay between two players.

OutcomeAnalyst is what Game uses to detect when a game is over.

Player represents a contestant, either human or computer.

TicTacToeStrategy describes an object Player uses to choose where to put a mark.

So far the only class which adopts the TicTacToeStrategy protocol is for testing purposes, named ScriptedStrategy.


One interesting thing about this class is that it supports a tiny domain-specific language. As seen in the following example from GameTests (slightly modified to fit this blog’s layout), a ScriptedStrategy is created for each player by parsing a text diagram of a Tic-tac-toe board showing where each Player should put its marks.


Up next I will work on the algorithmic strategy, so that the app’s user will have a challenging opponent to play against. This is a lot of fun!

Next article: Creating Tic-tac-toe in Swift: Artificial Intelligence

This entry was posted in Swift, Tic-tac-toe and tagged , . Bookmark the permalink.

1 Response to Creating Tic-tac-toe in Swift: Gameplay and data model

  1. Pingback: Dew Drop – November 30, 2015 (#2141) | Morning Dew

Comments are closed.