I am creating a connect 4 game using dynamic memory allocation the question is;
Write a two-player game of Connect Four where the user can set the width and height of the board and each player gets a turn to drop a token into the slot. Display the board using + for one side, x for the other, and _ to indicate blank spaces.
I have created the board. However I am unsure as how to make a start on getting the players to make moves.
Code:
#include <iostream>
using namespace std;
char **create_table(int width, int height, char blank) {
char **p_p_connect4 = new char*[height];
for(int i = 0; i < height; i++) {
p_p_connect4[i] = new char [width];
}
for(int i = 0; i < height; i++) {
for(int j = 0; j < height; j++) {
p_p_connect4[i][j] = blank;
}
}
return p_p_connect4;
}
void printTable(char **p_p_connect4, int width, int height) {
for(int i = 0; i < height; i++) {
for(int j = 0; j < height; j++) {
cout << p_p_connect4[i][j];
if (width-1) {
cout << " | ";
}
}
cout << endl;
if (height-1) {
for (int k =0; k < (width - 1)*5;++k) {
cout << "-";
}
cout << endl;
}
}
}
int main()
{
char blank = {'_'};
int height;
int width;
cout.flush() << "Please insert the sizes of table: ";
cin >> width;
cin >> height;
cout << endl;
char **p_p_connect4 = create_table(width,height,blank);
printTable(p_p_connect4,width,height);
return 0;
}