Connect Four Board- dynamically allocating 2D array using function

I cannot figure out how to dynamically allocate a 2D array in order to create the connect four board. We are given This basic format to expound upon. Could someone help me with the function for char**createBoard() ?


include <iostream>

using namespace std;

// constant globals
const char EMPTY = '-';
const char RED = 'R';
const char BLACK = 'B';

// non-constant globals
int rows;
int columns;

// function prototypes
/*
Dynamically allocates the board (rows, columns) and fills it with empty pieces.
*/
char** createBoard();

/*
Deletes the rows and columns of the board.
*/
void deleteBoard(char** board);

/*
Prints the board to the screen.
The top of the board is the 0th row.
The bottom of the board is the row-1th row.
The left-most column is 0.
The right-most column is columns-1.
Put | between the columns.
See the example output in Moodle.
*/
void printTheBoard(char** board);

/*
Returns true if the column is in the valid range (0 to columns-1)
*/
bool validColumn(int column);

/*
Returns true if the column in the given board is full (no empty spots).
*/
bool isColumnFull(char** board, int column);

/*
"Drops" a piece into the board at the given column.
The piece should "fall" to the bottom-most empty row.
Note: the top row of the board has an index of 0 and the bottom row has an index of rows - 1.
*/
void playPiece(char** board, int column, char piece);

/*
Takes in a player piece (black or red) and returns the opposite.
*/
char switchPlayer(char piece);



int main() {

// Your Code Goes Here

system("pause");
return 0;
}

// Your Function Definitions Go Here
The below code should create a board (not using contiguous memory). This makes coding easier (and accessing the array simpler), but is not as efficient as far as memory blocks but has efficiencies in other ways (trade-offs to analyze further). As you progress, you may want to consider a more contiguous block and then compute the index by using i*columns + j. For now, I kept it simple. I added an overload to the original createBoard as you said you wanted something dynamic. I realize you want the rows and columns to not be "static". I used a static variable at the top but it can be changed in the program using the cin/cout statements I added. So it's static because it's globel but it's not const so it (rows and columns) can be changed.

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
162
163
164
165
166
167
168
#include <iostream>
#include <cstdlib> // system call

using namespace std;

// constant globals
const char EMPTY = '-';
const char RED = 'R';
const char BLACK = 'B';

// non-constant globals
int rows = 7;
int columns = 6;

// function prototypes
/*
Dynamically allocates the board (rows, columns) and fills it with empty pieces.
*/
char** createBoard();
// Overload for if other than default 7 x 6.
char** createBoard(int rows, int columns);

/*
Deletes the rows and columns of the board.
*/
void deleteBoard(char** board);

/*
Prints the board to the screen.
The top of the board is the 0th row.
The bottom of the board is the row-1th row.
The left-most column is 0.
The right-most column is columns-1.
Put | between the columns.
See the example output in Moodle.
*/
void printTheBoard(char** board);

/*
Returns true if the column is in the valid range (0 to columns-1)
*/
bool validColumn(int column);

/*
Returns true if the column in the given board is full (no empty spots).
*/
bool isColumnFull(char** board, int column);

/*
"Drops" a piece into the board at the given column.
The piece should "fall" to the bottom-most empty row.
Note: the top row of the board has an index of 0 and the bottom row has an index of rows - 1.
*/
void playPiece(char** board, int column, char piece);

/*
Takes in a player piece (black or red) and returns the opposite.
*/
char switchPlayer(char piece);



int main() {
	char **board = NULL;
	char ans;
	
	// Your Code Goes Here
	cout << "Enter Y/y to Change board size:";
	ans = cin.get();
	if((ans == 'Y') || (ans == 'y'))
	{
		// Get rows and columns
		cout << "Enter rows ... ";
		cin >> rows;
		cout << "Enter columns ...";
		cin >> columns;
		// Creates a different-sized board.
		board = createBoard(rows, columns); 
	}
	else
	// Creates a default-sized board.
		board = createBoard(); 
	cin.ignore(); // Eat up return from ans.	
	
	// Now that the board is created and 
	// initialized, then access row/column with the syntax: board[row][column].

	// Here I'm just avoiding system(pause), my Linux does not have one.
	// Also, should be careful if Cygwin or MingW situations on top of 
	// Windows but I skipped that since I did this on Linux.
	#if defined(_WIN64)
	/* Windows (64-bit) */
	system("pause");
	#elif defined(_WIN32)
	/* Windows (32-bit) */
	system("pause");
	#else
	// Linux (or Windows)
	std::cout << "Pause - Press return to end." << std::endl;
	cin.clear();
	std::cin.get();
	#endif
	
	// delete the board
	if(board)
	{
		deleteBoard(board);
		board = NULL;
	}
	
	return 0;
}

// Your Function Definitions Go Here
char** createBoard()
{
	char **board = NULL;
	
	board = new char*[rows];
	if(board)
	{
		for(int i = 0; i < rows; i++)
		{
			board[i] = new char[columns];
		}
		// Initialize the board with '-'
		for(int i = 0; i < rows; i++)
			for(int j = 0; j < columns; j++)
				board[i][j] = '-';
	}
	else
	{
		// Error ...
	}
	
	return board;
}

char** createBoard(int rows, int columns)
{
	char **board = NULL;
	
	board = new char*[rows];
	if(board)
	{
		for(int i = 0; i < rows; i++)
		{
			board[i] = new char[columns];
		}
		// Initialize the board with '-'
		for(int i = 0; i < rows; i++)
			for(int j = 0; j < columns; j++)
				board[i][j] = '-';
	}
	else
	{
		// Error ...
	}
	
	return board;	
}

void deleteBoard(char **board)
{
	for(int i = 0; i < rows; i++)
		delete[] board[i];
	delete[] board;
}


Compile and run:

# g++ connfour.c -o connfour

[/output]
# ./connfour
Enter Y/y to Change board size:y
Enter rows ... 4
Enter columns ...6
Pause - Press return to end.

# ./connfour
Enter Y/y to Change board size:n
Pause - Press return to end.
[/output]
Last edited on
Topic archived. No new replies allowed.