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
|
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cctype>
const int NFACES = 6 ; // number of faces in a die
// return the value of a random face of the die
int random_face( const int die[NFACES] )
{
const int random_face = std::rand() % NFACES ;
return die[random_face] ;
}
// print the colour and faces of a die
void print( const char* colour, const int die[NFACES] )
{
std::cout << colour << ": " ;
for( int i = 0 ; i < NFACES ; ++i ) std::cout << die[i] << ' ' ;
std::cout << '\n' ;
}
void display( const int red[NFACES], const int green[NFACES], const int blue[NFACES] )
{
print( " red", red ) ;
print( "green", green ) ;
print( " blue", blue ) ;
}
void play( const int red[NFACES], const int green[NFACES], const int blue[NFACES] )
{
std::cout << "Enter the die colour (R G or B): " ;
char clr ;
std::cin >> clr ;
clr = std::toupper(clr) ;
if( clr == 'R' ) std::cout << random_face(red) << '\n' ;
else if( clr == 'G' ) std::cout << random_face(green) << '\n' ;
else if( clr == 'B' ) std::cout << random_face(blue) << '\n' ;
else std::cout << "invalid colour\n" ;
}
int main()
{
std::srand( std::time(nullptr) ) ;
const int red[NFACES] = { 1,4,4,4,4,4 };
const int green[NFACES] = { 2,2,2,5,5,5 };
const int blue[NFACES] = { 6,3,3,3,3,3 };
display( red, green, blue ) ;
play( red, green, blue ) ;
}
|