stuck on placing game piece

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 665 Views - Last Post: 22 March 2013 - 09:36 PM Rate Topic: -----

#16 clickspiker23  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 200
  • Joined: 29-October 10

Re: stuck on placing game piece

Posted 22 March 2013 - 06:24 PM

View Post#define, on 22 March 2013 - 05:46 PM, said:

By iteration I mean once time through the loop. Iteration


You could try printing the values in the loop :

     for (int rowTaken = ROWS; rowTaken >0 ; rowTaken--)
        {
        cout << "rowTaken : " << rowTaken << " localRowVariable : " << localRowVariable << endl;  




when i do this is what i get. i seriously have no idea why, i tried following it in code but i dont understand why the localRowVariable is being output twice. this is what i get

Column choices are 0 - 5
What column do you want to Place your piece: 3
rowTaken : 7 localRowVariable : 7
rowTaken : 6 localRowVariable : 6
rowTaken : 5 localRowVariable : 6
rowTaken : 4 localRowVariable : 5
rowTaken : 3 localRowVariable : 5
rowTaken : 2 localRowVariable : 4
rowTaken : 1 localRowVariable : 4
0 1 2 3 4 5
|-----------------------|
| | | | | | |
|-----------------------|
| | | | | | |
|-----------------------|
| | | | | | |
|-----------------------|
| | | | | | |
|-----------------------|
| | | | U | | |
|-----------------------|
| | | | U | | |
|-----------------------|
| | | | U | | |
|-----------------------|






I changed it back so the code is more "readable" (goes with what is happening)

        // start at the bottom of the board and loop until an empty spot is found
     for (int rowTaken = ROWS; rowTaken > 0; rowTaken--)
        {
         
         cout << "rowTaken : " << rowTaken << " localRowVariable : " << localRowVariable << endl;
         
       //if the spot is empty place the user piece 'U' in that spot
         if (GameBoard[localRowVariable][columnNum] == EMPTY)
         {
             GameBoard[localRowVariable][columnNum] = USER;
             
             
         }
         // otherwise decrement the row variable until and empty one is found
         else
         {
             
             localRowVariable--;
         }
         
        // cout << "rowTaken : " << rowTaken << " localRowVariable : " << localRowVariable << endl;
     }

Was This Post Helpful? 0
  • +
  • -

#17 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: stuck on placing game piece

Posted 22 March 2013 - 06:45 PM

The variable localRowVariable is only decremented when the spot is not empty.

You could try printing what is in the GameBoard spot.

 cout << "rowTaken : " << rowTaken 
      << " localRowVariable : " << localRowVariable 
      << " GameBoard[][] : (" << GameBoard[localRowVariable][columnNum] << ")"
      << endl;




How is GameBoard declared? I would expect the program to crash with a row number of 7.
Was This Post Helpful? 0
  • +
  • -

#18 clickspiker23  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 200
  • Joined: 29-October 10

Re: stuck on placing game piece

Posted 22 March 2013 - 07:02 PM

View Post#define, on 22 March 2013 - 06:45 PM, said:

The variable localRowVariable is only decremented when the spot is not empty.

You could try printing what is in the GameBoard spot.

 cout << "rowTaken : " << rowTaken 
      << " localRowVariable : " << localRowVariable 
      << " GameBoard[][] : (" << GameBoard[localRowVariable][columnNum] << ")"
      << endl;




How is GameBoard declared? I would expect the program to crash with a row number of 7.


GameBoard is declared like this

ROWS = 7;
ROWS = 6;

GameBoard[ROWS][COLUMNS])

View Post#define, on 22 March 2013 - 06:45 PM, said:

The variable localRowVariable is only decremented when the spot is not empty.

You could try printing what is in the GameBoard spot.

 cout << "rowTaken : " << rowTaken 
      << " localRowVariable : " << localRowVariable 
      << " GameBoard[][] : (" << GameBoard[localRowVariable][columnNum] << ")"
      << endl;




How is GameBoard declared? I would expect the program to crash with a row number of 7.


GameBoard is declared like this

ROWS = 7;
ROWS = 6;

GameBoard[ROWS][COLUMNS])
Was This Post Helpful? 0
  • +
  • -

#19 clickspiker23  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 200
  • Joined: 29-October 10

Re: stuck on placing game piece

Posted 22 March 2013 - 07:36 PM

I GOT IT!!!!!!!! YAY

By the way this is what i ended up figureing out. i still need to do error checking but this stacks one by one correctly

    // current attempt
    
    if (GameBoard[localRowVariable][columnNum] == EMPTY)
    {
        GameBoard[localRowVariable][columnNum] = USER;
    }
    else
    {
        while(GameBoard[localRowVariable][columnNum] != EMPTY)
        {
            localRowVariable--;
        }
        GameBoard[localRowVariable][columnNum] = USER;
    }

Was This Post Helpful? 0
  • +
  • -

#20 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: stuck on placing game piece

Posted 22 March 2013 - 08:29 PM

Very good.

Though I would have thought that :

if (GameBoard[ rowTaken-1 ][columnNum] == EMPTY)
{
    GameBoard[ rowTaken-1 ][columnNum] = USER;
}




would do the same, unless I'm missing something.
Was This Post Helpful? 0
  • +
  • -

#21 clickspiker23  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 200
  • Joined: 29-October 10

Re: stuck on placing game piece

Posted 22 March 2013 - 09:36 PM

View Post#define, on 22 March 2013 - 08:29 PM, said:

Very good.

Though I would have thought that :

if (GameBoard[ rowTaken-1 ][columnNum] == EMPTY)
{
    GameBoard[ rowTaken-1 ][columnNum] = USER;
}




would do the same, unless I'm missing something.

It does the same thing i just forgot to post the code below, when i posted the code. i subtracted 1 before i entered the loop, sorry

int localRowVariable = ROWS-1;

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2