stuck on placing game piece

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 clickspiker23  Icon User is offline

  • D.I.C Head

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

stuck on placing game piece

Posted 21 March 2013 - 10:12 PM

I am trying to place/drop a game piece into a column specified by the user. The program runs but doesnt do anything. foe example it asks the user for the column number and then justs runs but doesn't do anything. which has led me to believe something is wrong with my loop to place the piece. But i dont know what it is. this is the code i have for the placement of a user specified piece.

global variables
ROWS = 7;;
USER = 'U';
COMP = 'C';

cout << "Enter columnNum: ";
cin >> columnNum

    
     for ( int rowLooper = ROWS; rowLooper > 0; rowLooper--)
        {
            while(GameBoard[rowLooper][columnNum] != ' ')
            {
             
                GameBoard[rowLooper][columnNum] = USER;
            }
            
        }
    
     
PrintBoard(GameBoard);


basically the loop starts at the last row which would be the bottom and if the spot is taken in moves up a row until it encounters an empty spot in that case it places the user piece but it never gets past the loop part of the program and i dont know why. if needed i can paste all code, but this is the only code i think is relevent to my problem

Is This A Good Question/Topic? 0
  • +

Replies To: stuck on placing game piece

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: stuck on placing game piece

Posted 21 March 2013 - 10:43 PM

You need to make the "rowlooper" variable go down to > -1, since 0 is a valid array index in C.

You aren't changing the columnNum, inside the while loop.

This post has been edited by Adak: 21 March 2013 - 10:44 PM

Was This Post Helpful? 0
  • +
  • -

#3 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 21 March 2013 - 10:48 PM

View PostAdak, on 21 March 2013 - 10:43 PM, said:

You need to make the "rowlooper" variable go down to > -1, since 0 is a valid array index in C.

You aren't changing the columnNum, inside the while loop.


Why would columnNum have to change. thats where the user is dropping it?
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,605
  • Joined: 19-February 09

Re: stuck on placing game piece

Posted 21 March 2013 - 10:48 PM

Do you need the while loop?

If the cell is not a space then the while loop is entered, and the cell is set to not a space - the loop is infinite.
Was This Post Helpful? 0
  • +
  • -

#5 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 21 March 2013 - 10:51 PM

also couldnt i change rowLooper >= 0 would be the same as mankieng it > -1
Was This Post Helpful? 0
  • +
  • -

#6 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 - 10:26 AM

Im still stuck on this placement of the game piece. right now the only thing that i can get to actuallly do something is this code below.
i just added thte columnNum--; although the only reason i did that was because of adak suggestion. i dont see why i need it because the column isnt changing only the row would be changing but when i added it to the program it ran but just said fail. this is a run of the program




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

RUN FAILED (exit value 1, total time: 2s)





     for ( int rowTaken = ROWS; rowTaken > 0; rowTaken--)
        {
            while(GameBoard[rowTaken][columnNum] != ' ')
            {
                columnNum--;
                GameBoard[rowTaken][columnNum] = USER;
            }
            
        }
    
     
PrintBoard(GameBoard);

Was This Post Helpful? 0
  • +
  • -

#7 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 - 11:28 AM

View Post#define, on 21 March 2013 - 10:48 PM, said:

Do you need the while loop?

If the cell is not a space then the while loop is entered, and the cell is set to not a space - the loop is infinite.



I tried doing an if else but the i part never places the piece, even at the beginning of the game when everything is empty. this is how i set up the if else block of code it runs but it just reprints an empty board with no piece placed


  // place piece
   
    if (GameBoard[localRowVariable][columnNum] == EMPTY)
    {
        GameBoard[localRowVariable][columnNum] == USER;
        
    }
    
    else
    {
     for ( int rowTaken = ROWS; rowTaken > 0; rowTaken--)
        {
            while(GameBoard[rowTaken][columnNum] != ' ')
            {
                //rowTaken--;
                GameBoard[rowTaken][columnNum] = USER;
            }
            
        }


    }

