I am having problems with creating a Connect-4 game where the user defines the height and width of the board.
Thus far, I have coded the ability for a user to input the board dimensions and then display the board.
I have also created a two dimensional array using pointers to keep track of the various positions status.
My problem is that while I can get the board to display without reference to the pointer-based 2D array, I do not know how to pass the values from the array to the function that displays the board.
My code is as follows:
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
|
#include <iostream>
using namespace std;
//This function draws the board given parameters.
void drawBoard(int height, int width, int &board)
{
for (int i=0; i < height; i++)
{
const char* a = "┌";
const char* b = "─";
const char* c = "┐";
const char* d = "│";
const char* e = "└";
const char* f = "┘";
for (int j = 0; j < width; j++)
{
cout << a << b << c;
}
cout << "\n";
for (int j = 0; j < width; j++)
{
cout << d << board[i][j]<< " " <<d;
}
cout << "\n";
for (int j = 0; j < width; j++)
{
cout << e << b<<f;
}
cout << "\n";
}
}
int main()
{
//This allows player to create board size.
int height, width;
cout << "Please enter the height of the desired board: \n";
cin >> height;
cout << "Please enter the width of the desired board: \n";
cin >> width;
int **p_p_board;
p_p_board = new int* [height];
for ( int i = 0; i< height; i++)
{
p_p_board [i] = new int [width];
}
for ( int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
p_p_board [i][j] = 0;
}
}
drawBoard(height, width, &*p_p_board);
}
|
The errors I get are:
In function 'void drawBoard (int, int, int&)':
error: invalid types 'int[int]' for array subscript.
In function int main ()
error: invalid initialization of non-const reference of type 'int&' from a temporary of type 'int**'
How can I pass the pointer to pointer array to the drawBoard function.
There are no problems with the code below, but then I cannot track moves...
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
|
#include <iostream>
using namespace std;
//This function draws the board given parameters.
void drawBoard(int height, int width)
{
for (int i=0; i < height; i++)
{
const char* a = "┌";
const char* b = "─";
const char* c = "┐";
const char* d = "│";
const char* e = "└";
const char* f = "┘";
for (int j = 0; j < width; j++)
{
cout << a << b << c;
}
cout << "\n";
for (int j = 0; j < width; j++)
{
cout << d << " " <<d;
}
cout << "\n";
for (int j = 0; j < width; j++)
{
cout << e << b<<f;
}
cout << "\n";
}
}
int main()
{
//This allows player to create board size.
int height, width;
cout << "Please enter the height of the desired board: \n";
cin >> height;
cout << "Please enter the width of the desired board: \n";
cin >> width;
int **p_p_board;
p_p_board = new int* [height];
for ( int i = 0; i< height; i++)
{
p_p_board [i] = new int [width];
}
for ( int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
p_p_board [i][j] = 0;
}
}
drawBoard(height, width);
}
|