mirror of
https://github.com/sbrow/risk-multilang.git
synced 2025-12-29 16:37:39 -05:00
added
This commit is contained in:
1
rust/.envrc
Normal file
1
rust/.envrc
Normal file
@@ -0,0 +1 @@
|
||||
use nix;
|
||||
1
rust/.gitignore
vendored
Normal file
1
rust/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
target
|
||||
7
rust/Cargo.lock
generated
Normal file
7
rust/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "risk"
|
||||
version = "0.1.0"
|
||||
9
rust/Cargo.toml
Normal file
9
rust/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "risk"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
# ktm5e-dice = "0.1.0"
|
||||
9
rust/default.nix
Normal file
9
rust/default.nix
Normal file
@@ -0,0 +1,9 @@
|
||||
{ pkgs ? import <nixpkgs> {} }:
|
||||
pkgs.mkShell {
|
||||
# nativeBuildInputs is usually what you want -- tools you need to run
|
||||
nativeBuildInputs = with pkgs; [
|
||||
libiconv
|
||||
cargo
|
||||
rustc
|
||||
];
|
||||
}
|
||||
11
rust/src/main.rs
Normal file
11
rust/src/main.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
mod risk;
|
||||
use risk::fraction::Fraction;
|
||||
|
||||
fn main() {
|
||||
for result in risk::ONE_V_ONE {
|
||||
println!("{}", result);
|
||||
}
|
||||
|
||||
let x = Fraction::new(1, 2).multiply(Fraction::new(1, 3));
|
||||
println!("{}", x);
|
||||
}
|
||||
71
rust/src/risk.rs
Normal file
71
rust/src/risk.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
pub mod dice;
|
||||
pub mod fraction;
|
||||
|
||||
use std::fmt;
|
||||
use fraction::Fraction;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Casualties(u8, u8);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Result {
|
||||
casualties: Casualties,
|
||||
probability: Fraction,
|
||||
}
|
||||
|
||||
impl fmt::Display for Result {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
return write!(f, "{:?}: {}", self.casualties, self.probability);
|
||||
}
|
||||
}
|
||||
|
||||
pub const ONE_V_ONE: [Result; 2] = one_v_one();
|
||||
pub const TWO_V_ONE: [Result; 2] = two_v_one();
|
||||
|
||||
const fn one_v_one() -> [Result; 2] {
|
||||
let total = 6 * 6;
|
||||
let mut a: u8 = 0;
|
||||
let mut b: u8 = 0;
|
||||
|
||||
let mut attackers = 1;
|
||||
let mut defenders = 1;
|
||||
|
||||
while attackers <= 6 {
|
||||
while defenders <= 6 {
|
||||
if attackers > defenders {
|
||||
b += 1;
|
||||
} else {
|
||||
a += 1;
|
||||
}
|
||||
defenders += 1;
|
||||
}
|
||||
attackers += 1;
|
||||
defenders = 1;
|
||||
}
|
||||
|
||||
return [
|
||||
// (0, -1)
|
||||
Result{casualties: Casualties(0, 1), probability: Fraction::new(a as u128, total as u128)},
|
||||
// (-1, 0)
|
||||
Result{casualties: Casualties(1, 0), probability: Fraction::new(b as u128, total as u128)},
|
||||
];
|
||||
}
|
||||
|
||||
pub const fn two_v_one() -> [Result; 2] {
|
||||
let total = 216;
|
||||
let mut a: u8 = 0;
|
||||
let mut b: u8 = 0;
|
||||
|
||||
return [
|
||||
// (2, 0)
|
||||
Result{casualties: Casualties(0, 1), probability: Fraction::new(a as u128, total as u128)},
|
||||
// (1, 1)
|
||||
Result{casualties: Casualties(1, 0), probability: Fraction::new(b as u128, total as u128)},
|
||||
];
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn battle(attackers: u8, defenders: u8, probability: f32) -> Vec<Result> {
|
||||
return [].to_vec();
|
||||
}
|
||||
*/
|
||||
43
rust/src/risk/dice.rs
Normal file
43
rust/src/risk/dice.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
pub const fn one_d6() -> [u8; 6] {
|
||||
return [1, 2, 3 , 4, 5, 6];
|
||||
}
|
||||
|
||||
pub const fn two_d6() -> [[u8; 2]; 36] {
|
||||
let mut results = [[0; 2]; 36];
|
||||
let mut x: u8 = 1;
|
||||
let mut y: u8 = 1;
|
||||
|
||||
while x < 7 {
|
||||
while y < 7 {
|
||||
results[((x-1) * 6 + y - 1) as usize] = [x, y];
|
||||
y += 1;
|
||||
}
|
||||
x += 1;
|
||||
y = 1;
|
||||
};
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
pub const fn three_d6() -> [[u8; 3]; 216] {
|
||||
let mut results = [[0; 3]; 216];
|
||||
let mut x: u8 = 1;
|
||||
let mut y: u8 = 1;
|
||||
let mut z: u8 = 1;
|
||||
|
||||
while x < 7 {
|
||||
while y < 7 {
|
||||
while z < 7 {
|
||||
results[((x-1) * 36 + (y-1) * 6 + z - 1) as usize] = [x, y, z];
|
||||
z += 1
|
||||
}
|
||||
y += 1;
|
||||
z = 1;
|
||||
}
|
||||
x += 1;
|
||||
y = 1;
|
||||
z = 1;
|
||||
};
|
||||
|
||||
return results;
|
||||
}
|
||||
27
rust/src/risk/fraction.rs
Normal file
27
rust/src/risk/fraction.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Fraction {
|
||||
numerator: u128,
|
||||
denominator: u128,
|
||||
}
|
||||
|
||||
impl Fraction {
|
||||
pub const fn new(num: u128, denom: u128) -> Fraction {
|
||||
return Fraction{numerator: num, denominator: denom};
|
||||
}
|
||||
|
||||
pub fn percentage(&self) -> f64 {
|
||||
return self.numerator as f64 / self.denominator as f64;
|
||||
}
|
||||
|
||||
pub fn multiply(&self, frac: Fraction) -> Fraction {
|
||||
return Fraction::new(self.numerator * frac.numerator, self.denominator * frac.denominator);
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Fraction {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{} / {}", self.numerator, self.denominator)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user