### Dropping a piece - Connect 4 Game

Im having trouble dropping the pieces into the 2d array. I've spent way more time than this should take on these two functions. Anyone spot what I'm doing wrong? Or hint to a better way to do this?
 ``1234567891011121314151617181920212223242526272829303132`` ``````void PlayerMove(char **GameBoard, int winsize){ int move; int n=0; cout << "Place your piece [1 - " << winsize + 3 << "]" << endl; cin >> move; for(int i=0;i

I can post the rest of the code if needed.
If you know the width of the board, try a modulus operator to simplfy it. Hope that helps.
I should have been more clear. Okay. The bottem element is the only one that is getting filled with either 'x' or 'o'. I had it working to where it would fill to higher tiers but it was deleting others for some reason. Hard to explain.
let's say that the board is 5 elements wide. If you drop it from the top, you don't know right off the bat which row it will end up in, because it depends if there are any pieces in that column. The known is which column it's being dropped in (or will be known once the logic is figured out) but what you don't know until it drops is which element in the array it ends up in. Personally if the board size is known i'd almost rather use a single dimensional array and do modulus operation to find out exactly where it belongs, but with a 2d array, the trick is not finding out what column but what row. Perhaps you could use a recursive function to basically work from the bottom up until you find an empty element, or just work in a reverse loop starting at the bottom until you find an empty element. Since it's probably a small board, the 2nd would probably be ideal in terms of performance, but if it became a very large board or you wanted to make the program more portable, you might consider recursion. All of this is assuming that you have already learned recursion, if not, then stick with the 2nd option. Hope that helps.
Okay I'm at home now (as opposed to work) so I have a easier time taking a look at this. A few points here just by looking at the code:

1. You need to implement a guard against pieces getting overwritten. This is obvious I'm sure but basically if a spot in the array has an 'x' or 'o' then don't allow a piece to get placed there. One clever and easy way of doing this is if you drop a piece in a certain column, start from the bottom and work up until there is no 'x' or 'o'.

2. Just from looking at these functions, you may want to pass the move as another parameter and simply do the placement in these functions, and perhaps have another function for the logic (i.e.- determining which column to place the piece based on how the gameboard looks.

If you are still having trouble feel free to post the rest of the code.
Topic archived. No new replies allowed.