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]]
name = "lexa-random"
version = "0.1.0"
version = "1.0.0"
dependencies = [
"clap",
"rand",

View File

@ -1,7 +1,8 @@
[package]
name = "lexa-random"
version = "0.1.0"
version = "1.0.0"
edition = "2021"
authors = ["Lexa"]
[dependencies]
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::*;
use clap::Parser;
mod coin;
mod dice;
mod r_numbers;
#[derive(Parser, Debug)]
#[command(version, about, long_about= None)]
struct Args {
/// Number range.
range: i32,
use clap::{Args, Parser, Subcommand};
/// Start of range (non zero)
#[arg(short, long, default_value_t = 0)]
start: i32,
/// Amount of numbers to generate
#[derive(Parser)]
#[command(name = "Lexa's Ultimate Random Engine", version, about = "Lexa's Ultimate Random Engine", long_about= None)]
struct Cli {
/// 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)]
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() {
let args = Args::parse();
simple_rand(args.start, args.range, args.count);
coin_flip_run(args.count);
}
let cli = Cli::parse();
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));
match cli.command {
Command::Coin(args) => {
coin::coin_flip_run(args.count);
}
Command::Flip(args) => {
coin::coin_flip_run(args.count);
}
Command::Num(args) => {
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!("\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 {
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()
}

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!");
}