Skip to content

Instantly share code, notes, and snippets.

@paulosuzart
Last active June 13, 2019 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulosuzart/968d86ce9856ef275531031d2d11add1 to your computer and use it in GitHub Desktop.
Save paulosuzart/968d86ce9856ef275531031d2d11add1 to your computer and use it in GitHub Desktop.
book guess game modified a bit for fun
use std::cmp::Ordering;
use std::io;
use rand::Rng;
fn main() {
println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1, 101);
let mut attempts : Vec<u32> = Vec::new();
loop {
println!("Please input your guess.");
let mut buff = String::new();
match io::stdin().read_line(&mut buff) {
Ok(_byes_read) => {
println!("You typed {}", buff);
}
Err(_) => continue,
};
let guess: u32 = match buff.trim().parse::<u32>() {
Ok(num) => num,
Err(_) => {
println!("Ooops! I can read only numbers. Try again please.");
continue;},
};
attempts.push(guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win! after {} attempts. Here they are:", attempts.len());
println!("{:?}", attempts);
break;
}
};
}
}
extern crate actix_web;
use actix_web::{web, App, HttpServer};
use rand::Rng;
use std::cell::Cell;
use std::cmp::Ordering;
use std::io;
struct AppState {
guess: Cell<u32>,
}
fn index(guess: web::Path<u32>, state: web::Data<AppState>) -> &'static str {
match guess.as_ref().cmp(&state.guess.get()) {
Ordering::Less => "Too small!",
Ordering::Greater => "Too big!",
Ordering::Equal => {
state.guess.set(get_secret_number());
"You won!"
}
}
}
fn get_secret_number() -> u32 {
rand::thread_rng().gen_range(1, 101)
}
fn main() -> io::Result<()> {
let app = move || {
let state = web::Data::new(AppState {
guess: Cell::new(get_secret_number()),
});
App::new()
.register_data(state)
.service(web::resource("/guess/{guess}").to(index))
};
HttpServer::new(app).bind("localhost:8088")?.run()
}
extern crate actix_web;
use actix_session::{CookieSession, Session};
use actix_web::{web, App, Error, HttpServer};
use rand::Rng;
use std::cell::Cell;
use std::cmp::Ordering;
use std::io;
struct AppState {
guess: Cell<u32>,
}
fn update_attempts(session: &Session, guess: u32) -> Result<Vec<u32>, Error> {
let session_attempts = if let Some(mut attempts) = session.get::<Vec<u32>>("attempts")? {
attempts.push(guess);
session.set("attempts", attempts.clone())?;
attempts
} else {
let attempts = vec![guess];
session.set("attempts", attempts.clone())?;
attempts
};
Ok(session_attempts)
}
fn index(
guess: web::Path<u32>,
state: web::Data<AppState>,
session: Session,
) -> Result<String, Error> {
let session_attempts = update_attempts(&session, *guess)?;
let result = match guess.as_ref().cmp(&state.guess.get()) {
Ordering::Less => format!("{} is Too small!", guess),
Ordering::Greater => format!("{} is Too big!", guess),
Ordering::Equal => {
state.guess.set(get_secret_number());
session.clear();
format!(
"That's it, {} is the secret number. All attempts: {:?}",
guess, session_attempts
)
}
};
Ok(result)
}
fn get_secret_number() -> u32 {
rand::thread_rng().gen_range(1, 101)
}
fn main() -> io::Result<()> {
let app = move || {
let state = web::Data::new(AppState {
guess: Cell::new(get_secret_number()),
});
App::new()
.wrap(CookieSession::signed(&[0; 32]).secure(false))
.register_data(state)
.service(web::resource("/guess/{guess}").route(web::get().to(index)))
};
HttpServer::new(app).bind("localhost:8088")?.run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment