(Back to the Previous Post in the Series)

Tic Tac Toe, the game of X’s and O’s, was an oddly popular game in elementary school. When playing the game you quickly learn a couple of rules:

- Because X always places his mark first (alternating between X and O), there is an unfair advantage for the player placing the X marks.
- The player placing his mark in the center square typically has an advantage in the game.
- Once a player scores three marks in a row horizontally, vertically, or diagonally, there is no point in continuing the game.

–

–

–

–

–

–

–

Consider the above game matches. In the first two matches both X and O score three in a row, but which player won the match – it depends on the order in which the marks were placed. In the third match, X won with a diagnal three in a row. The final match resulted in a tie (neither player won). You can use a SQL statement similar to the following to output all of the roughly 362,000 combinations (note that not all combinations are unique – rotating the board 90 or 180 degrees to generate a unique combination probably is not fair, and the order in which the marks are placed could matter):

WITH N AS (SELECT ROWNUM N FROM DUAL CONNECT BY LEVEL<=9), C AS (SELECT 'XOX'||'OXO'||'XOX' C FROM DUAL) SELECT SUBSTR(C.C,N1.N,1) || SUBSTR(C.C,N2.N,1) || SUBSTR(C.C,N3.N,1) || CHR(10) || SUBSTR(C.C,N4.N,1) || SUBSTR(C.C,N5.N,1) || SUBSTR(C.C,N6.N,1) || CHR(10) || SUBSTR(C.C,N7.N,1) || SUBSTR(C.C,N8.N,1) || SUBSTR(C.C,N9.N,1) GAME FROM N N1, N N2, N N3, N N4, N N5, N N6, N N7, N N8, N N9, C WHERE N1.N<>N2.N AND N1.N<>N3.N AND N1.N<>N4.N AND N1.N<>N5.N AND N1.N<>N6.N AND N1.N<>N7.N AND N1.N<>N8.N AND N1.N<>N9.N AND N2.N<>N3.N AND N2.N<>N4.N AND N2.N<>N5.N AND N2.N<>N6.N AND N2.N<>N7.N AND N2.N<>N8.N AND N2.N<>N9.N AND N3.N<>N4.N AND N3.N<>N5.N AND N3.N<>N6.N AND N3.N<>N7.N AND N3.N<>N8.N AND N3.N<>N9.N AND N4.N<>N5.N AND N4.N<>N6.N AND N4.N<>N7.N AND N4.N<>N8.N AND N4.N<>N9.N AND N5.N<>N6.N AND N5.N<>N7.N AND N5.N<>N8.N AND N5.N<>N9.N AND N6.N<>N7.N AND N6.N<>N8.N AND N6.N<>N9.N AND N7.N<>N8.N AND N7.N<>N9.N AND N8.N<>N9.N;

The output of the above SQL statement should appear similar to the following:

GAME ---- ... XXX OOO OXX XXX OOO OXX ... XOX XOO OXX OXX XOO OXX XXO XOO XOX XXO XOO XOX XOX XOO XOX ...

The big problem with the above SQL statement is that it is not clear which player won in all cases. Ideally, the N1.N value would be used to output an * X* mark in the specified position, the N2.N value would be used to output an

*mark in a specified position, the N3.N value would be used to output an*

**O***in the specified position, etc. until one of the players places three marks in a row. For example, if N1.N is 5, an*

**X***would be placed in the center square, if N2.N is 9, an*

**X***would be placed in the bottom right square.*

**O**Now that we know that the order in which the marks are placed is important, how would be know when a player wins? You could experiment with the following:

**Vertical win, with three positions having the same resulting values:**

SELECT MOD(position - 1, 3) + 1 V FROM DUAL;

**Horizontal win, with three positions having the same resulting values:**

SELECT TRUNC((position - 1) / 3) + 1 V FROM DUAL;

**Diagnal win \ when V=0 in three positions:**

SELECT (MOD(position - 1, 3) + 1) - (TRUNC((position - 1) / 3) + 1) V FROM DUAL;

**Diagnal win / when V=0 (watch out, could end up in a V pattern) or V=2:**

SELECT ABS((MOD(position - 1, 3) + 1) - (TRUNC((position - 1) / 3) + 1)) V FROM DUAL;

———–

OK, now that I have explained the game, given you a couple of SQL statements to *possibly* help you with the solution… on to the challenge. **With the help of Oracle Database build a tic tac toe solver that will help a player win at tic tac toe.** Provided a current description of the board, for example ‘_X_’||’O_X’||’OX_’, display all board solutions that allows player O (or player X if his turn is next) to win. (Side note: I have not yet completed the solution to this challenge – it might be possible to accomplish this challenge with just a SQL statement.)

## Recent Comments