diff --git a/Cargo.lock b/Cargo.lock index 8af90fe..41776ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,290 +2,6 @@ # It is not intended for manual editing. version = 4 -[[package]] -name = "anstyle" -version = "1.0.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" - -[[package]] -name = "cc" -version = "1.2.51" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a0aeaff4ff1a90589618835a598e545176939b97874f7abc7851caa0618f203" -dependencies = [ - "find-msvc-tools", - "shlex", -] - -[[package]] -name = "clap" -version = "4.5.53" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9e340e012a1bf4935f5282ed1436d1489548e8f72308207ea5df0e23d2d03f8" -dependencies = [ - "clap_builder", -] - -[[package]] -name = "clap_builder" -version = "4.5.53" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d76b5d13eaa18c901fd2f7fca939fefe3a0727a953561fefdf3b2922b8569d00" -dependencies = [ - "anstyle", - "clap_lex", - "strsim", -] - -[[package]] -name = "clap_lex" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d" - -[[package]] -name = "codespan-reporting" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af491d569909a7e4dee0ad7db7f5341fef5c614d5b8ec8cf765732aba3cff681" -dependencies = [ - "serde", - "termcolor", - "unicode-width", -] - -[[package]] -name = "cxx" -version = "1.0.192" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbda285ba6e5866529faf76352bdf73801d9b44a6308d7cd58ca2379f378e994" -dependencies = [ - "cc", - "cxx-build", - "cxxbridge-cmd", - "cxxbridge-flags", - "cxxbridge-macro", - "foldhash", - "link-cplusplus", -] - -[[package]] -name = "cxx-build" -version = "1.0.192" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af9efde466c5d532d57efd92f861da3bdb7f61e369128ce8b4c3fe0c9de4fa4d" -dependencies = [ - "cc", - "codespan-reporting", - "indexmap", - "proc-macro2", - "quote", - "scratch", - "syn", -] - -[[package]] -name = "cxxbridge-cmd" -version = "1.0.192" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3efb93799095bccd4f763ca07997dc39a69e5e61ab52d2c407d4988d21ce144d" -dependencies = [ - "clap", - "codespan-reporting", - "indexmap", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "cxxbridge-flags" -version = "1.0.192" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3092010228026e143b32a4463ed9fa8f86dca266af4bf5f3b2a26e113dbe4e45" - -[[package]] -name = "cxxbridge-macro" -version = "1.0.192" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31d72ebfcd351ae404fb00ff378dfc9571827a00722c9e735c9181aec320ba0a" -dependencies = [ - "indexmap", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "equivalent" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" - -[[package]] -name = "find-msvc-tools" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "645cbb3a84e60b7531617d5ae4e57f7e27308f6445f5abf653209ea76dec8dff" - -[[package]] -name = "foldhash" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" - -[[package]] -name = "hashbrown" -version = "0.16.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" - -[[package]] -name = "indexmap" -version = "2.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ad4bb2b565bca0645f4d68c5c9af97fba094e9791da685bf83cb5f3ce74acf2" -dependencies = [ - "equivalent", - "hashbrown", -] - -[[package]] -name = "link-cplusplus" -version = "1.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f78c730aaa7d0b9336a299029ea49f9ee53b0ed06e9202e8cb7db9bae7b8c82" -dependencies = [ - "cc", -] - -[[package]] -name = "proc-macro2" -version = "1.0.104" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9695f8df41bb4f3d222c95a67532365f569318332d03d5f3f67f37b20e6ebdf0" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.42" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "scratch" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d68f2ec51b097e4c1a75b681a8bec621909b5e91f15bb7b840c4f2f7b01148b2" - -[[package]] -name = "serde" -version = "1.0.228" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" -dependencies = [ - "serde_core", - "serde_derive", -] - -[[package]] -name = "serde_core" -version = "1.0.228" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.228" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "shlex" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - -[[package]] -name = "strsim" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" - -[[package]] -name = "syn" -version = "2.0.112" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21f182278bf2d2bcb3c88b1b08a37df029d71ce3d3ae26168e3c653b213b99d4" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "termcolor" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" -dependencies = [ - "winapi-util", -] - [[package]] name = "tictactoe-rust-foropenspiel" version = "0.1.0" -dependencies = [ - "cxx", - "cxx-build", -] - -[[package]] -name = "unicode-ident" -version = "1.0.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" - -[[package]] -name = "unicode-width" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" - -[[package]] -name = "winapi-util" -version = "0.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" -dependencies = [ - "windows-sys", -] - -[[package]] -name = "windows-link" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" - -[[package]] -name = "windows-sys" -version = "0.61.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" -dependencies = [ - "windows-link", -] diff --git a/Cargo.toml b/Cargo.toml index 2323eb6..7f62d52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,3 @@ version = "0.1.0" edition = "2024" [dependencies] -cxx = "1.0.192" - -[build-dependencies] -cxx-build = "1.0.192" diff --git a/build.rs b/build.rs deleted file mode 100644 index 5b40c01..0000000 --- a/build.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn main() { - cxx_build::bridge("src/game.rs") - .file("src/main.cc") - .compile("tictactoe-rust-foropenspiel"); - - println!("cargo:rerun-if-changed=src/game.rs"); - println!("cargo:rerun-if-changed=src/game.cc"); - println!("cargo:rerun-if-changed=include/game.h"); -} diff --git a/include/main.h b/include/main.h deleted file mode 100644 index c8da83d..0000000 --- a/include/main.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -void run_cpp_demo(); diff --git a/src/game.rs b/src/game.rs index ad50d72..a23a996 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,24 +1,6 @@ use std::{fmt, str}; -#[cxx::bridge] -pub mod ffi { - // Rust types and signatures exposed to C++. - extern "Rust" { - type Player; - type GameState; - - fn init() -> Box; - fn do_action(self: &mut GameState, action: usize); - fn legal_actions(self: &GameState) -> Vec ; - fn to_string(self: &GameState) -> String; - } - - // C++ types and signatures exposed to Rust. - unsafe extern "C++" { - include!("tictactoe-rust-foropenspiel/include/main.h"); - fn run_cpp_demo(); - } -} +// ------------- Player #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Player { @@ -136,15 +118,10 @@ type Action = usize; pub struct GameState { pub current_player: Option, pub outcome: Option, - board: Board, + pub board: Board, pub num_moves: usize, } -// exposed to c++ -fn init() -> Box { - Box::new(GameState::default()) -} - impl Default for GameState { fn default() -> Self { GameState { @@ -158,7 +135,7 @@ impl Default for GameState { impl GameState { fn is_terminal(&self) -> bool { - self.outcome.is_some() || self.is_full() + return self.outcome != None || self.is_full(); } fn is_full(&self) -> bool { @@ -180,7 +157,7 @@ impl GameState { } fn undo_action(&mut self, player: Player, action: Action) { - self.board.set_cell_state(action, CellState::Empty); + self.board.set_cell_state(action.into(), CellState::Empty); self.current_player = Some(player); self.outcome = None; self.num_moves -= 1; diff --git a/src/main.cc b/src/main.cc deleted file mode 100644 index 45d42ec..0000000 --- a/src/main.cc +++ /dev/null @@ -1,21 +0,0 @@ -#include "tictactoe-rust-foropenspiel/include/main.h" -#include "tictactoe-rust-foropenspiel/src/game.rs.h" -#include -#include -#include -#include - -void run_cpp_demo() { - rust::Box game = init(); - - std::random_device rd; - std::mt19937 gen(rd()); - - rust::Vec actions = game->legal_actions(); - while (!actions.empty()) { - std::uniform_int_distribution dis(0, actions.size() - 1); - game->do_action(actions[dis(gen)]); - actions = game->legal_actions(); - } - std::cout << "Result: \n" << game->to_string().c_str(); -} diff --git a/src/main.rs b/src/main.rs index 8baa5f8..06394c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,13 @@ mod game; +use game::GameState; + fn main() { - println!("--- Calling C++ from Rust ---"); - game::ffi::run_cpp_demo(); - println!("--- Back in Rust ---"); -} \ No newline at end of file + let mut game = GameState::default(); + let mut actions = game.legal_actions(); + while !actions.is_empty() { + game.do_action(actions[0]); + actions = game.legal_actions(); + } + println!("Result: \n{game}"); +}