main.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. use std::io;
  2. use std::io::Read;
  3. // Objects
  4. struct Player {
  5. health: u32,
  6. money: u32,
  7. debt: u32,
  8. weed: u32,
  9. location: String,
  10. }
  11. impl Player {
  12. pub fn new(health: u32, money: u32, debt: u32, weed: u32, location: String) -> Self {
  13. Player { health, money, debt, weed, location }
  14. }
  15. pub fn add_weed(&mut self, add_amount: u32) {
  16. self.weed += add_amount;
  17. }
  18. pub fn dump(&mut self) {
  19. println!("Health: {}", self.health);
  20. println!("Money: {}", self.money);
  21. println!("Debt: {}", self.debt);
  22. println!("Weed: {}", self.weed);
  23. println!("Location: {}", self.location);
  24. }
  25. pub fn change_location(&mut self, new_location: String) {
  26. self.location = new_location;
  27. }
  28. }
  29. // "Low-level" Fuctions, called by "Top-level" functions
  30. fn prompt(prompt_text: String) -> io::Result<String> {
  31. println!("$ {}",prompt_text);
  32. let mut input = String::new();
  33. std::io::stdin().read_line(&mut input);
  34. let value: String = input.trim().parse().unwrap();
  35. println!("Got value: {}", value);
  36. Ok(value)
  37. }
  38. // "Top-level" Functions, called by main
  39. fn go_to_location(target_location: String, player: &mut Player) {
  40. let new_location = prompt("Select a new location, locations are\n\tBrooklyn\n\tManhatten".to_string()).unwrap();
  41. player.change_location(new_location);
  42. }
  43. fn main() {
  44. let mut player = Player::new(100,2000,2050,0,"Brooklyn".to_string());
  45. let mut objects = [player];
  46. let mut done = false;
  47. while ! done {
  48. objects[0].dump();
  49. let input = prompt("Enter quit to quit".to_string()).unwrap();
  50. if input == "quit".to_string() {
  51. done = true;
  52. } else if input == "change location".to_string() {
  53. go_to_location(input, &mut objects[0]);
  54. }
  55. }
  56. }