What am I doing wrong with my diagonal check in Connect Four?

  • Thread starter Thread starter Veronica_Oles
  • Start date Start date
Click For Summary
The discussion centers on troubleshooting a diagonal check function for Connect Four in C#. The primary issue identified is that the inner loop for variable j is incorrectly set to run from 5 to 6, which results in the loop body never executing. Participants suggest using more informative variable names and adding comments to clarify the code's purpose. Additionally, it's recommended to break down complex Boolean expressions for better readability. The overall focus is on correcting the loop logic to ensure the diagonal check functions as intended.
Veronica_Oles
Messages
141
Reaction score
3

Homework Statement


I am working on the diagonal check for the game Connect Four in C#. I need help with trying to figure out what is wrong with the diagonal check. I noticed a trend pertaining to the points. The j which is the rows goes down by 1 and the i which is the columns up by 1. I am struggling with the for loops now, I put j is equal to 5 since when you count the amount of rows it goes 0, 1, 2, 3, 4, 5, yet I still don't know what's wrong any suggestions would be greatly appreciated.

Homework Equations

The Attempt at a Solution



Code:
     private Boolean diagonal_winner_up()
        {
            Boolean winner = false;

            {
                for (int i = 0; i< 7; i++)
                {

                   for (int j = 5; j > 6; j++)
                   {
                       if (board[i,j] == 1 && board[j - 1, i + 1] == 1&& board[j - 2, i + 2] == 1 && board[j - 3, i + 3] == 1 || board[i,j] == 2 && board[j - 1, i + 1] == 2 && board[j - 2, i + 2] == 2 && board[j - 3, i + 3] == 2)
                       {
                           winner = true;
                           if (board[i, j] == 1)
                           {
                               player1_wins = 1;
                           }
                           else
                           {
                               player2_wins = 2;
                           }
                       }
                   }
                }
            }
            return winner;
        }
 
Physics news on Phys.org
This:
C:
for (int j = 5; j > 6; j++)
The body of the inner loop is never executed.
 
Veronica_Oles said:

Homework Statement


I am working on the diagonal check for the game Connect Four in C#. I need help with trying to figure out what is wrong with the diagonal check. I noticed a trend pertaining to the points. The j which is the rows goes down by 1 and the i which is the columns up by 1. I am struggling with the for loops now, I put j is equal to 5 since when you count the amount of rows it goes 0, 1, 2, 3, 4, 5, yet I still don't know what's wrong any suggestions would be greatly appreciated.

Homework Equations

The Attempt at a Solution



Code:
     private Boolean diagonal_winner_up()
        {
            Boolean winner = false;

            {
                for (int i = 0; i< 7; i++)
                {

                   for (int j = 5; j > 6; j++)
                   {
                       if (board[i,j] == 1 && board[j - 1, i + 1] == 1&& board[j - 2, i + 2] == 1 && board[j - 3, i + 3] == 1 || board[i,j] == 2 && board[j - 1, i + 1] == 2 && board[j - 2, i + 2] == 2 && board[j - 3, i + 3] == 2)
                       {
                           winner = true;
                           if (board[i, j] == 1)
                           {
                               player1_wins = 1;
                           }
                           else
                           {
                               player2_wins = 2;
                           }
                       }
                   }
                }
            }
            return winner;
        }
It would be a good idea to put in comments as you're writing the code, rather than add them later on. Comments would be especially helpful in describing what the inner loop is doing.

Also, splitting up that long Boolean expression makes it easier for a naive reader to comprehend, plus the reader doesn't have to scroll all the way to the right to see what it consists of.

Is i the row and j the column? If so, row and col would be more informative variable names.
C:
for (int j = 5; j > 6; j++)
{
      // First clump of conditions: check that player1 has cells blah, blah, blah.
      // Second clump of conditions: check that player2 has cells blah, blah, blah.
      if ( (board[i, j] == 1 && board[j - 1, i + 1] == 1 && board[j - 2, i + 2] == 1 && board[j - 3, i + 3] == 1) ||
           (board[i, j] == 2 && board[j - 1, i + 1] == 2 && board[j - 2, i + 2] == 2 && board[j - 3, i + 3] == 2) )
     {
            winner = true;
            if (board[i, j] == 1)
            {
                  player1_wins = 1;
            }
            else
            {
                   player2_wins = 2;
            }
     }
 }
 
Mark44 said:
It would be a good idea to put in comments as you're writing the code, rather than add them later on. Comments would be especially helpful in describing what the inner loop is doing.

Also, splitting up that long Boolean expression makes it easier for a naive reader to comprehend, plus the reader doesn't have to scroll all the way to the right to see what it consists of.

Is i the row and j the column? If so, row and col would be more informative variable names.
C:
for (int j = 5; j > 6; j++)
{
      // First clump of conditions: check that player1 has cells blah, blah, blah.
      // Second clump of conditions: check that player2 has cells blah, blah, blah.
      if ( (board[i, j] == 1 && board[j - 1, i + 1] == 1 && board[j - 2, i + 2] == 1 && board[j - 3, i + 3] == 1) ||
           (board[i, j] == 2 && board[j - 1, i + 1] == 2 && board[j - 2, i + 2] == 2 && board[j - 3, i + 3] == 2) )
     {
            winner = true;
            if (board[i, j] == 1)
            {
                  player1_wins = 1;
            }
            else
            {
                   player2_wins = 2;
            }
     }
}
Will do thanks though.
 
Mark44 said:
This:
C:
for (int j = 5; j > 6; j++)
The body of the inner loop is never executed.
Would I decrement the j which is the column?
 
Veronica_Oles said:
Would I decrement the j which is the column?
C:
for (/* 1 */ int j = 5; /* 2 */ j > 6; /* 4 */ j++)
{
    /* 3  -- loop body */
}
Here's how a for loop works:
  1. The initialization expression (1) is evaluated. This happens only once, before the first iteratation of the loop.
  2. The test expression (2) is evaluated. If it's true, the body of the loop (3) is executed. If it's false, control is transferred to whatever statement comes after the for loop.
  3. After the loop body executes, the update expression (4) is evaluated.
  4. Go to step 2.
With regard to your question, since I don't know what you're trying to do, I can't answer the question.
 
C:
for (int j = 5; j > 6; j++)
Continuing what I said in my previous post, here's what happens:
  1. j is initialized to 5.
  2. The test expression, j > 6, is false.
  3. Control passes to the first statement beyone the end of the loop body.
The update section, j++, is not evaluated here, so it wouldn't matter if you incremented or decremented it, or even if it wasn't there at all. For the loop as you have it, the update section is entirely irrelevant.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 11 ·
Replies
11
Views
10K
Replies
23
Views
2K
Replies
4
Views
5K