## Tic Tac Toe - already done on calc

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?

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.

sweetandy Wrote: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.

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!

Code:

`8 1 6`

3 5 7

4 9 2

Surely this would work if you had a grid like

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.

Code:

`1 1 1`

1 1 1

1 1 1

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.

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!

unknown Wrote: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.

Code:

`X 0 -`

- - -

- - -

would make X the winner with your idea.

Code:

`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

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

}

}

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.

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