I am writing a program for connect four I have all the code written and all works fine i was wondering however if anyone could offer any advice on a function to determine if a player has 4 or more in a row cause i just cant seem to think of a good way if all else fails i will write out every combonation ....which is a lot and i would rather not do that when i know there has to be a better way maybe like some kind of searching algorithm but im not sure any advice anyone?

There are a couple of non-exhaustive search ideas here:

http://www.experts-exchange.com/Programming/Algorithms/Q_23060580.html

They seem to be based mainly on two facts: 1) the board fills from the bottom, so there should be many solutions involving the top of the board that are not possible and 2) for the rows, if you haven't found a solution after the first 5, you know for sure if you will be able to find a solution by looking at the next two.

Good luck,

Dave

yeah i was thinking maybe making three functions one for checking vertical one for horizontal and one for diaganol and having the function check to the left until a different character is reaached then back to the right but i havnt quite figured out how to iimplement it yet

Member Avatar for iamthwee

^^ That sounds logical.

All you really need to do is check the last counter placed. So should be less computer intensive.

yeah i got that method to work for checking vertically but for somereason when i use the equivalent to check it horizontally it messes up here is my code for the checking horizontal function anyone have any idea where the logic error is it could be because how two dimensional arrays are stored in ram but i wanted a second opintion http://codepad.org/EJfgtGZr

Poat CODE not links.

void checkHorizontal(char grid[ROWS][COLUMMS], int position, int row, bool &winner, char player, int &gameCount)
{
	int count_left = 0;
	int count_right = 0;
	for(position; position>0; position--)
		if (grid[row][position-1] == player)
			count_left++;
		else
			position = 0;
	for(position; position<6; position++)
		if(grid[row][position+1]==player)
			count_right++;
		else
			position = 6;
	if((count_left+count_right) >= 4)
		winner = true;
	if(winner ==true)
	{
		system("cls");
		displayGrid(grid);
		gameCount = 42;
		cout << "Player " << player << "'s wins!\n";
		system("pause");
	}
}

actually i fount my error it was in the fact that the first for loop changed the position value and that was screwing up the count right so i just had to make two more variables one for each for loopo so one wouldnt be affected by the other

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.