use std::{ io::{self, Write}, iter, }; use rand::{seq::SliceRandom, thread_rng}; // use rayon::prelude::*; use kartsimrust::{ character::Character, combat::{make_them_fight, EncounterType}, random_character::random_character, stats::Stat, }; fn print_chars<'a, L>(characters: L) where L: IntoIterator, { println!("Name | Classes | HP MP AC DP | STR DEX CON STB | INT KND CMP CHA | Armor | Weapon"); for ch in characters { let mut c = ch.to_owned(); c.init_encounter(EncounterType::Physical); println!( "{name:<8} | {class1:<12} {class2:<12} | {hp:>2} {mp:>2} {ac:>2} {dp:>2} | {str:>3} {dex:>3} {con:>3} {stb:>3} | {int:>3} {knd:>3} {cmp:>3} {cha:>3} | {armor:<6} | {weapon:<15}", name = c.name, class1 = c.class1, class2 = c.class2, hp=c.hp, mp = c.mp, ac=c.ac(), dp = c.dice_pool(), str=c.stat(Stat::STR), dex=c.stat(Stat::DEX), con=c.stat(Stat::CON), stb=c.stat(Stat::STB), int=c.stat(Stat::INT), knd=c.stat(Stat::KND), cmp=c.stat(Stat::CMP), cha=c.stat(Stat::CHA), armor = c.armor, weapon = c.weapon, ); } } fn main() { let mut rng = thread_rng(); let n = 100; let keep = 20; let gen = 50000; let mut characters: Vec = iter::repeat_with(random_character).take(n).collect(); print_chars(characters.iter().take(keep).collect::>()); // print_chars(&characters1); // print_chars(&characters2); for g in 0..gen { characters.shuffle(&mut rng); let teams: (Vec<(usize, Character)>, Vec<(usize, Character)>) = characters .iter() .cloned() .enumerate() .partition(|(i, _)| *i % 2 == 0); let matches: Vec<(Character, Character)> = iter::zip(teams.0.iter(), teams.1.iter()) .map(|((_, c1), (_, c2))| (c1.to_owned(), c2.to_owned())) .collect(); let mut winners: Vec<(i64, Character)> = matches .iter() .map(|(c1, c2)| { let n_rounds = 50; let mut w: i64 = 0; for _i in 0..n_rounds { let mut c1l = c1.to_owned(); let mut c2l = c2.to_owned(); let t = make_them_fight(&mut c1l, &mut c2l); match t { Some(_turns) => { if c2l.hp <= 0 { w += 1 } } None => (), } } if w >= n_rounds / 2 { (-w, c1.to_owned()) } else { (-(n_rounds - w), c2.to_owned()) } }) .collect(); winners.sort_by_key(|(i, _)| *i); characters = winners .iter() .take(keep) .map(|(_, c)| c.clone()) .chain( (0..(n - keep)) .into_iter() .map(|_| random_character()) .take(n - keep), ) .collect(); if g % (gen / 100) == 0 { print!( "\r{:#>()); }