diff --git a/Cargo.lock b/Cargo.lock index 6457c9a..14ac976 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -95,6 +95,15 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "btoi" version = "0.4.3" @@ -150,6 +159,7 @@ dependencies = [ "log", "ratatui", "ruci", + "schachmatt", "shakmaty", "simplelog", "toml", @@ -235,6 +245,15 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + [[package]] name = "crossterm" version = "0.28.1" @@ -260,6 +279,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "deranged" version = "0.3.11" @@ -269,6 +298,16 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "dirs" version = "5.0.1" @@ -324,6 +363,16 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f81ec6369c545a7d40e4589b5597581fa1c441fe1cce96dd1de43159910a36a2" +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getrandom" version = "0.2.15" @@ -470,6 +519,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "memchr" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" + [[package]] name = "mio" version = "1.0.2" @@ -548,6 +603,49 @@ version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" +[[package]] +name = "pest" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "989e7521a040efde50c3ab6bbadafbe15ab6dc042686926be59ac35d74607df4" +dependencies = [ + "memchr", + "ucd-trie", +] + +[[package]] +name = "pest_derive" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "187da9a3030dbafabbbfb20cb323b976dc7b7ce91fcd84f2f74d6e31d378e2de" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49b401d98f5757ebe97a26085998d6c0eecec4995cad6ab7fc30ffdf4b052843" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pest_meta" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f27a2cfee9f9039c4d86faa5af122a0ac3851441a34865b8a043b46be0065a" +dependencies = [ + "pest", + "sha2", +] + [[package]] name = "powerfmt" version = "0.2.0" @@ -610,7 +708,7 @@ checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4" dependencies = [ "getrandom", "libredox", - "thiserror", + "thiserror 1.0.51", ] [[package]] @@ -649,6 +747,17 @@ version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" +[[package]] +name = "schachmatt" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d31a71e025b058c4f5b398d4afd48353efccb091b7fa63962874ecde3da0640" +dependencies = [ + "pest", + "pest_derive", + "thiserror 2.0.17", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -675,6 +784,17 @@ dependencies = [ "syn", ] +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "shakmaty" version = "0.27.3" @@ -799,7 +919,16 @@ version = "1.0.51" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f11c217e1416d6f036b870f14e0413d480dbf28edbee1f877abaf0206af43bb7" dependencies = [ - "thiserror-impl", + "thiserror-impl 1.0.51", +] + +[[package]] +name = "thiserror" +version = "2.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f63587ca0f12b72a0600bcba1d40081f830876000bb46dd2337a3051618f4fc8" +dependencies = [ + "thiserror-impl 2.0.17", ] [[package]] @@ -813,6 +942,17 @@ dependencies = [ "syn", ] +[[package]] +name = "thiserror-impl" +version = "2.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ff15c8ecd7de3849db632e14d18d2571fa09dfc5ed93479bc4485c7a517c913" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "time" version = "0.3.37" @@ -855,6 +995,18 @@ dependencies = [ "serde", ] +[[package]] +name = "typenum" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb" + +[[package]] +name = "ucd-trie" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971" + [[package]] name = "unicode-ident" version = "1.0.12" @@ -890,6 +1042,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index af226c7..6566057 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ log = "0.4.25" simplelog = "0.12.2" chrono = "0.4.39" shakmaty = { version = "0.27.3", default-features = false } +schachmatt = "0.3.0" [features] chess-tui = [] diff --git a/src/game_logic/game_board.rs b/src/game_logic/game_board.rs index 2f34606..8065bb0 100644 --- a/src/game_logic/game_board.rs +++ b/src/game_logic/game_board.rs @@ -7,6 +7,7 @@ use crate::{ pieces::{pawn::Pawn, PieceColor, PieceMove, PieceType}, utils::col_to_letter, }; +use schachmatt::{Fen, Position, Piece, PieceType as SMPieceType, PlayerColor, Field, CastlingRights}; /// ## visual representation /// @@ -532,4 +533,55 @@ impl GameBoard { result } + + pub fn move_history_to_pgn_content(&self) -> String { + let mut pgn_content = String::new(); + pgn_content.push_str("[Event \"\""); + pgn_content.push_str("[Site \"\"]"); + pgn_content.push_str("[Date \"\"]"); + pgn_content.push_str("[Round \"\"]"); + pgn_content.push_str("[White \"\"]"); + pgn_content.push_str("[Black \"\"]"); + pgn_content.push_str("[Result \"\"]"); + pgn_content.push_str("\n\n"); + for move_entry in &self.move_history { + let mut board_setup: [[Option; 8]; 8] = [[None; 8]; 8]; + for i in 0..8 { + let mut column_setup: [Option; 8] = [None; 8]; + for j in 0..8 { + let sm_piece_type = match self.board[i][j].unwrap().0 { + PieceType::Pawn => SMPieceType::Pawn, + PieceType::Knight => SMPieceType::Knight, + PieceType::Bishop => SMPieceType::Bishop, + PieceType::Rook => SMPieceType::Rook, + PieceType::Queen => SMPieceType::Queen, + PieceType::King => SMPieceType::King, + }; + let sm_player_color = match self.board[i][j].unwrap().1 { + PieceColor::White => PlayerColor::White, + PieceColor::Black => PlayerColor::Black, + }; + column_setup[j] = Some(Piece::new(sm_piece_type, sm_player_color)); + } + board_setup[i] = column_setup; + } + let player_color = match move_entry.piece_color { + PieceColor::White => PlayerColor::White, + PieceColor::Black => PlayerColor::Black, + }; + let position = Position::new( + board_setup, + player_color, + CastlingRights::new(false, false), // TODO: White Castling rights + CastlingRights::new(false, false), // TODO: Black Castling rights + None, // TODO: En-passant + 0, // TODO: Halfmove clock + 0, // TODO: Fullmove clocks + ); + pgn_content.push_str(&Fen::export(&position)); + } + + pgn_content + } + }