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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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<winsize+2;i++){
		if(GameBoard[i][move-1]!='_')n++;
	}
	if(n==0)GameBoard[winsize+1][move-1]='x';
	else if(n!=0)
		for(int i=0;GameBoard[i][move-1]=='_';i++){
		GameBoard[i-1][move-1]='x';
		}
	displayGameBoard(GameBoard, winsize);
	cout << endl;
	CompMove(GameBoard, winsize);
}
void CompMove(char **GameBoard, int winsize){
	int move = rand()%winsize+3;
	int n=0;
	for(int i=0;i<winsize+2;i++){
		if(GameBoard[i][move-1]!='_')n++;
	}
	if(n==0)GameBoard[winsize+1][move-1]='o';
	else if(n!=0)
		for(int i=0;GameBoard[i][move-1]=='_';i++){
		GameBoard[i-1][move-1]='o';
		}
	displayGameBoard(GameBoard, winsize);
	cout << endl;
	PlayerMove(GameBoard, winsize);
}

I can post the rest of the code if needed.
What is your problem?
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.