osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

tictactoe script - commented - may have pedagogical value


On Mon, Sep 4, 2017 at 9:26 PM,  <namenobodywants at gmail.com> wrote:
>
> """
> this program makes an optimal tictactoe move by answering the following questions
> in their given order until it is told where to put its mark:
>
> 1) can you win the game?
>     if so then do it
> 2) could your opponent win the game if it was his turn?
>     if so then put your own mark where a mark of his would win him the game
> 3) could you win the game if you had two turns in a row?
>     if so then make a move that leaves you with the largest number of second moves
>     that would win you the game if you could really make them
> 4) is the center square open?
>     if so then put your mark there
> 5) are any of the corners open?
>     if so then put your mark on one of them
> 6) put your mark on any open square
> """

I'm afraid its play is not optimal. Here's a transcript where the program lost.

Usually I've seen Tic Tac Toe implemented using the Minimax algorithm
since the decision tree for Tic Tac Toe is quite shallow.

py> play(oh)


 | |
-+-+-
 | |
-+-+-
 | |


cell number: 1


X| |
-+-+-
 |0|
-+-+-
 | |


cell number: 9


X| |0
-+-+-
 |0|
-+-+-
 | |X


cell number: 7


X| |0
-+-+-
 |0|
-+-+-
X|0|X


cell number: 4


X| |0
-+-+-
X|0|
-+-+-
X|0|X


X wins