• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Simple starter TicTacToe

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1anonymous22  Members

Posted 10 June 2011 - 12:31 PM

Hello GDNet community,
I coded a small TicTacToe game in C++ with SFML. It is not finished yet (far from that), but I managed to implement Human vs AI game using magic square algorithm (where all sums equal 15). I would like to know (by testing of course and by code recheck maybe) whether my AI is truly unbeatable.
Note: Win conditions are NOT implemented, only AI moves, so I would like to receive feedback whether the AI is unbeatable, not about win conditions.

In addition, I know I have coded this somehow dirtily so I would like some suggestions on how I could improve the applications structure (so far it uses all global functions and no OOP).

David D.

#### Attached Files

Edited by wiz3kid, 10 June 2011 - 01:29 PM.

### #2BeerNutts  Members

Posted 10 June 2011 - 01:24 PM

Well, it's not the most elegant tic-tac-toe I've seen, but it's a good try for an early game.

However, it's hard to tell what's happening. If you want others to easily understands what you're doing, you should provide comments. Especially for what the functions will do, what they'll return, etc.

You should structure your programming better: For example, doEdges(), do this:

void doEdges(char gameBoard[3][3], int magicBoard[3][3])
{
int edges[4] = { 1, 7, 9, 3 };
for each (int edge in edges)
{
int row, col;
getPosByNum(magicBoard, edge, row, col);
char edgec = gameBoard[row][col];
if (edgec != ' ')
{
gameBoard[row][col] = 'O';
break;
}
}
return;
}


In switch statements, don't use goto, use break;

Also, You should try and be more modular, IE, don't have functions modify the game board, have them return what move the AI should go to, and have the calling function modify the game board

Finally, I think it's beatable. If Human goes left center, AI goes center. Human goes Top center, AI will go to an edge (since Check corners will not find an 'X'). Then Human goes Top Left, he has a winning situation:

x|x|
-----
x|o|
-----
|o|


My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #3anonymous22  Members

Posted 10 June 2011 - 01:26 PM

update: improved AI, now preventing forks as mentioned