Merge pull request #897 from lmstolton/hamurabi

Changed get_input() to return Option<u32>
This commit is contained in:
Jeff Atwood
2023-09-04 13:47:50 -07:00
committed by GitHub

View File

@@ -46,28 +46,27 @@ fn run() {
loop { loop {
println!("HOW MANY ACRES DO YOU WISH TO BUY? "); println!("HOW MANY ACRES DO YOU WISH TO BUY? ");
if let Some(qty) = get_input() { if let Some(qty) = get_input() {
if qty < 0 { // A negative amount is impossible
impossible_task();
game_failed = true;
break 'main;
}
// Player decides not to buy any land // Player decides not to buy any land
if qty == 0 { if qty == 0 {
bought_land = false; bought_land = false;
break; break;
} }
// Trying to buy more land than you can afford? // Trying to buy more land than you can afford?
if land_price * qty as u32 > grain { if land_price * qty > grain {
insufficient_grain(grain); insufficient_grain(grain);
continue; continue;
} }
// Everything checks out OK // Everything checks out OK
if land_price * qty as u32 <= grain { if land_price * qty <= grain {
acres += qty as u32; acres += qty ;
grain -= land_price * qty as u32; grain -= land_price * qty ;
bought_land = true; bought_land = true;
break; break;
} }
} else {
impossible_task();
game_failed = true;
break 'main;
} }
} }
@@ -75,19 +74,18 @@ fn run() {
loop { loop {
println!("HOW MANY ACRES DO YOU WISH TO SELL? "); println!("HOW MANY ACRES DO YOU WISH TO SELL? ");
if let Some(qty) = get_input() { if let Some(qty) = get_input() {
if qty < 0 { // A negative amount is impossible
impossible_task();
game_failed = true;
break 'main;
}
// Everything checks out OK // Everything checks out OK
if qty as u32 <= acres { if qty <= acres {
acres -= qty as u32; acres -= qty;
grain += land_price * qty as u32; grain += land_price * qty;
break; break;
} }
// Trying to sell more land that you own // Trying to sell more land that you own
insufficient_land(acres); insufficient_land(acres);
} else {
impossible_task();
game_failed = true;
break 'main;
} }
} }
} }
@@ -95,50 +93,48 @@ fn run() {
loop { loop {
println!("HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? "); println!("HOW MANY BUSHELS DO YOU WISH TO FEED YOUR PEOPLE? ");
if let Some(qty) = get_input() { if let Some(qty) = get_input() {
if qty < 0 { // A negative amount is impossible
impossible_task();
game_failed = true;
break 'main;
}
// Trying to use more grain than is in silos? // Trying to use more grain than is in silos?
if qty as u32 > grain { if qty > grain {
insufficient_grain(grain); insufficient_grain(grain);
continue; continue;
} }
// Everything checks out OK // Everything checks out OK
bushels_fed = qty as u32; bushels_fed = qty;
grain -= bushels_fed; grain -= bushels_fed;
break; break;
} else {
impossible_task();
game_failed = true;
break 'main;
} }
} }
loop { loop {
println!("HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? "); println!("HOW MANY ACRES DO YOU WISH TO PLANT WITH SEED? ");
if let Some(qty) = get_input() { if let Some(qty) = get_input() {
if qty < 0 { // A negative amount is impossible
impossible_task();
game_failed = true;
break 'main;
}
// Trying to plant more acres than you own? // Trying to plant more acres than you own?
if qty as u32 > acres { if qty > acres {
insufficient_land(acres); insufficient_land(acres);
continue; continue;
} }
// Enough grain for seed? // Enough grain for seed?
if qty as u32 / 2 > grain { if qty / 2 > grain {
insufficient_grain(grain); insufficient_grain(grain);
continue; continue;
} }
// Enough people to tend the crops? // Enough people to tend the crops?
if qty as u32 > (10 * population) { if qty > (10 * population) {
insufficient_people(population); insufficient_people(population);
continue; continue;
} }
// Everything checks out OK // Everything checks out OK
planted = qty as u32; planted = qty;
grain -= planted / 2; grain -= planted / 2;
break; break;
} else {
impossible_task();
game_failed = true;
break 'main;
} }
} }
@@ -217,7 +213,7 @@ fn run() {
println!("\nSO LONG FOR NOW.\n"); println!("\nSO LONG FOR NOW.\n");
} }
fn get_input() -> Option<i32> { fn get_input() -> Option<u32> {
let mut input = String::new(); let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed read_line"); io::stdin().read_line(&mut input).expect("Failed read_line");
match input.trim().parse() { match input.trim().parse() {