PDA

View Full Version : Tic Tac Toe - already done on calc


sweetandy
2006.05.27, 11:24 AM
I was bored in Math class, so what did I do? Cracked open the Prgm function on my calculator and started experimenting. It didn't take long for me to get an idea for Tic Tac Toe, even though I haven't done too much coding.

My question here is basically, what do you think of the efficiency of this coding structure, and what do you think about porting the idea to something like ObjC / Cocoa in a universal binary? Is that possible with this setup?


Draw TTT board and surroundings
Set variables A-I to 0

// Thinking behind that:
// ABC
// DEF <--- TTT board
// GHI

GetX:
If winning combonation (ABC, DEF, GHI, ADG, BEH, etc.) multiplied together = 8
goto WinO
// It checks for the O winner in the X loop because that's where it goes after
// drawing the third O and setting the variable to 2.
Get an input key > N
// I used 1-9 on my calc for the TTT game board.
If N=7 and A=0
goto XdrawA
// Checks for A=0, b/c that means nobody has played A yet.
etc., etc.

GetO:
If winning combonation multiplied together = 1
goto WinX
// X is stored as 1, so the three multiplied together equals 1 if they are all X.
If all numbers A-I are bigger than 1
goto WinCat
//If all positions have been played but no winner has been found
// (leading to O being the next to play), then the cat has won.
If N=7 and A=0
goto OdrawA
etc., etc.

XdrawA:
A = 1
Draw the X there
goto GetO

XdrawB:
B = 1
Draw the X there
goto GetO
etc., etc.

OdrawA:
A = 2
Draw the O there
goto GetX
etc., etc.

Winning messages




That's the outline of my idea. I think it's pretty cool, since like it takes 1^3 or 2^3 for a win to be found, all other multiplied solutions either being a 0, 2, or 4, which the program completely ignores.

What do you think?

sweetandy
2006.05.27, 11:27 AM
Oops! I forgot to add, which is more applicable to this forum - how might I go about a random-playing, let alone logically-playing, AI for Tic Tac Toe?

imikedaman
2006.05.29, 10:27 AM
Heh, that reminds me of when I wrote a Tic Tac Toe game on my calc during government class back in high school. I also made Pong. :wacko:

There's no reason to "port" anything from the calculator to Obj-C - they are completely different languages on completely different architectures with different capabilities. Just write a Tic Tac Toe game for the computer from scratch.

unknown
2006.05.29, 10:56 AM
Oops! I forgot to add, which is more applicable to this forum - how might I go about a random-playing, let alone logically-playing, AI for Tic Tac Toe?

List of allowable moves, pick one at random.
edit: AI: replace stage 2 with pick the best one.

Zekaric
2006.05.29, 06:25 PM
A neat trick I found via AI class or somewhere is to set the board up as a magic square so that all rows, columns and diagonals add up to 15.8 1 6
3 5 7
4 9 2This way you can easily see what will produce a win or where to block by looking at what locations you hold and what locations your opponent holds and seeing if they add up to 15.

unknown
2006.05.29, 06:53 PM
Surely this would work if you had a grid like

1 1 1
1 1 1
1 1 1

and checked what added up to 3 instead of 15, unless the magic square has some other use as well?

Also, instead of checking for X and O seperatly, you can give X's the value 1 and O's the value 2 then, if a row/col/diagonal adds to 3 its a win for X and if it adds to 6 its a win for O.

BeyondCloister
2006.05.29, 07:07 PM
Also, instead of checking for X and O seperatly, you can give X's the value 1 and O's the value 2 then, if a row/col/diagonal adds to 3 its a win for X and if it adds to 6 its a win for O.

That would not work.

X 0 -
- - -
- - -


would make X the winner with your idea.

OneSadCookie
2006.05.29, 07:08 PM
The point of the magic square is that you don't need to know about rows, columns and diagonals -- if any three cells add to 15, they represent a row, column or diagonal.

imikedaman
2006.05.29, 10:00 PM
The basic design would be something like this:
if you can win then
place piece on winning spot
else if you can block then
place piece on blocking spot
else if there's a trick spot then
place piece on trick spot (trick spot being a place that guarantees you a win because there are two possible ways to score - look them up)
else
place piece randomly on the board, usually in the center if it's available
end if

Holmes
2006.06.04, 10:55 PM
I have a version of tic-tac-toe written in MIPs assembly if you want it ;)

It never loses if it gets the first move. If the human moves first it will either win or tie, depending on if the human is stupid. But anyway, you don't want assembly, so this is what I recommend you do:

Make one array to store the box states (either BOX_EMPTY, BOX_X, or BOX_O):

int grid[9];

Your tic-tac-toe boxes would correspond to the indices of grid in the following way:

012
345
678

for the detection of wins, you make an array like so:

int wins[][] = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{ 0,4,8},{2,4,6}};

It works the following way: if either player gets their marks in any group of three indices, that corresponds to a win. For example getting 1, 4, and 7 corresponds to getting a middle column win. So to check for wins you do this:

int i;
for (i=0;i<8;i++) {
if (grid[wins[i][0]] == BOX_X && grid[wins[i][1]] == BOX_X && grid[wins[i][2]] == BOX_X) {
//x wins
}
if (grid[wins[i][0]] == BOX_O && grid[wins[i][1]] == BOX_O && grid[wins[i][2]] == BOX_O)
//o wins
}
}

Holmes
2006.06.05, 01:01 AM
If you want a REAL trick for seeing if there are any winners, set up the board like this:

Start with a board filled up with ones:

01 01 01
01 01 01
01 01 01

When X moves, replace the one with the number below which corresponds to the square he moved into:

02 03 05
07 11 13
17 19 23

And when O moves, replace the one with the number below which corresponds to the square HE moved into:

29 31 37
41 43 47
53 59 61

When you check for a winner multiply all the numbers on the board together.
If the number you get is divisible by 30, 1001, 7429, 238, 627, 1495, 506, or 935, X wins. If the number you get is divisible by 33263, 82861, 190747, 63017, 78647, 106079, 76067, or 84323, then O wins.

That, my friends, is a true hack. :D

Also, Euclid would be proud of you (or stirring in his grave?)