Skip to content

Commit 3fbb685

Browse files
committed
refactor: move into separate modules separating cli/lib per game
1 parent eb88ec8 commit 3fbb685

File tree

24 files changed

+390
-356
lines changed

24 files changed

+390
-356
lines changed

Cargo.lock

Lines changed: 17 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/game-solver/Cargo.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ edition = "2021"
1313
"rayon" = ["dep:rayon", "xxhash", "dep:sysinfo", "dep:moka"]
1414
"reinforcement" = ["dep:rand", "dep:dfdx", "dep:itertools"]
1515

16-
[dev-dependencies]
17-
anyhow = "1.0.72"
18-
array2d = "0.3.0"
19-
ndarray = "0.15.6"
20-
2116
[dependencies]
2217
dfdx = { git = "https://github.com/coreylowman/dfdx.git", rev = "4722a99", optional = true }
2318
moka = { version = "0.12", optional = true, features = ["future"] }

crates/games-ui/Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "games-ui"
3+
description = "UI counterparts for the games crate"
4+
license = "MPL"
5+
edition = "2021"
6+
7+
[dependencies]
8+
eframe = { version = "0.27.2", default-features = false, features = [
9+
"accesskit",
10+
"default_fonts",
11+
"glow",
12+
"persistence"
13+
]}
14+
egui = "0.27.2"
15+
game-solver = { path = "../game-solver", features = ["rayon", "reinforcement"] }
16+
serde = { version = "1.0.203", features = ["derive"] }
17+
18+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
19+
env_logger = "0.11"
20+
21+
[target.'cfg(target_arch = "wasm32")'.dependencies]
22+
wasm-bindgen-futures = "0.4"
File renamed without changes.
File renamed without changes.
File renamed without changes.

crates/games-ui/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![warn(clippy::all, rust_2018_idioms)]
2+
3+
mod app;
4+
pub use app::TemplateApp;
File renamed without changes.

crates/games/Cargo.toml

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
[package]
22
name = "games"
3-
description = "List of games and their UI counterparts for game-solver."
43
license = "MPL"
54
edition = "2021"
65

76
[dependencies]
8-
eframe = { version = "0.27.2", default-features = false, features = [
9-
"accesskit",
10-
"default_fonts",
11-
"glow",
12-
"persistence"
13-
]}
14-
egui = "0.27.2"
157
game-solver = { path = "../game-solver", features = ["rayon", "reinforcement"] }
16-
serde = { version = "1.0.203", features = ["derive"] }
17-
18-
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
19-
env_logger = "0.11"
20-
21-
[target.'cfg(target_arch = "wasm32")'.dependencies]
22-
wasm-bindgen-futures = "0.4"
8+
anyhow = "1.0.72"
9+
array2d = "0.3.0"
10+
ndarray = "0.15.6"
11+
itertools = "0.13.0"

crates/games/README.md

Lines changed: 0 additions & 12 deletions
This file was deleted.

crates/games/src/games/chomp/cli.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::{collections::HashMap, env::args};
2+
3+
use game_solver::{game::Game, move_scores};
4+
5+
use crate::games::chomp::Chomp;
6+
7+
pub fn main() {
8+
let mut game = Chomp::new(6, 4);
9+
10+
// parse every move in args, e.g. 0-0 1-1 in args
11+
args().skip(1).for_each(|arg| {
12+
let numbers: Vec<usize> = arg
13+
.split('-')
14+
.map(|num| num.parse::<usize>().expect("Not a number!"))
15+
.collect();
16+
17+
game.make_move(&(numbers[0], numbers[1]));
18+
});
19+
20+
print!("{}", game);
21+
println!("Player {:?} to move", game.player());
22+
23+
let mut move_scores = move_scores(&game, &mut HashMap::new()).collect::<Vec<_>>();
24+
25+
if move_scores.is_empty() {
26+
println!("Player {:?} won!", game.player().opponent());
27+
} else {
28+
move_scores.sort_by_key(|m| m.1);
29+
move_scores.reverse();
30+
31+
let mut current_move_score = None;
32+
for (game_move, score) in move_scores {
33+
if current_move_score != Some(score) {
34+
println!("\n\nBest moves @ score {}:", score);
35+
current_move_score = Some(score);
36+
}
37+
print!("({}, {}), ", game_move.0, game_move.1);
38+
}
39+
println!();
40+
}
41+
}

crates/games/src/games/chomp.rs renamed to crates/games/src/games/chomp/mod.rs

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@
1111
//! However, it serves as a great test for the transposition table, as it is a game that commonly
1212
//! repeats positions (as it only has nxm - 1 positions).
1313
14+
pub mod cli;
15+
1416
use array2d::Array2D;
15-
use game_solver::{
16-
game::{Game, ZeroSumPlayer},
17-
move_scores,
18-
};
17+
use game_solver::game::{Game, ZeroSumPlayer};
1918

2019
use std::{
21-
collections::HashMap,
22-
env::args,
2320
fmt::{Display, Formatter},
2421
hash::Hash,
2522
};
@@ -125,44 +122,12 @@ impl Display for Chomp {
125122
}
126123
}
127124

