1
+ use rand:: { Rng , prelude:: { thread_rng} } ;
2
+ use std:: io;
3
+
4
+ fn main ( ) {
5
+ //DATA
6
+ let num_tries: u8 = 2 ; //number of tries the player gets each round, must be at least 1
7
+ let mut rng = thread_rng ( ) ;
8
+ let mut user_guess: u8 ;
9
+ let mut dice_1: u8 ;
10
+ let mut dice_2: u8 ;
11
+
12
+ //print welcome message
13
+ welcome ( ) ;
14
+
15
+ //game loop
16
+ loop {
17
+ //roll dice
18
+ dice_1 = rng. gen_range ( 1 ..=6 ) ;
19
+ dice_2 = rng. gen_range ( 1 ..=6 ) ;
20
+
21
+ //print dice
22
+ print_dice ( dice_1) ;
23
+ println ! ( " +" ) ;
24
+ print_dice ( dice_2) ;
25
+ println ! ( " =" ) ;
26
+
27
+ //get user guess, they have 2 tries
28
+ for t in 0 ..num_tries {
29
+ //get guess
30
+ user_guess = get_number_from_user_input ( "" , "That's not a valid number!" , 1 , 12 ) ;
31
+
32
+ //if they get it wrong
33
+ if user_guess != ( dice_1+dice_2) {
34
+ //print different message depending on what try they're on
35
+ if t < num_tries-1 { // user has tries left
36
+ println ! ( "NO, COUNT THE SPOTS AND GIVE ANOTHER ANSWER." ) ;
37
+ println ! ( " =" ) ;
38
+ }
39
+ else { //this is their last try
40
+ println ! ( "NO, THE ANSWER IS {}" , dice_1+dice_2) ;
41
+ }
42
+ }
43
+ else {
44
+ println ! ( "RIGHT!" ) ;
45
+ break ;
46
+ }
47
+ }
48
+
49
+ //play again
50
+ println ! ( "\n The dice roll again...." ) ;
51
+ }
52
+
53
+ }
54
+
55
+ /**
56
+ * prints the welcome message to the console
57
+ */
58
+ fn welcome ( ) {
59
+ println ! ( "
60
+ MATH DICE
61
+ CREATIVE COMPUTING MORRISTOWN, NEW JERSEY
62
+ \n \n
63
+ THIS PROGRAM GENERATES SUCCESSIVE PICTURES OF TWO DICE.
64
+ WHEN TWO DICE AND AN EQUAL SIGN FOLLOWED BY A QUESTION
65
+ MARK HAVE BEEN PRINTED, TYPE YOUR ANSWER AND THE RETURN KEY.
66
+ TO CONCLUDE THE LESSON, PRESS Ctrl+C AS YOUR ANSWER.\n
67
+ " ) ;
68
+ }
69
+
70
+ /**
71
+ * print the dice,
72
+ */
73
+ fn print_dice ( dice_value : u8 ) {
74
+ //data
75
+
76
+ //top
77
+ println ! ( " ----- " ) ;
78
+ //first layer
79
+ match dice_value {
80
+ 4 |5 |6 => println ! ( "| * * |" ) ,
81
+ 2 |3 => println ! ( "| * |" ) ,
82
+ _=>println ! ( "| |" ) ,
83
+ }
84
+
85
+ //second layer
86
+ match dice_value {
87
+ 1 |3 |5 => println ! ( "| * |" ) ,
88
+ 2 |4 => println ! ( "| |" ) ,
89
+ _=>println ! ( "| * * |" ) ,
90
+ }
91
+
92
+ //third layer
93
+ match dice_value {
94
+ 4 |5 |6 => println ! ( "| * * |" ) ,
95
+ 2 |3 => println ! ( "| * |" ) ,
96
+ _=>println ! ( "| |" ) ,
97
+ }
98
+
99
+ //bottom
100
+ println ! ( " ----- " ) ;
101
+ }
102
+
103
+ /**
104
+ * gets a integer from user input
105
+ */
106
+ fn get_number_from_user_input ( prompt : & str , error_message : & str , min : u8 , max : u8 ) -> u8 {
107
+ //input loop
108
+ return loop {
109
+ let mut raw_input = String :: new ( ) ; // temporary variable for user input that can be parsed later
110
+
111
+ //print prompt
112
+ println ! ( "{}" , prompt) ;
113
+ //read user input from standard input, and store it to raw_input
114
+ //raw_input.clear(); //clear input
115
+ io:: stdin ( ) . read_line ( & mut raw_input) . expect ( "CANNOT READ INPUT!" ) ;
116
+
117
+ //from input, try to read a number
118
+ match raw_input. trim ( ) . parse :: < u8 > ( ) {
119
+ Ok ( i) => {
120
+ if i < min || i > max { //input out of desired range
121
+ println ! ( "{} ({}-{})" , error_message, min, max) ;
122
+ continue ; // run the loop again
123
+ }
124
+ else {
125
+ break i; // this escapes the loop, returning i
126
+ }
127
+ } ,
128
+ Err ( e) => {
129
+ println ! ( "{} {}" , error_message, e. to_string( ) . to_uppercase( ) ) ;
130
+ continue ; // run the loop again
131
+ }
132
+ } ;
133
+ } ;
134
+ }
0 commit comments