Issue with passing info

So this all works except the display_board() function. It sends back an error that "board" is not initialized in the display_board() function, so it's not getting the randomized board. I'm not sure how to pass the board from the generate_board() function to the display_board() function. I've been playing around with pointers on it but can't seem to get that to work either. I did manage to get the generate_board() function to send back a broken version of the board, but I'm not sure why it did that. If someone could give me some advice on what to do, I'd appreciate it.

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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

void generate_board();
void display_board();
void get_move_from_user();
void make_move();

int main()
{
	generate_board();

	display_board();
	
	get_move_from_user();

	return 0;
}

void generate_board()
{
	const int SIZE = 25;
	const int MIN_VALUE = 0;
	const int MAX_VALUE = 24;
	int r;
	char tmp;

	char board[SIZE] = { ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X'};
	
	unsigned seed = time(0);
	srand(seed);

	for (int i = 0; i < SIZE; i++)
	{
		r = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
		tmp = board[i];
		board[i] = board[r];
		board[r] = tmp;
	}
}

void display_board()
{
	for (int i = 0; i < 25; i++)
	{
		cout << board[i] << " ";
		if (i % 5 == 4)
			cout << endl;
	}
}

void get_move_from_user()
{
	char move;

	cout << "enter a character to make a move: ";

	cin >> move;

	while (move != 'w' && move != 'a' && move != 's' && move != 'd')
	{
		cout << "That is not a valid input, please try again: ";
		cin >> move;
	}
}

void make_move()
{

}
Last edited on
Hello Das, I see you have a problem with passing your array by reference to display the board. Here's how you would do that.

You can define the size of the board globally and it will be a constant to be safe
 
#define BOARD_SIZE 25 


Here's the function definition you could use to pass an array by reference, you must use an array with the size of BOARD_SIZE
 
void display_board(int (&board)[BOARD_SIZE]);


This is what it would look like to use the function:
 
display_board(board);


You would get the following output:
1
2
3
4
5
6
  A B C D
E F G H I
J K L M N
O P Q R S
T U V W X
Press any key to continue . . .
Unfortunately, I'm not supposed to use global variables.

Also, plugging all that in still gave me an error. Says that " board is not initialized" when trying to call display_board(board) .

I tried something like below to return the board to main, but the board comes back with garbage information when I check it with the cout statement.

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
char * generate_board();
//void display_board(char *);
void get_move_from_user();
void make_move();

int main()
{
	char * z;
	z = generate_board();

	for (int i = 0; i < 25; i++)
	{
		cout << z[i] << " ";
		if (i % 5 == 4)
			cout << endl;
	}

	//display_board(*z);
	
	get_move_from_user();

	return 0;
}

char * generate_board()
{
	const int SIZE = 25;
	const int MIN_VALUE = 0;
	const int MAX_VALUE = 24;
	int r;
	char tmp;

	char board[SIZE] = { ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X'};
	
	unsigned seed = time(0);
	srand(seed);

	for (int i = 0; i < SIZE; i++)
	{
		r = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
		tmp = board[i];
		board[i] = board[r];
		board[r] = tmp;
	}
	return board;
}
Last edited on
Well then instead of creating a global variable fill the prototype like this then.

 
void display_board(int (&board)[25]);


The reason your board is not initialized is because you are creating your board locally inside of your generate_board() function. It should be easy for you to understand that.
Topic archived. No new replies allowed.