-4

I'm fairly new to this, been writing code for ~3 months and now I have to make a dice game for a group project. So far, the console shows what I expect it to show (5 random dice values)

enter image description here

But what I want to do to improve it is to show an actual die instead of an individual number, like ⚀⚁⚂⚃⚄⚅

These are the functions I've written so far for this part (for a separate header):

                            ///Lanza los dados
void cargarCubilete(int v[], int tam, int limite){
  int i;
  srand(time(NULL));
  for( i=0; i<tam; i++ ){
        v[i]=(rand()%limite)+1;
  }
}

                        ///MUESTRA EL CUBILETE

void mostrarCubilete(int v[], int tam){
    int i;
    for(i=0;i<tam;i++){
    cout<<v[i]<<"\t";
    }
}

Any tips on how to do it?

Thanks!

Marek R
  • 32,568
  • 6
  • 55
  • 140

2 Answers2

0

Here an additional answer using the same concept as already described.

Making use of compile time defined constexpr "pictures" and drawing them on the screen.

Basically nothing more to explain:

#include <iostream>
#include <array>
#include <random>

constexpr size_t NumberOfRows = 7u;
constexpr size_t NumberOfColumns = 9u;
constexpr size_t NumberOfFaces = 6u;

constexpr std::array<std::array<std::array<char, NumberOfColumns>, NumberOfRows>, NumberOfFaces> diceImage{ {
    {{
      {'+','-','-','-','-','-','-','-','+'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'|',' ',' ',' ','o',' ',' ',' ','|'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'+','-','-','-','-','-','-','-','+'},
    }},
    {{
      {'+','-','-','-','-','-','-','-','+'},
      {'|','o',' ',' ',' ',' ',' ',' ','|'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'|',' ',' ',' ',' ',' ',' ','o','|'},
      {'+','-','-','-','-','-','-','-','+'},
    }},
    {{
      {'+','-','-','-','-','-','-','-','+'},
      {'|','o',' ',' ',' ',' ',' ',' ','|'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'|',' ',' ',' ','o',' ',' ',' ','|'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'|',' ',' ',' ',' ',' ',' ','o','|'},
      {'+','-','-','-','-','-','-','-','+'},
    }},
    {{
      {'+','-','-','-','-','-','-','-','+'},
      {'|','o',' ',' ',' ',' ',' ','o','|'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'|','o',' ',' ',' ',' ',' ','o','|'},
      {'+','-','-','-','-','-','-','-','+'},
    }},
    {{
      {'+','-','-','-','-','-','-','-','+'},
      {'|','o',' ',' ',' ',' ',' ','o','|'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'|',' ',' ',' ','o',' ',' ',' ','|'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'|','o',' ',' ',' ',' ',' ','o','|'},
      {'+','-','-','-','-','-','-','-','+'},
    }},
    {{
      {'+','-','-','-','-','-','-','-','+'},
      {'|','o',' ',' ',' ',' ',' ','o','|'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'|','o',' ',' ',' ',' ',' ','o','|'},
      {'|',' ',' ',' ',' ',' ',' ',' ','|'},
      {'|','o',' ',' ',' ',' ',' ','o','|'},
      {'+','-','-','-','-','-','-','-','+'},
    }},
}};
constexpr size_t NumberOfDices = 5u;

int main() {

    std::random_device randomDevice;
    std::mt19937 diceResultGenerator(randomDevice());
    std::uniform_int_distribution<> diceResult(1, 6);

    std::array<int, NumberOfDices> result{};

    // Roll dices several times
    constexpr size_t NumberOfDiceRolls = 3u;
    for (size_t roll{}; roll < NumberOfDiceRolls; ++roll) {

        // Roll the dices
        std::cout << "\nResult: ";
        for (size_t diceIndex{}; diceIndex < NumberOfDices; ++diceIndex) {
            result[diceIndex] = diceResult(diceResultGenerator);
            std::cout << result[diceIndex] << ' ';
        }
        std::cout << "\n\n";

        // Paint result
        for (size_t row{}; row < NumberOfRows; ++row) {
            for (size_t diceIndex{}; diceIndex < NumberOfDices; ++diceIndex) {
                for (size_t column{}; column < NumberOfColumns; ++column)
                    std::cout << diceImage[result[diceIndex]-1][row][column];
                std::cout << '\t';
            }
            std::cout << '\n';
        }
        std::cout << "\n\n\n";
    }
}

Potential output:


Result: 3 6 6 3 5

+-------+       +-------+       +-------+       +-------+       +-------+
|o      |       |o     o|       |o     o|       |o      |       |o     o|
|       |       |       |       |       |       |       |       |       |
|   o   |       |o     o|       |o     o|       |   o   |       |   o   |
|       |       |       |       |       |       |       |       |       |
|      o|       |o     o|       |o     o|       |      o|       |o     o|
+-------+       +-------+       +-------+       +-------+       +-------+




Result: 3 2 2 6 2

+-------+       +-------+       +-------+       +-------+       +-------+
|o      |       |o      |       |o      |       |o     o|       |o      |
|       |       |       |       |       |       |       |       |       |
|   o   |       |       |       |       |       |o     o|       |       |
|       |       |       |       |       |       |       |       |       |
|      o|       |      o|       |      o|       |o     o|       |      o|
+-------+       +-------+       +-------+       +-------+       +-------+




Result: 1 6 5 2 2

+-------+       +-------+       +-------+       +-------+       +-------+
|       |       |o     o|       |o     o|       |o      |       |o      |
|       |       |       |       |       |       |       |       |       |
|   o   |       |o     o|       |   o   |       |       |       |       |
|       |       |       |       |       |       |       |       |       |
|       |       |o     o|       |o     o|       |      o|       |      o|
+-------+       +-------+       +-------+       +-------+       +-------+

A M
  • 14,694
  • 5
  • 19
  • 44
0

But what I want to do to improve it is to show an actual die instead of an individual number, like ⚀⚁⚂⚃⚄⚅

Note those characters are non-ASCII. So to make it work you have to take care of encoding of characters (encoding) for each step (source, building, running on some specific system). At final step terminal used has to have locale set in such way this characters are covered (not all encodings have this characters) and also font used by this terminal program have to have those characters.

So there is a lot of places where something may went wrong, see some my SO answer if you need more details.

Most common encoding is UTF-8 so es a result this characters are not single byte, so the have to be kept in code as some form string.

For example this is working fine:

#include <array>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
#include <random>

using namespace std::literals;

constexpr std::array DiceSides {
    "⚀"sv, "⚁"sv, "⚂"sv, "⚃"sv, "⚄"sv, "⚅"sv
};

int main()
{
    // std::locale::global(std::locale{"<locale used for exacutable>"});
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dice(1, 6);

    std::cout.imbue(std::locale(""));
    for (size_t i = 0; i < 20; ++i) {
        auto x = dice(gen);
        std::cout << i + 1 << ": " << x << " = " << DiceSides[x - 1] << '\n';
    }

    return 0;
}

https://godbolt.org/z/WaMYeKoM6

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • This is exactly what i was looking for, thanks! I'm sorry, I'm still figuring out how this site works, so I don't know how to "interact properly"? – Matías Bergés Jun 23 '22 at 16:50