Was This Post Helpful? 0
  • +
  • -

#8 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: stuck on placing game piece

Posted 22 March 2013 - 01:34 PM

Is this checkers or chess or what is the game here?

There are different ways to place a game piece onto a board. Two major ways:

1) Basic

Loop through the whole board. Print each square on the board, including the new game piece, at the right row and column. Works fine for Tic-Tac-Toe and other simple and small board game displays.

2) Advanced

You calculate the exact location the piece should be moved to, using variables like:

Lmargin - left margin - the distance between the leftmost column of the board and the leftmost column of the display.
Tmargin - top margin - the distance between the topmost row of the board, and the topmost column of the display
WIDTH - the width of each square on the board. For chess and checkers, this is typically 3, or 5 chars on a console display.
HEIGHT - the height of each square on the board. Again, an odd number is best, 3 or 5 chars on a console is common, but this depends a lot on the height of the font being using by the console.

Using the above, the exact position of the piece is:

x = Lmargin + WIDTH * columnNum
y = Tmargin + HEIGHT * rowNum

Then, using a header file like Ncurses, Windows.h, or conio.h, you move the cursor to the exact location you need, and print out the game piece:

SetConsoleCursorPosition() is the function in Windows.h. gotoxy() is the function in conio.h. In the case of Pelles C, all non-standard functions in conio.h are renamed with a leading underscore - _gotoxy(x,y).

This post has been edited by Adak: 22 March 2013 - 04:16 PM

Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,605
  • Joined: 19-February 09

Re: stuck on placing game piece

Posted 22 March 2013 - 01:48 PM

View Postclickspiker23, on 22 March 2013 - 06:28 PM, said:

I tried doing an if else but the i part never places the piece, even at the beginning of the game when everything is empty. this is how i set up the if else block of code it runs but it just reprints an empty board with no piece placed



I meant why have a while loop in the for loop?
The for loop iterates througn the rows, what would the while loop iterate through?
Why are you checking whether the cell is NOT empty?

You are saying just change the cell - if the cell is NOT empty. Is that what you want?

   for ( int rowTaken = ROWS; rowTaken > 0; rowTaken--)
      {
          if(GameBoard[rowTaken][columnNum] != ' ')
          {
              //rowTaken--;
              GameBoard[rowTaken][columnNum] = USER;
          }
          
      }




How have you initialized the GameBoard?

.

This post has been edited by #define: 22 March 2013 - 01:48 PM

Was This Post Helpful? 0
  • +
  • -

#10 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 - 02:45 PM

View Post#define, on 22 March 2013 - 01:48 PM, said:

View Postclickspiker23, on 22 March 2013 - 06:28 PM, said:

I tried doing an if else but the i part never places the piece, even at the beginning of the game when everything is empty. this is how i set up the if else block of code it runs but it just reprints an empty board with no piece placed



I meant why have a while loop in the for loop?
The for loop iterates througn the rows, what would the while loop iterate through?
Why are you checking whether the cell is NOT empty?

You are saying just change the cell - if the cell is NOT empty. Is that what you want?

   for ( int rowTaken = ROWS; rowTaken > 0; rowTaken--)
      {
          if(GameBoard[rowTaken][columnNum] != ' ')
          {
              //rowTaken--;
              GameBoard[rowTaken][columnNum] = USER;
          }
          
      }




How have you initialized the GameBoard?

.


yes. the gameboard is initialized in the beginning to all empty spots (i used the space bar). ok i guess i kind of get what you are saying, i changed my code just a little bit and added comments to it so you can see what i am thinking is happening at that point in the code. but right now it works but for some reason it places 3 pieces no matter what column is chosen. below is an output with code.

when i copy paste all the rows are stretched out. in my program output they are all evenly spaced

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


