Print a 3-D drawing of a die, using asterisks

Does anyone know how to write a code to print a 3-d drawing of a die, , using asterisks?
Die is 3D
It displays everything correctly (random 1-6, i guess)

Why 3D, when all you would really need, would be the face of the die, showing the pips?

Like this..

1
2
3
4
5
6
7
8
9
10
11
string die[5][6] = { 
		{ "\xDA\xC4\xC4\xC4\xBF", "\xDA\xC4\xC4\xC4\xBF", "\xDA\xC4\xC4\xC4\xBF", "\xDA\xC4\xC4\xC4\xBF", "\xDA\xC4\xC4\xC4\xBF", "\xDA\xC4\xC4\xC4\xBF" },
		{ "\xB3\x20\x20\x20\xB3", "\xB3\x4F\x20\x20\xB3", "\xB3\x4F\x20\x20\xB3", "\xB3\x4F\x20\x4F\xB3", "\xB3\x4F\x20\x4F\xB3", "\xB3\x4F\x20\x4F\xB3" },
		{ "\xB3\x20\x4F\x20\xB3", "\xB3\x20\x20\x20\xB3", "\xB3\x20\x4F\x20\xB3", "\xB3\x20\x20\x20\xB3", "\xB3\x20\x4F\x20\xB3", "\xB3\x4F\x20\x4F\xB3" },
		{ "\xB3\x20\x20\x20\xB3", "\xB3\x20\x20\x4F\xB3", "\xB3\x20\x20\x4F\xB3", "\xB3\x4F\x20\x4F\xB3", "\xB3\x4F\x20\x4F\xB3", "\xB3\x4F\x20\x4F\xB3" },
		{ "\xC0\xC4\xC4\xC4\xD9", "\xC0\xC4\xC4\xC4\xD9", "\xC0\xC4\xC4\xC4\xD9", "\xC0\xC4\xC4\xC4\xD9", "\xC0\xC4\xC4\xC4\xD9", "\xC0\xC4\xC4\xC4\xD9" }
	};
	for (int x = 0; x < 5; x++)
		cout << die[x][0] << "  " << die[x][1] << "  " << die[x][2] << "  " << die[x][3] << "  " << die[x][4] << "  " << die[x][5] << "  " << endl;

//cout all 6 dice faces 


'\x20' is hex for a space
'\x4F' is hex for a O. Used for the pip on die
The rest of the hexes make up the border of the die
Last edited on
You'll have to get out some graph paper (or an editor) and draw what you want to see.
Then write code to create it.
If you want to do a 3D drawing of a die then then you are going to need to show 2 or 3 faces, which means you will need to know how the spots are arranged on a die ie the sum of the number of spots on opposite sides equals 7.
If you are curious and want to go that far:
http://www.toddstrong.com/personalthoughts/dice_and_spots.php

That said, I don't think it really matters much so long as you are consistent about keeping the same number of spots to the same face. Don't worry about rotating them or anything.

Here is the series of (ASCII-art) drawings I made when looking at this problem:

     + + +
    + + +  @
   + + +  @@
         @@@
  * * *  @@
  * * *  @
  * * *
    .-------.
   / + + + /|
  / + + + /@|
 / + + + /@@|
.------- @@@'
| * * * |@@/
| * * * |@/
| * * * |/
'-------'

    .-------.
   / * * * /|
  / * * * /*|
 / * * * /**|
.------- ***'
| * * * |**/
| * * * |*/
| * * * |/
'-------'

    .-------.
   /       /|
  /   *   /*|
 /       /  |
.-------    '
|     * |  /
|   *   |*/
| *     |/
'-------'

The thing to note is that each side can be represented by a small 3x3 array of asterisks:

|   |  *|  *|* *|* *|***|
| * |   | * |   | * |   |
|   |*  |*  |* *|* *|***|

Again, it does not really matter how the spots are oriented. The important facts are that they are a regular 3x3 array and that each side of the drawing is easily generated from that array. It is also suggestive that the spots are numbered 1-6: make an array of these 3x3 arrays.

What I recommend is that you have a 2D array drawing of an empty die:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
typedef char die_type[9][14];

die_type die =
{
  "    .-------.",
  "   /       /|",
  "  /       / |",
  " /       /  |",
  ".-------    '",
  "|       |  / ",
  "|       | /  ",
  "|       |/   ",
  "'-------'    "
};

Then you need three functions: each one fills in a specific side with a specific number of spots:

1
2
3
void fill_top  ( die_type die, spots_type spots );
void fill_front( die_type die, spots_type spots );
void fill_right( die_type die, spots_type spots );

Now you can create an image of the die easily enough:

1
2
3
fill_top  ( die, spots[1-1] );  // top gets 1 spot
fill_front( die, spots[2-1] );  // front gets 2 spots
fill_right( die, spots[3-1] );  // side gets 3 spots 

And then you can print it:

1
2
for (auto line : die)
  std::cout << line << "\n";

Hope this helps.

I would personally use os instead of asterisks.
very nice! my teacher wanted asterisks.
thanks for all your help.
i am going to need to learn how to put it in code now.
this first class assignment is quite challenging.
I tried this, but my number is always end up at 6. can someone show me how to display
6 or anything in 3-d?




#include "stdafx.h"
#include <random> // C++11
#include <iostream>

using namespace std;
int main()
{
int numberRolled;
numberRolled = rand() % 6 + 1;
cout << "The number rolled is " << numberRolled << ".\n";
system("PAUSE");
return 0;
}


Topic archived. No new replies allowed.