Check for (improved) winning moves

For starters, this is NOT homework. I like programming for fun, and I am almost 64 years of age. Now, on to my problem..
I wrote a 'Connect Four' game, and right now it will only check for four in a row. I would like to be able to check UNTIL the space checked is NOT your piece, and THEN signal all the pieces that make up the win, if it's more than 3.
Here's my row checking section in my function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
bool Winning_Move(int ConnectBoard[][7],int player)
{
 int win = 0;
 int Show_Win[6][7];// To show winning markers

 Clear_Show_Win(Show_Win);

 bool over = false;
 text(black on light_gray); // My color function 
 // Check rows
 for(int rows = 0;rows < 6;rows++)
 {
	win = 0;
	Clear_Show_Win(Show_Win);
	for(int x = 0;x < 7; x++)
	 if( ConnectBoard[rows][x] == player)
	 {
		win++;
		Show_Win[rows][x] = player;
		if(win == 4 )
		{
		 gotoXY(35,20,"Player ");
		 cout << player << " wins";
		 Show_The_Win(Show_Win, player);// This function flashes the winning move
		 over = true;
		}
	 }
	 else
	 {
		win = 0;
		Clear_Show_Win(Show_Win);
	 }
 }
// Rest of code to check columns and diagonals, not shown 


The game works, but will only show the first 4 markers that make up the win, even if there are more than 4 continuous markers in the row.
Thanks for any help or ideas.

whitenite1 wrote:
The game works, but will only show the first 4 markers that make up the win, even if there are more than 4 continuous markers in the row.

Your problem is not with the function you have posted. Your problem is in your Show_The_Win function which you have not posted here...
@Smac89

I don't believe the problem lies with the Show_The_Win function. That gets called WHEN I have 4 in a row. I'm trying to figure out how to check each marker in the row, adding one to the win count, and when I get to a non-player marker, then go to the function. If I get to a non-player marker, and the win variable is not over 3, then reset the win variable to 0, and continue to the end of the row.

I can't figure out how to keep checking without resetting before getting to the end of the row.
row = 'OOOOO--'
win variable would equal 5, but would reset when it reaches the - ( non-player piece)

If it helps, here is the Show_The_Win function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void Show_The_Win(int Show_Win[][7], int player)
{
 int PlayerColor[2] = {12,9}; // Colors for each player, light_red and light_blue
 text(PlayerColor[player-1] on light_gray);// Sets text color depending on player number
 for(int z=1;z<24;z++)
 {
	for(int a = 0;a < 6;a++)
	 for(int b = 0;b < 7; b++)
	 {
		if (Show_Win[a][b] == player)
		{
		 if(z%2==0) // Flashes between player marker and O
		 {
			text(black on light_gray);
			gotoXY(34+(b*2),10+a,"O");
		 }
		 else
		 {
			text(PlayerColor[player-1] on light_gray);
			gotoXY(34+(b*2),10+a,"\x0F");
		 }
		}
	 }
	 Sleep(140);
 }
}

Last edited on
I'd examine each position on the board to see if it is the starting position of a winning streak in any direction. Note that if you do this, you only need to check 3 directions (left, down, and down/left). Something like this (which I'm typing off the top of my head so it's probably full of errors):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Calculate the length of the run for the current player starting at startRow,startCol
/ and going in the direction rowInc,colInc
int runLen(int startRow, in startCol, int rowInc, int colInc)
{
    int result = 0;
    int r= startRow;
    int c = startCol
    do {
        if (ConnectBoard[r][c] != player) {
            break;
        }
        ++result;
        r += rowInc;
        c += colInc;
    } while (r < 6 && c <7);
    return result;
    }
}

Then inside winningMove:
for (int row = 0; row < 6; ++row) {
    for (int col = 0; col < 7; ++col) {
        if (runLen(row,col, 1, 0) >= 4 ||
            runLen(row,col, 1, 1) >= 4 ||
            runLen(row,col, 0, 1) >0 4)) {
               // it's a winner
        }
    }
}


I have left out the code that sets/checks Show_Win. You can set it inside runLen().

Hope this helps.
This should fix the problem in your Winning_Move function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    for(int x = 0;x < 7; x++)
    {
        if( ConnectBoard[rows][x] == player)
	{
                win++;
		Show_Win[rows][x] = player;
        }
        else if(win >= 4 )
    	{
		gotoXY(35,20,"Player ");
		cout << player << " wins";
		Show_The_Win(Show_Win, player);// This function flashes the winning move
		over = true;
        }
        else
	{
		win = 0;
		Clear_Show_Win(Show_Win);
	}
    }
Last edited on
@Smac89

That worked beautifully for the rows. I'm now tweaking it for using it on the right and left diagonals. Almost have it working correctly. Many thanks.

@dhayden

I thank you also for your help. Appreciate the ideas.

Topic archived. No new replies allowed.