Skip to content

Commit

Permalink
New structure (#11)
Browse files Browse the repository at this point in the history
* Small tweaks

* Removed the bad greedy programs

Added one new and kept the bad programs spirit by creating random-choosing, similar programs

* Copy programs

Some programs that copy the opponents last move

* Separate files for different types of programs

* A tab

* Updated documentation (in 1 spot) to fit new structure

* Score calculation is provided as a function to programs
  • Loading branch information
wilzet authored Jan 17, 2024
1 parent 90bc862 commit eca6dd2
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 302 deletions.
17 changes: 10 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Config {
}

if value > MAX_THREADS {
return Err(concat!("Value must be less than or equal to", MAX_THREADS_STRING, " for argument: --threads"));
return Err(concat!("Value must be less than or equal to ", MAX_THREADS_STRING, " for argument: --threads"));
}

return Err("Value must be greater than 0 for argument: --threads");
Expand Down Expand Up @@ -135,8 +135,8 @@ impl Config {
///
/// * `--min <u32>` - The minimum amount of rounds
/// * `--max <u32>` - The maximum amount of rounds
/// * `--games` - Displays all games outcomes if this is provided
/// * `--threads` - Specify the amount of threads used
/// * `--games` - Displays all the games outcomes if this is provided
/// * `--threads <u32>` - Specify the amount of threads used
///
/// If only `--min` is provided, the config will have `rounds == --min`.
/// Likewise if only `--max` is provided, the config will have `rounds == --max`.
Expand Down Expand Up @@ -182,7 +182,7 @@ impl Config {
Config {
rounds: random_rounds(MIN_ROUNDS, MAX_ROUNDS),
show_games: false,
threadpool: ThreadPool::with_name("Games".into(), 20),
threadpool: ThreadPool::with_name("Games".into(), DEFAULT_THREADS),
}
}

Expand Down Expand Up @@ -292,7 +292,7 @@ fn random_rounds(min: u32, max: u32) -> u32 {
/// # Examples
///
/// ```
/// # use tourney::programs::*;
/// # use tourney::programs::all::*;
/// use tourney::config::*;
/// use tourney::game::Player;
///
Expand Down Expand Up @@ -348,7 +348,10 @@ pub fn run<'a>(config: &Config, players: &'a Vec<Player>) -> Result<Vec<(i32, &'
#[cfg(test)]
mod tests {
use super::*;
use crate::programs::*;
use crate::programs::{
prisoners::*,
greedy::*,
};

#[test]
fn config_test() {
Expand Down Expand Up @@ -402,4 +405,4 @@ mod tests {
assert!(random_rounds(10, 10) == 10);
assert!(random_rounds(10, 11) < 12);
}
}
}
23 changes: 22 additions & 1 deletion src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,25 @@ pub fn play(player_1: Player, player_2: Player, rounds: u32) -> (i32, i32) {
}
}

pub fn calculate_scores(last_moves: &[Move]) -> (i32, i32) {
last_moves.iter()
.fold((0, 0), |acc, m| {
match m {
(Color::Red, Color::Red) => (acc.0 + 1, acc.1 + 1),
(Color::Red, Color::Green) => (acc.0 + 3, acc.1),
(Color::Green, Color::Red) => (acc.0, acc.1 + 3),
(Color::Green, Color::Green) => (acc.0 + 2, acc.1 + 2),
(Color::Blue, Color::Blue) => acc,
(Color::Blue, _) => (acc.0 - 1, acc.1 + 1),
(_, Color::Blue) => (acc.0 + 1, acc.1 - 1),
}
})
}

#[cfg(test)]
mod tests {
use super::*;
use crate::programs::greedy_blue_and_friendly;
use crate::programs::greedy::greedy_blue_and_friendly;

fn test_strategy(last_moves: &[Move]) -> Color {
if let Some(last_move) = last_moves.last() {
Expand Down Expand Up @@ -262,4 +277,10 @@ mod tests {
assert_eq!(play(p_1.clone(), p_1.clone(), 100), (149, 149));
assert_ne!(p_1.get_name(), p_2.get_name());
}

#[test]
fn score_calculation_test() {
assert_eq!(calculate_scores(&[]), (0, 0));
assert_eq!(calculate_scores(&[(Color::Green, Color::Green), (Color::Blue, Color::Red)]), (1, 3));
}
}
11 changes: 6 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{env, process};
use tourney::config::*;
use tourney::programs::*;
use tourney::programs::all::*;
use tourney::game::Player;

fn main() {
Expand All @@ -26,13 +26,14 @@ fn main() {
Player::with_name("Greedy blue", blue),
Player::with_name("Try to guess", try_to_guess),
Player::with_name("Random", random),
Player::with_name("Friendly, greedy if winning", greedy_if_winning_else_friendly),
Player::with_name("Evil, greedy if winning", greedy_if_winning_else_evil),
Player::with_name("Friendly, greedy if 2x", greedy_if_2x_score_else_friendly),
Player::with_name("Evil, greedy if 2x", greedy_if_2x_score_else_evil),
Player::with_name("ChatGPT adaptive", chat_gpt_adaptive),
Player::with_name("ChatGPT proactive", chat_gpt_proactive),
Player::with_name("ChatGPT versatile", chat_gpt_versatile),
Player::with_name("Cooperate until defection", cooperate_until_defection),
Player::with_name("Random, greedy if winning", greedy_if_winning_else_random),
Player::with_name("Random, greedy if 2x", greedy_if_2x_score_else_random),
Player::with_name("Copy opponent", copy),
Player::with_name("Copy opponent, unless blue", smarter_copy),
];

println!("Pairing every program... ({0} games)\n", players.len() * (players.len() - 1) / 2);
Expand Down
Loading

0 comments on commit eca6dd2

Please sign in to comment.