Browse Source

Added a bunch of stuff and started cleaning up a few things

Spesk1 4 years ago
parent
commit
b2167cf479
2 changed files with 213 additions and 38 deletions
  1. 5 2
      README.md
  2. 208 36
      src/main.rs

+ 5 - 2
README.md

@@ -4,5 +4,8 @@ My attempt at writing something like the classic DOS game Drug Wars in Rust.
 
 TODO:
 * Add price fluctuation events
-* Get arrested by the cops, lose money/drugs
-* Implement "head stash" to store money/drugs in case of arrest
+* -Get arrested by the cops, lose money/drugs- DONE
+* -Implement "head stash" to store money/drugs in case of arrest- DONE
+
+Bugs:
+* Hitting enter advances the main loop, not intended behavior

+ 208 - 36
src/main.rs

@@ -1,5 +1,4 @@
 use std::io;
-use std::io::Read;
 use rand::Rng;
 use std::{thread, time};
 
@@ -45,9 +44,77 @@ impl Market {
     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 HeadStash {
+    weed: u32,
+    cocaine: u32,
+    heroin: u32,
+    acid: u32,
+    xtc: u32,
+    ludes: u32,
+    money: u32,
 }
 
+impl HeadStash {
+
+    pub fn new(weed: u32, cocaine: u32, heroin: u32, acid: u32, xtc: u32, ludes: u32, money: u32) -> Self {
+        HeadStash { weed, cocaine, heroin, acid, xtc, ludes, money }
+    }
+
+    pub fn dump(&self) {
+        println!("You've got ${} in your head stash",self.money);
+        println!("You're head stash has:\nWeed: {}    Cocaine: {}\nHeroin: {}    Acid: {}\nXTC: {}    Ludes: {}\n", self.weed, self.cocaine, self.heroin, self.acid, self.xtc, self.ludes);
+    }
+
+    pub fn move_drug_to_stash(&mut self, drug_to_move: &String, amount_to_move: &u32) {
+        match drug_to_move.as_ref() {
+            "weed" => self.weed += amount_to_move,
+            "cocaine" => self.cocaine += amount_to_move,
+            "heroin" => self.heroin += amount_to_move,
+            "acid" => self.acid += amount_to_move,
+            "xtc" => self.xtc += amount_to_move,
+            "ludes" => self.ludes += amount_to_move,
+            _ => panic!("move_drug_to_stash got a drug_to_move value that doesn't make sense"),
+        }
+    }
+
+    pub fn grab_from_stash(&mut self, drug_to_grab: &String, amount_to_grab: &u32) {
+        match drug_to_grab.as_ref() {
+            "weed" => self.weed -= amount_to_grab,
+            "cocaine" => self.cocaine -= amount_to_grab,
+            "heroin" => self.heroin -= amount_to_grab,
+            "acid" => self.acid -= amount_to_grab,
+            "xtc" => self.xtc -= amount_to_grab,
+            "ludes" => self.ludes -= amount_to_grab,
+            _ => panic!("sell_transaction got a drug_to_sell value that doesn't make sense"),
+        }
+    }
+
+    pub fn bank_stash(&mut self, amount_to_bank: &u32) {
+        self.money += amount_to_bank;
+    }
+
+    pub fn withdraw_stash(&mut self, amount_to_withdraw: &u32) {
+        self.money -= amount_to_withdraw;
+    }
+
+    pub fn get_head_stash_amount(&self, drug: &String) -> u32 {
+        let amount;
+        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;
+    }
+
+}
 struct Player {
     health: u32,
     money: u32,
@@ -63,6 +130,9 @@ struct Player {
     stash_size: u32,
 }
 
+// TODO
+// A lot of the functions for Player duplicate code, need to make some "generic" functions
+
 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 }
@@ -80,7 +150,7 @@ impl Player {
     }
 
     pub fn get_stash_amount(&self, drug: &String) -> u32 {
-        let mut amount = 0;
+        let amount;
         match drug.as_ref() {
             "weed" => amount = self.weed,
             "cocaine" => amount = self.cocaine,
@@ -99,37 +169,18 @@ impl Player {
         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"),
-        }
+        self.gain_drug(&drug_to_buy,&add_amount);
     }
 
     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.lose_drug(&drug_to_sell,&amount_to_sell);
         self.money += revenue;
     }
 
     
     pub fn debt_interest(&mut self) {
-        let mut interest_amount: u32;
+        let interest_amount: u32;
         match self.debt {
             0 => interest_amount = 0,
             _ => interest_amount = ( self.debt as f32 * 0.2 ) as u32,
@@ -161,10 +212,6 @@ impl Player {
         }
     }
 
-    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;
@@ -200,6 +247,47 @@ impl Player {
         }
     }
 
+    pub fn arrested(&mut self) {
+        self.money = 0;
+        self.weed = 0;
+        self.cocaine = 0;
+        self.heroin = 0;
+        self.xtc = 0;
+        self.acid = 0;
+        self.ludes = 0;
+    }
+
+    pub fn lose_money(&mut self, amount: &u32) {
+        self.money -= amount;
+    }
+
+    pub fn gain_money(&mut self, amount: &u32) {
+        self.money += amount;
+    }
+
+    pub fn lose_drug(&mut self, drug: &String, amount: &u32) {
+        match drug.as_ref() {
+            "weed" => self.weed -= amount,
+            "cocaine" => self.cocaine -= amount,
+            "heroin" => self.heroin -= amount,
+            "acid" => self.acid -= amount,
+            "xtc" => self.xtc -= amount,
+            "ludes" => self.ludes -= amount,
+            _ => panic!("lose_drug got a drug value that doesn't make sense"),
+        }
+    }
+
+    pub fn gain_drug(&mut self, drug: &String, amount: &u32) {
+        match drug.as_ref() {
+            "weed" => self.weed += amount,
+            "cocaine" => self.cocaine += amount,
+            "heroin" => self.heroin += amount,
+            "acid" => self.acid += amount,
+            "xtc" => self.xtc += amount,
+            "ludes" => self.ludes += amount,
+            _ => panic!("lose_drug got a drug value that doesn't make sense"),
+        }
+    }
 }
 
 ////////////////////////////////////////////////////////