code

    // start at the bottom of the board and if the loop until an empty spot is found
     for (int rowTaken = ROWS; rowTaken > 0; rowTaken--)
        {
         
         // if the bottom of the board is NOT empty (either taken by the user or computer)
         // then subtract 1 and go the the next row but still in the same column
         if (GameBoard[localRowVariable][columnNum] != EMPTY)
         {
             localRowVariable--;
             
         }
         // at this point an empty spot is found and is set the user piece of 'U'
         else
         {
             GameBoard[localRowVariable][columnNum] = USER;
         }


View PostAdak, on 22 March 2013 - 01:34 PM, said:

Is this checkers or chess or what is the game here?

There are different ways to place a game piece onto a board. Two major ways:

1) Basic

Loop through the whole board. Print each square on the board, including the new game piece, at the right row and column. Works fine for Tic-Tac-Toe and other simple and small board game displays.

2) Advanced

You calculate the exact location the piece should be moved to, using variables like:

Lmargin - left margin - the distance between the leftmost column of the board and the leftmost column of the display.
Tmargin - top margin - the distance between the topmost row of the board, and the topmost column of the display
WIDTH - the width of each square on the board. For chess and checkers, this is typically 3, or 5 chars on a console display.
HEIGHT - the height of each square on the board. Again, an even number is best, 3 or 5 chars on a console is common, but this depends a lot on the height of the font being using by the console.

Using the above, the exact position of the piece is:

x = Lmargin + WIDTH * columnNum
y = Tmargin + HEIGHT * rowNum

Then, using a header file like Ncurses, Windows.h, or conio.h, you move the cursor to the exact location you need, and print out the game piece:

SetConsoleCursorPosition() is the function in Windows.h. gotoxy() is the function in conio.h. In the case of Pelles C, all non-standard functions in conio.h are renamed with a leading underscore - _gotoxy(x,y).




it is connect four. basically choose a column to drop a piece and try and connect four pieces, horizontally, diagonally, or vertically. so i am using the basic method you described. the advanced methed you describe sounds like battle ship or connect the dots but is definitely good info. i may have to try and mess with a game like that just for fun. i have been able to get some output. i dont want to double post but it is in a reply above this one
Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,605
  • Joined: 19-February 09

Re: stuck on placing game piece

Posted 22 March 2013 - 03:59 PM

To show output that has spaces Other styles/SQL Code can be used.


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


You can test for an empty spot using == :

// if an empty spot is found 
if(GameBoard[rowTaken][columnNum] == ' ')
{
  //...
}      




In arrays the index usually go from 0 to ROWS-1,
GameBoard[0][columnNum] to GameBoard[ROWS-1][columnNum].


So you could take account of that this way :

for ( int rowTaken = ROWS; rowTaken > 0; rowTaken--)
   {
       // if an empty spot is found 
       if(GameBoard[rowTaken-1][columnNum] == ' ')
       {
           // the user piece is set to 'U'
           GameBoard[rowTaken-1][columnNum] = USER;
       }
       
   }




or this way :


for ( int rowTaken = ROWS-1; rowTaken >= 0; rowTaken--)
   {
       // if an empty spot is found 
       if( GameBoard[rowTaken][columnNum] == ' ' )
       {
           // the user piece is set to 'U'
           GameBoard[rowTaken][columnNum] = USER;
       }
       
   }


Was This Post Helpful? 0
  • +
  • -

#12 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 - 04:13 PM

View Post#define, on 22 March 2013 - 03:59 PM, said:

To show output that has spaces Other styles/SQL Code can be used.


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


You can test for an empty spot using == :

// if an empty spot is found 
if(GameBoard[rowTaken][columnNum] == ' ')
{
  //...
}      




In arrays the index usually go from 0 to ROWS-1,
GameBoard[0][columnNum] to GameBoard[ROWS-1][columnNum].


So you could take account of that this way :

