Skip to content

atrigent/ai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 

Repository files navigation

My tic-tac-toe game uses the minimax algorithm. It is capable of using the multiplayer
formulation described in the textbook, with a vector of utilities at each node. It is slow
to start up because it generates the entire game tree at the beginning. This allows gameplay
to be smooth. Theoretically, this code could use more than two players or more than 3x3
boards. However, memory usage and CPU time limitations make that pretty impractical. For the
simple 3x3 case, this program takes about 5 seconds to start on my machine and uses about 26MB
of RAM. I have not gotten it to successfully generate the tree for a 4x4 game yet - it requires
multiple gigabytes of RAM and multiple minutes of time at least.

One thing that I added to the minimax algorithm was the concept of "distance" - how many turns
will be required to reach a state. By including this information, it is possible to prefer
paths that more quickly result in an endgame. I added this when I observed the AI making
unintuitive moves when it could always win but when one win state would take more moves
to get to than the other win state. It should prefer the one with fewer moves (for example,
if it can win on the next move), but it was sometimes choosing the longer path. While this
property could be interpreted as being "sadistic" and therefore possibly a desireable trait
for an adversarial AI to have, I thought that it should try to favor fast games.

I did not separate the AI algorithm kernels out of the rest of the game code, so in that sense
it might not be as general as it could be. However, the algorithms are sufficiently simple
that I don't think there would be much value to that. As I've said before, the algorithm
can be used with any board size and any number of players. I thought about supporting non-square
boards, but I ultimately decided against it. I don't think it would be very difficult to support
though - the main difference would be that there are no diagonals.

This code also implements all of the optimizations having to do with equivalent rotations
and reflections of the game board. This allows the game tree to be as small as possible. It
also stops expanding the game tree when a win or tie is encountered.

Gameplay is simple - just enter your move by entering the coordinates. The game will figure
it out, as long as there are two numbers in your input somewhere.

As a bonus, you can also play this game by sshing to [email protected], password threeinarow.

Again, this was written using Python 3.2.

:)

About

Code that I wrote in my AI class (CS 4100).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages