Algorithm for playing Tic-Tac-Toe
Take a tic-tac-toe square, and value the squares as follows:
For each possible square you can pick in a move, add the values for each full row, column, or diagonal that it is on (a "full" row or diagonal means having 3 squares, occupied or not), adding 2 for each marker of your own on that row, column, or diagonal, and 1 for each marker of your opponent on that row, column, or diagonal. Then multiply by -1 for each opponent's marker on that row, column, or diagonal [*]. Take the square with the highest value; choose randomly among squares when there are ties for highest value.
So, initially, before any markers are placed, the cells will have the values:
The center, having a value of 24, will always be taken first.
The proper response for O, looking at the values:
Upper Left Square:
Add left column: 2+1+2 = 5
Add top row: 2+1+2 = 5
Add diagonal: (2+3(for square)+1(for opponent marker on square)+2) x -1 = -8
5+5-8 = 2
Upper Middle Square:
Add top row: 2+1+2 = 5
Add center column: (1+3(for square)+1(for opponent marker on square)+1)x-1= -6
5-6 = -1
Upper Right Square:
Add right column: 2+1+2 = 5
Add top row: 2+1+2 = 5
Add diagonal: (2+3(for square)+1(for opponent marker on square)+2) x -1 = -8
5+5-8 = 2
Middle Left Square:
Add left column: 2+1+2 = 5
Add middle row: (1+3(for square)+1(for opponent marker on square)+1)x-1= -6
5-6 = -1
Middle Right Square:
Add right column: 2+1+2 = 5
Add middle row: (1+3(for square)+1(for opponent marker on square)+1)x-1= -6
5-6 = -1
Lower Left Square:
Add left column: 2+1+2 = 5
Add bottom row: 2+1+2 = 5
Add diagonal: (2+3(for square)+1(for opponent marker on square)+2) x -1 = -8
5+5-8 = 2
Lower Middle Square:
Add bottom row: 2+1+2 = 5
Add center column: (1+3(for square)+1(for opponent marker on square)+1)x-1= -6
5-6 = -1
Upper Right Square:
Add right column: 2+1+2 = 5
Add bottom row: 2+1+2 = 5
Add diagonal: (2+3(for square)+1(for opponent marker on square)+2) x -1 = -8
5+5-8 = 2
So, since any of the corners have a value of 2, which is the highest value, randomly pick one.
And so on.
Yeah, the things I do for fun on Saturday... wild, huh?
If you see a hole in my algorithm, point it out, I just did it while sitting here, but it looks good to me...
[*] you'll note that means that two markers of the opponent cancel out each other's multiplication effect.
Take a tic-tac-toe square, and value the squares as follows:
2|1|2
_____
1|3|1
_____
2|1|2
For each possible square you can pick in a move, add the values for each full row, column, or diagonal that it is on (a "full" row or diagonal means having 3 squares, occupied or not), adding 2 for each marker of your own on that row, column, or diagonal, and 1 for each marker of your opponent on that row, column, or diagonal. Then multiply by -1 for each opponent's marker on that row, column, or diagonal [*]. Take the square with the highest value; choose randomly among squares when there are ties for highest value.
So, initially, before any markers are placed, the cells will have the values:
17|10|17
________
10|24|10
________
17|10|17
The center, having a value of 24, will always be taken first.
| |
_____
|X|
_____
| |
The proper response for O, looking at the values:
Upper Left Square:
Add left column: 2+1+2 = 5
Add top row: 2+1+2 = 5
Add diagonal: (2+3(for square)+1(for opponent marker on square)+2) x -1 = -8
5+5-8 = 2
Upper Middle Square:
Add top row: 2+1+2 = 5
Add center column: (1+3(for square)+1(for opponent marker on square)+1)x-1= -6
5-6 = -1
Upper Right Square:
Add right column: 2+1+2 = 5
Add top row: 2+1+2 = 5
Add diagonal: (2+3(for square)+1(for opponent marker on square)+2) x -1 = -8
5+5-8 = 2
Middle Left Square:
Add left column: 2+1+2 = 5
Add middle row: (1+3(for square)+1(for opponent marker on square)+1)x-1= -6
5-6 = -1
Middle Right Square:
Add right column: 2+1+2 = 5
Add middle row: (1+3(for square)+1(for opponent marker on square)+1)x-1= -6
5-6 = -1
Lower Left Square:
Add left column: 2+1+2 = 5
Add bottom row: 2+1+2 = 5
Add diagonal: (2+3(for square)+1(for opponent marker on square)+2) x -1 = -8
5+5-8 = 2
Lower Middle Square:
Add bottom row: 2+1+2 = 5
Add center column: (1+3(for square)+1(for opponent marker on square)+1)x-1= -6
5-6 = -1
Upper Right Square:
Add right column: 2+1+2 = 5
Add bottom row: 2+1+2 = 5
Add diagonal: (2+3(for square)+1(for opponent marker on square)+2) x -1 = -8
5+5-8 = 2
So, since any of the corners have a value of 2, which is the highest value, randomly pick one.
And so on.
Yeah, the things I do for fun on Saturday... wild, huh?

If you see a hole in my algorithm, point it out, I just did it while sitting here, but it looks good to me...
[*] you'll note that means that two markers of the opponent cancel out each other's multiplication effect.

VIEW 5 of 5 COMMENTS
tiger_fodder:
Ah the 50s...before Civil Rights and Macs. No...I prefer it now.
tiger_fodder:
I'm glad that I wasn't drafted for the Battle of Hastings. I was busy smoking hash with all the hippies in York. We fled to Ireland for a bit, until we could come back. The worst part was that we had to learn that damned French language to keep up with the Norman chicks.