Connect Four - Drop Counter Logic

Any help is much appreciated - I am a total noob to C++ and require some help regarding a connect 4 style game. I have seen other topics to try and help but I am still stuck... Currently it is only a 1 player game until I get the logic to work for the counters to drop.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "pch.h"
#include <iostream>

using namespace std;

const int numRows = 6;
const int numCols = 6;

int main()
{
	char board[numRows][numCols];			

	int row, col;

	for (row = 0; row < numRows; row++)		
		for (col = 0; col < numCols; col++)
			board[row][col] = '-';

	for (row = 0; row < numRows; row++)				
	{
		for (col = 0; col < numCols; col++)
			cout << board[row][col] << "    ";
		cout << endl;
	}

	cout << "0    1    2    3    4    5 " << endl;

	cout << "Place your first counter" << endl;

	int inputCol;

	cout << "Column: ";
	cin >> inputCol;

	while (inputCol > 0 && inputCol < 6) {
		if (board[5][inputCol] = '-') {
			board[5][inputCol] = 'X';
		}
		else if (board[4][inputCol] = '-') {
			board[4][inputCol] = 'X';
		}
		else if (board[3][inputCol] = '-') {
			board[3][inputCol] = 'X';
		}
		else if (board[2][inputCol] = '-') {
			board[2][inputCol] = 'X';
		}
		else if (board[1][inputCol] = '-') {
			board[1][inputCol] = 'X';
		}
		else  {
			board[0][inputCol] = 'X';
		}
	}
	
	for (row = 0; row < numRows; row++)				
	{
		for (col = 0; col < numCols; col++)
			cout << board[row][col] << "    ";
		cout << endl;
	}
	cout << "0    1    2    3    4    5 " << endl;
}
> if (board[5][inputCol] = '-')
Well there's a lot of = where you should have ==

A decent compiler would warn you about this.

Thank you for your reply!

I changed this and also added a ++ counter to create a loop for the player to reenter values.
Thanks for your help :)

For reference, I'm using Visual Studio 2017
Hello ClarkeyBoy99,

As salem c pointed the "==" makes a big difference.

To put it another way "=" is to set (a = b) and "==" means to compare.

Lines 6 and 7. Constant variables are normally in all caps.

Line 13. Unless you intend to use these variables outside the for loops,which you do not, it is better to define them inside the for loop and keep them local to the for loop.

The while loop is better used after the "cin" to validate the input before you move on.

The while loop that you have is an endless loop because the value of "inputCol" never changes. Eventually the only thing this loop does is the "else" statement.

The last for loop would work better inside the while loop to redraw the board after you make a change.

I would likely change the while loop to a do/while loop. Then near the end change the player, call a function to check for a winner and get new input for a new "inputCol". Not necessarily in that order. Then you can do something in the while condition to continue playing if not a winner.

Right now the if/else if/else statements are working correctly with the proper fix. It is just that it never stops.

I would consider putting the code to display the board in a function and just call it when needed instead of writing it two or more times before you are finished.

Hope that helps,

Andy
Hi Andy,

Thank you for taking the time to help me out. I really do appreciate it.

I will take on board what you have said - I've sorted that endless loop so thank you for that too...

I have amended the code with what you said and I'm happy with how it runs for now... I think adding a second player will be a challenge but we will see.

Cheers
Topic archived. No new replies allowed.