Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone I am currently making a connect 4 game in c#. I was just wanting to know if anyone could help me with checking horizontal, vertical and diagonal wins in a 7x6 board. Below is the code of the board. If you need more code on anything else please let me know I really really need help as I am really stuck and need to finish this in 2 weeks

What I have tried:

splitting wins up into horizontal,vertical and diagonal methods
Posted
Updated 4-Apr-16 12:32pm
v3

1 solution

You didn't tell us in which way your method CheckHorizontal(..) does not work and I find it not very intuitive to read, so I'll just show you how I would do it:

Declare an array of X/Y-tuples which describe the translation from the position at which the method currently "looks at" to the next position, for 0°, 45°, 90° and 135°. For the tuples I'd use a custom class to avoid the "Item1", "Item2" property-names of the inbuilt Tuple<,> class. I'm setting the origin of the coordinate system in the lower left corner.
C#
// pseudocode:              N        NE       E      SE
array directionSteps = { {0, -1}, {1, -1}, {1, 0}, {1, 1} }


The remainder explained in comments:
C#
// semi-pseudocode:  

int xCenter = ...   // x-coordinate of tile placed last
int yCenter = ...   // y-coordinate of tile placed last
int tileColor = ... // value of tile placed last (probably 1, resp. 2)

// outer loop loops over those direction-steps N, NE, E, SE:
foreach (directionStep in directionSteps)
{
    int matchingTiles = 1; // tile placed last is the first match

    // to check each direction also into the opposite direction
    // have a multiplier of -1 / 1 for each of them:
    for (int directionUpDown = -1; directionUpDown <= 1; directionUpDown += 2)
    {
        int xStep = directionStep.X * directionUpDown;
        int yStep = directionStep.Y * directionUpDown;

        // "walk" into the current direction, starting 1 tile away;
        // distance will be 5 at maximum
        for (int distance = 1; distance <= 5; distance++)
        {
            // now "looking" at these coordinates:
            int x = xCenter + xStep * distance;
            int y = yCenter + yStep * distance;

            if (IsOutsideOfGrid(x, y))
                break;

            if (Grid[x, y] == tileColor)
                matchingTiles++;
            else
                break;
        }
    }

    if (matchingTiles >= 4)
        // player with that tileColor has won
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900