request for code : Py Tic Tac Toe in action


Furman Smith


I read
"I played against your script and got a win,
which, as you know, is not possible
with optimal play by the opponent. A while ago
I wrote a script that plays optimal tic-tac-toe.
Let me know if you want to look at it."

I would certainly appreciate the code.


=== === === extra of little or no interest:

I'm learning Python in order to analyze a game that I invented
(the last article listed at ) called
naught or cross.

Neither player "owns" a symbol -- "x" or "o" -- but wins when
achieving three of the same symbol in a tic-tac-toe line.

When you place your symbol on the board you tell me which symbol
I am to place. I place the assigned symbol where-ever I choose
and then tell you which symbol to place.

I started a hand analysis yesterday and it took hours to
determine part of the game tree. I estimated, assuming other parts
of the tree would take as much work, that I had about 750 times
as much work left to do. So it is time to program -- which
I love and I've been intending to learn Python anyway.

I've taught Fortran and Pascal (and BASIC, AWK, Logo, etc) years
ago and need to learn the object oriented approach. Perhaps your
work will make it make sense.

So far I'm thinking of a game position as a tuple where the first
component shows the locations occupied by the symbol which is to
be placed and the second component shows the locations of the
other symbol.

I'm using the following scheme for cell locations in order to simplify
translating a position to a standard position (rotating by adding
mod 8 to the outside cells and flipping by subtracting the outside
cells from 8):
0 1 2
7 8 3
6 5 4

This is already more than you probably want to know but let me give an
example. If you have just placed the piece that I've named and the
board looks like
o . .
x o .
x . . and you tell me to place an "x"

(which is position (67,08) with x's specified in the first component,
which is equivalent to standard position (12,08))
then I might place the x in the middle right and tell you to place
an x on your move. Therefore the board looks like
o . .
x o x
x . . and you are to place an x (this is equivalent to (367,08)).

Since you wrote a Python program to play tic-tac-toe optimally,
I bet that you will place your x in the lower right and
tell me to place an o. So the position is
o . .
x o x
x . x and I am to place an o. (a position equivalent to (08,3467)).

You have me. No matter where I place the o and no matter whether I
tell you to place an x or an o after that, you will be able to make
three in a line.

I found a neat Web page which was cached by Google,
c_Tac_Toe_(Python)+Py+Tic+Tac+Toe&hl=en&gl=us&ct=clnk&cd=10 ,
and I'm enjoying reading it but I'd appreciate seeing your approach


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question