Connect4 Game

I'm working on an assignment using classes to create a Connect4 game. The classes themselves were created by my teacher and I only have programmed the main function. I am having trouble getting the game to work properly however. My game seems to play twice for the human, placing a two pieces at one time in the column that the user chooses. Any help is appreciated.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  #include "hw6.h"

//---------------------------------------------------
// Constructor function
//---------------------------------------------------
Connect4::Connect4()
{
   ClearBoard();
}

//---------------------------------------------------
// Destructor function
//---------------------------------------------------
Connect4::~Connect4()
{
   // Intentionally empty
}

//---------------------------------------------------
// Clear the Connect4 board
//---------------------------------------------------
void Connect4::ClearBoard()
{
   // Initialize Connect4 board
   for (int c = 0; c < COLS; c++)
   for (int r = 0; r < ROWS; r++)
      board[r][c] = ' ';

   // Initialize column counters
   for (int c = 0; c < COLS; c++)
      count[c] = 0;
}

//---------------------------------------------------
// Add player's piece to specified column in board
//---------------------------------------------------
bool Connect4::MakeMove(int col, char player)
{
   // Error checking
   if ((col < 0) || (col >= COLS) || (count[col] >= ROWS))
      return false;

   // Make move
   int row = count[col];
   board[row][col] = player;
   count[col]++;
   return true;
}

//---------------------------------------------------
// Check to see if player has won the game
//---------------------------------------------------
bool Connect4::CheckWin(char player)
{
   // Loop over all starting positions
   for (int c = 0; c < COLS; c++)
   for (int r = 0; r < ROWS; r++)
   if (board[r][c] == player)
   {
      // Check row
      int count = 0;
      for (int d = 0; d < WIN; d++)
         if ((r+d < ROWS) && 
            (board[r+d][c] == player)) count++;
      if (count == WIN) return true;
    
      // Check column
      count = 0;
      for (int d = 0; d < WIN; d++)
         if ((c+d < COLS) && 
            (board[r][c+d] == player)) count++;
      if (count == WIN) return true;
    
      // Check first diagonal
      count = 0;
      for (int d = 0; d < WIN; d++)
         if ((r+d < ROWS) && (c+d < COLS) && 
            (board[r+d][c+d] == player)) count++;
      if (count == WIN) return true;
    
      // Check second diagonal
      count = 0;
      for (int d = 0; d < WIN; d++)
         if ((r-d >= 0) && (c+d < COLS) && 
            (board[r-d][c+d] == player)) count++;
      if (count == WIN) return true;
   }
   return false;
}

//---------------------------------------------------
// Print the Connect4 board
//---------------------------------------------------
void Connect4::PrintBoard()
{
   // Print the Connect4 board
   for (int r = ROWS-1; r >= 0; r--)
   {
      // Draw dashed line
      cout << "+";
      for (int c = 0; c < COLS; c++)
         cout << "---+";
      cout << "\n";

      // Draw board contents
      cout << "| ";
      for (int c = 0; c < COLS; c++)
         cout << board[r][c] << " | ";
      cout << "\n";
   }

   // Draw dashed line
   cout << "+";
   for (int c = 0; c < COLS; c++)
      cout << "---+";
   cout << "\n";

   // Draw column numbers
   cout << "  ";
   for (int c = 0; c < COLS; c++)
      cout << c << "   ";
   cout << "\n\n";
}

//---------------------------------------------------
// Main program to play Connect4 game
//---------------------------------------------------
int main()
{
	int choice;
	int counter = 0;
	srand (time(NULL));
	Connect4 board;
	cout << "Welcome to Connect 4!" << endl << "Your Pieces will be labeled 'H' for human. While the computer's will be labeled 'C'" << endl;
	board.PrintBoard();
	cout << "Where would you like to make your first move? (0-6)";
	cin >> choice;
	while (board.MakeMove(choice,'H') == false){
	cin >> choice;
	}
	counter++;
	while (board.CheckWin('C') == false && board.CheckWin('H') == false && counter != 21){
	while (board.MakeMove(rand() % 7, 'C') == false){}
	board.PrintBoard();
	cout << "Where would you like to make your next move?" << endl;
	cin >> choice;
	board.MakeMove(choice,'H');
	while (board.MakeMove(choice,'H') == false){
	cin >> choice;
	}
	counter++;
	}
	

	if (board.CheckWin('C')){
	cout << "Computer Wins!" << endl;}
	else if (counter == 21){cout << "Tie Game!" << endl;}
	else {cout << "Human Wins!" << endl;}
	board.PrintBoard();

}
HA. nevermind found my stupid mistake.
Topic archived. No new replies allowed.