V1.0.0 - Cli fully implemented

This commit is contained in:
Lexa 2024-10-07 22:26:33 +01:00
parent 7736118275
commit af6e5087fd
6 changed files with 130 additions and 44 deletions

2
Cargo.lock generated
View File

@ -134,7 +134,7 @@ checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]] [[package]]
name = "lexa-random" name = "lexa-random"
version = "0.1.0" version = "1.0.0"
dependencies = [ dependencies = [
"clap", "clap",
"rand", "rand",

View File

@ -1,7 +1,8 @@
[package] [package]
name = "lexa-random" name = "lexa-random"
version = "0.1.0" version = "1.0.0"
edition = "2021" edition = "2021"
authors = ["Lexa"]
[dependencies] [dependencies]
clap = { version = "4.5.19", features = ["derive"] } clap = { version = "4.5.19", features = ["derive"] }

24
src/coin.rs Normal file
View File

@ -0,0 +1,24 @@
use rand::{thread_rng, Rng};
pub fn coin_flip_run(count: u32) {
if count < 2 {
if coin_flip() {
println!("Flipped a coin, it landed on: Heads!");
} else {
println!("Flipped a coin, it landed on: Tails!");
}
} else {
let mut heads_count : u32 = 0;
for _ in 0..count {
if coin_flip() {
heads_count += 1;
}
}
println!("Flipped {} coins\nHeads: {}\tTails: {}", count, heads_count, count - heads_count);
}
}
fn coin_flip() -> bool {
let mut rng = thread_rng();
rng.gen()
}

19
src/dice.rs Normal file
View File

@ -0,0 +1,19 @@
use rand::{thread_rng, Rng};
pub fn roll_dice(sides: u32, count: u32) {
if count < 2 {
println!("You rolled a {}!\n(with a {} sided dice)", roll(sides), sides);
} else {
println!("You've rolled {}, {} sided dice", count, sides);
print!("Results: ");
for _ in 0..count {
print!("{}, ", roll(sides));
}
println!();
}
}
fn roll(sides: u32) -> u32{
let mut rng = thread_rng();
rng.gen_range(1..sides)
}

View File

@ -1,54 +1,85 @@
use rand::prelude::*; mod coin;
use clap::Parser; mod dice;
mod r_numbers;
#[derive(Parser, Debug)] use clap::{Args, Parser, Subcommand};
#[command(version, about, long_about= None)]
struct Args {
/// Number range.
range: i32,
/// Start of range (non zero) #[derive(Parser)]
#[arg(short, long, default_value_t = 0)] #[command(name = "Lexa's Ultimate Random Engine", version, about = "Lexa's Ultimate Random Engine", long_about= None)]
start: i32, struct Cli {
/// Amount of numbers to generate /// Command to generate random for.
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
/// Flip coins.
Coin(CoinArgs),
/// Alias of "coin"
Flip(CoinArgs),
/// Roll dice
Dice(DiceArgs),
/// Alias of "dice"
Roll(DiceArgs),
/// Generate random numbers
Num(NumArgs),
/// Alias of "num"
Number(NumArgs),
}
#[derive(Args)]
struct CoinArgs {
/// Amount of coins to flip
#[arg(short, long, default_value_t = 1)] #[arg(short, long, default_value_t = 1)]
count: u32, count: u32,
} }
#[derive(Args)]
struct DiceArgs {
/// Amount of dice to roll
#[arg(short, long, default_value_t = 1)]
count: u32,
/// Number of sides per die
#[arg(short, long, default_value_t = 6)]
sides: u32,
}
#[derive(Args)]
struct NumArgs {
/// Amount of numbers to generate
#[arg(short, long, default_value_t = 1)]
count: u32,
/// Lowest number
#[arg(short, long, default_value_t = 1)]
low: i32,
/// Highest number
#[arg(short, long, default_value_t = 100)]
high: i32,
}
fn main() { fn main() {
let args = Args::parse(); let cli = Cli::parse();
simple_rand(args.start, args.range, args.count);
coin_flip_run(args.count);
}
fn simple_rand(range_start: i32, range: i32, count: u32) { match cli.command {
println!("Generating {} random integers between: {} and {}", count, range_start, range); Command::Coin(args) => {
let mut rng = thread_rng(); coin::coin_flip_run(args.count);
for _ in 0..count {
print!("{}, ", rng.gen_range(range_start..range));
}
println!("\nFinished!");
}
fn coin_flip_run(count: u32) {
if count < 2 {
if coin_flip() {
println!("Flipped a coin, it landed on: Heads!");
} else {
println!("Flipped a coin, it landed on: Tails!");
} }
} else { Command::Flip(args) => {
let mut heads_count : u32 = 0; coin::coin_flip_run(args.count);
for _ in 0..count { }
if coin_flip() { Command::Num(args) => {
heads_count += 1; r_numbers::simple_rand(args.low, args.high, args.count);
} }
Command::Number(args) => {
r_numbers::simple_rand(args.low, args.high, args.count);
}
Command::Dice(args) => {
dice::roll_dice(args.sides, args.count);
}
Command::Roll(args) => {
dice::roll_dice(args.sides, args.count);
} }
println!("Flipped {} coins\nHeads: {}\tTails: {}", count, heads_count, count - heads_count);
} }
}
fn coin_flip() -> bool {
let mut rng = thread_rng();
rng.gen()
} }

11
src/r_numbers.rs Normal file
View File

@ -0,0 +1,11 @@
use rand::{thread_rng, Rng};
pub fn simple_rand(range_start: i32, range: i32, count: u32) {
println!("Generating {} random integers between: {} and {}", count, range_start, range);
let mut rng = thread_rng();
for _ in 0..count {
print!("{}, ", rng.gen_range(range_start..range));
}
println!("\nFinished!");
}