Thread: loop not incrementing correctly

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    87

    loop not adding to variable

    Hello, so I have been working on a program that will eventually allow you to play connect four. As of now I am still worknig on the basic functions: playing a move, testing if the move wins and etc. my current code is this:
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    const short BOARDX = 7;
    const short BOARDY = 6;
    
    void output_board();
    short play_move(short x, short player); //0 = fail 1 = succeed 2 = game won
    bool test_win(short x, short y);
    bool test_v(short x, short y);
    bool test_h(short x, short y);
    bool test_d(short x, short y);
    
    
    vector<vector<char> > board(BOARDX, vector<char>(BOARDY));
    
    int main()
    {
        cout << "Hello, this is a bare bones rendering of the connect four game." << endl;
        for(short a = BOARDY; a >= 0; a--) //for testing
        {
          cout << play_move(a,1) << endl;
          output_board();
        }
    
        return 0;
    }
    
    void output_board()
    {
      for(short y = BOARDY - 1; y >= 0; y--)
        {
          for(short x = 0; x < BOARDX; x++)
          {
            cout << "|";
            switch (board[x][y])
            {
              case 0:
                cout << " ";
                break;
              case 1:
                cout << "X";
                break;
              case 2:
                cout << "O";
                break;
              default:
                cout << "E";
                break;
            }
          }
          cout << "|" << endl;
        }
      return;
    }
    
    short play_move(short x, short player)
    {
      //starting from the bottom, selects the bottom unoccupied space and puts the player there
      //returns 2 if the game is won, 1 if it is successful, and 0 if the column is filled
      for(short y = 0; y < BOARDY; y++)
      {
        if(!board[x][y])
        {
          board[x][y] = player;
          if(test_win(x, y))
            return 2;
          return 1;
        }
      }
      return 0;
    }
    
    bool test_win(short x, short y)
    {
      //test the entered location for a win
      if(board[x][y])
      {
        if(test_h(x, y))
          return 1;
        if(test_v(x, y))
          return 1;
        if(test_d(x, y))
          return 1;
      }
      return 0;
    }
    
    bool test_v(short x, short y)
    {
      //test verticals
      short line_length = 1;
      for(short x_down = x - 1; x_down >= 0 && board[x][y] == board[x_down][y]; x_down--)
        line_length++;
      if(line_length >= 4)
        return 1;
      return 0;
    }
    
    bool test_h(short x, short y)
    {
      //test horizontals, right is currently not working
      short line_length = 1;
      for(short y_right = y + 1; y_right < BOARDY && board[x][y] == board[x][y_right]; y_right++)
        cout << line_length++ << endl;// cout for testing
      for(short y_left = y - 1; y_left >= 0 && board[x][y] == board[x][y_left]; y_left--)
        line_length++;
      if(line_length >= 4)
        return 1;
      return 0;
    }
    
    bool test_d(short x, short y)
    {
      //test for diagonals with a positive slope
      short line_length = 1;
      short x_positive = x + 1;
      short x_negative = x - 1;
      short y_positive = y + 1;
      short y_negative = y - 1;
      for(; x_positive < BOARDX && y_positive < BOARDY && board[x][y] == board[x_positive][y_positive]; x_positive++ * y_positive++)
        line_length++;
      for(; x_negative >= 0 && y_negative >= 0 && board[x][y] == board[x_negative][y_negative]; x_negative-- * y_negative--)
        line_length++;
      if(line_length >= 4)
        return 1;
      // now test for diagonals with a negative slope
      line_length = 1;
      x_positive = x + 1;
      x_negative = x - 1;
      y_positive = y + 1;
      y_negative = y - 1;
      for(; x_positive < BOARDX && y_negative >= 0 && board[x][y] == board[x_positive][y_negative]; x_positive++ * y_negative--)
        line_length++;
      for(; x_negative >= 0 && y_positive < BOARDY && board[x][y] == board[x_negative][y_positive]; x_negative-- * y_positive++)
        line_length++;
      if(line_length >= 4)
        return 1;
      return 0;
    }
    When run,Ii get this output:
    Code:
    Hello, this is a bare bones rendering of the connect four game.
    1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | |X|
    1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | |X|X|
    1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | |X|X|X|
    1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | |X|X|X|X|
    1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | |X|X|X|X|X|
    1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | |X|X|X|X|X|X|
    1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    |X|X|X|X|X|X|X|
    
    Process returned 0 (0x0)   execution time : 0.109 s
    Press any key to continue.
    The problem I am having is, test_h does not appear to check to the right. which is done by this line:
    Code:
    for(short y_right = y + 1; y_right < BOARDY && board[x][y] == board[x][y_right]; y_right++)
        cout << line_length++ << endl;
    For whatever reason, this does not add to line_length, but the other half of test_h works fine. I have asked my teacher about this, rewritten the line 3 times and for the life of me I cannot figure out what is wrong.
    Last edited by bobknows; 11-13-2012 at 11:06 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Have you got vertical and horizontal mixed up?
    Code:
    (gdb) break test_h
    Breakpoint 1 at 0x401047: file bar.cpp, line 98.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    Hello, this is a bare bones rendering of the connect four game.
    
    Breakpoint 1, test_h (x=6, y=0) at bar.cpp:98
    98	  short line_length = 1;
    (gdb) cont
    Continuing.
    1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | |X|
    
    Breakpoint 1, test_h (x=5, y=0) at bar.cpp:98
    98	  short line_length = 1;
    (gdb) cont
    Continuing.
    1
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | | | |
    | | | | | |X|X|
    
    Breakpoint 1, test_h (x=4, y=0) at bar.cpp:98
    98	  short line_length = 1;
    (gdb) s
    99	  for (short y_right = y + 1; y_right < BOARDY && board[x][y] == board[x][y_right]; y_right++)
    (gdb)
    I see x counting down with the changing board, but y is always 0, so the loop always does the same thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop not working correctly
    By ilndboy11 in forum C Programming
    Replies: 2
    Last Post: 11-01-2012, 09:25 PM
  2. Replies: 3
    Last Post: 10-07-2012, 09:56 AM
  3. Loop not working Correctly
    By patneel in forum C Programming
    Replies: 4
    Last Post: 03-15-2012, 07:25 AM
  4. Lost in a loop, sum not adding up correctly... Please help.
    By matthayzon89 in forum C Programming
    Replies: 1
    Last Post: 09-26-2010, 03:48 PM
  5. Variable not incrementing correctly.
    By DonFord81 in forum C Programming
    Replies: 4
    Last Post: 07-21-2010, 02:10 PM