Multiple variable issue

Hello!!
I'm writing a little dice game, but I have a problem how I should make the variable layout.

The program should show 6 dices' value in some ascii graphics that look for example like this:
1
2
3
4
|#   #|
|  #  |
|#   #|
 ----- 


To write this out for all 6 dices would take around 240 variables (dices*rows*characters).
Then I thought that I could use a multidimensional array with one dimension for each dice, one dimension for each row and one dimension for each character.

How can I do that?

I've tried like this:
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
#include <time.h>
#include <iostream>
using namespace std;

int main() {
    // diceValue[0-5] contains the dices' value
    int diceValue[6];

    // diceGFX[diceID][row][GFX characters] contains the dices' graphics
    char diceGFX[6][5][9];

    // Counting var
    int i;
    
    // initiates random-function
    srand (time(NULL));

    for (i = 0; i < 6; i++) {
        diceValue[i] = rand() % 6 + 1;
    }
    
    for (i = 0; i < 6; i++) {
        switch (diceValue[i]) {
            case 1:
                diceGFX[i][0][] = " -----  "; // Here is where all the error is
                diceGFX[i][1][] = "|     | "; // like: "expected primary-expression before ']' token"
                diceGFX[i][2][] = "|  #  | "; // I've also tried like this: diceGFX[i][0][] = {" ","-","-","-","-","-"," "," "};
                diceGFX[i][3][] = "|     | "; // but that didn't work either 
                diceGFX[i][4][] = " -----  ";
                break;

// [...] Same for dice value 2-6 [...]

            default:
                break;
        }
        
    }

    for (i = 0; i < 6; i++) {
        // Prints the rows with graphics
        cout << diceGFX[i][0][] << endl; // Here is also alot of errors
        cout << diceGFX[i][1][] << endl; // like: "expected primary-expression before ']' token"
        cout << diceGFX[i][2][] << endl; // But if I set a value in the last [] I only show one character
        cout << diceGFX[i][3][] << endl; 
        cout << diceGFX[i][4][] << endl;
    }

    return 0;
}

I think I know what I'm doing wrong but how can I make it right?

Thanks
Davud
Initialize your char[][][] at its declaration, not in a for loop.
EDIT: Like this:
1
2
3
4
5
6
7
8
9
10
char [6][5][6]={
	{
	 	" ----- ",
		"|     |",
		"|  #  |",
		"|     |",
		" ----- "
	},
	//the others...
}
Last edited on
Rocketboy9000

can you explain whats what in char[][][] please?
It's an array of arrays of arrays. You need a number in each [].
I mean what each [] mean row, diceID etc. do you understand?
oh. it's char diceGFX[value][row][character]
I left the name out of the code above. Here it is with the names:
1
2
3
4
5
6
7
8
9
10
11
12
const int diesides=6;
const int dieGFXrows=5;
char diceGFX[6][5][6]={
	{
	 	" ----- ",
		"|     |",
		"|  #  |",
		"|     |",
		" ----- "
	},
	//the others...
}

After completing the literal array, you would print out for example one die by doing:
1
2
3
4
int x=rand()%diesides;
for(int i=0;i<dieGFXrows;i++){
	cout << dieGFX[x][i] << endl;
}
Last edited on
Here is an alternative way to structure the data. Use a bitset< 9 > for each of the six dice values with the top row of three characters being indexes 0-2, the middle row indexes 3-5 and bottom row indexes 6-8. A set bit (value 1) means output the # character and a cleared bit (value 0) means output a space character. Thus a six would be represented by the bit values 101101101. Then use a vector of these six bitsets with the value of the die used to index (zero-based) into the vector to pull up the correct bitset. Below is a short program that sets up the data together with a function to display a die. The program displays the six die values with a blank line in between and then exits.

It would probably be better done to spin this off into a dice class than doing everything in main(). One potential refinement would be to add a third int parameter with a default value of zero as the number of spaces to offset the output from the left edge of the console window. That is, displayDie becomes:

