-1

I've written the following method in C# for a small Forms program I'm working on, but whenever I try to run this method the entire program freezes.

There are no errors and I've tried to do for (int n = 0; n<8; n++) instead of while(true), but that didn't seem to change anything...

Any ideas? Thanks in advance!

public bool legalMove(int y, int x)
    {
        // Check if the cell is occupied
        if (grid[x, y] != 0)
            return false;

        // Check if there's an opponents circle somewhere around it
        for (int i = -1; i<=1; i++)
            for (int j = -1; i<=1; j++)
            {
                if (i == 0 && j == 0)
                    continue;

                int row = x + i;
                int col = y + j;

                if (row >= 0 && row < 8 && col >= 0 && col < 8 && grid[col,row] == -turn)
                {
                    // Now we know that there's an opponents circle somewhere around this space, we now check if it can be captured
                    while(true)
                    {
                        row += i;
                        col += j;

                        if (row < 0 || row > 7 || col < 0 || col > 7 || grid[row, col] == 0)
                            return false; // Outside of the board or an empty space
                        else if (grid[row, col] == turn)
                            return true; // No empty spaces between our cell and another cell of ours 
                    }
                }
            }
        return false; // No cell found around ours
    }
Jasper
  • 29
  • 4
  • 1
    https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – wohlstad Dec 15 '22 at 11:21

1 Answers1

6

Here's the problem: for (int j = -1; i<=1; j++). Should be j<=1 instead of i<=1.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103