Does anyone have a good Lesson 12 solution? *spoilers?* lol

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
krutin86
Posts: 3
Joined: March 8th, 2013, 8:12 pm

Does anyone have a good Lesson 12 solution? *spoilers?* lol

Post by krutin86 » March 12th, 2013, 4:12 am

So I'm running into a situation where the computer will opt to block me instead of winning especially when its chances of winning are in the 3rd column and/or 3rd row because it has to start on the top left and work its way to the bottom right. One way around this is by running the below code twice: Checking for X and then checking for O. I'm too lazy for that. My loops don't check for diagonals, I wanted to win (can't have another skynet!)

What I am curious about is if there are any other ways that I'm not thinking of. I'm very curious to see what you guys have come up with, I think it would benefit all of us if we shared our algorithms - there's always room for learning, right? If you have something you'd like to share with the community, come on down!

This is my code:

Code: Select all

int Game::doAITurn()
{
	int yCOOR = 0;
	int xCOOR = 0;


	for (yCOOR = 0; yCOOR < 3; yCOOR++)
	{
		xCOOR = 0;
		
		if (getSquareState(xCOOR, yCOOR) == getSquareState(xCOOR , (yCOOR + 1) % 3) && getSquareState(xCOOR, yCOOR) != EMPTY)
		{
			AIMoveX = xCOOR;
			AIMoveY = (yCOOR + 2) % 3; 
			if (getSquareState(AIMoveX, AIMoveY) == EMPTY)
			{
				//break
				return 0;
			}
		}

	
		for (xCOOR = 0; xCOOR < 3; xCOOR++)
		{

			if (getSquareState(xCOOR, yCOOR) == getSquareState((xCOOR + 1) % 3, yCOOR) && getSquareState(xCOOR, yCOOR) != EMPTY)
			{
				AIMoveY = yCOOR;
				AIMoveX = (xCOOR + 2) % 3; 
				if (getSquareState(AIMoveX, AIMoveY) == EMPTY)
				{
					//break
					return 0;
				}
			}
		}
	}
}

Post Reply