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

Discussion Overview

The discussion revolves around troubleshooting a diagonal check implementation for the game Connect Four in C#. Participants are exploring issues related to the for loop structure and the logic used to determine a winning condition based on diagonal placements of pieces.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the for loop structure, specifically the condition for (int j = 5; j > 6; j++), noting that the inner loop body is never executed.
  • Another participant suggests that the loop should decrement j instead, questioning whether j should be the column index.
  • There are suggestions to improve code readability by adding comments and splitting long Boolean expressions into smaller parts.
  • Participants discuss the implications of the loop's initialization and test expressions, emphasizing that the current setup leads to the loop not executing at all.

Areas of Agreement / Disagreement

Participants generally agree on the issue with the loop condition, but there is no consensus on the best approach to resolve it. The discussion remains unresolved regarding the correct implementation of the diagonal check logic.

Contextual Notes

There are limitations in understanding the intended logic of the diagonal check, particularly regarding the indexing of rows and columns. The discussion highlights the need for clarity in variable naming and loop structure.

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 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 11 ·
Replies
11
Views
10K
Replies
23
Views
2K