From d11ac0f4f5277c1fce93c9edb371b24efac51aed Mon Sep 17 00:00:00 2001 From: Petr Janik <485122@mail.muni.cz> Date: Mon, 24 Aug 2020 22:41:36 +0200 Subject: [PATCH 1/4] Init adventure game in week 4 --- 04/adventure-game/Cargo.lock | 5 +++++ 04/adventure-game/Cargo.toml | 9 +++++++++ 04/adventure-game/src/main.rs | 3 +++ 3 files changed, 17 insertions(+) create mode 100644 04/adventure-game/Cargo.lock create mode 100644 04/adventure-game/Cargo.toml create mode 100644 04/adventure-game/src/main.rs diff --git a/04/adventure-game/Cargo.lock b/04/adventure-game/Cargo.lock new file mode 100644 index 0000000..c23c547 --- /dev/null +++ b/04/adventure-game/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adventure-game" +version = "0.1.0" diff --git a/04/adventure-game/Cargo.toml b/04/adventure-game/Cargo.toml new file mode 100644 index 0000000..90fa6de --- /dev/null +++ b/04/adventure-game/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "adventure-game" +version = "0.1.0" +authors = ["Petr Janik <485122@mail.muni.cz>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/04/adventure-game/src/main.rs b/04/adventure-game/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/04/adventure-game/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From f348492bb64fad53bb244eb3484d0b3f0315059c Mon Sep 17 00:00:00 2001 From: Petr Janik <485122@mail.muni.cz> Date: Mon, 24 Aug 2020 22:49:31 +0200 Subject: [PATCH 2/4] Add adventure game progress from last week to 04 --- 04/adventure-game/src/main.rs | 3 - .../.run/Run 04_adventure_game.run.xml | 14 +++ .../Cargo.lock | 2 +- .../Cargo.toml | 2 +- 04/adventure_game/src/main.rs | 102 ++++++++++++++++++ 04/adventure_game/story.txt | 20 ++++ 6 files changed, 138 insertions(+), 5 deletions(-) delete mode 100644 04/adventure-game/src/main.rs create mode 100644 04/adventure_game/.run/Run 04_adventure_game.run.xml rename 04/{adventure-game => adventure_game}/Cargo.lock (83%) rename 04/{adventure-game => adventure_game}/Cargo.toml (89%) create mode 100644 04/adventure_game/src/main.rs create mode 100644 04/adventure_game/story.txt diff --git a/04/adventure-game/src/main.rs b/04/adventure-game/src/main.rs deleted file mode 100644 index e7a11a9..0000000 --- a/04/adventure-game/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -} diff --git a/04/adventure_game/.run/Run 04_adventure_game.run.xml b/04/adventure_game/.run/Run 04_adventure_game.run.xml new file mode 100644 index 0000000..6895359 --- /dev/null +++ b/04/adventure_game/.run/Run 04_adventure_game.run.xml @@ -0,0 +1,14 @@ + + + + \ No newline at end of file diff --git a/04/adventure-game/Cargo.lock b/04/adventure_game/Cargo.lock similarity index 83% rename from 04/adventure-game/Cargo.lock rename to 04/adventure_game/Cargo.lock index c23c547..7696895 100644 --- a/04/adventure-game/Cargo.lock +++ b/04/adventure_game/Cargo.lock @@ -1,5 +1,5 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] -name = "adventure-game" +name = "adventure_game" version = "0.1.0" diff --git a/04/adventure-game/Cargo.toml b/04/adventure_game/Cargo.toml similarity index 89% rename from 04/adventure-game/Cargo.toml rename to 04/adventure_game/Cargo.toml index 90fa6de..50ef64a 100644 --- a/04/adventure-game/Cargo.toml +++ b/04/adventure_game/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "adventure-game" +name = "adventure_game" version = "0.1.0" authors = ["Petr Janik <485122@mail.muni.cz>"] edition = "2018" diff --git a/04/adventure_game/src/main.rs b/04/adventure_game/src/main.rs new file mode 100644 index 0000000..6589238 --- /dev/null +++ b/04/adventure_game/src/main.rs @@ -0,0 +1,102 @@ +use std::{env, io}; +use std::collections::HashMap; +use std::fs::File; +use std::io::{BufRead, BufReader, Error}; + +type Choice = (String, u32); + +struct Scene { + story: String, + choices: Vec, +} + +impl Scene { + fn from(story: String, choices: Vec) -> Scene { + Scene { + story, + choices, + } + } +} + +fn main() -> std::io::Result<()> { + let args = get_args(); + + let scenes = parse_scenes(&args)?; + + play(&scenes)?; + + Ok(()) +} + +fn play(scenes: &HashMap) -> Result<(), Error> { + const INITIAL_SCENE_INDEX: u32 = 0; + + let mut current_scene: &Scene = &scenes[&INITIAL_SCENE_INDEX]; + while !current_scene.choices.is_empty() { + println!("{}", current_scene.story); + for (choice, _) in ¤t_scene.choices { + println!("{}", choice); + } + let choice = get_choice_from_user(current_scene.choices.len() as u32)?; + let next_scene: u32 = current_scene.choices[choice as usize - 1].1; + current_scene = &scenes[&next_scene]; + } + println!("{}", current_scene.story); + Ok(()) +} + +pub fn get_choice_from_user(num_of_choices: u32) -> Result { + loop { + println!("What do you choose to do?"); + let mut choice = String::new(); + + io::stdin().read_line(&mut choice)?; + match choice.trim().parse::() { + Ok(val) => { + if val < 1 || val > num_of_choices { + continue; + } + break Ok(val); + } + Err(_) => continue + }; + } +} + +fn parse_scenes(arguments: &[String]) -> Result, Error> { + let input_file = File::open(&arguments[1])?; + let input_reader = BufReader::new(input_file); + + let mut scenes = HashMap::new(); + + let mut lines: Vec = Vec::new(); + for line in input_reader.lines() { + let current_line = line?; + + if current_line.is_empty() { + let id: u32 = lines[0].parse().unwrap(); + let story = String::from(&lines[1]); + let choices = lines.iter().skip(2).map(|line| { + let choice_and_next_scene: Vec<&str> = line.split(';').collect(); + (String::from(choice_and_next_scene[0]), choice_and_next_scene[1].trim().parse().unwrap()) + }).collect(); + let scene = Scene::from( + story, + choices); + scenes.insert(id, scene); + lines.clear(); + } else { + lines.push(current_line); + } + } + Ok(scenes) +} + +fn get_args() -> Vec { + let arguments: Vec<_> = env::args().collect(); + if arguments.len() != 2 { + panic!("Usage: {} ", arguments[0]) + }; + arguments +} \ No newline at end of file diff --git a/04/adventure_game/story.txt b/04/adventure_game/story.txt new file mode 100644 index 0000000..2f5f76a --- /dev/null +++ b/04/adventure_game/story.txt @@ -0,0 +1,20 @@ +0 +The adventure begins! +1) Go left; 1 +2) Go right; 2 + +1 +You see a dragon. +1) Attack him; 3 +2) Pet him; 1 +3) Run away; 4 + +2 +You went right. You win! + +3 +You attacked the dragon. He ate you. Game over! + +4 +Coward! Game over! + From 5ed11aab016071811cf3b3207f87affb6945e2b4 Mon Sep 17 00:00:00 2001 From: Petr Janik <485122@mail.muni.cz> Date: Wed, 26 Aug 2020 15:36:10 +0200 Subject: [PATCH 3/4] Finish implementation of 04 adventure game --- .../.run/Run 04_adventure_game.run.xml | 2 +- 04/adventure_game/Cargo.lock | 159 +++++++++++++++++ 04/adventure_game/Cargo.toml | 5 +- 04/adventure_game/src/main.rs | 162 ++++++++++++------ 04/adventure_game/story.json | 71 ++++++++ 04/adventure_game/story.txt | 20 --- 6 files changed, 347 insertions(+), 72 deletions(-) create mode 100644 04/adventure_game/story.json delete mode 100644 04/adventure_game/story.txt diff --git a/04/adventure_game/.run/Run 04_adventure_game.run.xml b/04/adventure_game/.run/Run 04_adventure_game.run.xml index 6895359..0aa8061 100644 --- a/04/adventure_game/.run/Run 04_adventure_game.run.xml +++ b/04/adventure_game/.run/Run 04_adventure_game.run.xml @@ -1,7 +1,7 @@