Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


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.

  • You cannot reply to this topic
2 replies to this topic

#1 anonymous22   Members   -  Reputation: 199

Like
0Likes
Like

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

Thanks in advance,
David D.

Attached Files

  • Attached File  XvsO.zip   245.52KB   22 downloads

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


Sponsor:

#2 BeerNutts   Crossbones+   -  Reputation: 3018

Like
1Likes
Like

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)

#3 anonymous22   Members   -  Reputation: 199

Like
0Likes
Like

Posted 10 June 2011 - 01:26 PM

update: improved AI, now preventing forks as mentioned
Attached File  XvsO.zip   245.67KB   25 downloads




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.



PARTNERS