128-
fn main() {
129-
let mut game = Chomp::new(6, 4);
130-
131-
// parse every move in args, e.g. 0-0 1-1 in args
132-
args().skip(1).for_each(|arg| {
133-
let numbers: Vec<usize> = arg
134-
.split('-')
135-
.map(|num| num.parse::<usize>().expect("Not a number!"))
136-
.collect();
137-
138-
game.make_move(&(numbers[0], numbers[1]));
139-
});
140-
141-
print!("{}", game);
142-
println!("Player {:?} to move", game.player());
143-
144-
let mut move_scores = move_scores(&game, &mut HashMap::new()).collect::<Vec<_>>();
145-
146-
if move_scores.is_empty() {
147-
println!("Player {:?} won!", game.player().opponent());
148-
} else {
149-
move_scores.sort_by_key(|m| m.1);
150-
move_scores.reverse();
151-
152-
let mut current_move_score = None;
153-
for (game_move, score) in move_scores {
154-
if current_move_score != Some(score) {
155-
println!("\n\nBest moves @ score {}:", score);
156-
current_move_score = Some(score);
157-
}
158-
print!("({}, {}), ", game_move.0, game_move.1);
159-
}
160-
println!();
161-
}
162-
}
163-
164125
#[cfg(test)]
165126
mod tests {
127+
use std::collections::HashMap;
128+
129+
use game_solver::move_scores;
130+
166131
use super::*;
167132

168133
#[test]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::{collections::HashMap, env::args};
2+
3+
use game_solver::{game::Game, move_scores};
4+
5+
use crate::games::domineering::DomineeringGame;
6+
7+
pub fn main() {
8+
let mut game = DomineeringGame::new();
9+
10+
// parse every move in args, e.g. 0-0 1-1 in args
11+
args().skip(1).for_each(|arg| {
12+
let numbers: Vec<usize> = arg
13+
.split('-')
14+
.map(|num| num.parse::<usize>().expect("Not a number!"))
15+
.collect();
16+
17+
game.make_move(&(numbers[0], numbers[1]));
18+
});
19+
20+
print!("{}", game);
21+
println!("Player {:?} to move", game.player());
22+
23+
let mut move_scores = move_scores(&game, &mut HashMap::new()).collect::<Vec<_>>();
24+
25+
if !move_scores.is_empty() {
26+
move_scores.sort_by_key(|m| m.1);
27+
move_scores.reverse();
28+
29+
let mut current_move_score = None;
30+
for (game_move, score) in move_scores {
31+
if current_move_score != Some(score) {
32+
println!("\n\nBest moves @ score {}:", score);
33+
current_move_score = Some(score);
34+
}
35+
print!("({}, {}), ", game_move.0, game_move.1);
36+
}
37+
println!();
38+
} else {
39+
println!("Player {:?} won!", game.player().opponent());
40+
}
41+
}

crates/games/src/games/domineering.rs renamed to crates/games/src/games/domineering/mod.rs

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
//!
55
//! Learn more: https://en.wikipedia.org/wiki/Connect_Four
66
7+
pub mod cli;
8+
79
use array2d::Array2D;
8-
use game_solver::{
9-
game::{Game, ZeroSumPlayer},
10-
move_scores,
11-
};
10+
use game_solver::game::{Game, ZeroSumPlayer};
1211

1312
use std::{
14-
collections::HashMap,
15-
env::args,
1613
fmt::{Display, Formatter},
1714
hash::Hash,
1815
};
@@ -133,44 +130,12 @@ impl<const WIDTH: usize, const HEIGHT: usize> Display for Domineering<WIDTH, HEI
133130
// n, m
134131
type DomineeringGame = Domineering<5, 5>;
135132

136-
fn main() {
137-
let mut game = DomineeringGame::new();
138-
139-
// parse every move in args, e.g. 0-0 1-1 in args
140-
args().skip(1).for_each(|arg| {
141-
let numbers: Vec<usize> = arg
142-
.split('-')
143-
.map(|num| num.parse::<usize>().expect("Not a number!"))
144-
.collect();
145-
146-
game.make_move(&(numbers[0], numbers[1]));
147-
});
148-
149-
print!("{}", game);
150-
println!("Player {:?} to move", game.player());
151-
152-
let mut move_scores = move_scores(&game, &mut HashMap::new()).collect::<Vec<_>>();
153-
154-
if !move_scores.is_empty() {
155-
move_scores.sort_by_key(|m| m.1);
156-
move_scores.reverse();
157-
158-
let mut current_move_score = None;
159-
for (game_move, score) in move_scores {
160-
if current_move_score != Some(score) {
161-
println!("\n\nBest moves @ score {}:", score);
162-
current_move_score = Some(score);
163-
}
164-
print!("({}, {}), ", game_move.0, game_move.1);
165-
}
166-
println!();
167-
} else {
168-
println!("Player {:?} won!", game.player().opponent());
169-
}
170-
}
171-
172133
#[cfg(test)]
173134
mod tests {
135+
use std::collections::HashMap;
136+
137+
use game_solver::move_scores;
138+
174139
use super::*;
175140

176141
/// Get the winner of a generic configuration of domineering

crates/games/src/games/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub mod chomp;
2+
pub mod domineering;
3+
pub mod nim;
4+
pub mod order_and_chaos;
5+
pub mod reversi;
6+
pub mod tic_tac_toe;

0 commit comments

Comments
 (0)