Connect 4 Dropping Pieces

Hello, I am trying to write a program that resembles the Connect 4 game. One problem I'm having is finding the proper way to print the game pieces(x and o) into a pointer to pointer array. It prints an = looking sign for each point in the array, but that doesn't seem to be what it actually is. When I enter a column number into drop it places an 'o' into the entire column. I need help figuring out how to replace each point in the array as well as how to stack them. Any help would be great.

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
33
34
35
36
37
38
39
40
char** BuildGameBoard(int WinSize)
{
	char **GameBoard = new char *[WinSize + EXTRA_ROWS];
	for(int i = 0; i < (WinSize + EXTRA_ROWS); i++)
	{
		GameBoard[i] = new char[WinSize + EXTRA_COLS];	
	}
	return GameBoard;
}

void DisplayGameBoard(char **GameBoard, int WinSize)
{
	for(int i = 0; i < (WinSize + EXTRA_ROWS); i++)
	{
		for(int j = 0; j < (WinSize + EXTRA_COLS); j++)
		{
			cout << GameBoard[i][j] << " ";
		}
		cout << endl;
	}
}

void PlayerMove(char **GameBoard, int WinSize)
{
	int drop;
	cout << "Enter column to drop a piece in [1-" << WinSize + EXTRA_COLS << "]: ";
	cin >> drop;	
	if(drop >= 1 && drop <= (WinSize + EXTRA_ROWS))
	{
		for(int i = 0; i < (WinSize + EXTRA_ROWS); i++)
		{
			if(GameBoard[i][drop - 1] != 'x' && GameBoard[i][drop - 1] != 'o')
			{
				GameBoard[i][drop - 1] = 'o';
			}
		}
	}
	DisplayGameBoard(GameBoard, WinSize);
	cout << endl;
}


By the way, WinSize equals the user input between three and six, EXTRA_ROWS = 2, and EXTRA_COLS = 3.
Last edited on
Your logic for checking where to place the dropped piece is wrong. it is currently "if there is neither an x or an o in the space, put an o there". It's probably best if you count backwards (since pieces obey gravity) and your if condition should just check for an empty space. Also, use break to leave the loop after you change the appropriate slot.
Would this be correct on the counting down part
 
for(int i = (WinSize + EXTRA_ROWS) - 1; GameBoard[i][drop - 1] >= 0; --i)


Also, I'm having trouble stacking the x's and o's I can get the bottom line just not anything above it.
Last edited on
It should be:
for(int i = (WinSize + EXTRA_ROWS) - 1; i >= 0; --i)

Don't know what else might be wrong. Maybe update your post with what you have now?
Topic archived. No new replies allowed.