Commit 0adf0e71 authored by DevDoggo's avatar DevDoggo

Started on Assignment 2. Vector for guesses and Get_input function now work.

parent cc2636a2
[[package]]
name = "bitflags"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "colored"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "fuchsia-zircon"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "fuchsia-zircon-sys"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "guessing_game"
version = "0.1.0"
dependencies = [
"colored 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rand"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf"
"checksum colored 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b0aa3473e85a3161b59845d6096b289bb577874cafeaf75ea1b1beaa6572c7fc"
"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
"checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73"
"checksum libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd41f331ac7c5b8ac259b8bf82c75c0fb2e469bbf37d2becbba9a6a2221965b"
"checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5"
"checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[package]
name = "guessing_game"
version = "0.1.0"
authors = ["DevDoggo <devdoggo@protonmail.com>"]
[dependencies]
rand = "0.4.0"
colored = "1.6"
# Rust Guessing Game
### Description
The program is a typical guessing game. You're asked to guess a specific number between 1 and 100, for which the program will tell you it was incorrect or correct. It will also comment on how many times you've guessed, probably with the intention to degrade the user for his failures. The program is a part of the course D7018E.
### Dependencies
#### Crates
* rand
* colored
### How to Build
``` bash
git clone https://github.com/DevDoggo/D7018E-Rust-Course.git
cd D7018E-Rust-Course/a1_guessing_game
cargo build --release
```
### How to run
In the programs directory:
``` bash
cargo run --release
```
extern crate rand;
extern crate colored;
use std::io; //standard in-out
use std::cmp::Ordering;
use rand::Rng;
use std::io::Write; //seemingly needed to use io::stdout().flush()
use colored::*;
fn get_input() -> Result<u32, String> {
//returns io::Result, this has .expect() method
let mut guess = String::new();
print!("\nYour guess is...: ");
io::stdout().flush().unwrap(); //Throws away the result, and grabs the OK if it exists.
io::stdin().read_line(&mut guess);
//parse makes the string into the number type (in this case u32)
//parse() can crash too with strange input. So .expect is here too!
match guess.trim().parse() {
Ok(num) => Ok(num), //Ok(num),
Err(_) => Err(String::from("in parsing u32, ")),
}
}
fn print_guesses(v: &Vec<(u32, String)>) {
println!("Results:\nAttempt | Guess");
for g in v {
println!(" {} | {}", g.0, g.1);
}
}
fn main() {
println!("{}", "\n\nGuess the number or perish. :)".bold());
//Inclusive Lower Bound, Exclusive Upper Bound (1-100 are possible)
let secret_number = rand::thread_rng().gen_range(1, 101);
println!("The secret number is: {}, but don't tell a soul.", secret_number);
//Vector to hold all guesses, first to last.
let mut guesses: Vec<(u32, String)>= Vec::new();
let mut guess_pair: (u32, String);
let mut guess: u32;
let mut attempts = 0;
loop{
match get_input() {
Ok(num) => {
println!("Number {}!", num);
guess = num;
}
Err(error) => {
println!("Error {}!", error);
continue;
}
};
attempts += 1;
guess_pair = (attempts, guess.to_string());
guesses.push(guess_pair);
println!("\nYou guessed: {}!", guess);
print!("That's ");
//with guess, compare to secret_number
match guess.cmp(&secret_number) {
Ordering::Less => {
print!("{}", "too small!".red().bold());
}
Ordering::Greater => {
print!("{}", "too big!".red().bold());
}
Ordering::Equal => {
print!("{}", "the correct number!\n".green().bold());
println!("And you've only guessed {} time(s)?! How?!\n", attempts);
break;
}
}
println!("\nYou've guessed {} time(s) already!", attempts.to_string().yellow())
}
//println!("{:?}", guesses);
print_guesses(&guesses);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment