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

  • Thread starter Veronica_Oles
  • Start date
In summary, the author is trying to figure out what is wrong with the diagonal check for the game Connect Four. He noticed a trend concerning the points, and is struggling with the for loops.
  • #1
Veronica_Oles
142
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
  • #2
This:
C:
for (int j = 5; j > 6; j++)
The body of the inner loop is never executed.
 
  • #3
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;
            }
     }
 }
 
  • #4
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.
 
  • #5
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?
 
  • #6
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.
 
  • #7
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.
 

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

What is Connect Four Diagonal Check?

Connect Four Diagonal Check is a method used to determine if a player has won a game of Connect Four by getting four of their game pieces in a diagonal line.

How does Connect Four Diagonal Check work?

Connect Four Diagonal Check works by checking each game piece placed by a player and seeing if there are any other game pieces in the same diagonal line. If there are four game pieces in a row, the player has won.

What happens if a player gets four game pieces in a diagonal line?

If a player gets four game pieces in a diagonal line, they have won the game of Connect Four. This is known as getting a "Connect Four".

Can Connect Four Diagonal Check be used to determine a tie game?

No, Connect Four Diagonal Check is only used to determine if a player has won the game. It cannot be used to determine a tie game, as it does not take into account all possible game piece placements.

Are there any alternative methods to determine a winning game of Connect Four?

Yes, there are other methods to determine a winning game of Connect Four, such as using a vertical or horizontal check. These methods check for four game pieces in a row either vertically or horizontally on the game board.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
890
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Introductory Physics Homework Help
Replies
7
Views
752
Back
Top