fog-of-chess

[toy] Chess, but with the fog of war.
Log | Files | Refs

commit 1243d253cb0d392ea9045cda15d7de45b9e4ac9f
parent fa7ff45f7c62261a0122c88793c69c6088958b69
Author: Jack Mordaunt <jackmordaunt@gmail.com>
Date:   Thu, 26 Mar 2020 18:52:40 +0800

Feat: test subcommand. Entry point for testing scenarios.

Diffstat:
MCargo.lock | 136+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
MCargo.toml | 2++
Msrc/main.rs | 158++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
3 files changed, 259 insertions(+), 37 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -52,6 +52,14 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "approx" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -78,6 +86,16 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "hermit-abi 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "autocfg" version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -237,6 +255,8 @@ dependencies = [ name = "chess" version = "0.1.0" dependencies = [ + "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "derive_builder 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "ggez 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -251,6 +271,20 @@ dependencies = [ ] [[package]] +name = "clap" +version = "2.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "claxon" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -401,6 +435,38 @@ dependencies = [ ] [[package]] +name = "darling" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "darling_core 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", + "darling_macro 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "darling_core" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "ident_case 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "darling_macro" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "darling_core 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "deflate" version = "0.7.20" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -410,6 +476,29 @@ dependencies = [ ] [[package]] +name = "derive_builder" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "darling 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", + "derive_builder_core 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "derive_builder_core" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "darling 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "directories" version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -792,7 +881,7 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.1.5" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", @@ -804,6 +893,11 @@ version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "image" version = "0.22.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1172,7 +1266,7 @@ name = "num_cpus" version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "hermit-abi 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "hermit-abi 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1797,6 +1891,16 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "strsim" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "syn" version = "0.15.44" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1837,6 +1941,14 @@ dependencies = [ ] [[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "thread_local" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1877,6 +1989,11 @@ version = "1.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "unicode-width" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "unicode-xid" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2118,10 +2235,12 @@ dependencies = [ "checksum alsa-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b0edcbbf9ef68f15ae1b620f722180b82a98b6f0628d30baa6b8d2a5abc87d58" "checksum andrew 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9b7f09f89872c2b6b29e319377b1fbe91c6f5947df19a25596e121cf19a7b35e" "checksum android_glue 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "000444226fcff248f2bc4c7625be32c63caccfecc2723a2b9f78a7487a49c407" +"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" "checksum approx 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f0e60b75072ecd4168020818c0107f2857bb6c4e64252d8d3983f6263b40a5c3" "checksum arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0d382e583f07208808f6b1249e60848879ba3543f57c32277bf52d69c2f0f0ee" "checksum arrayvec 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9" "checksum arrayvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" +"checksum atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" "checksum autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" "checksum backtrace 0.3.40 (registry+https://github.com/rust-lang/crates.io-index)" = "924c76597f0d9ca25d762c25a4d369d51267536465dc5064bdf0eb073ed477ea" "checksum backtrace-sys 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "5d6575f128516de27e3ce99689419835fce9643a9b215a14d2b5b685be018491" @@ -2143,6 +2262,7 @@ dependencies = [ "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" "checksum cgl 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "55e7ec0b74fe5897894cbc207092c577e87c52f8a59e8ca8d97ef37551f60a49" "checksum clang-sys 0.28.1 (registry+https://github.com/rust-lang/crates.io-index)" = "81de550971c976f176130da4b2978d3b524eaa0fd9ac31f3ceb5ae1231fb4853" +"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" "checksum claxon 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f86c952727a495bda7abaf09bafdee1a939194dd793d9a8e26281df55ac43b00" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum cocoa 0.18.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1706996401131526e36b3b49f0c4d912639ce110996f3ca144d78946727bce54" @@ -2160,7 +2280,12 @@ dependencies = [ "checksum crossbeam-epoch 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5064ebdbf05ce3cb95e45c8b086f72263f4166b29b97f6baff7ef7fe047b55ac" "checksum crossbeam-utils 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "04973fa96e96579258a5091af6003abde64af786b860f18622b82e026cca60e6" "checksum crossbeam-utils 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce446db02cdc3165b94ae73111e570793400d0794e46125cc4056c81cbb039f4" +"checksum darling 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858" +"checksum darling_core 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b" +"checksum darling_macro 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72" "checksum deflate 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)" = "707b6a7b384888a70c8d2e8650b3e60170dfc6a67bb4aa67b6dfca57af4bedb4" +"checksum derive_builder 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a2658621297f2cf68762a6f7dc0bb7e1ff2cfd6583daef8ee0fed6f7ec468ec0" +"checksum derive_builder_core 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2791ea3e372c8495c0bc2033991d76b512cd799d07491fbd6890124db9458bef" "checksum directories 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "551a778172a450d7fc12e629ca3b0428d00f6afa9a43da1b630d54604e97371c" "checksum dirs-sys 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "afa0b23de8fd801745c471deffa6e12d248f962c9fd4b4c33787b055599bde7b" "checksum discard 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" @@ -2200,8 +2325,9 @@ dependencies = [ "checksum glyph_brush 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "131d7d6f3e7ea0988a707da50ae876f8a9c4c7eb397ccfac9fe9309d4ebe8726" "checksum glyph_brush 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6ec4a0787fffc6692ec1ccef9fe09a8e02df1b7fc8fadd5af96662dc979473f9" "checksum glyph_brush_layout 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "62b6d460a62dc8a2d3402689b02824ca03d40ec6f1673b9d2016d1f30ce5cb62" -"checksum hermit-abi 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f629dc602392d3ec14bfc8a09b5e644d7ffd725102b48b81e59f90f2633621d7" +"checksum hermit-abi 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1010591b26bbfe835e9faeabeb11866061cc7dcebffd56ad7d0942d0e61aefd8" "checksum hound 3.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8a164bb2ceaeff4f42542bdb847c41517c78a60f5649671b2a07312b6e117549" +"checksum ident_case 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" "checksum image 0.22.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b4be8aaefbe7545dc42ae925afb55a0098f226a3fe5ef721872806f44f57826" "checksum inflate 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1cdb29978cc5797bd8dcc8e5bf7de604891df2a8dc576973d71a281e916db2ff" "checksum io-kit-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f21dcc74995dd4cd090b147e79789f8d65959cbfb5f0b118002db869ea3bd0a0" @@ -2318,15 +2444,19 @@ dependencies = [ "checksum stdweb-derive 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c87a60a40fccc84bef0652345bbbbbe20a605bf5d0ce81719fc476f5c03b50ef" "checksum stdweb-internal-macros 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "58fa5ff6ad0d98d1ffa8cb115892b6e69d67799f6763e162a1c9db421dc22e11" "checksum stdweb-internal-runtime 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" +"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +"checksum strsim 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" "checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" "checksum syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "dff0acdb207ae2fe6d5976617f887eb1e35a2ba52c13c7234c790960cdad9238" "checksum synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545" "checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" +"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum tiff 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d7b7c2cfc4742bd8a32f2e614339dd8ce30dbcf676bb262bd63a2327bc5df57d" "checksum toml 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "01d1404644c8b12b16bfcffa4322403a91a451584daaaa7c28d3152e6cbc98cf" "checksum twox-hash 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3bfd5b7557925ce778ff9b9ef90e3ade34c524b5ff10e239c69a42d546d2af56" "checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" +"checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" diff --git a/Cargo.toml b/Cargo.toml @@ -8,3 +8,5 @@ edition = "2018" [dependencies] ggez = "0.5.1" +clap = "2.33.0" +derive_builder = "0.9.0" diff --git a/src/main.rs b/src/main.rs @@ -1,5 +1,7 @@ -#![windows_subsystem = "windows"] +// #![windows_subsystem = "windows"] +use clap::{App, Arg, SubCommand}; +use derive_builder::*; use ggez::event::{self, EventHandler}; use ggez::graphics; use ggez::graphics::{Color, DrawParam, MeshBuilder, Text}; @@ -7,6 +9,7 @@ use ggez::input::keyboard::{is_key_pressed, KeyCode}; use ggez::input::mouse::MouseButton; use ggez::{conf::WindowMode, conf::WindowSetup}; use ggez::{Context, ContextBuilder, GameResult}; +use std::error::Error; const PURE_APPLE: Color = Color { r: 106.0 / 256.0, @@ -30,12 +33,27 @@ const WIZARD_GREY: Color = Color { }; fn main() { + let app = App::new("Fog Of Chess") + .subcommand(SubCommand::with_name("test").arg(Arg::with_name("scenario").required(true))) + .get_matches(); + let board = match app.subcommand_matches("test") { + Some(test) => match Board::scenario( + test.value_of("scenario") + .expect("scenario argument missing"), + ) { + Some(board) => board, + None => panic!("scenario does not exist"), + }, + None => Board::new(), + }; + // let fog = app.is_present("no-fog"); + // TODO: Game builder API. let (mut ctx, mut event_loop) = ContextBuilder::new("Fog of War", "Jack Mordaunt") .window_mode(WindowMode::default().dimensions(800.0, 600.0)) .window_setup(WindowSetup::default().title("Fog of Chess")) .build() .expect("creating game loop"); - let mut g = Game::new(&mut ctx).expect("creating new game instance"); + let mut g = Game::with_board(&mut ctx, board).expect("creating new game instance"); match event::run(&mut ctx, &mut event_loop, &mut g) { Ok(_) => {} Err(e) => println!("error: {}", e), @@ -53,6 +71,7 @@ impl EventHandler for Game { let (col, row) = ((x / w_size).floor() as i32, (y / h_size).floor() as i32); if is_key_pressed(ctx, KeyCode::LShift) { if self.contains_ally((col, row)) { + // BUG: Avoid duplicates. self.selected.push((col, row)); } } else { @@ -214,7 +233,7 @@ impl EventHandler for Game { } /// Unique chess units. -#[derive(Debug)] +#[derive(Clone, Debug)] pub enum Unit { Pawn, Rook, @@ -225,14 +244,14 @@ pub enum Unit { } /// Player denotes the two unique players that can own units. -#[derive(Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq)] pub enum Player { White, Black, } /// Piece is a Unit-Player pair that represents a piece on the board. -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct Piece { pub unit: Unit, pub player: Player, @@ -241,10 +260,11 @@ pub struct Piece { } /// Board contains the location information of each piece. -#[derive(Default)] +#[derive(Clone, Default)] pub struct Board([[Option<Piece>; 8]; 8]); /// Game contains meta information. +#[derive(Clone, Builder)] pub struct Game { pub board: Board, pub turn: Player, @@ -267,6 +287,34 @@ impl Game { )?, }) } + /// Create a new game with the specified board. + pub fn with_board(ctx: &mut Context, b: Board) -> GameResult<Self> { + Ok(Game { + board: b, + turn: Player::White, + selected: vec![], + font: graphics::Font::new_glyph_font_bytes( + ctx, + include_bytes!("../res/DejaVuSansMono.ttf"), + )?, + }) + } + /// Create a board for the given scenario by name. + pub fn scenario(ctx: &mut Context, scenario: &str) -> Result<Self, Box<dyn Error>> { + let board = match Board::scenario(scenario) { + Some(b) => b, + None => return Err("scenario does not exist".into()), + }; + Ok(Game { + board: board, + turn: Player::White, + selected: vec![], + font: graphics::Font::new_glyph_font_bytes( + ctx, + include_bytes!("../res/DejaVuSansMono.ttf"), + )?, + }) + } /// Moves calculates all valid moves for the currently selected piece. pub fn moves(&self, pos: (i32, i32)) -> Vec<(i32, i32)> { let (x, y) = pos; @@ -469,35 +517,38 @@ impl Game { } // TODO: impl castle move. fn castle_move(&mut self) { - // Consider the first two moves of the selection as king and rook. - // Attempt the castle: - // - King and Rook must in original positions. - // - The two spaces between them must be empty. + // Outline: + // Consider the first two moves of the selection as king and rook. + // Attempt the castle: + // - King and Rook must be in original positions. + // - The two spaces between them must be empty. + // - King and Rook swap to the middle two pieces, completing the castle. + // // Clone out the first two selected coordinates. - // let pieces = self - // .selected - // .iter() - // .cloned() - // .take(2) - // .collect::<Vec<(i32, i32)>>(); - // // Check for King and Rook. - // if let ( - // Some(Piece { - // unit: Unit::King, .. - // }), - // Some(Piece { - // unit: Unit::Rook, .. - // }), - // ) = (self.board.get(pieces[0]), self.board.get(pieces[1])) - // { - // let (king, rook) = (pieces[0], pieces[1]); - // if (king.0 - rook.0).abs() == 2 { - // // correct distance - - // } - // // - Original positions - // // - Empty between them - // } + let pieces = self + .selected + .iter() + .cloned() + .take(2) + .collect::<Vec<(i32, i32)>>(); + let (king_pos, rook_pos) = (pieces[0], pieces[1]); + // Check for King and Rook. + if let ( + Some(&Piece { + unit: Unit::King, .. + }), + Some(&Piece { + unit: Unit::Rook, .. + }), + ) = (self.board.get(king_pos), self.board.get(rook_pos)) + { + if (king_pos.0 - rook_pos.0).abs() == 2 { + // correct distance + println!("can castle!"); + } + // - Original positions + // - Empty between them + } } } @@ -680,6 +731,45 @@ impl Board { ], ]) } + /// scenario sets up a board for the given scenario, identified by name. + pub fn scenario(title: &str) -> Option<Self> { + match title { + "castle" => Some(Board::castle_test()), + _ => None, + } + } + /// castle_test creates a new board for testing castle moves. + fn castle_test() -> Self { + use Player::*; + use Unit::*; + Board([ + [ + Some(Piece { + unit: Rook, + player: White, + moved: 0, + }), + None, + None, + Some(Piece { + unit: King, + player: White, + moved: 0, + }), + None, + None, + None, + None, + ], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + [None, None, None, None, None, None, None, None], + ]) + } /// Get the piece at the specified (x, y) coordinate. pub fn get(&self, pos: (i32, i32)) -> Option<&Piece> { let (x, y) = pos;