Newsgroups: comp.ai.games
Path: cantaloupe.srv.cs.cmu.edu!bb3.andrew.cmu.edu!newsfeed.pitt.edu!gatech!news.mathworks.com!fu-berlin.de!news.dfn.de!news.uni-augsburg.de!news
From: Pitbull <schaefer@malaga.math.uni-augsburg.de>
Subject: Re: Tic-Tac-Toe Hash
Message-ID: <DMBJCJ.HJA@Yuri.CC.Uni-Augsburg.DE>
Sender: news@Yuri.CC.Uni-Augsburg.DE (The News Administrator)
Nntp-Posting-Host: almeria.math.uni-augsburg.de
Organization: Computer Center, University of Augsburg, Germany
References:  <4eta6j$j7a@news.bu.edu>
Date: Mon, 5 Feb 1996 19:55:30 GMT
Lines: 101

meissner@cns.bu.edu (Karl Meissner) wrote:
>
> 
> I am writing a genetic algorithm that breeds tic-tac-toe players.
> One of the representations that I would like to try is a 
> hash table.  Given a tic-tac-toe board, index into a table
> of moves.  Then the string dna can just be the interpreted as
> the enteries in a hash table.
> Genetic algorithms are sensitive to the length of 
> of the strings that they are operating on.  Generally, the shorter
> the better.
> 
> Since there are only three states, empty, x and o, there are
> not more than 3^9 = 19683 possible boards.  But many of those 
> boards are illegal, such as a board with all X's.  This 
> also does not take into account rotation and reflection.
> 
> 
  xox  ..x   ...
> o..  ..o   ..o   o..
> x..  ...   .ox   xox
> 
> ox.  ...   x..   xox
> ..o  ..o   o..   ..o
> ..x  xox   xo.   ...
> 
> Can all be treated as the same (x goes in the center and forks)
> 
> So my question is ..
> What is a hash function that encodes all tic-tac-toe boards, 
> so that it ranges over the shortest possible interval
> without colliding unique boards and thus I have a small hash table?  
> 
> A good function would probably fold in rotation and reflection,..

Well i actually think the crux of your problem is to find a method of
distinguishing rotated or mirrored boards.

Let us try this encoding:
o=-1 x=+1 .=0
and use number the edges as -1..+1 :
Compute 
dx(board)=sum over all squares( occupant(square)*xvalue(square)
dy(board)=sum over all squares( occupant(square)*yvalue(square).

This gives us the following laws for boards a,b:
symmetry in x: dx(a)=-dx(b)
symmetry in y: dy(a)=-dy(b) 
skewness     : dx(a)= dy(b) and dy(a)=-dx(b)

Now, based on this function, mirror boards 
with dx<0 on the x-axis,
with dy<0 on the y-axis,
'skew boards on the xy axis.

I haven't really thought this thru, because i think tictactoe really
is trivial after you have found a way to compress it(sic!), so here 
is a piece of C to help you. tell me what you find.

<source>
#include <stdio.h>

#define D(i,j) (( b[(i)+1][(j)+1]=='o' )?-1:(  b[(i)+1][(j)+1]=='x'?+1:0 )) 
#define sc(k) scanf("%s",&b[k]);

void main() {
 char b[3][4];
 int i,j,dx,dy,dxy;
 
 while (!0) {
  printf("enter\n");
  sc(0);sc(1);sc(2);
  printf("\n");

  dx=0;dy=0;dxy=0;
  for(i=-1; i<2; i++)
    for(j=-1; j<2; j++)
      { dx+=i*D(i,j); dy+=j*D(i,j); dxy+=i*j*D(i,j); printf("%i",D(i,j)); }
  printf("\n");

  printf("dx=%i,dy=%i,dxy=%i\n",dx,dy,dxy);
 }
}

</source>

examples:
ox.
x..
o..

.x
.o
..

stop with CTRL-C.

Peter

> 	Karl

