Simple starter TicTacToe

This topic is 2417 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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. Edited by wiz3kid

Share on other sites
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| 

Share on other sites
update: improved AI, now preventing forks as mentioned
[attachment=3055:XvsO.zip]