Sign in to follow this  
?????????

Simple starter TicTacToe

Recommended Posts

?????????    199
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).

Thanks in advance,
David D. Edited by wiz3kid

Share this post


Link to post
Share on other sites
BeerNutts    4401
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:
[code]

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;
}
[/code]

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:


[code]
x|x|
-----
x|o|
-----
|o|
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this