void displayDie( int n, const vector< bitset< 9 > >& dieValues, int offset = 0 )

The only change to the code would be to output offset number of spaces at the beginning of each cout statement in the function.

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
#include <iostream>
#include <bitset>
#include <string>
#include <vector>

using std::string;
using std::bitset;
using std::vector;

void displayDie( int n, const vector< bitset< 9 > >& dieValues );

int main()
{
    vector< bitset< 9 > > dieValues( 6 );

    dieValues[ 5 ] = bitset< 9 >( "101101101" );   // six
    dieValues[ 4 ] = bitset< 9 >( "101010101" );   // five
    dieValues[ 3 ] = bitset< 9 >( "101000101" );   // four
    dieValues[ 2 ] = bitset< 9 >( "100010001" );   // three
    dieValues[ 1 ] = bitset< 9 >( "100000001" );   // two
    dieValues[ 0 ] = bitset< 9 >( "000010000" );   // one

    for ( int i = 1; i <= 6; ++i )
    {
        displayDie( i, dieValues );
        std::cout << "\n";
    }

    return 0;
}

void displayDie( int n, const vector< bitset< 9 > >& dieValues )
{
    // We will index into dieValues using n to tell us which bitset
    // to use in output, then loop through the bitset outputting 
    // the '#' character or a space
    --n;
    size_t i = 0;
    while( i < 9 )
    {
        for( int k = 0; k < 3; ++k )
        {
            std::cout << ( dieValues[ n ][ i ] ? '#' : ' ' );
            ++i;
        }
        std::cout << "\n";
    }
}

@Alrededor

Bitset seams a little over my head right now. But I will read up on it...
And it will be in a function later in the real game. This is just a part of the hole game. I'd like to split up my projects in to smaller pieces to easer solve the problems :)

@rocketboy9000

If I want to print out the graphics like this then:
1
2
3
4
q
|#   #| |     |
|  #  | |  #  |
|#   #| |     |
 -----   ----- 


Before I wrote the code somthing like this:
1
2
3
4
5
6
cout << diceGFX[][0][0] << diceGFX[][1][0] << endl;
cout << diceGFX[][0][1] << diceGFX[][1][1] << endl;
cout << diceGFX[][0][2] << diceGFX[][1][2] << endl;
cout << diceGFX[][0][3] << diceGFX[][1][3] << endl;
cout << diceGFX[][0][4] << diceGFX[][1][4] << endl;
Last edited on
Solved it!

Here is the result:
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
#include <time.h>
#include <iostream>
using namespace std;

int main() {
    // diceValue[0-5] 
    int diceValue[6];
    
    // diceGFX[value][row][GFX]
    char diceGFX[6][5][9] = {
        {
            " -----  ",
            "|     | ",
            "|  #  | ",
            "|     | ",
            " -----  "
        },
        {
            " -----  ",
            "|#    | ",
            "|     | ",
            "|    #| ",
            " -----  "
        },
        {
            " -----  ",
            "|#    | ",
            "|  #  | ",
            "|    #| ",
            " -----  "
        },
        {
            " -----  ",
            "|#   #| ",
            "|     | ",
            "|#   #| ",
            " -----  "
        },
        {
            " -----  ",
            "|#   #| ",
            "|  #  | ",
            "|#   #| ",
            " -----  "
        },
        {
            " -----  ",
            "|#   #| ",
            "|#   #| ",
            "|#   #| ",
            " -----  "
        },
    };
    
    srand (time(NULL));
    
    for (int i = 0; i < 6; i++) {
        diceValue[i] = rand() % 6;
    }
    
    
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 6; j++) {
            cout << diceGFX[diceValue[j]][i];
        }
        cout << endl;
    }
    
    return 0;
}


edit: shorted down the couts
Last edited on
Topic archived. No new replies allowed.