Merge branch 'feature/botStrategy' into develop

This commit is contained in:
Henri Bourcereau 2024-10-18 16:18:47 +02:00
commit 5d4401a1fe
5 changed files with 318 additions and 130 deletions

223
Cargo.lock generated
View file

@ -187,9 +187,9 @@ dependencies = [
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.75" version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6"
[[package]] [[package]]
name = "approx" name = "approx"
@ -1161,6 +1161,15 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53" checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53"
[[package]]
name = "castaway"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0abae9be0aaf9ea96a3b1b8b1b55c602ca751eba1b1500220cea4ecbafe7c0d5"
dependencies = [
"rustversion",
]
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.83" version = "1.0.83"
@ -1251,7 +1260,7 @@ dependencies = [
"anyhow", "anyhow",
"bincode", "bincode",
"bot", "bot",
"itertools 0.13.0", "itertools",
"pico-args", "pico-args",
"pretty_assertions", "pretty_assertions",
"renet", "renet",
@ -1266,7 +1275,6 @@ dependencies = [
"bincode", "bincode",
"crossterm", "crossterm",
"ratatui", "ratatui",
"renet",
"store", "store",
] ]
@ -1302,6 +1310,20 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "compact_str"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6050c3a16ddab2e412160b31f2c871015704239bca62f72f6e5f0be631d3f644"
dependencies = [
"castaway",
"cfg-if",
"itoa",
"rustversion",
"ryu",
"static_assertions",
]
[[package]] [[package]]
name = "concurrent-queue" name = "concurrent-queue"
version = "2.3.0" version = "2.3.0"
@ -1466,15 +1488,15 @@ dependencies = [
[[package]] [[package]]
name = "crossterm" name = "crossterm"
version = "0.27.0" version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df" checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6"
dependencies = [ dependencies = [
"bitflags 2.4.1", "bitflags 2.4.1",
"crossterm_winapi", "crossterm_winapi",
"libc", "mio 1.0.2",
"mio",
"parking_lot", "parking_lot",
"rustix",
"signal-hook", "signal-hook",
"signal-hook-mio", "signal-hook-mio",
"winapi", "winapi",
@ -1609,12 +1631,12 @@ dependencies = [
[[package]] [[package]]
name = "errno" name = "errno"
version = "0.3.5" version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860" checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys 0.48.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -1979,15 +2001,15 @@ dependencies = [
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.4.1" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.3.3" version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]] [[package]]
name = "hexasphere" name = "hexasphere"
@ -2045,12 +2067,6 @@ dependencies = [
"hashbrown 0.14.2", "hashbrown 0.14.2",
] ]
[[package]]
name = "indoc"
version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e186cfbae8084e513daff4240b4797e342f988cecda4fb6c939150f96315fd8"
[[package]] [[package]]
name = "inflections" name = "inflections"
version = "1.1.1" version = "1.1.1"
@ -2086,6 +2102,16 @@ dependencies = [
"generic-array", "generic-array",
] ]
[[package]]
name = "instability"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b23a0c8dfe501baac4adf6ebbfa6eddf8f0c07f56b058cc1288017e32397846c"
dependencies = [
"quote",
"syn 2.0.38",
]
[[package]] [[package]]
name = "instant" name = "instant"
version = "0.1.12" version = "0.1.12"
@ -2119,15 +2145,6 @@ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "itertools"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.13.0" version = "0.13.0"
@ -2260,9 +2277,9 @@ dependencies = [
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.149" version = "0.2.161"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1"
[[package]] [[package]]
name = "libloading" name = "libloading"
@ -2296,9 +2313,9 @@ dependencies = [
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.4.10" version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da2479e8c062e40bf0066ffa0bc823de0a9368974af99c9f6df941d2c231e03f" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
@ -2422,6 +2439,19 @@ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "mio"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec"
dependencies = [
"hermit-abi",
"libc",
"log",
"wasi",
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "naga" name = "naga"
version = "0.12.3" version = "0.12.3"
@ -2538,7 +2568,7 @@ dependencies = [
"kqueue", "kqueue",
"libc", "libc",
"log", "log",
"mio", "mio 0.8.9",
"walkdir", "walkdir",
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
@ -2999,19 +3029,22 @@ checksum = "9c8a99fddc9f0ba0a85884b8d14e3592853e787d581ca1816c91349b10e4eeab"
[[package]] [[package]]
name = "ratatui" name = "ratatui"
version = "0.24.0" version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ebc917cfb527a566c37ecb94c7e3fd098353516fb4eb6bea17015ade0182425" checksum = "fdef7f9be5c0122f890d58bdf4d964349ba6a6161f705907526d891efabba57d"
dependencies = [ dependencies = [
"bitflags 2.4.1", "bitflags 2.4.1",
"cassowary", "cassowary",
"compact_str",
"crossterm", "crossterm",
"indoc", "instability",
"itertools 0.11.0", "itertools",
"lru", "lru",
"paste", "paste",
"strum", "strum",
"strum_macros",
"unicode-segmentation", "unicode-segmentation",
"unicode-truncate",
"unicode-width", "unicode-width",
] ]
@ -3154,15 +3187,15 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.38.21" version = "0.38.37"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b426b0506e5d50a7d8dafcf2e81471400deb602392c7dd110815afb4eaf02a3" checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811"
dependencies = [ dependencies = [
"bitflags 2.4.1", "bitflags 2.4.1",
"errno", "errno",
"libc", "libc",
"linux-raw-sys", "linux-raw-sys",
"windows-sys 0.48.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -3261,12 +3294,12 @@ dependencies = [
[[package]] [[package]]
name = "signal-hook-mio" name = "signal-hook-mio"
version = "0.2.3" version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af" checksum = "34db1a06d485c9142248b7a054f034b349b212551f3dfd19c94d45a754a217cd"
dependencies = [ dependencies = [
"libc", "libc",
"mio", "mio 1.0.2",
"signal-hook", "signal-hook",
] ]
@ -3357,18 +3390,18 @@ checksum = "fe895eb47f22e2ddd4dabc02bce419d2e643c8e3b585c78158b349195bc24d82"
[[package]] [[package]]
name = "strum" name = "strum"
version = "0.25.0" version = "0.26.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125" checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06"
dependencies = [ dependencies = [
"strum_macros", "strum_macros",
] ]
[[package]] [[package]]
name = "strum_macros" name = "strum_macros"
version = "0.25.3" version = "0.26.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23dc1fa9ac9c169a78ba62f0b841814b7abae11bdd047b9c58f893439e309ea0" checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be"
dependencies = [ dependencies = [
"heck", "heck",
"proc-macro2", "proc-macro2",
@ -3669,10 +3702,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
[[package]] [[package]]
name = "unicode-width" name = "unicode-truncate"
version = "0.1.11" version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" checksum = "b3644627a5af5fa321c95b9b235a72fd24cd29c648c2c379431e6628655627bf"
dependencies = [
"itertools",
"unicode-segmentation",
"unicode-width",
]
[[package]]
name = "unicode-width"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
@ -4052,6 +4096,15 @@ dependencies = [
"windows-targets 0.48.5", "windows-targets 0.48.5",
] ]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.6",
]
[[package]] [[package]]
name = "windows-targets" name = "windows-targets"
version = "0.42.2" version = "0.42.2"
@ -4082,6 +4135,22 @@ dependencies = [
"windows_x86_64_msvc 0.48.5", "windows_x86_64_msvc 0.48.5",
] ]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm 0.52.6",
"windows_aarch64_msvc 0.52.6",
"windows_i686_gnu 0.52.6",
"windows_i686_gnullvm",
"windows_i686_msvc 0.52.6",
"windows_x86_64_gnu 0.52.6",
"windows_x86_64_gnullvm 0.52.6",
"windows_x86_64_msvc 0.52.6",
]
[[package]] [[package]]
name = "windows_aarch64_gnullvm" name = "windows_aarch64_gnullvm"
version = "0.42.2" version = "0.42.2"
@ -4094,6 +4163,12 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.42.2" version = "0.42.2"
@ -4106,6 +4181,12 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.42.2" version = "0.42.2"
@ -4118,6 +4199,18 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.42.2" version = "0.42.2"
@ -4130,6 +4223,12 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.42.2" version = "0.42.2"
@ -4142,6 +4241,12 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.42.2" version = "0.42.2"
@ -4154,6 +4259,12 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.42.2" version = "0.42.2"
@ -4166,6 +4277,12 @@ version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]] [[package]]
name = "winit" name = "winit"
version = "0.28.7" version = "0.28.7"
@ -4181,7 +4298,7 @@ dependencies = [
"instant", "instant",
"libc", "libc",
"log", "log",
"mio", "mio 0.8.9",
"ndk", "ndk",
"objc2", "objc2",
"once_cell", "once_cell",

View file

@ -4,81 +4,59 @@ use store::{
CheckerMove, Color, Dice, GameEvent, GameState, Player, PlayerId, PointsRules, Stage, TurnStage, CheckerMove, Color, Dice, GameEvent, GameState, Player, PlayerId, PointsRules, Stage, TurnStage,
}; };
pub trait BotStrategy {
fn get_game(&self) -> &GameState;
fn get_mut_game(&mut self) -> &mut GameState;
fn calculate_points(&self) -> u8;
fn calculate_adv_points(&self) -> u8;
fn choose_move(&self) -> (CheckerMove, CheckerMove);
fn set_player_id(&mut self, player_id: PlayerId);
fn init_players(&mut self) {
self.get_mut_game().init_player("p1");
self.get_mut_game().init_player("p2");
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct Bot { pub struct DefaultStrategy {
pub game: GameState, pub game: GameState,
pub player_id: PlayerId, pub player_id: PlayerId,
color: Color, pub color: Color,
schools_enabled: bool,
} }
impl Default for Bot { impl Default for DefaultStrategy {
fn default() -> Bot { fn default() -> Self {
Bot { let game = GameState::default();
game: GameState::default(), let mut strategy = Self {
player_id: 1,
color: Color::Black,
schools_enabled: false,
}
}
}
// impl PlayerEngine for Bot {}
impl Bot {
/// new initialize a bot
/// # Examples
/// ```let mut bot = Bot::new(Color::Black);
/// assert_eq!(bot.game.stage, Stage::PreGame);
/// ```
pub fn new(color: Color, schools_enabled: bool) -> Self {
let mut game = GameState::default();
game.init_player("p1");
game.init_player("p2");
let player_id = match color {
Color::White => 1,
Color::Black => 2,
};
Self {
game, game,
player_id, player_id: 2,
color, color: Color::Black,
schools_enabled: false, };
strategy
} }
} }
pub fn handle_event(&mut self, event: &GameEvent) -> Option<GameEvent> { impl DefaultStrategy {
self.game.consume(event); fn new() -> Self {
// println!("bot game {:?}", self.game); Self::default()
// println!("bot player_id {:?}", self.player_id);
if self.game.active_player_id == self.player_id {
return match self.game.turn_stage {
TurnStage::MarkAdvPoints => Some(GameEvent::Mark {
player_id: self.player_id,
points: self.calculate_adv_points(),
}),
TurnStage::RollDice => Some(GameEvent::Roll {
player_id: self.player_id,
}),
TurnStage::MarkPoints => Some(GameEvent::Mark {
player_id: self.player_id,
points: self.calculate_points(),
}),
TurnStage::Move => Some(GameEvent::Move {
player_id: self.player_id,
moves: self.choose_move(),
}),
_ => None,
};
} }
None }
impl BotStrategy for DefaultStrategy {
fn get_game(&self) -> &GameState {
&self.game
}
fn get_mut_game(&mut self) -> &mut GameState {
&mut self.game
}
fn set_player_id(&mut self, player_id: PlayerId) {
self.player_id = player_id;
} }
fn calculate_points(&self) -> u8 { fn calculate_points(&self) -> u8 {
let dice_roll_count = self let dice_roll_count = self
.game .get_game()
.players .players
.get(&self.player_id) .get(&self.player_id)
.unwrap() .unwrap()
@ -89,7 +67,7 @@ impl Bot {
fn calculate_adv_points(&self) -> u8 { fn calculate_adv_points(&self) -> u8 {
let dice_roll_count = self let dice_roll_count = self
.game .get_game()
.players .players
.get(&self.player_id) .get(&self.player_id)
.unwrap() .unwrap()
@ -116,29 +94,108 @@ impl Bot {
} }
} }
#[derive(Debug)]
pub struct Bot<BotStrategy> {
pub player_id: PlayerId,
strategy: BotStrategy,
color: Color,
schools_enabled: bool,
}
impl Default for Bot<DefaultStrategy> {
fn default() -> Self {
Self {
player_id: 2,
strategy: DefaultStrategy::default(),
color: Color::Black,
schools_enabled: false,
}
}
}
impl<BS> Bot<BS>
where
BS: BotStrategy,
{
/// new initialize a bot
/// # Examples
/// ```let mut bot = Bot::new(Color::Black);
/// assert_eq!(bot.game.stage, Stage::PreGame);
/// ```
pub fn new(mut strategy: BS, color: Color, schools_enabled: bool) -> Self {
let game = strategy.get_mut_game();
strategy.init_players();
let player_id = match color {
Color::White => 1,
Color::Black => 2,
};
strategy.set_player_id(player_id);
Self {
player_id,
strategy,
color,
schools_enabled: false,
}
}
pub fn handle_event(&mut self, event: &GameEvent) -> Option<GameEvent> {
let game = self.strategy.get_mut_game();
game.consume(event);
// println!("bot game {:?}", self.game);
// println!("bot player_id {:?}", self.player_id);
if game.active_player_id == self.player_id {
return match game.turn_stage {
TurnStage::MarkAdvPoints => Some(GameEvent::Mark {
player_id: self.player_id,
points: self.strategy.calculate_adv_points(),
}),
TurnStage::RollDice => Some(GameEvent::Roll {
player_id: self.player_id,
}),
TurnStage::MarkPoints => Some(GameEvent::Mark {
player_id: self.player_id,
points: self.strategy.calculate_points(),
}),
TurnStage::Move => Some(GameEvent::Move {
player_id: self.player_id,
moves: self.strategy.choose_move(),
}),
_ => None,
};
}
None
}
pub fn get_state(&self) -> &GameState {
self.strategy.get_game()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
#[test] #[test]
fn test_new() { fn test_new() {
let bot = Bot::new(Color::Black, false); let bot = Bot::new(DefaultStrategy::new(), Color::Black, false);
assert_eq!(bot.game.stage, Stage::PreGame); assert_eq!(bot.get_state().stage, Stage::PreGame);
} }
#[test] #[test]
fn test_consume() { fn test_consume() {
let mut bot = Bot::new(Color::Black, false); let mut bot = Bot::new(DefaultStrategy::new(), Color::Black, false);
let mut event = bot.handle_event(&GameEvent::BeginGame { goes_first: 2 }); let mut event = bot.handle_event(&GameEvent::BeginGame { goes_first: 2 });
assert_eq!(event, Some(GameEvent::Roll { player_id: 2 })); assert_eq!(event, Some(GameEvent::Roll { player_id: 2 }));
assert_eq!(bot.get_state().active_player_id, 2);
event = bot.handle_event(&GameEvent::BeginGame { goes_first: 1 }); event = bot.handle_event(&GameEvent::BeginGame { goes_first: 1 });
assert_eq!(event, None); assert_eq!(event, None);
assert_eq!(bot.get_state().active_player_id, 1);
bot.handle_event(&GameEvent::RollResult { bot.handle_event(&GameEvent::RollResult {
player_id: 2, player_id: 1,
dice: Dice { values: (2, 3) }, dice: Dice { values: (2, 3) },
}); });
assert_eq!(bot.game.turn_stage, TurnStage::Move); assert_eq!(bot.get_state().turn_stage, TurnStage::Move);
} }
} }

View file

@ -1,6 +1,7 @@
use itertools::Itertools; use itertools::Itertools;
use crate::game_runner::Game; use crate::game_runner::Game;
use bot::BotStrategy;
use store::{CheckerMove, GameEvent, GameState, PointsRules, Stage, TurnStage}; use store::{CheckerMove, GameEvent, GameState, PointsRules, Stage, TurnStage};
#[derive(Debug, Default)] #[derive(Debug, Default)]

View file

@ -1,14 +1,26 @@
use bot::Bot; use bot::{Bot, BotStrategy, DefaultStrategy};
use store::{CheckerMove, DiceRoller, GameEvent, GameState, PlayerId, TurnStage}; use store::{CheckerMove, DiceRoller, GameEvent, GameState, PlayerId, TurnStage};
// Application Game // Application Game
#[derive(Debug, Default)] #[derive(Debug)]
pub struct Game { pub struct Game {
pub state: GameState, pub state: GameState,
pub dice_roller: DiceRoller, pub dice_roller: DiceRoller,
pub first_move: Option<CheckerMove>, pub first_move: Option<CheckerMove>,
pub player_id: Option<PlayerId>, pub player_id: Option<PlayerId>,
bot: Bot, bot: Bot<DefaultStrategy>,
}
impl Default for Game {
fn default() -> Self {
Self {
state: GameState::default(),
dice_roller: DiceRoller::default(),
first_move: None,
player_id: None,
bot: Bot::default(),
}
}
} }
impl Game { impl Game {
@ -20,7 +32,8 @@ impl Game {
// bot // bot
let bot_id: PlayerId = state.init_player("bot").unwrap(); let bot_id: PlayerId = state.init_player("bot").unwrap();
let bot_color = state.player_color_by_id(&bot_id).unwrap(); let bot_color = state.player_color_by_id(&bot_id).unwrap();
let bot: Bot = Bot::new(bot_color, schools_enabled); let bot_strategy = DefaultStrategy::default();
let bot: Bot<DefaultStrategy> = Bot::new(bot_strategy, bot_color, schools_enabled);
let mut game = Self { let mut game = Self {
state, state,

View file

@ -6,9 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
anyhow = "1.0.75" anyhow = "1.0.89"
bincode = "1.3.3" bincode = "1.3.3"
crossterm = "0.27.0" crossterm = "0.28.1"
ratatui = "0.24.0" ratatui = "0.28.1"
renet = "0.0.13" # renet = "0.0.13"
store = { path = "../store" } store = { path = "../store" }