use std::io; use std::io::Read; use rand::Rng; use std::{thread, time}; ////////// // Objects ////////// struct Market { // Store current values of drugs on the market weed: u32, cocaine: u32, heroin: u32, acid: u32, xtc: u32, ludes: u32, } impl Market { pub fn new(weed: u32, cocaine: u32, heroin: u32, acid: u32, xtc: u32, ludes: u32) -> Self { Market { weed, cocaine, heroin, acid, xtc, ludes } } // This is normal market fluctuation when moving locations, as opposed to an event that drives prices up/down pub fn change_prices(&mut self) { let mut rng = rand::thread_rng(); let weed_diff = rng.gen_range(90,600); let cocaine_diff = rng.gen_range(19000,40000); let heroin_diff = rng.gen_range(7000,20000); let acid_diff = rng.gen_range(300,1500); let xtc_diff = rng.gen_range(50,500); let ludes_diff = rng.gen_range(15,190); self.weed = weed_diff; self.cocaine = cocaine_diff; self.heroin = heroin_diff; self.acid = acid_diff; self.xtc = xtc_diff; self.ludes = ludes_diff; } pub fn dump(&self) { println!("Prices\nWeed: ${}\tCocaine: ${}\nHeroin: ${}\tAcid: ${}\nXTC: ${}\tLudes: ${}\n", self.weed, self.cocaine, self.heroin, self.acid, self.xtc, self.ludes); } } struct Player { health: u32, money: u32, debt: u32, weed: u32, cocaine: u32, heroin: u32, acid: u32, xtc: u32, ludes: u32, location: String, loan_timer: u32, stash_size: u32, } impl Player { pub fn new(health: u32, money: u32, debt: u32, weed: u32, cocaine: u32, heroin: u32, acid: u32, xtc: u32, ludes: u32, location: String, loan_timer: u32, stash_size: u32 ) -> Self { Player { health, money, debt, weed, cocaine, heroin, acid, xtc, ludes, location, loan_timer, stash_size } } pub fn dump(&self) { println!("Health: {}\nMoney: ${}\tDebt: ${}", self.health, self.money, self.debt); println!("You're in {}\n", self.location); println!("You can hold {} units in your stash",self.stash_size); println!("You're holding:\nWeed: {} Cocaine: {}\nHeroin: {} Acid: {}\nXTC: {} Ludes: {}\n", self.weed, self.cocaine, self.heroin, self.acid, self.xtc, self.ludes); } pub fn change_location(&mut self, new_location: String) { self.location = new_location; } pub fn get_stash_amount(&self, drug: &String) -> u32 { let mut amount = 0; match drug.as_ref() { "weed" => amount = self.weed, "cocaine" => amount = self.cocaine, "heroin" => amount = self.heroin, "acid" => amount = self.acid, "xtc" => amount = self.xtc, "ludes" => amount = self.ludes, _ => amount = 0, } return amount; } pub fn buy_transaction(&mut self, drug_to_buy: &String, amount_to_buy: &u32, market_price: &u32) { let cost = market_price * amount_to_buy; self.money -= cost; let add_amount = amount_to_buy; match drug_to_buy.as_ref() { "weed" => self.weed += add_amount, "cocaine" => self.cocaine += add_amount, "heroin" => self.heroin += add_amount, "acid" => self.acid += add_amount, "xtc" => self.xtc += add_amount, "ludes" => self.ludes += add_amount, _ => panic!("buy_transaction got a drug_to_buy value that doesn't make sense"), } } pub fn sell_transaction(&mut self, drug_to_sell: &String, amount_to_sell: &u32, market_price: &u32) { let revenue = market_price * amount_to_sell; match drug_to_sell.as_ref() { "weed" => self.weed -= amount_to_sell, "cocaine" => self.cocaine -= amount_to_sell, "heroin" => self.heroin -= amount_to_sell, "acid" => self.acid -= amount_to_sell, "xtc" => self.xtc -= amount_to_sell, "ludes" => self.ludes -= amount_to_sell, _ => panic!("sell_transaction got a drug_to_sell value that doesn't make sense"), } self.money += revenue; } pub fn debt_interest(&mut self) { let mut interest_amount: u32; match self.debt { 0 => interest_amount = 0, _ => interest_amount = ( self.debt as f32 * 0.2 ) as u32, } self.debt += interest_amount; } pub fn remove_debt(&mut self, payback_amount: u32) { if (self.debt as i32 - payback_amount as i32) < 0 { self.money -= payback_amount; self.debt = 0; } else { self.money -= payback_amount; self.debt -= payback_amount; } } pub fn borrow_from_shark(&mut self, borrow_amount: u32) { self.money += borrow_amount; self.debt += borrow_amount; } pub fn take_damage(&mut self, damage_amount: u32) { if (self.health as i32 - damage_amount as i32) < 0 { self.health = 0; } else { self.health -= damage_amount; } } pub fn take_heal(&mut self, heal_amount: u32) { self.health += heal_amount; } pub fn decrease_loan_timer(&mut self, timer_amount: u32) { if (self.loan_timer as i32 - timer_amount as i32) < 0 { self.loan_timer = 0; } else { self.loan_timer -= timer_amount; } } pub fn increase_loan_timer(&mut self, timer_amount: u32) { self.loan_timer += timer_amount; } pub fn buy_trenchcoat(&mut self, cost: u32) { self.stash_size += 25; self.money -= cost; } pub fn buy_body_armor(&mut self, cost: u32) { self.health += 2; self.money -= cost; } pub fn stash_fill(&self) -> u32 { let stash_fill = self.weed + self.cocaine + self.heroin + self.xtc + self.acid + self.ludes; return stash_fill; } } //////////////////////////////////////////////////////// // "Low-level" Fuctions, called by "Top-level" functions //////////////////////////////////////////////////////// fn prompt(prompt_text: String) -> io::Result { println!("$ {}",prompt_text); let mut input = String::new(); std::io::stdin().read_line(&mut input); let value: String = input.trim().parse().unwrap(); Ok(value) } // This hurts portability but works for now fn clear_term() { print!("{}[2J", 27 as char); } fn get_drug_market_value(drug_to_buy: &String, market: &Market) -> io::Result { let market_value: u32; match drug_to_buy.as_ref() { "weed" => market_value = market.weed, "cocaine" => market_value = market.cocaine, "heroin" => market_value = market.heroin, "acid" => market_value = market.acid, "xtc" => market_value = market.xtc, "ludes" => market_value = market.ludes, _ => panic!("get_drug_market_value got a drug_to_buy value that doesn't make sense!"), } Ok(market_value) } // This is maybe stupid? Or could be more idomatic ?? TODO? // May should use match inside of let ? fn get_drug_as_string(drug: &String) -> io::Result { let mut drug_as_string: String = String::new(); match drug.as_ref() { "weed" => drug_as_string = "weed".to_string(), "cocaine" => drug_as_string = "cocaine".to_string(), "heroin" => drug_as_string = "heroin".to_string(), "acid" => drug_as_string = "acid".to_string(), "xtc" => drug_as_string = "xtc".to_string(), "ludes" => drug_as_string = "ludes".to_string(), _ => drug_as_string = "null".to_string(), } Ok(drug_as_string) } //////////////////////////////////// // Events -- happen randomly in main //////////////////////////////////// fn trenchcoat_event(player: &mut Player) { let mut relative_cost = ( player.money as f64 * 0.25 ) as u32; if relative_cost < 200 { relative_cost = 200; } println!("A guy is selling his trench count, it'll allow you to carry 25 more units, want to buy it for ${} ?", relative_cost); println!("You have ${}", player.money); let response = prompt("Yes/no?".to_string()).unwrap(); if response.contains("yes") { if player.money < relative_cost { println!("You cant afford this!"); } else { player.buy_trenchcoat(relative_cost); } } } fn body_armor_event(player: &mut Player) { let mut relative_cost = ( player.money as f64 * 0.3 ) as u32; if relative_cost < 300 { relative_cost = 300; } println!("A guy is selling some body armor, it'll increase your health by 2, want to buy it for ${} ?", relative_cost); println!("You have ${}", player.money); let response = prompt("Yes/no?".to_string()).unwrap(); if response.contains("yes") { if player.money < relative_cost { println!("You cant afford this!"); } else { player.buy_body_armor(relative_cost); } } } fn cops_event(player: &mut Player) { let one_second = time::Duration::from_secs(1); println!("The cops found you! Run!"); thread::sleep(one_second); let mut rng = rand::thread_rng(); let mut escape: u32; let mut hit: u32; let mut done = false; while ! done { escape = rng.gen_range(0,5); hit = rng.gen_range(0,10); if escape == 0 { println!("You got away!"); thread::sleep(one_second); done = true; } else { println!("You cant get away, the cops are firing!"); thread::sleep(one_second); if hit == 0 { println!("You're hit for 2 damage!"); player.take_damage(2); thread::sleep(one_second); } else { println!("They miss and you keep running!"); thread::sleep(one_second); } } } } //////////////////////////////////////// // "Top-level" Functions, called by main //////////////////////////////////////// fn go_to_location(player: &mut Player, market: &mut Market) { let mut loc_loop = false; while ! loc_loop { let mut new_location = prompt("Select a new location, locations are\nBrooklyn Midtown Harlem CentralPark".to_string()).unwrap(); match new_location.as_ref() { "Brooklyn" => println!("Going to Brooklyn"), "Midtown" => println!("Going to Midtown"), "Harlem" => println!("Going to Harlem"), "CentralPark" => println!("Going to CentralPark"), _ => new_location = "null".to_string(), } if new_location != "null".to_string() { if new_location == player.location { println!("Youre already in {}", player.location); } else { player.change_location(new_location); market.change_prices(); loc_loop = true; } } else { println!("Not a valid location, try again"); } } } fn buy_drugs(player: &mut Player, market: &mut Market) { let mut done = false; let mut drug_to_buy: String = String::new(); while ! done { drug_to_buy = prompt("What drug will you buy?".to_string()).unwrap(); drug_to_buy = get_drug_as_string(&drug_to_buy).unwrap(); if drug_to_buy != "null".to_string() { done = true; } else { println!("Try again"); } } let mut done1 = false; while ! done1 { let drug_market_value: u32 = get_drug_market_value(&drug_to_buy, &market).unwrap(); let affordable_amount: u32 = (player.money / drug_market_value) as u32; println!("You can afford {} {}", affordable_amount, drug_to_buy); let amount_to_buy = prompt("How many will you buy?".to_string()).unwrap(); let amount_to_buy_int: u32 = amount_to_buy.parse().unwrap(); let current_stash_fill = player.stash_fill(); if amount_to_buy_int > affordable_amount { println!("You cant afford that many"); } else if amount_to_buy_int > player.stash_size { println!("You cant hold that much!"); } else if (amount_to_buy_int + current_stash_fill) > player.stash_size { println!("You cant hold that much!"); } else { println!("Buying {} {}", amount_to_buy_int, drug_to_buy); player.buy_transaction(&drug_to_buy,&amount_to_buy_int,&drug_market_value); done1 = true; } } } fn sell_drugs(player: &mut Player, market: &mut Market) { let mut done = false; let mut drug_to_sell: String = String::new(); while ! done { drug_to_sell = prompt("What drug will you sell?".to_string()).unwrap(); drug_to_sell = get_drug_as_string(&drug_to_sell).unwrap(); if drug_to_sell != "null".to_string() { done = true; } else { println!("Try again"); } } let mut done1 = false; let mut amount_to_sell: String = String::new(); while ! done1 { amount_to_sell = prompt("How many to sell?".to_string()).unwrap(); let amount_to_sell_int: u32 = amount_to_sell.parse().unwrap(); let drug_market_value: u32 = get_drug_market_value(&drug_to_sell, &market).unwrap(); let amount_possible_to_sell: u32 = player.get_stash_amount(&drug_to_sell); if amount_to_sell_int > amount_possible_to_sell { println!("You don't have that many {}, try again", drug_to_sell); } else { println!("Selling {} {}", amount_to_sell_int, drug_to_sell); player.sell_transaction(&drug_to_sell,&amount_to_sell_int,&drug_market_value); done1 = true; } } } fn loan_shark(player: &mut Player) { if player.location != "Brooklyn".to_string() { println!("Can only talk to the loanshark in Brooklyn"); } else { let mut loan_done = false; while ! loan_done { let action = prompt("What do you want to do? (borrow,repay)".to_string()).unwrap(); if action.contains("repay") { let payback_amount: u32 = prompt("How much to pay back?".to_string()).unwrap().parse().unwrap(); if payback_amount > player.money { println!("You dont have that much money"); } else { player.remove_debt(payback_amount); println!("You now owe the loanshark: ${}", player.debt); loan_done = true; } } else if action.contains("borrow") { let borrow_amount = prompt("How much do you want to borrow? (Limit 20000)".to_string()).unwrap().parse().unwrap(); if borrow_amount > 20000 { println!("You cant borrow that much"); } else { player.borrow_from_shark(borrow_amount); loan_done = true; } } } } } /////// // Main /////// fn main() { let mut player = Player::new(10,2000,2050,0,0,0,0,0,0,"Brooklyn".to_string(),10,50); let mut market = Market::new(0,0,0,0,0,0); Market::change_prices(&mut market); // let mut objects = [player,market]; let mut done = false; while ! done { let input = prompt("Type start, quit, or help".to_string()).unwrap(); if input == "quit".to_string() { done = true; } else if input == "start".to_string() { println!("Starting the game"); let mut main_loop = false; while ! main_loop { clear_term(); let mut rng = rand::thread_rng(); let event_chance = rng.gen_range(0,9); println!("Event chance is: {}",event_chance); if event_chance == 2 { trenchcoat_event(&mut player); } if event_chance == 1 { body_armor_event(&mut player); } if event_chance == 0 { cops_event(&mut player); } if player.health == 0 { println!("You died! Game over!"); main_loop = true; } if player.debt != 0 { if player.loan_timer == 0 { println!("** The loan shark wants his money! He beats you to a pulp to remind you! **"); player.take_damage(2); player.increase_loan_timer(5); println!("** You take 2 damage! **"); } } player.dump(); market.dump(); if player.location == "Brooklyn".to_string() { println!("You're currently in {}, what will you do? (buy,sell,jet,loanshark)",player.location); } else { println!("You're currently in {}, what will you do? (buy,sell,jet)",player.location); } let action = prompt("".to_string()).unwrap(); if action.contains("buy") { buy_drugs(&mut player, &mut market); } else if action.contains("sell") { sell_drugs(&mut player, &mut market); } else if action.contains("jet") { go_to_location(&mut player, &mut market); player.debt_interest(); player.decrease_loan_timer(1); } else if action.contains("loanshark") { loan_shark(&mut player); } else if action == "quit".to_string() { std::process::exit(0); } } } } }