We can assign each board cell a number in a manner like this:

We don’t have objects in Ampscript, so we can use the following notation to represent the board as a single string:

`set @fields = "123456789"`

Then if any of the fields changes to `X`

or `O`

, we just replace the corresponding number with that character:

This example would be represented in this format as:

`set @fields = "12X4O6789"`

## Win Condition

As we all know, this game must end when one player has managed to form a straight line in one of the:

- 3 rows (
`r1`

,`r2`

,`r3`

) - 3 columns (
`c1`

,`c2`

,`c3`

) - 2 diagonals (
`d1`

,`d2`

)

To represent each of those lines, we need to break the `@fields`

string into pieces to build the rows, columns and diagonals:

```
/* Get current field values */
set @f1 = Substring(@fields, 1, 1) /* π Top Left Corner */
set @f2 = Substring(@fields, 2, 1) /* π Top Middle Edge */
set @f3 = Substring(@fields, 3, 1) /* π Top Right Corner */
set @f4 = Substring(@fields, 4, 1) /* π Middle Left Edge */
set @f5 = Substring(@fields, 5, 1) /* π¨ Middle Middle Middle */
set @f6 = Substring(@fields, 6, 1) /* π Middle Right Edge */
set @f7 = Substring(@fields, 7, 1) /* π Bottom Left Corner */
set @f8 = Substring(@fields, 8, 1) /* π Bottom Middle Edge */
set @f9 = Substring(@fields, 9, 1) /* π Bottom Right Corner */
/* Build rows, columns and diagonals */
set @r1 = Concat(@f1, @f2, @f3, '^')
set @r2 = Concat(@f4, @f5, @f6, '^')
set @r3 = Concat(@f7, @f8, @f9, '^')
set @c1 = Concat(@f1, @f4, @f7, '^')
set @c2 = Concat(@f2, @f5, @f8, '^')
set @c3 = Concat(@f3, @f6, @f9, '^')
set @d1 = Concat(@f1, @f5, @f9, '^')
set @d2 = Concat(@f7, @f5, @f3, '^')
```

The `^`

character is used just as separator before we build our final `@verification`

string that will be used to determine if anyone has won the game:

`set @verification = Concat(@r1, @r2, @r3, @c1, @c2, @c3, @d1, @d2)`

We can now simply use the `IndexOf`

function to discover if any of the lines added together in the `@verification`

string contains a sequence of three `X`

‘s or `O`

‘s that’s not interrupted by our `^`

separator.

##### Board Examples

##### Example 1

After 4 moves:

For this board our variables would get the following values:

```
set @fields = "X23OO6X89"
/* Rows */
set @r1 = 'X23^'
set @r2 = 'OO6^'
set @r3 = 'X89^'
/* Columns */
set @c1 = 'XOX^'
set @c2 = '2O8^'
set @c3 = '369^'
/* Diagonals */
set @d1 = 'XO9^'
set @d2 = 'XO3^'
set @verification = 'X23^OO6^X89^XOX^2O8^369^XO9^XO3^'
```

##### Example 2

After 6 moves:

For this board our variables would get the following values:

```
set @fields = 'XXO4O67OX'
/* Rows */
set @r1 = 'XXO^'
set @r2 = '4O6^'
set @r3 = '7OX^'
/* Columns */
set @c1 = 'X47^'
set @c2 = 'XOO^'
set @c3 = 'O6X^'
/* Diagonals */
set @d1 = 'XOX^'
set @d2 = '7OO^'
set @verification = 'XXO^4O6^7OX^X47^XOO^O6X^XOX^7OO^'
```

##### Example 3

A draw after 9 moves:

For this board our variables would get the following values:

```
set @fields = 'XOOOXXXOX'
/* Rows */
set @r1 = 'XOO^'
set @r2 = 'OXX^'
set @r3 = 'XOX^'
/* Columns */
set @c1 = 'XOX^'
set @c2 = 'OXO^'
set @c3 = 'OXX^'
/* Diagonals */
set @d1 = 'XXX^'
set @d2 = 'XXO^'
set @verification = 'XOO^OXX^XOX^XOX^OXO^OXX^XXX^XXO^'
```