The purpose of this program is to create a connect 4 game. The trouble I'm having is understanding syntax. My instructor gave us hints to help with creating this game. These hints are specifically aimed at the function that generates the game board.
Function char** BuildGameBoard(int WinSize)
Hint 1:
char** GameBoard = newchar *[WinSize+EXTRA_ROWS];
Here is what I understand. I'm creating a pointer that points to a char that happens to point to another pointer. The above described pointer is GameBoard. Then I'm setting GameBoard pointer to a pointer equal to (new, meaning dynamically allocated memory) char pointer, referenced to[WinSize+EXTRA_ROWS].
That last part is where I get lost. I don't see a name that I can reference that pointer. It is like giving a pointer the name that is the size of an array.
Hint 2:
"use [a] loop to allocate the char array for each of the rows."
I believe this second hint will make more sense once I better understand what the first hint is saying. If you could please make your answers as "dumbed down" as possible, I would appreciate it. Any sort of examples are welcome!
#include <iostream>
usingnamespace std ;
constint EXTRA_ROWS = 3;
constint EXTRA_COLUMS = 2;
int GetConnectionSize(void);
char** BuildGameBoard(int WinSize);
void DisplayGameBoard(char**GameBoard, int WinSize);
void PlayerMove(char**GameBoard, int WinSize);
void ComputerMove(char**GameBoard, int WinSize);
bool CheckRowWin(char**GameBoard, int WinSize);
bool CheckColumnWin(char**GameBoard, int WinSize);
bool CheckRightDiagonalWin(char**GameBoard, int WinSize);
bool CheckLeftDiagonalWin(char**GameBoard, int WinSize);
bool CheckTie(char**GameBoard, int WinSize);
void DestoryGameBoard(char**GameBoard, int WinSize);
int main()
{
int WinConnectionSize;
WinConnectionSize = GetConnectionSize();
return 0 ;
}
int GetConnectionSize(){
int WinSize;
bool validSize = true;
do{
cout << "Enter the amount of connections requied to win the game: ";
cin >> WinSize;
if (WinSize < 3 || WinSize > 6){
cout << "Invalid connect size!";
cout << " Please enter a value between 3 and 6." << endl;
validSize = false;
cin.ignore(); //May have to remove
}
else{
validSize = true;
}
}while(validSize == false);
return WinSize;
}
char** BuildGameBoard(int WinSize){
char** GameBoard = newchar *[WinSize+EXTRA_ROWS];
//use a loop to allocate the char array for each of the rows
}
What you are doing is making a 2 dimensional array. This is what you have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
char** GameBoard; // This defines our 2d array (2 pointers = 2d)
// Create the gameboard
// Think of it this way: We are allocating an array of pointers.
// Because the pointers could potentially become arrays at some point,
// this allows us to say that we are creating an array of arrays (2d array)
GameBoard = newchar* [WinSize+EXTRA_ROWS];
// Use a loop to do this, just giving example:
// Create a new array. Now GameBoard[0] is a pointer to this array,
// and so GameBoard[0][0] is the top-left corner.
GameBoard[0] = newchar[WinSize + EXTRA_COLUMNS];
Basically, just reiterating what I wrote above, the 1st hint is you need to create a 2D array. Now, to create a 2D array, the best way is to simply create lots of 1D arrays (the normal kind), and link them together. What the first hint is telling you is how to create an array of arrays, and the second hint is telling you how to create the arrays that they point to.
Just something I thought I should mention: When deleting the game board, make sure you go through the whole array and delete the arrays they point to, and thendelete the entire array, otherwise you will get a memory leak.
I’m still not getting it although I do believe I’m closer. Do you mind going into a little more detail on how the actual process takes place?
Like char** GameBoard = newchar *[WinSize+EXTRA_ROWS];
Goes through and creates rows. (if that is what it does)
Then
1 2 3 4
for(int i = 0; i < (WinSize + EXTRA_ROWS); i++)
{
GameBoard[i] = newchar[EXTRA_COLUMS];
}
Goes through and links the rows making columns?
I'm just trying to think of this like the creation of an array. If we make an array that is array[5][5] you start with row 0, then go through and assign array[0][0] =..., [0][1]...[0][4] then move on to row 1 and so on. How does this double pointer process compare?
On kind of a side note, are these two things equal?
1:
char array[1];
array[0] = “X”;
[0] [1] [2] [3] [4] // That first line represents this
V V V V V
[0] [0] [0] [0] [0] // Then the loop creates each row below
[1] [1] [1] [1] [1] // gameboard[I], I corresponding to
[2] [2] [2] [2] [2] // the numbers in each of the rows
The second row would be Gameboard[0]. Then I would use the pointer at address Gameboard to reference element [0][0].
Well I guess that leads to my next question. How do I assign a value to each piece of my array at this point? Is there a way I can directly assign a value to Gameboard[0][0]?
Also will I assign values to that first row or merely use it as a reference?
Edit:I believe I figured this out. Thanks for the help!