Random Number sorted and drawn as a sign

Hi,
i'm trying to programm a game. For that i need random numbers sorted, so the biggest number is first : 31, 32, 41, 42, 43, 51, 52, 53, 54, 61, 62, 63, 64, 65, 11, 22, 33, 44, 55, 66, 21. 21 is game-winning and numbers are allowed only from 1 to 6. It should be outputted in signs.


Heres how far i got:

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <ctime>
#include <iostream>
#include <stdlib.h>
#include <string>

void eins()
{
			cout << "  /|  " << endl;
			cout << " / |  " << endl;
			cout << "   |  " << endl;
			cout << "   |  " << endl;
			cout << " __|__" << endl;
}

void zwei()
{
			cout << " ___ " << endl;
			cout << "|   |" << endl;
			cout << "    |" << endl;
			cout << "   / " << endl;
			cout << " /   " << endl;
			cout << "|____" << endl;
}
void drei()
{
			cout << " ___ " << endl;
			cout << "|   |" << endl;
			cout << "    |" << endl;
			cout << "  __| " << endl;
			cout << "    |" << endl;
			cout << "    |" << endl;
			cout << "|___|" << endl;
}
void vier()
{
			cout << "|	  " << endl;
			cout << "|          " << endl;
			cout << "|___|__ " << endl;
			cout << "      |   " << endl;
			cout << "      |   " << endl;
}
void fuenf()
{
			cout << "_____" << endl;
			cout << "|    " << endl;
			cout << "|___ " << endl;
			cout << "    |" << endl;
			cout << "____|" << endl;
}
void sechs()
{
			cout << " _______  " << endl;
			cout << "|             " << endl;
			cout << "|            " << endl;
			cout << "|_______  " << endl;
			cout << "|       | " << endl;
			cout << "|_______| " << endl;
}

void wuerfel()
{
	srand(time(NULL));
	int w1 = rand() % 6 + 1;  //random number 1
	int w2 = rand() % 6 + 1;  //random number 2

//IF i set a breakpoint here, it seams to work, dont know why its normally not
      
        
	if (w2 >= w1)  //w2 is the  higher number,so put it first
	{

		if (w2 == 1)
		{
			eins();
		}
		else if (w2 == 2)
		{
			zwei();
		}
		else if (w2 == 3)
		{
			drei();
		}
		else if (w2 == 4)
		{
			vier();
		}
		else if (w2 == 5)
		{
			fuenf();
		}
		else
		{
			sechs();
		}
		
		if (w1 == 2)
		{
			zwei();
		}
		else if (w1 == 3)
		{
			drei();
		}
		else if (w1 == 4)
		{
			vier();
		}

		else if (w1 == 5)
		{
			fuenf();
		}
		else {	//6
			sechs();
		}
		
	}


	if (w2 < w1)  //w1 is the higher number, so put it first
	{
		

		if (w1 == 2)
		{
			zwei();
		}
		else if (w1 == 3)
		{
			drei();
		}
		else if (w1 == 4)
		{
			vier();
		}

		else if (w1 == 5)
		{
			fuenf();
		}
		else {	//6
			sechs();
		}

		if (w2 == 1)
		{
			eins();
		}
		else if (w2 == 2)
		{
			zwei();
		}
		else if (w2 == 3)
		{
			drei();
		}
		else if (w2 == 4)
		{
			vier();
		}
		else if (w2 == 5)
		{
			fuenf();
		}
		else
		{
			sechs();
		}
	}
}

int main()
{
  wuerfel();
 
  system("pause");
  return 0;
}


-------------------------------------------------------------------------------
Sometimes it seems to ignore which number is higher and writes down for example 45 instead of 54.
Is there a smarter way to do it? Mayby a switch case instead of all the if- statements?
Thanks for your answers.
Last edited on
yeah fixed it, sorry for that.
Is there a smarter way to do it?

Yes, you should simplify your code following the KISS principle. https://en.wikipedia.org/wiki/KISS_principle
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
#include <iostream>
#include <time.h>

using namespace std;

void console_print( int number )
{
    cout << number; // here you can create some sophisticated console image for each digit
}

