Thread: Checking connect 5 diagonally help

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    43

    Checking connect 5 diagonally help

    Hi,
    I am making a connect 5 program with a 15x15 array. I have have the check vertical and horizontal parts working but the diagonally is giving me a problem. Here is half the method, is suppose to check up and to the left for the same color chip but everytime it only loops once. Example, chips at 0,0 1,1 2,2 and when a 3,3 chip is dropped it only counts the 3,3 chip and loops once to count the 2,2 chip. Heres the code thanks!

    Code:
    void checkDiagonal(char a[][15], int prow, int pcol, char playerStone)
    {
       int numInline = 1;
       int i, j;
    
       for(i= prow-1; i >= 0; i--) //Check diagonal up left
          for(j= pcol-1; j >= 0; j--)
          {
             if(numInline < 5)
                if(a[i][j] == playerStone)
                {
                   numInline++;
                   printf("%s %d %d %d", "Up Left: ", numInline, i, j);
                   break;     //To get back to outer for loop
                }
                else
                   break;
             else
             {
                if(playerStone == 'O')
                {
                   printBoard(a, 15, boardAxis);
                   printf("%s", "Player 1  wins diagonally up left!!!!\n");
                }
                else
                {
                   printBoard(a, 15, boardAxis);
                   printf("%s", "Player 2  wins diagonally up up left!!!!\n");
                }
                exit(0);
             }
          }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So what do you think a break statement does, other than quit the loop?

    Edit: And for that matter, I'm not sure exactly how you intend your loops to work. Your loop should go, roughly, from 1 to 5 -- you can't move one row over and scan the column, and then move another row over and scan that column, et cetera. You need to, each time, move just one space over and one space up.
    Last edited by tabstop; 09-04-2009 at 03:30 PM.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    I want to quit the inner loop so it goes back to the outer loop to decrease the row #. It does the same exact thing without the break statement, I thought it would fix it but I apparently it didn't.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  5. win checking in connect four
    By Mr_Jack in forum Game Programming
    Replies: 3
    Last Post: 03-20-2004, 10:27 PM