for ( int rowTaken = ROWS; rowTaken > 0; rowTaken--)
   {
       // if an empty spot is found 
       if(GameBoard[rowTaken-1][columnNum] == ' ')
       {
           // the user piece is set to 'U'
           GameBoard[rowTaken-1][columnNum] = USER;
       }
       
   }




or this way :


for ( int rowTaken = ROWS-1; rowTaken >= 0; rowTaken--)
   {
       // if an empty spot is found 
       if( GameBoard[rowTaken][columnNum] == ' ' )
       {
           // the user piece is set to 'U'
           GameBoard[rowTaken][columnNum] = USER;
       }
       
   }



Thanks for the info about the other/sql button.

but i already tried what you suggested (see attempt 3 in commented out code below) and switched stuff around using the != but it places three pieces on top of each other. those three U in the output are from 1 input of selecting a column not three separate inputs of column 3. and i dont know why it is placing three pieces.


this is four different ways i have tried (the uncommented one and // attempt 3 are the only ones that come close. i have tried other ways but deleted and started over before i just started commenting stuff out to see what ive tried before.

    // current attempt
   
      //   start at the bottom of the board and loop until an empty spot is found
     for (int rowTaken = ROWS; rowTaken >0 ; rowTaken--)
        {
         
         // if the bottom of the board is NOT empty (either taken by the user or computer)
         // then subtract 1 and go the the next row but still in the same column
         if (GameBoard[localRowVariable][columnNum] != EMPTY)
         {
             localRowVariable--;   
         }
        //  at this point an empty spot is found and is set the user piece of 'U'
         else
         {
              GameBoard[localRowVariable][columnNum] = USER;
          
         }
         
         
     }

    

         
         
         // Attempt One place piece
         
         
//   
//    if (GameBoard[localRowVariable][columnNum] == EMPTY)
//    {
//        GameBoard[localRowVariable][columnNum] == USER;
//        
//    }
//    // otherwise the spot is taken loop through rows until an empty spot is taken
//    else
//    {
         
         
         
         
         
         // attempt two place piece
         
         
         
//    while(GameBoard[rowTaken][columnNum] != ' ')
//            {
//     //rowTaken--;
//      GameBoard[rowTaken][columnNum] = USER;
//            }
            
       // }
    
    
    
        
    // attempt 3
    
    
//        // start at the bottom of the board and loop until an empty spot is found
//     for (int rowTaken = ROWS; rowTaken > 0; rowTaken--)
//        {
//         
//         // if the bottom of the board is NOT empty (either taken by the user or computer)
//         // then subtract 1 and go the the next row but still in the same column
//         if (GameBoard[localRowVariable][columnNum] == EMPTY)
//         {
//             GameBoard[localRowVariable][columnNum] = USER;
//             
//         }
//         // at this point an empty spot is found and is set the user piece of 'U'
//         else
//         {
//             localRowVariable--;
//         }
//         
//         
//     }
//    

Was This Post Helpful? 0
  • +
  • -

#13 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,605
  • Joined: 19-February 09

Re: stuck on placing game piece

Posted 22 March 2013 - 04:43 PM

Why use another variable localRowVariable? You already have a row variable. It has three pieces because you are not updating localRowVariable every iteration, you don't show the initialization of localRowVariable.

If you print out the row variables in the loop that might help you find out where it is going wrong.
Was This Post Helpful? 0
  • +
  • -

#14 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 - 05:22 PM

View Post#define, on 22 March 2013 - 04:43 PM, said:

Why use another variable localRowVariable? You already have a row variable. It has three pieces because you are not updating localRowVariable every iteration, you don't show the initialization of localRowVariable.

If you print out the row variables in the loop that might help you find out where it is going wrong.


localRowVariable = ROWS (which is = 7)

im not sure what you mean by updating it every iteration?
Was This Post Helpful? 0
  • +
  • -

#15 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,605
  • Joined: 19-February 09

Re: stuck on placing game piece

Posted 22 March 2013 - 05:46 PM

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;  


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2