/Bilu

AI to win at every tic tac toe

Primary LanguagePython

Bilu

AI to win at every tic-tac-toe game.

This script, show how to win at every tic tac toe game with simple if and else statements, based in the simplicity of the game.

You can use it to try to improve you skills in this game, for fun, or for whatever you want.

If you beat the script, please send your moves to: victorcortezcb@gmail.com i will be glad to know about it.

Tic-tac-toe has a limited range of possible games, and most of them are a symmetric form of another.

#Discription

(Taken from http://mathworld.wolfram.com/Tic-Tac-Toe.html)

The game of tic-tac-toe, also spelled ticktacktoe and also known as 3-in-a-row or "naughts and crosses," is a game in which players alternate placing pieces (typically Xs for the first player and Os for the second) on a 3×3 board. The first player to get three pieces in a row (vertically, horizontally, or diagonally) is the winner.

For the usual 3×3 board, a draw can always be obtained, making it a futile game.

A generalized n-in-a-row on an k×m board can also be considered, as can a generalization to a three-dimensional "board." The game consisting of getting five (or more) in a row on a board variously considered to be of size 13×13 or 15×15 is known as go-moku. The specific case of 4×4×4 tic-tac-toe is known as qubic.

For 2-in-a-row on any board larger than 1×2, the first player has a trivial win. In "revenge" tic-tac-toe (in which n-in-a-row wins, but loses if the opponent can make n-in-a-row on the next move), even 2-in-a-row is non-trivial. For instance, n=2 on a 1×5 board is won for the first player if he starts in the second or fourth square, but not if he starts elsewhere.

In 3-in-a-row, the first player wins for any board at least 3×4. The first player also wins on a 3×3 board with an augmented corner square, with three distinct winning first moves (Gardner 1978).

If the board is at least 5×6, the first player can win for n=4 (the 5×5 board is a draw). The game is believed to be a draw for 4×7, is undecided for 4×8, believed to be a proven win for 4×9, and has been proved as a win for 4×11 by means of variation trees (Ma).

For n=5, a draw can always be obtained on a 5×5 board, but the first player can win if the board is at least 15×15. The cases n=6 and 7 have not yet been fully analyzed for an n×n board, although draws can always be forced for n=8 and 9.

In higher dimensions, for any n-in-a-row, there exists a dimension d board (n×n×...×n) with a winning strategy for the first player (Hales and Jewett 1963). The Hales-Jewett theorem, a central result in Ramsey theory, even allows for more than two players, a dimension d will still exist that gives a first player win. For 3×3×3 and 4×4×4, the first player can always win (Gardner 1979), thus establishing d=3 for n=3 and n=4. For n=8, Golomb has proven d>3 with a Hales-Jewett pairing strategy (Ma 2005). Values of d for other n are unknown, and the Hales-Jewett theorem does not help, as it is existential and not constructive.