1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Connect Four Diagonal Check

  1. Jun 16, 2016 #1
    1. The problem statement, all variables and given/known data
    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.

    2. Relevant equations


    3. The attempt at a solution

    Code (C):
         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;
            }
     
  2. jcsd
  3. Jun 16, 2016 #2

    Mark44

    Staff: Mentor

    This:
    Code (C):
    for (int j = 5; j > 6; j++)
    The body of the inner loop is never executed.
     
  4. Jun 16, 2016 #3

    Mark44

    Staff: Mentor

    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.
    Code (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;
                }
         }
     }
     
  5. Jun 16, 2016 #4
    Will do thanks though.
     
  6. Jun 17, 2016 #5
    Would I decrement the j which is the column?
     
  7. Jun 17, 2016 #6

    Mark44

    Staff: Mentor

    Code (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.
     
  8. Jun 17, 2016 #7

    Mark44

    Staff: Mentor

    Code (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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Connect Four Diagonal Check
  1. Connect Four Games (Replies: 10)

  2. Connect Four Game (Replies: 5)

Loading...