Skip to content

Commit 25faf4a

Browse files
Merge pull request #897 from lmstolton/hamurabi
Changed get_input() to return Option<u32>
2 parents 1ebccd6 + 7dff2a6 commit 25faf4a

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

43_Hammurabi/rust/src/main.rs

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -46,99 +46,95 @@ fn run() {
4646
loop {
4747
println!("HOW MANY ACRES DO YOU WISH TO BUY? ");
4848
if let Some(qty) = get_input() {
49-
if qty < 0 { // A negative amount is impossible
50-
impossible_task();
51-
game_failed = true;
52-
break 'main;
53-
}
5449
// Player decides not to buy any land
5550
if qty == 0 {
5651
bought_land = false;
5752
break;
5853
}
5954
// Trying to buy more land than you can afford?
60-
if land_price * qty as u32 > grain {
55+
if land_price * qty > grain {
6156
insufficient_grain(grain);
6257
continue;
6358
}
6459
// Everything checks out OK
65-
if land_price * qty as u32 <= grain {
66-
acres += qty as u32;
67-
grain -= land_price * qty as u32;
60+
if land_price * qty <= grain {
61+
acres += qty ;
62+
grain -= land_price * qty ;
6863
bought_land = true;
6964
break;
7065
}
66+
} else {
67+
impossible_task();
68+
game_failed = true;
69+
break 'main;
7170
}
7271
}
7372

7473
if !bought_land {
7574
loop {
7675
println!("HOW MANY ACRES DO YOU WISH TO SELL? ");
7776
if let Some(qty) = get_input() {
78-
if qty < 0 { // A negative amount is impossible
79-
impossible_task();
80-
game_failed = true;
81-
break 'main;
82-
}
8377
// Everything checks out OK
84-
if qty as u32 <= acres {
85-
acres -= qty as u32;
86-
grain += land_price * qty as u32;
78+
if qty <= acres {
79+
acres -= qty;
80+
grain += land_price * qty;
8781
break;
8882
}
8983
// Trying to sell more land that you own
9084
insufficient_land(acres);
85+
} else {
86+
impossible_task();
87+
game_failed = true;
88+
break 'main;
9189
}
9290
}
9391
}
9492

9593
loop {
9694
println!("HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? ");
9795
if let Some(qty) = get_input() {
98-
if qty < 0 { // A negative amount is impossible
99-
impossible_task();
100-
game_failed = true;
101-
break 'main;
102-
}
10396
// Trying to use more grain than is in silos?
104-
if qty as u32 > grain {
97+
if qty > grain {
10598
insufficient_grain(grain);
10699
continue;
107100
}
108101
// Everything checks out OK
109-
bushels_fed = qty as u32;
102+
bushels_fed = qty;
110103
grain -= bushels_fed;
111104
break;
105+
} else {
106+
impossible_task();
107+
game_failed = true;
108+
break 'main;
112109
}
113110
}
114111

115112
loop {
116113
println!("HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? ");
117114
if let Some(qty) = get_input() {
118-
if qty < 0 { // A negative amount is impossible
119-
impossible_task();
120-
game_failed = true;
121-
break 'main;
122-
}
123115
// Trying to plant more acres than you own?
124-
if qty as u32 > acres {
116+
if qty > acres {
125117
insufficient_land(acres);
126118
continue;
127119
}
128120
// Enough grain for seed?
129-
if qty as u32 / 2 > grain {
121+
if qty / 2 > grain {
130122
insufficient_grain(grain);
131123
continue;
132124
}
133125
// Enough people to tend the crops?
134-
if qty as u32 > (10 * population) {
126+
if qty > (10 * population) {
135127
insufficient_people(population);
136128
continue;
137129
}
138130
// Everything checks out OK
139-
planted = qty as u32;
131+
planted = qty;
140132
grain -= planted / 2;
141133
break;
134+
} else {
135+
impossible_task();
136+
game_failed = true;
137+
break 'main;
142138
}
143139
}
144140

@@ -217,7 +213,7 @@ fn run() {
217213
println!("\nSO LONG FOR NOW.\n");
218214
}
219215

220-
fn get_input() -> Option<i32> {
216+
fn get_input() -> Option<u32> {
221217
let mut input = String::new();
222218
io::stdin().read_line(&mut input).expect("Failed read_line");
223219
match input.trim().parse() {

0 commit comments

Comments
 (0)