void show()
{
    int w1 = rand() % 6 + 1;  //random number 1
    int w2 = rand() % 6 + 1;  //random number 2

    console_print(w1>w2?w1:w2);
    console_print(w1>w2?w2:w1);
    cout << endl;
}

int main()
{
  srand(time(NULL));
  for( int index {0} ; index<50 ; ++index ) show();
  system("pause");
  return 0;
}
Last edited on
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
#include <iostream>
#include <string>
#include <random>
using namespace std;

const int DigitHeight = 5, DigitWidth = 6;
const char* Digits[DigitHeight] {
" **** |  **  | **** | **** |**  **|******| **** |******| **** | **** |",
"**  **| ***  |**  **|**  **|**  **|**    |**    |   ** |**  **|**  **|",
"**  **|  **  |   ** |   ***|******|***** |***** |  **  | **** | *****|",
"**  **|  **  |  **  |**  **|    **|    **|**  **| **   |**  **|    **|",
" **** |******|******| **** |    **|***** | **** |**    | **** | **** |"
};

void print_number(int n) {
    string s = to_string(n);
    for (int lines = 0; lines < DigitHeight; ++lines) {
        for (char ch: s) {
            int d = ch - '0';
            d *= (DigitWidth + 1);
            for (int i = d; i < d + DigitWidth; ++i) cout << Digits[lines][i];
            cout << "    ";
        }
        cout << '\n';
    }
}

void roll_dice() {
    static default_random_engine Rnd{random_device{}()};
    static uniform_int_distribution<> Dist(1, 6);
    int w1 = Dist(Rnd), w2 = Dist(Rnd);
    if (w1 < w2) swap(w1, w2);
    print_number(w1 * 10 + w2);
}

int main() {
    roll_dice();
}

wow, thank you two for the fast answers! :)
Yes, you should simplify your code following the KISS principle. https://en.wikipedia.org/wiki/KISS_principle
Yeah sure, thanks ^^

console_print(w1>w2?w1:w2);
console_print(w1>w2?w2:w1);
rly nice, didnt knew that before. I will definitly use this in the future.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const int DigitHeight = 5, DigitWidth = 6;
const char* Digits[DigitHeight] {
" **** |  **  | **** | **** |**  **|******| **** |******| **** | **** |",
"**  **| ***  |**  **|**  **|**  **|**    |**    |   ** |**  **|**  **|",
"**  **|  **  |   ** |   ***|******|***** |***** |  **  | **** | *****|",
"**  **|  **  |  **  |**  **|    **|    **|**  **| **   |**  **|    **|",
" **** |******|******| **** |    **|***** | **** |**    | **** | **** |"
};

void print_number(int n) {
    string s = to_string(n);
    for (int lines = 0; lines < DigitHeight; ++lines) {
        for (char ch: s) {
            int d = ch - '0';
            d *= (DigitWidth + 1);
            for (int i = d; i < d + DigitWidth; ++i) cout << Digits[lines][i];
            cout << "    ";
        }
        cout << '\n';
    }
}



good idea
so here's how i solved it with the knowledge i think I'm allowed to use ^^




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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <ctime>
#include <string>
using namespace std;

string eins[]	= { " _   ","/ |  ","  |  ","  |  ","__|__" };
string zwei[]	= { " ___ ","|   |","   / ","  /  ","|____" };
string drei[]	= { " ___ ","|   |","  __|","    |","|___|" };
string vier[]	= { "  _  "," /   ","/__|_","   | ","   | " };
string fuenf[]	= { " ___ ","|    ","|___ ","    |"," ___|" };
string sechs[]	= { " ___ ","|    ","|___ ","|   |","|___|" };


