# Connect Four Diagonal Check

## 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 whats wrong any suggestions would be greatly appreciated.

## 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;
}

Mark44
Mentor
This:
C:
for (int j = 5; j > 6; j++)
The body of the inner loop is never executed.

Mark44
Mentor

## 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 whats wrong any suggestions would be greatly appreciated.

## 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;
}
}
}

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.

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?

Mark44
Mentor
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.

Mark44
Mentor
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.