From e4cef3aa810cc8bcf56d23d2d6d16e599863e1c4 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 24 Oct 2021 19:04:10 +0000 Subject: [PATCH] :hatching_chick: making an adventure game --- .gitignore | 1 + Cargo.lock | 7 ++++ Cargo.toml | 8 +++++ src/main.rs | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..432eb52 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "kajam_10" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3f1d46c --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "kajam_10" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..6b6fa3e --- /dev/null +++ b/src/main.rs @@ -0,0 +1,98 @@ +use std::io::{ + Write, + stdin, + stdout, +}; + +/// As a dare to myself, I won't use any error-handling crates. + +#[derive (Debug)] +enum GameError { + IoError, +} + +impl From for GameError { + fn from (_: std::io::Error) -> Self { + Self::IoError + } +} + +type Result = std::result::Result ; + +/// Prints a string, naively wrapping at 80-character boundaries +/// This means a period might be wrapped separate from the sentence it ends +/// , and frankly I think that's part of the charm. + +fn print (mut s: &str) { + const COLS: usize = 80; + + while s.len () > COLS { + println! ("{}", &s [0..80]); + s = &s [80..]; + } + + println! ("{}", s); +} + +fn read_input () -> Result { + { + let mut stdout = stdout (); + stdout.write_all (b"> ")?; + stdout.flush ()?; + } + + let mut buffer = String::new (); + stdin ().read_line (&mut buffer)?; + + // I don't know why I need the type annotation here, but I do. + let x: &[_] = &['\r', '\n']; + let buffer = buffer.trim_end_matches (x).to_string (); + + Ok (buffer) +} + +#[derive (Debug, PartialEq)] +enum PlayerAction { + Nonsense, +} + +fn parse_input (_s: &str) -> PlayerAction { + PlayerAction::Nonsense +} + +fn main () -> Result <()> { + print ("Welcome to SEROTONIN DEPOSITORY, the only adventure game ever made."); + print (""); + print ("You have been consensually kidnapped by a diabolical ADVENTURE GAME ENTHUSIAST and encouraged to solve PUZZLES for their sick PLEASURE. The only winning move is to solve all the PUZZLES."); + print (""); + print ("Press ENTER if you dare to begin."); + + let input = read_input ()?; + + if ! input.is_empty () { + print ("That was more than just ENTER but OKAY, overachiever."); + } + + print (""); + print ("You are in a small room. In one corner is a TABLE. The door behind you is locked. Obvious exits are a DOOR locked by a KEYPAD, and an EMERGENCY EXIT."); + + let input = read_input ()?; + let _action = parse_input (&input); + + Ok (()) +} + +#[cfg (test)] +mod test { + #[test] + fn parse_input () { + use super::PlayerAction::*; + + for (input, expected) in [ + ("", Nonsense), + ].into_iter () { + let actual = super::parse_input (input); + assert_eq! (actual, expected); + } + } +}