Creating a cube with multiple planes

Hello all,

I am trying to create a cube that can be viewed in multiple layers along what i assume would be the Z axis -

For example

00
00

This would be the first layer, then under this would be

11
11

(only filling this space on the screen, where a key would be required to view the next layer down)

If anyone has played dwarf fortress i want something that is similar to the way Z levels work.

I have tried creating this with 3d vectors and arrays but am only just starting in Games Development at Uni and getting quite confused.

Its for a project i am working on with a friend to create a simulation of ants moving in a terrarium, and we are trying to figure out the 'sand' in which the ants will dig

There is no requirement of size (the hope is that users will be able to define a cube size)

But to start with at (10,10,10) would be perfect.

we dont want anyone to do it for us, just maybe give us some hints about where we may start with this.

Many thanks for any help --

CJ

(P.S this is a personal project and not Uni related!!)
Last edited on
I have tried creating this with 3d vectors ... and getting quite confused.

The syntax for multi-dimensional std::vectors is awkward, but I’d give them a second chance.
Supposing you want your ‘ground’ to be described not by digits, but by weird characters like @, #, & or worst, you can try something like this (just hints):

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
#include <chrono>
#include <iostream>
#include <limits>
#include <random>
#include <vector>

// Let's choose some symbols which are likely to be there on a large variety
// of keyboards:
const std::vector<int> print_symbols { '@', '#', '*', '+', '-' };

struct Cube {
    static std::mt19937 eng;
    int edge;
    std::vector<std::vector<std::vector<int>>> cube;
    Cube();
    Cube(int edge_arg);
    int getRndInt(int min, int max);
    int createCube();
    void fillCubeWithRndVals();
    friend std::ostream& operator<<(std::ostream& os, const Cube& rhs)
    {
        int level { 1 };
        for(const auto& a : rhs.cube) {
            os << "level " << level << ":\n";
            for(const auto& b : a) {
                for(const auto& c : b) { os << static_cast<char>(c); }
                os << '\n';
            }
            os << "\n\n";
            ++level;
        }
        return os;
    }
};

std::mt19937 Cube::eng { static_cast<unsigned>( std::chrono::high_resolution_clock::now()
                                                .time_since_epoch()
                                                .count() ) };

Cube::Cube() : edge {} { createCube(); }

Cube::Cube(int edge_arg) : edge { edge_arg } { createCube(); }

int Cube::getRndInt(int min, int max)
{
    std::uniform_int_distribution<> rnd(min, max);
    return rnd(Cube::eng);
}

int Cube::createCube()
{
    if(!edge) { edge = getRndInt(2, 5); } // choose your boundaries
    cube.resize(edge);
    for(auto& square : cube) {
        square.resize(edge);
        for(auto& row : square) { row.resize(edge); } // all zero initialized
    }
    std::cout << "Cube edge is: " << edge
              << "\nStarting cube is:\n" << *this;
    fillCubeWithRndVals();
    std::cout << "\nAfter filling with random values:\n" << *this;
    return edge;
}

void Cube::fillCubeWithRndVals()
{
    for(auto& square : cube) {
        for(auto& row : square) {
            for(auto& cell : row) { cell = print_symbols.at(getRndInt(0, 4)); }
        }
    }
}

void waitForEnter();

int main()
{
    Cube cube;
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


Output:
Cube edge is: 2
Starting cube is:
level 1:




level 2:





After filling with random values:
level 1:
#+
*+


level 2:
*#
++



Press ENTER to continue...

Good luck!
Brilliant we will give these a shot and see how it works, we managed to have some success with 3D areays but feel that vectors will still be the way, also, we are filling with all sorts of symbols! Will keep you posted how it turns out,

Many thanks

CJ
Topic archived. No new replies allowed.