From 9f6be9b6699826679c09812cd48eb78b4f006bde Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Tue, 28 Jan 2025 15:05:32 -0500 Subject: [PATCH] woah --- Cargo.lock | 235 ++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/agent.rs | 18 ---- src/board.rs | 2 +- src/complexagent.rs | 43 ++++++++ src/game.rs | 4 +- src/main.rs | 21 ++-- src/misc.rs | 22 +++++ 8 files changed, 317 insertions(+), 29 deletions(-) create mode 100644 src/complexagent.rs diff --git a/Cargo.lock b/Cargo.lock index cee0251..1befcc7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,42 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +[[package]] +name = "bitflags" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" +dependencies = [ + "cfg-if", + "libc", + "wasi", + "windows-targets", +] + +[[package]] +name = "libc" +version = "0.2.169" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" + [[package]] name = "num" version = "0.4.3" @@ -86,4 +122,203 @@ name = "othello" version = "0.1.0" dependencies = [ "num", + "rand", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy 0.7.35", +] + +[[package]] +name = "proc-macro2" +version = "1.0.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94" +dependencies = [ + "rand_chacha", + "rand_core", + "zerocopy 0.8.14", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b08f3c9802962f7e1b25113931d94f43ed9725bebc59db9d0c3e9a23b67e15ff" +dependencies = [ + "getrandom", + "zerocopy 0.8.14", +] + +[[package]] +name = "syn" +version = "2.0.96" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" + +[[package]] +name = "wasi" +version = "0.13.3+wasi-0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +dependencies = [ + "wit-bindgen-rt", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[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]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen-rt" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +dependencies = [ + "bitflags", +] + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive 0.7.35", +] + +[[package]] +name = "zerocopy" +version = "0.8.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a367f292d93d4eab890745e75a778da40909cab4d6ff8173693812f79c4a2468" +dependencies = [ + "zerocopy-derive 0.8.14", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3931cb58c62c13adec22e38686b559c86a30565e16ad6e8510a337cedc611e1" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] diff --git a/Cargo.toml b/Cargo.toml index 99c751b..988a871 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,4 @@ edition = "2021" [dependencies] num = "0.4.3" +rand = "0.9.0" diff --git a/src/agent.rs b/src/agent.rs index 9291316..de059c6 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -52,21 +52,3 @@ impl Agent for QueueAgent { self.color } } - -pub struct AutoAgent { - color: Piece, -} - -impl Agent for AutoAgent { - fn next_move(&mut self, _: &Board) -> Option<(usize, usize)> { - todo!("next_move not implemented") - } - - fn name(&self) -> &'static str { - "Auto Agent" - } - - fn color(&self) -> Piece { - self.color - } -} diff --git a/src/board.rs b/src/board.rs index d98443e..95b7fa1 100644 --- a/src/board.rs +++ b/src/board.rs @@ -1,7 +1,7 @@ use crate::{misc::split_from, piece::Piece}; use std::fmt; -const BOARD_SIZE: usize = 8; +pub const BOARD_SIZE: usize = 8; #[derive(Copy, Clone)] pub struct Board { diff --git a/src/complexagent.rs b/src/complexagent.rs new file mode 100644 index 0000000..fa1aa06 --- /dev/null +++ b/src/complexagent.rs @@ -0,0 +1,43 @@ +use crate::{ + agent::Agent, + board::{Board, BOARD_SIZE}, + misc::{offsets, split_from}, + piece::Piece, +}; +use rand::{seq::IteratorRandom, Rng}; + +pub struct ComplexAgent { + color: Piece, +} + +impl Agent for ComplexAgent { + fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> { + (0..BOARD_SIZE) + .zip(0..BOARD_SIZE) + .filter(|&(i, j)| board.get(i, j) == &Some(!self.color())) + .flat_map(|(i, j)| offsets(i, j, 1)) + .filter(|&(i, j)| i < BOARD_SIZE && j < BOARD_SIZE) + .filter(|&(i, j)| board.get(i, j).is_none()) + .choose(&mut rand::rng()) + .or_else(|| { + (0..BOARD_SIZE) + .zip(0..BOARD_SIZE) + .filter(|&(i, j)| board.get(i, j).is_none()) + .choose(&mut rand::rng()) + }) + } + + fn name(&self) -> &'static str { + "Complex Agent" + } + + fn color(&self) -> Piece { + self.color + } +} + +impl ComplexAgent { + pub const fn new(color: Piece) -> Self { + Self { color } + } +} diff --git a/src/game.rs b/src/game.rs index f9fee2f..7520f1f 100644 --- a/src/game.rs +++ b/src/game.rs @@ -37,7 +37,7 @@ impl Game { todo!("handle invalid player move"); } } else { - todo!("handle player not having a move to make"); + panic!("player {} did not make a move", player_i); } } @@ -53,7 +53,7 @@ impl Game { println!("{}", self); - std::thread::sleep(Duration::from_millis(500)); + std::thread::sleep(Duration::from_millis(100)); } } } diff --git a/src/main.rs b/src/main.rs index b6295b7..18514f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,27 @@ use agent::QueueAgent; +use complexagent::ComplexAgent; use game::Game; use piece::Piece; mod agent; mod board; +mod complexagent; mod game; mod misc; mod piece; fn main() { - let player1 = QueueAgent::new( - [(0, 0), (1, 0), (1, 2), (3, 2), (3, 0), (3, 4), (0, 1)], - Piece::Black, - ); - let player2 = QueueAgent::new( - [(1, 1), (2, 2), (3, 3), (3, 1), (0, 2), (4, 2), (3, 5)], - Piece::White, - ); + // let player1 = QueueAgent::new( + // [(0, 0), (1, 0), (1, 2), (3, 2), (3, 0), (3, 4), (0, 1)], + // Piece::Black, + // ); + // let player2 = QueueAgent::new( + // [(1, 1), (2, 2), (3, 3), (3, 1), (0, 2), (4, 2), (3, 5)], + // Piece::White, + // ); + + let player1 = ComplexAgent::new(Piece::Black); + let player2 = ComplexAgent::new(Piece::White); let mut game = Game::new(Box::new(player1), Box::new(player2)); game.game_loop(); } diff --git a/src/misc.rs b/src/misc.rs index 5630cd8..aa717d3 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -1,5 +1,7 @@ use std::{iter::Rev, ops::RangeInclusive}; +use num::CheckedSub; + pub fn split_from(min: T, max: T, x: T) -> Vec> where T: num::Integer + Copy, @@ -22,6 +24,26 @@ where output } +pub fn offsets(i: usize, j: usize, range: usize) -> Vec<(usize, usize)> { + let mut output = Vec::new(); + for i_o in [-(range as i16), 0, range as i16] { + let new_i = i as i16 + i_o; + if 0 > new_i { + continue; + } + + for j_o in [-(range as i16), 0, range as i16] { + let new_j = j as i16 + j_o; + if 0 > new_j { + continue; + } + output.push((new_i as usize, new_j as usize)); + } + } + + output +} + #[cfg(test)] mod test { use super::*;