@@ -209,7 +297,7 @@ impl Player {
 fn prompt(prompt_text: String) -> io::Result<String> {
     println!("$ {}",prompt_text);
     let mut input = String::new();
-    std::io::stdin().read_line(&mut input);
+    std::io::stdin().read_line(&mut input)?;
 
     let value: String = input.trim().parse().unwrap();
     Ok(value)
@@ -240,10 +328,12 @@ fn get_drug_market_value(drug_to_buy: &String, market: &Market) -> io::Result<u3
 }
 
 // This is maybe stupid? Or could be more idomatic ?? TODO?
+// This honestly might not even be needed? Don't really
+// understand why I made this
 // May should use match inside of let ?
 fn get_drug_as_string(drug: &String) -> io::Result<String> {
 
-    let mut drug_as_string: String = String::new();
+    let mut drug_as_string: String;
     match drug.as_ref() {
         "weed" => drug_as_string = "weed".to_string(),
         "cocaine" => drug_as_string = "cocaine".to_string(),
@@ -317,7 +407,7 @@ fn cops_event(player: &mut Player) {
         let mut done = false;
         while ! done {
             escape = rng.gen_range(0,5);
-            hit = rng.gen_range(0,10);
+            hit = rng.gen_range(0,3);
             if escape == 0 {
                 println!("You got away!");
                 thread::sleep(one_second);
@@ -326,9 +416,14 @@ fn cops_event(player: &mut Player) {
                 println!("You cant get away, the cops are firing!");
                 thread::sleep(one_second);
                 if hit == 0 {
-                    println!("You're hit for 2 damage!");
+                    println!("You're hit for 4 damage!");
                     player.take_damage(4);
                     thread::sleep(one_second);
+                    let arrest = rng.gen_range(0,1);
+                    if arrest == 0 {
+                        println!("The cops got you! They confiscate your cash and stash!");
+                        player.arrested();
+                    }
                 } else {
                     println!("They miss and you keep running!");
                     thread::sleep(one_second);
@@ -421,7 +516,7 @@ fn sell_drugs(player: &mut Player, market: &mut Market) {
     }
 
     let mut done1 = false;
-    let mut amount_to_sell: String = String::new();
+    let mut amount_to_sell: String;
     while ! done1 {
         amount_to_sell = prompt("How many to sell?".to_string()).unwrap();
         let amount_to_sell_int: u32 = amount_to_sell.parse().unwrap();
@@ -468,6 +563,80 @@ fn loan_shark(player: &mut Player) {
     }
 }
 
+fn head_stash_op(player: &mut Player, head_stash: &mut HeadStash) {
+
+    if player.location != "Brooklyn".to_string() {
+        println!("Your head stash is in Brooklyn");
+    } else {
+        let mut head_stash_done = false;
+        while ! head_stash_done {
+            let action = prompt("What do you want to do? (bank,withdraw)".to_string()).unwrap();
+            if action.contains("bank") {
+                let bank_type = prompt("What do you want to bank? (cash,drugs)".to_string()).unwrap();
+                if bank_type.contains("cash") {
+                    let bank_amount: u32 = prompt("How much do you want to bank?".to_string()).unwrap().parse().unwrap();
+                    if bank_amount > player.money {
+                        println!("You don't have that much money on you!");
+                    } else {
+                        head_stash.bank_stash(&bank_amount);
+                        player.lose_money(&bank_amount);
+                        head_stash_done = true;
+                    }
+                }
+
+                if bank_type.contains("drugs") {
+                    let mut drug_type = prompt("What drug to you want to stash?".to_string()).unwrap();
+                    drug_type = get_drug_as_string(&drug_type).unwrap();
+                    if drug_type != "null".to_string() {
+                        let stash_amount: u32 = prompt("How much do you want to stash?".to_string()).unwrap().parse().unwrap();
+                        if stash_amount > player.get_stash_amount(&drug_type) {
+                            println!("You don't have that much in your stash!");
+                        } else {
+                            head_stash.move_drug_to_stash(&drug_type,&stash_amount);
+                            player.lose_drug(&drug_type,&stash_amount);
+                            head_stash_done = true;
+                        }
+                    } else {
+                        println!("Try again");
+                    }
+                }
+            }
+
+            if action.contains("withdraw") {
+                let withdraw_type = prompt("What do you want to withdraw? (cash,drugs)".to_string()).unwrap();
+                if withdraw_type.contains("cash") {
+                    let withdraw_amount: u32 = prompt("How much do you want to withdraw?".to_string()).unwrap().parse().unwrap();
+                    if withdraw_amount > head_stash.money {
+                        println!("You don't have that much money in the stash!");
+                    } else {
+                        head_stash.withdraw_stash(&withdraw_amount);
+                        player.gain_money(&withdraw_amount);
+                        head_stash_done = true;
+                    }
+                }
+
+                if withdraw_type.contains("drugs") {
+                    let mut drug_type = prompt("What drug to you want to withdraw?".to_string()).unwrap();
+                    drug_type = get_drug_as_string(&drug_type).unwrap();
+                    if drug_type != "null".to_string() {
+                        let withdraw_amount: u32 = prompt("How much do you want to stash?".to_string()).unwrap().parse().unwrap();
+                        if withdraw_amount > head_stash.get_head_stash_amount(&drug_type) {
+                            println!("You dont have that much {} in your stash!",&drug_type);
+                        } else {
+                            head_stash.grab_from_stash(&drug_type,&withdraw_amount);
+                            player.gain_drug(&drug_type,&withdraw_amount);
+                            head_stash_done = true;
+                        }
+                    } else {
+                        println!("Try again");
+                    }
+                }
+            }
+        }
+    }
+}
+
+
 ///////
 // Main
 ///////
@@ -475,8 +644,8 @@ fn loan_shark(player: &mut Player) {
 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);
+    let mut head_stash = HeadStash::new(0,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();
@@ -518,10 +687,11 @@ fn main() {
                 }
 
                 player.dump();
+                head_stash.dump();
                 market.dump();
 
                 if player.location == "Brooklyn".to_string() {
-                    println!("You're currently in {}, what will you do? (buy,sell,jet,loanshark)",player.location);
+                    println!("You're currently in {}, what will you do? (buy,sell,jet,loanshark,headstash)",player.location);
                 } else {
                     println!("You're currently in {}, what will you do? (buy,sell,jet)",player.location);
                 }
@@ -536,7 +706,9 @@ fn main() {
                     player.debt_interest();
                     player.decrease_loan_timer(1);
                 } else if action.contains("loanshark") {
-                        loan_shark(&mut player);
+                    loan_shark(&mut player);
+                } else if action.contains("headstash") {
+                    head_stash_op(&mut player, &mut head_stash);
                 } else if action == "quit".to_string() {
                     std::process::exit(0);
                 }