>We used to use SUMS of row and columns (with X=1 and 0=0) ...
>
>anyway it must me a simple game to solve since it's impossible to
loose on
>this game. You can always predict all the possibilities till
the end of
>the game... And with not that much memory! (not like chess...
)
>
>Does anyone knows an algorithm to play this game?
You can win TicTacToe by treating the grid as a magic square
(wherein you assign each cell a unique value from 1..9).
There are 8 winners, e.g. numbered sequentially top-left->bottom right:
147, 159, 123, 258, 357, 369, 456 and 789 which add up to 15 (their
'magic
cell values').. and which take part in (n) possible winning scenarios.
Your code might begin with an array of 'winners possible from here'
at each
round and randomly (or not) pick cells with the right potential...
e.g.
pick #1 because it could win 3 ways, #5 could win 4 ways.. but
it needs to
be clever enough to play #1(forced to start at #1) -#5(opponent picks
hub)
-#9(responds keeping 4 potential plays open)
-----
Subject: Re: TicTacToe
Indeed, but it is also published on my website (click on the articles
link, and go for the Component Building article). Unfortuantely, that
article still "wraps" a component around the MAGIC.DLL, for which I've
found the implementation, which follows below:
function WinMove(Game: HGame; ID: TPlayer): TPlace;
{WinMove is in fact only a no-lose move using the magic square
algorithm:
8 3 4
1 5 9
6 7 2
If any three (different) moves of the same player
result in a
sum of 15, then this player has won. The same MS-algorithm
is
used to find the next move to win, or prevent the
opponent to
win.
}
var
tmp,i,j: Integer;
begin
tmp := 0;
i := 0;
repeat
Inc(i);
if GameBoard[Game].Board[i] = ID then
begin
j := i;
if j < LastPlace then
repeat
Inc(j);
if GameBoard[Game].Board[j]
= ID then
begin
tmp := 15 - i - j;
if (tmp < 1) or (tmp > LastPlace) or
(GameBoard[Game].Board[tmp] <> NoneID) then tmp := 0
end
until (j >= LastPlace) or
(tmp <> 0)
end
until (i >= LastPlace) or (tmp <> 0);
WinMove := tmp
end {WinMove};
As the comments says, I'm using the Magic Square algorithm, which is
a
no-lose rather than an always-win scenario...
----------