(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.)

Gary Colbran(09:43:49) :That’s tough. My attempt so far……….

Assuming -1 for X, 0 for unplayed and 1 for O, we could try the following..

Originally 19683 options in the game at any combination.

SQL> select 3*3*3*3*3*3*3*3*3 from dual;

19683

Let’s whittle it down to where the number of moves are 1 ahead of the other in any combination, or equal.

That leaves us 8953 options.

Now let’s find rows where X or O has won.

2304 options. Now we assume X goes first, hence the total values will be -1 or 0.

Ok, let’s get the formatting out and identify the winning games.

And take out rows where both sides win, which is impossible.

Now we need to find if X or O win, which is a -3 (X) or 3 (O).

So I can add the winner of the game in there too.

That gives the 1332 options where X can win.

So, given a position in the game we would have to return the rows from there, still assuming that X went first.

Which is fine for all results, let’s whittle it down a bit. This was difficult and I borrow some other code from elsewhere.

Don’t know why I need the select distinct as yet.

Cheers, Gary.

Charles Hooper(08:33:44) :Gary,

Wow! Great explanations as you stepped through the development of the final solution. As a bonus, you did not use my partial solution, which I tried to use as the basis for a solution without much success for the better part of a couple of hours.

Posting that much detail as quickly as you did, a couple of minor problems are to be expected – see his followup post for the un-Wordpress mangled version of the WHOWINS function. Most people will also have to drop the “

control.” characters in front of the TYPE definition name as well as the function name. Some of the intermediate SQL statements result in errors (when tested on 11.2.0.2) – focus on Gary’s description and the transformation to the SQL statement rather than whether or not an error is produced.So that people do not get hung up on the minor glitches, what I encountered:

Well done.

Gary Colbran(09:47:15) :Final function did not go in……

Cheers, Gary.

Radoslav Golian(18:27:26) :Not the best solution, but seem to be working🙂

works in 11.2+

Charles Hooper(09:10:23) :Radoslav,

Wow, a recursive SQL statement with regular expressions type solution for the problem. I thought that regular expressions might be part of the solution, but I did not have any idea how to get started with such a solution. I worked with regular expressions way back when I was learning Turbo Pascal, but there have not been enough occasions for me to revisit the logic of regular expressions since that time.

I made a slight adjustment at the end of your SQL statement to add line breaks so that it was easier to confirm that your solution worked:

Very well done, and efficient because it does not waste time generating 362,000 combinations (like my initial starting point) of which only a small number of combinations are useful.