//Numbers 1-6 in console:
/*
			cout << " _   " << endl;
			cout << "/ |  " << endl;
			cout << "  |  " << endl;
			cout << "  |  " << endl;
			cout << "__|__" << endl;
*/
/*
			cout << " ___ " << endl;
			cout << "|   |" << endl;
			cout << "   / " << endl;
			cout << "  /  " << endl;
			cout << " /___" << endl;



*/
/*
			cout << " ___ " << endl;
			cout << "|   |" << endl;
			cout << "  __|" << endl;
			cout << "    |" << endl;
			cout << "|___|" << endl;
*/  
/*
			cout << "  _   " << endl;
			cout << " /   " << endl;
			cout << "/__|_" << endl;
			cout << "   | " << endl;
			cout << "   | " << endl;
*/  
/*
			cout << " ___ " << endl;
			cout << "|    " << endl;
			cout << "|___ " << endl;
			cout << "    |" << endl;
			cout << " ___|" << endl;
*/
/*
			cout << " ___ " << endl;
			cout << "|    " << endl;
			cout << "|___ " << endl;
			cout << "|   |" << endl;
			cout << "|___|" << endl;
*/

void wuerfelausgabe(int zahl1, int zahl2)
{
	string stringZahl[5];

	for (int j = 0; j < 5; j++)		
	{
		// Number 1
		if (zahl1 == 1)
		{
		stringZahl[j] = eins[j];
		}
		else if (zahl1 == 2)
		{
		stringZahl[j] = zwei[j];
		}
		else if (zahl1 == 3)
		{
		stringZahl[j] = drei[j];
		}
		else if (zahl1 == 4)
		{
		stringZahl[j] = vier[j];
		}
		else if (zahl1 == 5)
		{
		stringZahl[j] = fuenf[j];
		}
		else
		{
		stringZahl[j] = sechs[j];
		}

		//Number2
		if (zahl2 == 1)
		{
		stringZahl[j] += "  " + eins[j];
		}
		else if (zahl2 == 2)
		{
		stringZahl[j] += "  " + zwei[j];
		}
		else if (zahl2 == 3)
		{
		stringZahl[j] += "  " + drei[j];
		}
		else  if (zahl2 == 4)
		{
		stringZahl[j] += "  " + vier[j];
		}
		else if (zahl2 == 5)
		{
		stringZahl[j] += "  " + fuenf[j];
		}
		else
		{
		stringZahl[j] += "  " + sechs[j];
		}
		cout << stringZahl[j] << endl;	//Output
	}
}

void wuerfel()
{
	int max;
	int min;
	int w1 = rand() % 6 + 1;
	int w2 = rand() % 6 + 1;

	if (w1 >= w2) //find the higher number
	{
		max = w1;
		min = w2;
	}
	else
	{
		max = w2;
		min = w1;
	}
	
	wuerfelausgabe(max, min);	//give the two digits to the output function
	
}





int main()
{
	srand(time(NULL));

	
	for (int i = 0; i < 50; i++) {
		wuerfel();
		cout << endl;
		cout << endl;

	}
	

	
	
	system("pause");
	return 0;
}
Last edited on
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
#include <iostream>
#include <string>
#include <sstream>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;

const string digits[] = { "+--+\n|  |\n+  +\n|  |\n+--+\n",
                          "   +\n   |\n   +\n   |\n   +\n",
                          "+--+\n   |\n+--+\n|   \n+--+\n",
                          "+--+\n   |\n+--+\n   |\n+--+\n",
                          "+  +\n|  |\n+--+\n   |\n   +\n",
                          "+--+\n|   \n+--+\n   |\n+--+\n",
                          "+--+\n|   \n+--+\n|  |\n+--+\n",
                          "+--+\n   |\n   +\n   |\n   +\n",
                          "+--+\n|  |\n+--+\n|  |\n+--+\n",
                          "+--+\n|  |\n+--+\n   |\n   +\n" };

string operator *( const string a, const string b )
{
   const string SPACE = "  ";
   stringstream ssa( a ), ssb( b );
   string result, parta, partb;
   while( getline( ssa, parta ) && getline( ssb, partb ) ) result += parta + SPACE + partb + '\n';
   return result;
}

int main()
{
   srand( time( 0 ) );
   int n;
   do
   {
      int r = rand() % 36;
      int a = 1 + r / 6, b = 1 + r % 6;
      if ( a < b ) swap( a, b );
      n = 10 * a + b;
      cout << digits[a] * digits[b] << '\n';
   } while( n != 21 );
}
Last edited on
Topic archived. No new replies allowed.