Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
6d415468e0
|
1
.gitignore
vendored
@@ -1,2 +1 @@
|
||||
/target
|
||||
/rdraught-wasm/dist
|
||||
|
35
Cargo.toml
@@ -1,13 +1,5 @@
|
||||
[workspace]
|
||||
members = [
|
||||
"rdraught",
|
||||
"rdraught-cli",
|
||||
"rdraught-gtk",
|
||||
"rdraught-w4",
|
||||
"rdraught-wasm",
|
||||
"rdraught-sdl",
|
||||
"rdraught-ui-common",
|
||||
]
|
||||
members = ["rdraught", "rdraught-cli", "rdraught-ui", "rdraught-w4"]
|
||||
resolver = "3"
|
||||
|
||||
[workspace.package]
|
||||
@@ -20,24 +12,17 @@ edition = "2024"
|
||||
rust-version = "1.87"
|
||||
|
||||
[workspace.dependencies]
|
||||
rdraught = { path = "rdraught", version = "0.1" }
|
||||
rdraught-ui-common = { path = "rdraught-ui-common", version = "0.1" }
|
||||
base64 = "0.22"
|
||||
rdraught = { path = "rdraught", version = "0.1.0" }
|
||||
cairo-rs = "0.20"
|
||||
cairo-sys-rs = "0.20"
|
||||
librsvg = "2.60"
|
||||
gtk4 = {version = "0.9", features = ["v4_10"] }
|
||||
gdk4 = "0.9"
|
||||
gio = "0.20"
|
||||
glib = "0.20"
|
||||
wasm4 = "0.2"
|
||||
wasm4-sys = "0.1"
|
||||
strum = "0.27"
|
||||
strum_macros = "0.27"
|
||||
heapless = "0.8"
|
||||
wasm-bindgen = "0.2"
|
||||
web-sys = "0.3"
|
||||
console_error_panic_hook = "0.1"
|
||||
sdl3 = { version = "0.14.35" }
|
||||
sdl3-sys = { version = "0.5" }
|
||||
rmath = {version = "0.1.0", registry = "gitea"}
|
||||
gio = "0.20.12"
|
||||
glib = "0.20.12"
|
||||
wasm4 = "0.2.0"
|
||||
wasm4-sys = "0.1.3"
|
||||
strum = "0.27.1"
|
||||
strum_macros = "0.27.1"
|
||||
heapless = "0.8.0"
|
||||
|
||||
|
@@ -1,35 +0,0 @@
|
||||
use glib::ExitCode;
|
||||
use rdraught::{DraughtsBoard, DraughtsGame, Move, Piece, Player, Position};
|
||||
use rdraught_gtk::run;
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn main() -> ExitCode {
|
||||
println!("move: {}", size_of::<Move>());
|
||||
println!("game: {}", size_of::<DraughtsGame>());
|
||||
|
||||
// let boards = [[(Position::new(2, 4), Piece::CrownedRedPawn)]];
|
||||
|
||||
// for pieces in boards.into_iter() {
|
||||
// let map = pieces.into_iter().collect::<HashMap<Position, Piece>>();
|
||||
// let board = DraughtsBoard::new(|p| match map.get(&p) {
|
||||
// None => Piece::NoPiece,
|
||||
// Some(piece) => *piece,
|
||||
// });
|
||||
// println!("{:?}", board);
|
||||
// for (pos, piece) in map.iter() {
|
||||
// assert_eq!(*piece, board.get(*pos));
|
||||
// }
|
||||
// }
|
||||
ExitCode::SUCCESS
|
||||
// let mut pieces = HashMap::<Position, Piece>::new();
|
||||
// pieces.insert(Position::new(2, 4), Piece::CrownedRedPawn);
|
||||
// pieces.insert(Position::new(5, 5), Piece::CrownedWhitePawn);
|
||||
// let game = DraughtsGame::new(
|
||||
// |p| match pieces.get(&p) {
|
||||
// None => Piece::NoPiece,
|
||||
// Some(piece) => *piece,
|
||||
// },
|
||||
// Player::Red,
|
||||
// );
|
||||
// run(game)
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
[package]
|
||||
name = "rdraught-sdl"
|
||||
authors.workspace = true
|
||||
edition.workspace = true
|
||||
homepage.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
rust-version.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
# [lib]
|
||||
# crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
|
||||
[dependencies.sdl3]
|
||||
workspace = true
|
||||
features = ["build-from-source-static"]
|
@@ -1,75 +0,0 @@
|
||||
use core::error::Error;
|
||||
use sdl3::event::Event;
|
||||
use sdl3::keyboard::Keycode;
|
||||
use sdl3::pixels::Color;
|
||||
use sdl3::rect::Rect;
|
||||
use std::time::Duration;
|
||||
|
||||
const WINDOW_WIDTH: u32 = 640;
|
||||
const WINDOW_HEIGHT: u32 = 640;
|
||||
const BOARD_SIZE: usize = 8;
|
||||
const SQUARE_SIZE: u32 = WINDOW_WIDTH / BOARD_SIZE as u32;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
// Initialize SDL
|
||||
let sdl_context = sdl3::init()?;
|
||||
let video_subsystem = sdl_context.video()?;
|
||||
|
||||
// Create window
|
||||
let window = video_subsystem
|
||||
.window("Chessboard", WINDOW_WIDTH, WINDOW_HEIGHT)
|
||||
.position_centered()
|
||||
.build()
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
// Create canvas
|
||||
let mut canvas = window.into_canvas();
|
||||
|
||||
// Set default color
|
||||
canvas.set_draw_color(Color::RGB(255, 255, 255));
|
||||
canvas.clear();
|
||||
canvas.present();
|
||||
|
||||
// Main loop
|
||||
let mut event_pump = sdl_context.event_pump()?;
|
||||
'running: loop {
|
||||
// Handle events
|
||||
for event in event_pump.poll_iter() {
|
||||
match event {
|
||||
Event::Quit { .. }
|
||||
| Event::KeyDown {
|
||||
keycode: Some(Keycode::Escape),
|
||||
..
|
||||
} => break 'running,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Draw chessboard
|
||||
canvas.set_draw_color(Color::RGB(255, 255, 255));
|
||||
canvas.clear();
|
||||
|
||||
for row in 0..BOARD_SIZE {
|
||||
for col in 0..BOARD_SIZE {
|
||||
let color = if (row + col) % 2 == 0 {
|
||||
Color::RGB(240, 217, 181) // Light squares
|
||||
} else {
|
||||
Color::RGB(181, 136, 99) // Dark squares
|
||||
};
|
||||
|
||||
canvas.set_draw_color(color);
|
||||
canvas.fill_rect(Rect::new(
|
||||
(col as u32 * SQUARE_SIZE) as i32,
|
||||
(row as u32 * SQUARE_SIZE) as i32,
|
||||
SQUARE_SIZE,
|
||||
SQUARE_SIZE,
|
||||
))?;
|
||||
}
|
||||
}
|
||||
|
||||
canvas.present();
|
||||
std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
@@ -1,16 +0,0 @@
|
||||
[package]
|
||||
name = "rdraught-ui-common"
|
||||
authors.workspace = true
|
||||
edition.workspace = true
|
||||
homepage.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
rust-version.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
[lib]
|
||||
crate-type = ["lib"]
|
||||
|
||||
[dependencies]
|
||||
rdraught.workspace = true
|
||||
rmath.workspace = true
|
@@ -1,261 +0,0 @@
|
||||
use rmath::NumericalMatrix;
|
||||
use rmath::SMatrix;
|
||||
use std::clone::Clone;
|
||||
use std::cmp::Eq;
|
||||
use std::cmp::PartialEq;
|
||||
use std::fmt::Display;
|
||||
use std::marker::Copy;
|
||||
use std::ops::Add;
|
||||
use std::ops::Div;
|
||||
use std::ops::Mul;
|
||||
use std::ops::Neg;
|
||||
use std::ops::Sub;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Point {
|
||||
x: f64,
|
||||
y: f64,
|
||||
}
|
||||
|
||||
impl Point {
|
||||
pub fn x(&self) -> f64 {
|
||||
self.x
|
||||
}
|
||||
|
||||
pub fn y(&self) -> f64 {
|
||||
self.y
|
||||
}
|
||||
|
||||
pub fn new(x: f64, y: f64) -> Point {
|
||||
Point { x, y }
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<Point> for Point {
|
||||
fn add(self, rhs: Point) -> Self::Output {
|
||||
Point {
|
||||
x: self.x() + rhs.x(),
|
||||
y: self.y() + rhs.y(),
|
||||
}
|
||||
}
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Sub<Point> for Point {
|
||||
fn sub(self, rhs: Point) -> Self::Output {
|
||||
Point {
|
||||
x: self.x() - rhs.x(),
|
||||
y: self.y() - rhs.y(),
|
||||
}
|
||||
}
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Mul<f64> for Point {
|
||||
fn mul(self, rhs: f64) -> Self::Output {
|
||||
Point::new(self.x * rhs, self.y * rhs)
|
||||
}
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Div<f64> for Point {
|
||||
fn div(self, rhs: f64) -> Self::Output {
|
||||
Point::new(self.x / rhs, self.y / rhs)
|
||||
}
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Neg for Point {
|
||||
fn neg(self) -> Self::Output {
|
||||
Point::new(-self.x(), -self.y())
|
||||
}
|
||||
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl PartialEq for Point {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.x.eq(&other.x) && self.y.eq(&other.y)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Point {}
|
||||
|
||||
impl Clone for Point {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl Copy for Point {}
|
||||
|
||||
impl Display for Point {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Point({}, {})", self.x, self.y)
|
||||
}
|
||||
}
|
||||
|
||||
impl Point {}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Rect {
|
||||
tl: Point,
|
||||
br: Point,
|
||||
}
|
||||
|
||||
impl Rect {
|
||||
pub fn new(tl: Point, br: Point) -> Rect {
|
||||
Rect { tl, br }
|
||||
}
|
||||
|
||||
pub fn from_size(tl: Point, width: f64, height: f64) -> Rect {
|
||||
Rect {
|
||||
tl,
|
||||
br: tl + Point::new(width, height),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn width(&self) -> f64 {
|
||||
self.br.x - self.tl.x
|
||||
}
|
||||
|
||||
pub fn height(&self) -> f64 {
|
||||
self.br.y - self.tl.y
|
||||
}
|
||||
|
||||
pub fn tl(&self) -> Point {
|
||||
self.tl
|
||||
}
|
||||
|
||||
pub fn br(&self) -> Point {
|
||||
self.br
|
||||
}
|
||||
|
||||
pub fn center(&self) -> Point {
|
||||
(self.tl + self.br) / 2.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Rect {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Rect({}, {})", self.tl, self.br)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Xform(SMatrix<f64, 3, 3>);
|
||||
|
||||
impl Xform {
|
||||
pub fn new(xx: f64, xy: f64, yx: f64, yy: f64, tx: f64, ty: f64) -> Xform {
|
||||
Xform(SMatrix::<f64, 3, 3>::new(|pos| match pos {
|
||||
(0, 0) => xx,
|
||||
(0, 1) => yx,
|
||||
(1, 0) => xy,
|
||||
(1, 1) => yy,
|
||||
(2, 0) => tx,
|
||||
(2, 1) => ty,
|
||||
(2, 2) => 1f64,
|
||||
_ => 0f64,
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn rot(alpha: f64) -> Xform {
|
||||
let sa = alpha.sin();
|
||||
let ca = alpha.cos();
|
||||
Xform(SMatrix::<f64, 3, 3>::new(|position| match position {
|
||||
(0, 0) => ca,
|
||||
(1, 1) => ca,
|
||||
(1, 0) => -sa,
|
||||
(0, 1) => sa,
|
||||
(2, 2) => 1f64,
|
||||
_ => 0f64,
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn scale(x: f64, y: f64) -> Xform {
|
||||
Xform(SMatrix::<f64, 3, 3>::new(|position| match position {
|
||||
(0, 0) => x,
|
||||
(1, 1) => y,
|
||||
(2, 2) => 1f64,
|
||||
_ => 0f64,
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn xlate(x: f64, y: f64) -> Xform {
|
||||
Xform(SMatrix::<f64, 3, 3>::new(|position| match position {
|
||||
(0, 0) => 1f64,
|
||||
(1, 1) => 1f64,
|
||||
(2, 2) => 1f64,
|
||||
(2, 0) => x,
|
||||
(2, 1) => y,
|
||||
_ => 0f64,
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn xx(&self) -> f64 {
|
||||
self.0[(0, 0)]
|
||||
}
|
||||
|
||||
pub fn xy(&self) -> f64 {
|
||||
self.0[(1, 0)]
|
||||
}
|
||||
|
||||
pub fn tx(&self) -> f64 {
|
||||
self.0[(2, 0)]
|
||||
}
|
||||
|
||||
pub fn yx(&self) -> f64 {
|
||||
self.0[(1, 0)]
|
||||
}
|
||||
|
||||
pub fn yy(&self) -> f64 {
|
||||
self.0[(1, 1)]
|
||||
}
|
||||
|
||||
pub fn ty(&self) -> f64 {
|
||||
self.0[(2, 1)]
|
||||
}
|
||||
|
||||
pub fn invert(&self) -> Xform {
|
||||
Xform(self.0.clone().invert())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Xform {
|
||||
fn default() -> Self {
|
||||
Xform(SMatrix::<f64, 3, 3>::identity(3))
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<&Xform> for Point {
|
||||
fn mul(self, rhs: &Xform) -> Self::Output {
|
||||
Point::new(
|
||||
self.x * rhs.0[(0, 0)] + self.y * rhs.0[(1, 0)] + rhs.0[(2, 0)],
|
||||
self.x * rhs.0[(0, 1)] + self.y * rhs.0[(1, 1)] + rhs.0[(2, 1)],
|
||||
)
|
||||
}
|
||||
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Mul<Xform> for Xform {
|
||||
type Output = Xform;
|
||||
|
||||
fn mul(self, rhs: Xform) -> Self::Output {
|
||||
Xform(self.0 * rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<&Xform> for &Xform {
|
||||
type Output = Xform;
|
||||
|
||||
fn mul(self, rhs: &Xform) -> Self::Output {
|
||||
Xform(&self.0 * &rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Xform {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
@@ -1,5 +0,0 @@
|
||||
mod geo2d;
|
||||
mod types;
|
||||
|
||||
pub use geo2d::{Point, Rect, Xform};
|
||||
pub use types::{SharedMutable, SharedMutableRef, new_shared_mut, new_shared_mut_ref};
|
@@ -1,12 +0,0 @@
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
pub type SharedMutable<T> = Rc<Cell<T>>;
|
||||
pub type SharedMutableRef<T> = Rc<RefCell<T>>;
|
||||
pub fn new_shared_mut_ref<T>(obj: T) -> SharedMutableRef<T> {
|
||||
Rc::new(RefCell::new(obj))
|
||||
}
|
||||
|
||||
pub fn new_shared_mut<T>(obj: T) -> SharedMutable<T> {
|
||||
Rc::new(Cell::new(obj))
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
[package]
|
||||
name = "rdraught-gtk"
|
||||
name = "rdraught-ui"
|
||||
authors.workspace = true
|
||||
edition.workspace = true
|
||||
homepage.workspace = true
|
||||
@@ -12,17 +12,16 @@ version.workspace = true
|
||||
gtk4.workspace = true
|
||||
gdk4.workspace = true
|
||||
rdraught = {workspace = true, features = ["std"]}
|
||||
rdraught-ui-common = { workspace = true }
|
||||
librsvg.workspace = true
|
||||
cairo-rs.workspace = true
|
||||
gio.workspace = true
|
||||
glib.workspace = true
|
||||
|
||||
[lib]
|
||||
name = "rdraught_gtk"
|
||||
name = "rdraught_ui"
|
||||
crate-type = ["lib"]
|
||||
bench = false
|
||||
|
||||
[[bin]]
|
||||
name = "rdraught-gtk"
|
||||
name = "rdraught-ui"
|
||||
path = "src/main.rs"
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
19
rdraught-ui/examples/ai_debugger.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use glib::ExitCode;
|
||||
use rdraught::draughts::DraughtsGame;
|
||||
use rdraught::{draughts::Piece, draughts::Player, position::Position};
|
||||
use rdraught_ui::run;
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let mut pieces = HashMap::<Position, Piece>::new();
|
||||
pieces.insert(Position::new(2, 4), Piece::CrownedRedPawn);
|
||||
pieces.insert(Position::new(5, 5), Piece::CrownedWhitePawn);
|
||||
let game = DraughtsGame::new(
|
||||
|p| match pieces.get(&p) {
|
||||
None => Piece::NoPiece,
|
||||
Some(piece) => *piece,
|
||||
},
|
||||
Player::Red,
|
||||
);
|
||||
run(game)
|
||||
}
|
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
96
rdraught-ui/src/geo2d.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
use std::clone::Clone;
|
||||
use std::cmp::Eq;
|
||||
use std::cmp::PartialEq;
|
||||
use std::fmt::Display;
|
||||
use std::marker::Copy;
|
||||
use std::ops::Add;
|
||||
use std::ops::Div;
|
||||
use std::ops::Mul;
|
||||
use std::ops::Neg;
|
||||
use std::ops::Sub;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Point {
|
||||
x: f64,
|
||||
y: f64,
|
||||
}
|
||||
|
||||
impl Point {
|
||||
pub fn x(&self) -> f64 {
|
||||
self.x
|
||||
}
|
||||
|
||||
pub fn y(&self) -> f64 {
|
||||
self.y
|
||||
}
|
||||
|
||||
pub fn new(x: f64, y: f64) -> Point {
|
||||
Point { x, y }
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<&Point> for &Point {
|
||||
fn add(self, rhs: &Point) -> Self::Output {
|
||||
Point {
|
||||
x: self.x() + rhs.x(),
|
||||
y: self.y() + rhs.y(),
|
||||
}
|
||||
}
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Sub<&Point> for &Point {
|
||||
fn sub(self, rhs: &Point) -> Self::Output {
|
||||
Point {
|
||||
x: self.x() - rhs.x(),
|
||||
y: self.y() - rhs.y(),
|
||||
}
|
||||
}
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Mul<f64> for &Point {
|
||||
fn mul(self, rhs: f64) -> Self::Output {
|
||||
Point::new(self.x * rhs, self.y * rhs)
|
||||
}
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Div<f64> for &Point {
|
||||
fn div(self, rhs: f64) -> Self::Output {
|
||||
Point::new(self.x / rhs, self.y / rhs)
|
||||
}
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Neg for &Point {
|
||||
fn neg(self) -> Self::Output {
|
||||
Point::new(-self.x(), -self.y())
|
||||
}
|
||||
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl PartialEq for Point {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.x.eq(&other.x) && self.y.eq(&other.y)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Point {}
|
||||
|
||||
impl Clone for Point {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl Copy for Point {}
|
||||
|
||||
impl Display for Point {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Point({}, {})", self.x, self.y)
|
||||
}
|
||||
}
|
||||
|
||||
impl Point {}
|
@@ -1,7 +1,7 @@
|
||||
use gtk4::{Align, Application, Box, CheckButton, Label, Orientation, Window, prelude::*};
|
||||
|
||||
use crate::types::SharedMutable;
|
||||
use rdraught::Player;
|
||||
use rdraught_ui_common::SharedMutable;
|
||||
|
||||
pub(crate) fn create(application: &Application, current_player: SharedMutable<Player>) -> Window {
|
||||
let label = Label::builder().label("Main player:").build();
|
@@ -1,5 +1,8 @@
|
||||
mod geo2d;
|
||||
|
||||
mod final_dialog;
|
||||
mod greeting_dialog;
|
||||
mod rdraught_application;
|
||||
mod types;
|
||||
|
||||
pub use rdraught_application::run;
|
@@ -1,7 +1,8 @@
|
||||
use glib::ExitCode;
|
||||
use rdraught::draughts::DraughtsGame;
|
||||
use rdraught_ui;
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let game = DraughtsGame::default();
|
||||
rdraught_gtk::run(game)
|
||||
rdraught_ui::run(game)
|
||||
}
|
@@ -1,21 +1,23 @@
|
||||
use super::geo2d::Point;
|
||||
use core::f64::consts::PI;
|
||||
use gdk4::cairo::{Context as CairoContext, Matrix, Rectangle};
|
||||
use glib::ExitCode;
|
||||
use gtk4::glib::{MainContext, Propagation};
|
||||
use gtk4::{self as gtk, gdk::ffi::GDK_BUTTON_PRIMARY};
|
||||
use gtk4::{Application, Button, DrawingArea, HeaderBar, prelude::*};
|
||||
use gtk4::{Application, DrawingArea, prelude::*};
|
||||
use rdraught::{
|
||||
DraughtsBoard, DraughtsGame, Move, Piece, Player, Position, RDraughtApplication,
|
||||
DraughtsBoard, DraughtsGame, Error, Move, Piece, Player, Position, RDraughtApplication,
|
||||
RectangularBoard,
|
||||
};
|
||||
use rsvg::SvgHandle;
|
||||
use std::thread;
|
||||
const SQUARE_SIZE: f64 = 1.0;
|
||||
|
||||
use super::final_dialog;
|
||||
use super::greeting_dialog;
|
||||
use rdraught_ui_common::{
|
||||
Point, SharedMutable, SharedMutableRef, new_shared_mut, new_shared_mut_ref,
|
||||
};
|
||||
use super::types;
|
||||
|
||||
use types::{SharedMutable, SharedMutableRef, new_shared_mut, new_shared_mut_ref};
|
||||
|
||||
const CROWN_RED: &[u8] = include_bytes!("crown_red.svg");
|
||||
const CROWN_WHITE: &[u8] = include_bytes!("crown_white.svg");
|
||||
@@ -242,64 +244,14 @@ fn create_game_window(
|
||||
.default_width(800)
|
||||
.default_height(800)
|
||||
.build();
|
||||
let header_bar = HeaderBar::new();
|
||||
window.set_titlebar(Some(&header_bar));
|
||||
|
||||
let undo_button = Button::new();
|
||||
undo_button.set_icon_name("edit-undo-symbolic");
|
||||
undo_button.set_sensitive(false);
|
||||
|
||||
let redo_button = Button::new();
|
||||
redo_button.set_icon_name("edit-redo-symbolic");
|
||||
redo_button.set_sensitive(false);
|
||||
|
||||
let selected_piece: SharedMutable<Option<Position>> = new_shared_mut(None);
|
||||
let available_moves = new_shared_mut_ref(Vec::<Move>::new());
|
||||
// Create a DrawingArea widget where we will draw the chessboard.
|
||||
let drawing_area = DrawingArea::new();
|
||||
// Add the drawing area to the window.
|
||||
window.set_child(Some(&drawing_area));
|
||||
|
||||
header_bar.pack_start(&undo_button);
|
||||
{
|
||||
let da = drawing_area.clone();
|
||||
let rd = rd.clone();
|
||||
let undo_btn_clone = undo_button.clone();
|
||||
let redo_btn_clone = redo_button.clone();
|
||||
let selected_piece = selected_piece.clone();
|
||||
let available_moves = available_moves.clone();
|
||||
undo_button.connect_clicked(move |_| {
|
||||
let mut rd = rd.borrow_mut();
|
||||
if rd.can_undo() {
|
||||
rd.undo().ok();
|
||||
redo_btn_clone.set_sensitive(rd.can_redo());
|
||||
undo_btn_clone.set_sensitive(rd.can_undo());
|
||||
selected_piece.set(None);
|
||||
available_moves.borrow_mut().clear();
|
||||
da.queue_draw();
|
||||
}
|
||||
});
|
||||
}
|
||||
header_bar.pack_start(&redo_button);
|
||||
{
|
||||
let da = drawing_area.clone();
|
||||
let rd = rd.clone();
|
||||
let undo_btn_clone = undo_button.clone();
|
||||
let redo_btn_clone = redo_button.clone();
|
||||
let selected_piece = selected_piece.clone();
|
||||
let available_moves = available_moves.clone();
|
||||
redo_button.connect_clicked(move |_| {
|
||||
let mut rd = rd.borrow_mut();
|
||||
if rd.can_redo() {
|
||||
rd.redo().ok();
|
||||
redo_btn_clone.set_sensitive(rd.can_redo());
|
||||
undo_btn_clone.set_sensitive(rd.can_undo());
|
||||
selected_piece.set(None);
|
||||
available_moves.borrow_mut().clear();
|
||||
da.queue_draw();
|
||||
}
|
||||
});
|
||||
}
|
||||
let selected_piece: SharedMutable<Option<Position>> = new_shared_mut(None);
|
||||
let available_moves = new_shared_mut_ref(Vec::<Move>::new());
|
||||
// Get the allocation information for the widget.
|
||||
let board_width = SQUARE_SIZE * DraughtsBoard::rows() as f64;
|
||||
let board_height = SQUARE_SIZE * DraughtsBoard::columns() as f64;
|
||||
@@ -477,8 +429,6 @@ fn create_game_window(
|
||||
let drawing_area = drawing_area.clone();
|
||||
let available_moves = available_moves.clone();
|
||||
let window = window.clone();
|
||||
let undo_btn_clone = undo_button.clone();
|
||||
let redo_btn_clone = redo_button.clone();
|
||||
gesture.connect_pressed(move |gesture, _, x, y| {
|
||||
gesture.set_state(gtk::EventSequenceState::Claimed);
|
||||
if let Some(winner) = rd.borrow().game().winner() {
|
||||
@@ -493,7 +443,7 @@ fn create_game_window(
|
||||
};
|
||||
let p = transform_point(&Point::new(x, y), &inverse);
|
||||
if board_clone.contains(&p) {
|
||||
let p = p - board_clone.tl();
|
||||
let p = &p - &board_clone.tl();
|
||||
// println!("Point: {:?}", p);
|
||||
let position = match current_player {
|
||||
Player::White => Position::new(
|
||||
@@ -505,7 +455,7 @@ fn create_game_window(
|
||||
(8.0 - p.x() / SQUARE_SIZE) as u8,
|
||||
),
|
||||
};
|
||||
// println!("Selected position: {:?}", position);
|
||||
println!("Selected position: {:?}", position);
|
||||
let piece = {
|
||||
let rd = rd.borrow();
|
||||
let draughts_game = rd.game();
|
||||
@@ -521,19 +471,19 @@ fn create_game_window(
|
||||
for mv in am.into_iter() {
|
||||
if mv.get_end_position() == pos {
|
||||
let mut rd_app = rd.borrow_mut();
|
||||
// println!("Applied move: {:?}", mv);
|
||||
println!("Applied move: {:?}", mv);
|
||||
rd_app.apply_move(mv).unwrap();
|
||||
// let game_copy = rd_app.game().clone();
|
||||
// thread::spawn(move || {
|
||||
// if let (Some(mv), analyzed_moves) =
|
||||
// game_copy.get_best_move(10)
|
||||
// {
|
||||
// println!(
|
||||
// "Next best move: {:?}, analyzed moves: {}",
|
||||
// mv, analyzed_moves
|
||||
// );
|
||||
// }
|
||||
// });
|
||||
let game_copy = rd_app.game().clone();
|
||||
thread::spawn(move || {
|
||||
if let (Some(mv), analyzed_moves) =
|
||||
game_copy.get_best_move(10)
|
||||
{
|
||||
println!(
|
||||
"Next best move: {:?}, analyzed moves: {}",
|
||||
mv, analyzed_moves
|
||||
);
|
||||
}
|
||||
});
|
||||
move_applied = true;
|
||||
break;
|
||||
}
|
||||
@@ -541,9 +491,6 @@ fn create_game_window(
|
||||
}
|
||||
if move_applied {
|
||||
selected_piece.set(None);
|
||||
let rd = rd.borrow();
|
||||
undo_btn_clone.set_sensitive(rd.can_undo());
|
||||
redo_btn_clone.set_sensitive(rd.can_redo());
|
||||
}
|
||||
}
|
||||
if !move_applied {
|
@@ -1,20 +0,0 @@
|
||||
[build]
|
||||
target = "wasm32-unknown-unknown"
|
||||
|
||||
[target.wasm32-unknown-unknown]
|
||||
rustflags = [
|
||||
"-A", "dead_code",
|
||||
|
||||
# Import memory from WASM-4
|
||||
"-C", "link-arg=--import-memory",
|
||||
"-C", "link-arg=--initial-memory=65536",
|
||||
"-C", "link-arg=--max-memory=65536",
|
||||
# "-C", "link-args=--relocatable",
|
||||
|
||||
# Reserve 2044 bytes of stack space, offset from 6580.
|
||||
# Bump this value, 16-byte aligned, if the framebuffer gets corrupted.
|
||||
"-C", "link-arg=-zstack-size=14752",
|
||||
|
||||
# Not working? https://github.com/rust-lang/rust/issues/46645#issuecomment-423912553
|
||||
# "-C", "link-arg=--global-base=14752",
|
||||
]
|
@@ -13,10 +13,7 @@ wasm4.workspace = true
|
||||
wasm4-sys.workspace = true
|
||||
rdraught.workspace = true
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[profile.release]
|
||||
opt-level = "z"
|
||||
lto = true
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
default-target = "wasm32-unknown-unknown"
|
||||
targets = []
|
||||
|
@@ -1,223 +0,0 @@
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
use rdraught::{DraughtsBoard, DraughtsGame, Piece, Position};
|
||||
use wasm4::draw::{Color, DrawIndex, DrawIndices, Framebuffer};
|
||||
use wasm4::rt::{Resources, Runtime};
|
||||
// use wasm4 as w4;
|
||||
use core::arch::wasm32::unreachable;
|
||||
use wasm4::sys::DRAW_COLORS;
|
||||
use wasm4::trace;
|
||||
// mod wasm4;
|
||||
|
||||
// use wasm4::{DRAW_COLORS, oval, rect};
|
||||
|
||||
// mod palette;
|
||||
|
||||
// static mut GAME: DraughtsGame = DraughtsGame::default();
|
||||
struct Rdraught_W4 {
|
||||
framebuffer: Framebuffer,
|
||||
game: DraughtsGame,
|
||||
}
|
||||
|
||||
impl Runtime for Rdraught_W4 {
|
||||
fn start(rs: Resources) -> Self {
|
||||
rs.framebuffer.replace_palette([
|
||||
Color(0x00edeada),
|
||||
Color(0x00c74634),
|
||||
Color(0x005d8d60),
|
||||
Color(0x00100f24),
|
||||
]);
|
||||
Rdraught_W4 {
|
||||
framebuffer: rs.framebuffer,
|
||||
game: DraughtsGame::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self) {
|
||||
let indices = DrawIndices::from_array([
|
||||
DrawIndex::Third,
|
||||
DrawIndex::Fourth,
|
||||
DrawIndex::Fourth,
|
||||
DrawIndex::Transparent,
|
||||
]);
|
||||
unsafe {
|
||||
*DRAW_COLORS = indices.into_u16();
|
||||
}
|
||||
|
||||
self.framebuffer.rect([10, 10], [50, 50]);
|
||||
self.framebuffer.oval([50, 50], [10, 10]);
|
||||
let SQUARE_SIZE = 20u32;
|
||||
for i in 0..8i32 {
|
||||
for j in 0..8i32 {
|
||||
let indices = if (i + j) % 2 == 0 {
|
||||
DrawIndices::from_array([
|
||||
DrawIndex::Third,
|
||||
DrawIndex::Transparent,
|
||||
DrawIndex::Fourth,
|
||||
DrawIndex::Transparent,
|
||||
])
|
||||
} else {
|
||||
DrawIndices::from_array([
|
||||
DrawIndex::Fourth,
|
||||
DrawIndex::Transparent,
|
||||
DrawIndex::Fourth,
|
||||
DrawIndex::Transparent,
|
||||
])
|
||||
};
|
||||
unsafe {
|
||||
*DRAW_COLORS = indices.into_u16();
|
||||
}
|
||||
let tl = [i as i32 * SQUARE_SIZE as i32, j as i32 * SQUARE_SIZE as i32];
|
||||
self.framebuffer.rect(tl, [SQUARE_SIZE, SQUARE_SIZE]);
|
||||
|
||||
let score = self.game.score_for_player(rdraught::Player::Red);
|
||||
// let msg = format!("{}", score);
|
||||
// w4::trace(msg.as_str());
|
||||
|
||||
let piece = self.game.piece_at(Position::new(i as u8, j as u8).unwrap());
|
||||
match piece {
|
||||
Piece::SimpleWhitePawn => {
|
||||
let indices = DrawIndices::from_array([
|
||||
DrawIndex::First,
|
||||
DrawIndex::Transparent,
|
||||
DrawIndex::Transparent,
|
||||
DrawIndex::Transparent,
|
||||
]);
|
||||
unsafe {
|
||||
*DRAW_COLORS = indices.into_u16();
|
||||
}
|
||||
self.framebuffer.oval(tl, [SQUARE_SIZE, SQUARE_SIZE]);
|
||||
}
|
||||
Piece::SimpleRedPawn => {
|
||||
let indices = DrawIndices::from_array([
|
||||
DrawIndex::Second,
|
||||
DrawIndex::Transparent,
|
||||
DrawIndex::Transparent,
|
||||
DrawIndex::Transparent,
|
||||
]);
|
||||
unsafe {
|
||||
*DRAW_COLORS = indices.into_u16();
|
||||
}
|
||||
self.framebuffer.oval(tl, [SQUARE_SIZE, SQUARE_SIZE]);
|
||||
}
|
||||
Piece::CrownedRedPawn => {}
|
||||
Piece::CrownedWhitePawn => {}
|
||||
Piece::NoPiece => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wasm4::main! { Rdraught_W4 }
|
||||
|
||||
// use wasm4::*;
|
||||
// use wasm4_sys;
|
||||
|
||||
// #[unsafe(no_mangle)]
|
||||
// fn update() {
|
||||
// unsafe {
|
||||
// wasm4_sys::rect(10, 10, 32, 32);
|
||||
// }
|
||||
// }
|
||||
// #[unsafe(no_mangle)]
|
||||
// fn start() {
|
||||
// palette::change_palette(0usize);
|
||||
// palette::set_draw_color(2u16);
|
||||
// }
|
||||
|
||||
// #[unsafe(no_mangle)]
|
||||
// fn update() {
|
||||
// // if self.count % 60 == 0 {
|
||||
// // w4::trace("tick");
|
||||
// // self.count = 0;
|
||||
// // }
|
||||
// // self.count += 1;
|
||||
// // let indices = DrawIndices::from_array([
|
||||
// // DrawIndex::Third,
|
||||
// // DrawIndex::Fourth,
|
||||
// // DrawIndex::Fourth,
|
||||
// // DrawIndex::Transparent,
|
||||
// // ]);
|
||||
// // unsafe {
|
||||
// // *DRAW_COLORS = 0x;
|
||||
// // }
|
||||
// palette::set_draw_color([3, 4, 4, 0]);
|
||||
|
||||
// rect(10, 10, 50, 50);
|
||||
// oval(50, 50, 10, 10);
|
||||
// let SQUARE_SIZE = 20u32;
|
||||
// for i in 0..8i32 {
|
||||
// for j in 0..8i32 {
|
||||
// // let indices = if (i + j) % 2 == 0 {
|
||||
// // DrawIndices::from_array([
|
||||
// // DrawIndex::Third,
|
||||
// // DrawIndex::Transparent,
|
||||
// // DrawIndex::Fourth,
|
||||
// // DrawIndex::Transparent,
|
||||
// // ])
|
||||
// // } else {
|
||||
// // DrawIndices::from_array([
|
||||
// // DrawIndex::Fourth,
|
||||
// // DrawIndex::Transparent,
|
||||
// // DrawIndex::Fourth,
|
||||
// // DrawIndex::Transparent,
|
||||
// // ])
|
||||
// // };
|
||||
// // unsafe {
|
||||
// // *DRAW_COLORS = indices.into_u16();
|
||||
// // }
|
||||
// let tl = [i as i32 * SQUARE_SIZE as i32, j as i32 * SQUARE_SIZE as i32];
|
||||
// rect(tl.0, tl.1, SQUARE_SIZE, SQUARE_SIZE);
|
||||
|
||||
// // let score = self.game.score_for_player(rdraught::Player::Red);
|
||||
// // let msg = format!("{}", score);
|
||||
// // w4::trace(msg.as_str());
|
||||
// unsafe {
|
||||
// let piece = GAME.piece_at(Position::new(i as u8, j as u8).unwrap());
|
||||
// match piece {
|
||||
// Piece::SimpleWhitePawn => {
|
||||
// // let indices = DrawIndices::from_array([
|
||||
// // DrawIndex::First,
|
||||
// // DrawIndex::Transparent,
|
||||
// // DrawIndex::Transparent,
|
||||
// // DrawIndex::Transparent,
|
||||
// // ]);
|
||||
// // unsafe {
|
||||
// // *DRAW_COLORS = indices.into_u16();
|
||||
// // }
|
||||
// oval(tl, [SQUARE_SIZE, SQUARE_SIZE]);
|
||||
// }
|
||||
// Piece::SimpleRedPawn => {
|
||||
// // let indices = DrawIndices::from_array([
|
||||
// // DrawIndex::Second,
|
||||
// // DrawIndex::Transparent,
|
||||
// // DrawIndex::Transparent,
|
||||
// // DrawIndex::Transparent,
|
||||
// // ]);
|
||||
// // unsafe {
|
||||
// // *DRAW_COLORS = indices.into_u16();
|
||||
// // }
|
||||
// oval(tl, [SQUARE_SIZE, SQUARE_SIZE]);
|
||||
// }
|
||||
// Piece::CrownedRedPawn => {}
|
||||
// Piece::CrownedWhitePawn => {}
|
||||
// Piece::NoPiece => {}
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
#[panic_handler]
|
||||
fn panic_handler(_panic_info: &core::panic::PanicInfo<'_>) -> ! {
|
||||
trace("panic error");
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
if let Some(cause) = _panic_info.payload().downcast_ref::<&str>() {
|
||||
trace(cause);
|
||||
}
|
||||
|
||||
unreachable()
|
||||
}
|
45
rdraught-w4/src/main.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
#![no_main]
|
||||
|
||||
use w4::draw::{Color, Framebuffer};
|
||||
use w4::rt::Resources;
|
||||
use wasm4 as w4;
|
||||
|
||||
struct Rdraught_wasm4 {
|
||||
framebuffer: Framebuffer,
|
||||
}
|
||||
|
||||
impl w4::rt::Runtime for Rdraught_wasm4 {
|
||||
fn start(rs: Resources) -> Self {
|
||||
rs.framebuffer.replace_palette([
|
||||
Color(0xff000000),
|
||||
Color(0x00ff0000),
|
||||
Color(0x0000ff00),
|
||||
Color(0xffff0000),
|
||||
]);
|
||||
Rdraught_wasm4 {
|
||||
framebuffer: rs.framebuffer,
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self) {
|
||||
// if self.count % 60 == 0 {
|
||||
// w4::trace("tick");
|
||||
// self.count = 0;
|
||||
// }
|
||||
// self.count += 1;
|
||||
self.framebuffer.rect([10, 10], [50, 50]);
|
||||
self.framebuffer.oval([50, 50], [10, 10]);
|
||||
}
|
||||
}
|
||||
|
||||
w4::main! { Rdraught_wasm4 }
|
||||
|
||||
// use wasm4::*;
|
||||
// use wasm4_sys;
|
||||
|
||||
// #[unsafe(no_mangle)]
|
||||
// fn update() {
|
||||
// unsafe {
|
||||
// wasm4_sys::rect(10, 10, 32, 32);
|
||||
// }
|
||||
// }
|
@@ -1,29 +0,0 @@
|
||||
use super::wasm4::DRAW_COLORS;
|
||||
use super::wasm4::PALETTE;
|
||||
|
||||
type Palette = [u32; 4];
|
||||
|
||||
pub const W4_DEFAULT: Palette = [0xe0f8cf, 0x86c06c, 0x306850, 0x071821];
|
||||
|
||||
pub const PONG_OG: Palette = [0x1b1b1b, 0xdcdcdc, 0x1b1b1b, 0xdcdcdc];
|
||||
|
||||
pub const ICE_CREAM_GB: Palette = [0xfff6d3, 0xf9a875, 0xeb6b6f, 0x7c3f58];
|
||||
|
||||
pub const HOLLOW: Palette = [0x0f0f1b, 0x565a75, 0xc5b7be, 0xf9fbf5];
|
||||
|
||||
const PALETTES: [Palette; 4] = [PONG_OG, ICE_CREAM_GB, HOLLOW, W4_DEFAULT];
|
||||
|
||||
#[inline]
|
||||
fn palette_by_idx<T: Into<usize>>(idx: T) -> Palette {
|
||||
PALETTES[idx.into() % PALETTES.len()]
|
||||
}
|
||||
|
||||
pub fn change_palette<T: Into<usize>>(idx: T) {
|
||||
unsafe {
|
||||
*PALETTE = palette_by_idx(idx);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_draw_color<T: Into<u16>>(idx: T) {
|
||||
unsafe { *DRAW_COLORS = idx.into() }
|
||||
}
|
@@ -1,196 +0,0 @@
|
||||
//
|
||||
// WASM-4: https://wasm4.org/docs
|
||||
|
||||
// ┌───────────────────────────────────────────────────────────────────────────┐
|
||||
// │ │
|
||||
// │ Platform Constants │
|
||||
// │ │
|
||||
// └───────────────────────────────────────────────────────────────────────────┘
|
||||
|
||||
pub const SCREEN_SIZE: u32 = 160;
|
||||
pub const FONT_SIZE: u32 = 8;
|
||||
|
||||
// ┌───────────────────────────────────────────────────────────────────────────┐
|
||||
// │ │
|
||||
// │ Memory Addresses │
|
||||
// │ │
|
||||
// └───────────────────────────────────────────────────────────────────────────┘
|
||||
|
||||
pub static mut PALETTE: *mut [u32; 4] = 0x04 as *mut [u32; 4];
|
||||
pub const DRAW_COLORS: *mut u16 = 0x14 as *mut u16;
|
||||
pub const GAMEPAD1: *const u8 = 0x16 as *const u8;
|
||||
pub const GAMEPAD2: *const u8 = 0x17 as *const u8;
|
||||
pub const GAMEPAD3: *const u8 = 0x18 as *const u8;
|
||||
pub const GAMEPAD4: *const u8 = 0x19 as *const u8;
|
||||
pub const MOUSE_X: *const i16 = 0x1a as *const i16;
|
||||
pub const MOUSE_Y: *const i16 = 0x1c as *const i16;
|
||||
pub const MOUSE_BUTTONS: *const u8 = 0x1e as *const u8;
|
||||
pub static mut FRAMEBUFFER: *mut [u8; 6400] = 0xa0 as *mut [u8; 6400];
|
||||
|
||||
pub const BUTTON_1: u8 = 1;
|
||||
pub const BUTTON_2: u8 = 2;
|
||||
pub const BUTTON_LEFT: u8 = 16;
|
||||
pub const BUTTON_RIGHT: u8 = 32;
|
||||
pub const BUTTON_UP: u8 = 64;
|
||||
pub const BUTTON_DOWN: u8 = 128;
|
||||
|
||||
// ┌───────────────────────────────────────────────────────────────────────────┐
|
||||
// │ │
|
||||
// │ Drawing Functions │
|
||||
// │ │
|
||||
// └───────────────────────────────────────────────────────────────────────────┘
|
||||
|
||||
/// Copies pixels to the framebuffer.
|
||||
pub fn blit(sprite: &[u8], x: i32, y: i32, width: u32, height: u32, flags: u32) {
|
||||
unsafe { extern_blit(sprite.as_ptr(), x, y, width, height, flags) }
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "blit"]
|
||||
fn extern_blit(sprite: *const u8, x: i32, y: i32, width: u32, height: u32, flags: u32);
|
||||
}
|
||||
|
||||
/// Copies a subregion within a larger sprite atlas to the framebuffer.
|
||||
pub fn blit_sub(
|
||||
sprite: &[u8],
|
||||
x: i32,
|
||||
y: i32,
|
||||
width: u32,
|
||||
height: u32,
|
||||
src_x: u32,
|
||||
src_y: u32,
|
||||
stride: u32,
|
||||
flags: u32,
|
||||
) {
|
||||
unsafe {
|
||||
extern_blit_sub(
|
||||
sprite.as_ptr(),
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
src_x,
|
||||
src_y,
|
||||
stride,
|
||||
flags,
|
||||
)
|
||||
}
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "blitSub"]
|
||||
fn extern_blit_sub(
|
||||
sprite: *const u8,
|
||||
x: i32,
|
||||
y: i32,
|
||||
width: u32,
|
||||
height: u32,
|
||||
src_x: u32,
|
||||
src_y: u32,
|
||||
stride: u32,
|
||||
flags: u32,
|
||||
);
|
||||
}
|
||||
|
||||
pub const BLIT_2BPP: u32 = 1;
|
||||
pub const BLIT_1BPP: u32 = 0;
|
||||
pub const BLIT_FLIP_X: u32 = 2;
|
||||
pub const BLIT_FLIP_Y: u32 = 4;
|
||||
pub const BLIT_ROTATE: u32 = 8;
|
||||
|
||||
/// Draws a line between two points.
|
||||
pub fn line(x1: i32, y1: i32, x2: i32, y2: i32) {
|
||||
unsafe { extern_line(x1, y1, x2, y2) }
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "line"]
|
||||
fn extern_line(x1: i32, y1: i32, x2: i32, y2: i32);
|
||||
}
|
||||
|
||||
/// Draws an oval (or circle).
|
||||
pub fn oval(x: i32, y: i32, width: u32, height: u32) {
|
||||
unsafe { extern_oval(x, y, width, height) }
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "oval"]
|
||||
fn extern_oval(x: i32, y: i32, width: u32, height: u32);
|
||||
}
|
||||
|
||||
/// Draws a rectangle.
|
||||
pub fn rect(x: i32, y: i32, width: u32, height: u32) {
|
||||
unsafe { extern_rect(x, y, width, height) }
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "rect"]
|
||||
fn extern_rect(x: i32, y: i32, width: u32, height: u32);
|
||||
}
|
||||
|
||||
/// Draws text using the built-in system font.
|
||||
pub fn text(text: &str, x: i32, y: i32) {
|
||||
unsafe { extern_text(text.as_ptr(), text.len(), x, y) }
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "textUtf8"]
|
||||
fn extern_text(text: *const u8, length: usize, x: i32, y: i32);
|
||||
}
|
||||
|
||||
// ┌───────────────────────────────────────────────────────────────────────────┐
|
||||
// │ │
|
||||
// │ Sound Functions │
|
||||
// │ │
|
||||
// └───────────────────────────────────────────────────────────────────────────┘
|
||||
|
||||
/// Plays a sound tone.
|
||||
pub fn tone(frequency: u32, duration: u32, volume: u32, flags: u32) {
|
||||
unsafe { extern_tone(frequency, duration, volume, flags) }
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "tone"]
|
||||
fn extern_tone(frequency: u32, duration: u32, volume: u32, flags: u32);
|
||||
}
|
||||
|
||||
pub const TONE_PULSE1: u32 = 0;
|
||||
pub const TONE_PULSE2: u32 = 1;
|
||||
pub const TONE_TRIANGLE: u32 = 2;
|
||||
pub const TONE_NOISE: u32 = 3;
|
||||
pub const TONE_MODE1: u32 = 0;
|
||||
pub const TONE_MODE2: u32 = 4;
|
||||
pub const TONE_MODE3: u32 = 8;
|
||||
pub const TONE_MODE4: u32 = 12;
|
||||
|
||||
// ┌───────────────────────────────────────────────────────────────────────────┐
|
||||
// │ │
|
||||
// │ Storage Functions │
|
||||
// │ │
|
||||
// └───────────────────────────────────────────────────────────────────────────┘
|
||||
|
||||
extern "C" {
|
||||
/// Reads up to `size` bytes from persistent storage into the pointer `dest`.
|
||||
pub fn diskr(dest: *mut u8, size: u32) -> u32;
|
||||
|
||||
/// Writes up to `size` bytes from the pointer `src` into persistent storage.
|
||||
pub fn diskw(src: *const u8, size: u32) -> u32;
|
||||
}
|
||||
|
||||
// ┌───────────────────────────────────────────────────────────────────────────┐
|
||||
// │ │
|
||||
// │ Other Functions │
|
||||
// │ │
|
||||
// └───────────────────────────────────────────────────────────────────────────┘
|
||||
|
||||
extern "C" {
|
||||
/// Copies `size` bytes from `srcPtr` into `destPtr`.
|
||||
#[link_name = "memcpy"]
|
||||
pub fn memcpy(dest: *mut u8, src: *const u8, size: usize) -> usize;
|
||||
|
||||
/// Fills memory at `destPtr` with `size` bytes of the fixed value `value`.
|
||||
#[link_name = "memset"]
|
||||
pub fn memset(dest: *mut u8, byte: u8, size: usize) -> usize;
|
||||
}
|
||||
|
||||
/// Prints a message to the debug console.
|
||||
pub fn trace(text: &str) {
|
||||
unsafe { extern_trace(text.as_ptr(), text.len()) }
|
||||
}
|
||||
extern "C" {
|
||||
#[link_name = "traceUtf8"]
|
||||
fn extern_trace(trace: *const u8, length: usize);
|
||||
}
|
@@ -1,3 +0,0 @@
|
||||
[build]
|
||||
target = "wasm32-unknown-unknown"
|
||||
|
@@ -1,39 +0,0 @@
|
||||
[package]
|
||||
name = "rdraught-wasm"
|
||||
authors.workspace = true
|
||||
edition.workspace = true
|
||||
homepage.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
rust-version.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
# [lib]
|
||||
# crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
rdraught.workspace = true
|
||||
rdraught-ui-common.workspace = true
|
||||
wasm-bindgen.workspace = true
|
||||
console_error_panic_hook.workspace = true
|
||||
rmath.workspace = true
|
||||
base64.workspace = true
|
||||
|
||||
|
||||
[dependencies.web-sys]
|
||||
workspace = true
|
||||
features = [
|
||||
'DomMatrix',
|
||||
'CanvasRenderingContext2d',
|
||||
'CssStyleDeclaration',
|
||||
'Document',
|
||||
'Element',
|
||||
'EventTarget',
|
||||
'HtmlCanvasElement',
|
||||
'HtmlElement',
|
||||
'MouseEvent',
|
||||
'Node',
|
||||
'HtmlImageElement',
|
||||
'SvgImageElement',
|
||||
'Window',
|
||||
]
|
27
rdraught-wasm/dist/index.html
vendored
@@ -1,27 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
|
||||
<style>
|
||||
body {
|
||||
margin: 0px;
|
||||
}
|
||||
</style>
|
||||
<link rel="modulepreload" href="/rdraught-wasm-1d648b9090e33d1f.js" crossorigin="anonymous" integrity="sha384-T7IqpVlu6X9Lrf6TI9E2mHL6bPl2B4DHWKUGmbtsfGa1QSmnJBFB8IRe7clx0yWb"><link rel="preload" href="/rdraught-wasm-1d648b9090e33d1f_bg.wasm" crossorigin="anonymous" integrity="sha384-v+/7HPNa68RnAHRmXFFl7L7bUCWjUJnc8AGKHfn6l5NJiPANQ7yqdGRb+abf1wyh" as="fetch" type="application/wasm"></head>
|
||||
|
||||
<body>
|
||||
|
||||
<script type="module">
|
||||
import init, * as bindings from '/rdraught-wasm-1d648b9090e33d1f.js';
|
||||
const wasm = await init({ module_or_path: '/rdraught-wasm-1d648b9090e33d1f_bg.wasm' });
|
||||
|
||||
|
||||
window.wasmBindings = bindings;
|
||||
|
||||
|
||||
dispatchEvent(new CustomEvent("TrunkApplicationStarted", {detail: {wasm}}));
|
||||
|
||||
</script></body>
|
||||
|
||||
</html>
|
@@ -1,16 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
|
||||
<style>
|
||||
body {
|
||||
margin: 0px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
</body>
|
||||
|
||||
</html>
|
@@ -1,27 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="45" height="45">
|
||||
<g style="fill:none; fill-opacity:0; fill-rule:evenodd; stroke:#ffc837; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(0,0)">
|
||||
<path
|
||||
d="M 22.5,11.63 L 22.5,6"
|
||||
style="fill:none; stroke:#ffc837; stroke-linejoin:miter;" />
|
||||
<path
|
||||
d="M 20,8 L 25,8"
|
||||
style="fill:none; stroke:#ffc837; stroke-linejoin:miter;" />
|
||||
<path
|
||||
d="M 22.5,25 C 22.5,25 27,17.5 25.5,14.5 C 25.5,14.5 24.5,12 22.5,12 C 20.5,12 19.5,14.5 19.5,14.5 C 18,17.5 22.5,25 22.5,25"
|
||||
style="fill:#ffffff; stroke:#ffc837; stroke-linecap:butt; stroke-linejoin:miter;" />
|
||||
<path
|
||||
d="M 11.5,37 C 17,40.5 27,40.5 32.5,37 L 32.5,30 C 32.5,30 41.5,25.5 38.5,19.5 C 34.5,13 25,16 22.5,23.5 L 22.5,27 L 22.5,23.5 C 19,16 9.5,13 6.5,19.5 C 3.5,25.5 11.5,29.5 11.5,29.5 L 11.5,37 z "
|
||||
style="fill:#ffffff; stroke:#ffc837;" />
|
||||
<path
|
||||
d="M 11.5,30 C 17,27 27,27 32.5,30"
|
||||
style="fill:none; stroke:#ffc837;" />
|
||||
<path
|
||||
d="M 11.5,33.5 C 17,30.5 27,30.5 32.5,33.5"
|
||||
style="fill:none; stroke:#ffc837;" />
|
||||
<path
|
||||
d="M 11.5,37 C 17,34 27,34 32.5,37"
|
||||
style="fill:none; stroke:#ffc837;" />
|
||||
</g>
|
||||
</svg>
|
Before Width: | Height: | Size: 1.5 KiB |
@@ -1,27 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="45" height="45">
|
||||
<g style="fill:none; fill-opacity:0; fill-rule:evenodd; stroke:#776b00; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(0,0)">
|
||||
<path
|
||||
d="M 22.5,11.63 L 22.5,6"
|
||||
style="fill:none; stroke:#776b00; stroke-linejoin:miter;" />
|
||||
<path
|
||||
d="M 20,8 L 25,8"
|
||||
style="fill:none; stroke:#776b00; stroke-linejoin:miter;" />
|
||||
<path
|
||||
d="M 22.5,25 C 22.5,25 27,17.5 25.5,14.5 C 25.5,14.5 24.5,12 22.5,12 C 20.5,12 19.5,14.5 19.5,14.5 C 18,17.5 22.5,25 22.5,25"
|
||||
style="fill:#ffffff; stroke:#776b00; stroke-linecap:butt; stroke-linejoin:miter;" />
|
||||
<path
|
||||
d="M 11.5,37 C 17,40.5 27,40.5 32.5,37 L 32.5,30 C 32.5,30 41.5,25.5 38.5,19.5 C 34.5,13 25,16 22.5,23.5 L 22.5,27 L 22.5,23.5 C 19,16 9.5,13 6.5,19.5 C 3.5,25.5 11.5,29.5 11.5,29.5 L 11.5,37 z "
|
||||
style="fill:#ffffff; stroke:#776b00;" />
|
||||
<path
|
||||
d="M 11.5,30 C 17,27 27,27 32.5,30"
|
||||
style="fill:none; stroke:#776b00;" />
|
||||
<path
|
||||
d="M 11.5,33.5 C 17,30.5 27,30.5 32.5,33.5"
|
||||
style="fill:none; stroke:#776b00;" />
|
||||
<path
|
||||
d="M 11.5,37 C 17,34 27,34 32.5,37"
|
||||
style="fill:none; stroke:#776b00;" />
|
||||
</g>
|
||||
</svg>
|
Before Width: | Height: | Size: 1.5 KiB |
@@ -1,61 +0,0 @@
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
use web_sys::CanvasRenderingContext2d;
|
||||
use web_sys::MouseEvent;
|
||||
extern crate console_error_panic_hook;
|
||||
|
||||
// #[wasm_bindgen(start)]
|
||||
fn main() -> Result<(), JsValue> {
|
||||
let document = web_sys::window().unwrap().document().unwrap();
|
||||
let canvas = document
|
||||
.create_element("canvas")?
|
||||
.dyn_into::<web_sys::HtmlCanvasElement>()?;
|
||||
document.body().unwrap().append_child(&canvas)?;
|
||||
canvas.set_width(640);
|
||||
canvas.set_height(480);
|
||||
canvas.style().set_property("border", "solid")?;
|
||||
let context = canvas
|
||||
.get_context("2d")?
|
||||
.unwrap()
|
||||
.dyn_into::<web_sys::CanvasRenderingContext2d>()?;
|
||||
let context = Rc::new(context);
|
||||
let pressed = Rc::new(Cell::new(false));
|
||||
{
|
||||
let context = context.clone();
|
||||
let pressed = pressed.clone();
|
||||
let closure = Closure::<dyn FnMut(_)>::new(move |event: web_sys::MouseEvent| {
|
||||
context.begin_path();
|
||||
context.move_to(event.offset_x() as f64, event.offset_y() as f64);
|
||||
pressed.set(true);
|
||||
});
|
||||
canvas.add_event_listener_with_callback("mousedown", closure.as_ref().unchecked_ref())?;
|
||||
closure.forget();
|
||||
}
|
||||
{
|
||||
let context = context.clone();
|
||||
let pressed = pressed.clone();
|
||||
let closure = Closure::<dyn FnMut(_)>::new(move |event: web_sys::MouseEvent| {
|
||||
if pressed.get() {
|
||||
context.line_to(event.offset_x() as f64, event.offset_y() as f64);
|
||||
context.stroke();
|
||||
context.begin_path();
|
||||
context.move_to(event.offset_x() as f64, event.offset_y() as f64);
|
||||
}
|
||||
});
|
||||
canvas.add_event_listener_with_callback("mousemove", closure.as_ref().unchecked_ref())?;
|
||||
closure.forget();
|
||||
}
|
||||
{
|
||||
let closure = Closure::<dyn FnMut(_)>::new(move |event: web_sys::MouseEvent| {
|
||||
pressed.set(false);
|
||||
context.line_to(event.offset_x() as f64, event.offset_y() as f64);
|
||||
context.stroke();
|
||||
});
|
||||
canvas.add_event_listener_with_callback("mouseup", closure.as_ref().unchecked_ref())?;
|
||||
closure.forget();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
@@ -1,446 +0,0 @@
|
||||
use base64::Engine;
|
||||
use base64::engine::general_purpose::STANDARD as b64;
|
||||
use core::f64::consts::PI;
|
||||
use rdraught::{DraughtsBoard, DraughtsGame, Move, Piece, Player, Position, RectangularBoard};
|
||||
use rdraught_ui_common::{
|
||||
Point, Rect, SharedMutable, SharedMutableRef, Xform, new_shared_mut, new_shared_mut_ref,
|
||||
};
|
||||
use wasm_bindgen::prelude::*;
|
||||
use web_sys::{CanvasRenderingContext2d, Document, HtmlImageElement, MouseEvent};
|
||||
|
||||
extern crate console_error_panic_hook;
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
// Use `js_namespace` here to bind `console.log(..)` instead of just
|
||||
// `log(..)`
|
||||
#[wasm_bindgen(js_namespace = console)]
|
||||
fn log(s: &str);
|
||||
|
||||
// The `console.log` is quite polymorphic, so we can bind it with multiple
|
||||
// signatures. Note that we need to use `js_name` to ensure we always call
|
||||
// `log` in JS.
|
||||
#[wasm_bindgen(js_namespace = console, js_name = log)]
|
||||
fn log_u32(a: u32);
|
||||
|
||||
// Multiple arguments too!
|
||||
#[wasm_bindgen(js_namespace = console, js_name = log)]
|
||||
fn log_many(a: &str, b: &str);
|
||||
}
|
||||
|
||||
macro_rules! console_log {
|
||||
// Note that this is using the `log` function imported above during
|
||||
// `bare_bones`
|
||||
($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
|
||||
}
|
||||
|
||||
struct PieceDrawer {
|
||||
red_crown: HtmlImageElement,
|
||||
white_crown: HtmlImageElement,
|
||||
}
|
||||
|
||||
impl PieceDrawer {
|
||||
fn new(document: &Document) -> Result<PieceDrawer, JsValue> {
|
||||
const CROWN_RED: &[u8] = include_bytes!("crown_red.svg");
|
||||
const CROWN_WHITE: &[u8] = include_bytes!("crown_white.svg");
|
||||
let red_crown = document
|
||||
.create_element("img")?
|
||||
.dyn_into::<HtmlImageElement>()?;
|
||||
let src = b64.encode(CROWN_RED);
|
||||
let src = format!("data:image/svg+xml;base64,{}", src);
|
||||
red_crown.set_src(&src);
|
||||
let white_crown = document
|
||||
.create_element("img")?
|
||||
.dyn_into::<HtmlImageElement>()?;
|
||||
let src = b64.encode(CROWN_WHITE);
|
||||
let src = format!("data:image/svg+xml;base64,{}", src);
|
||||
white_crown.set_src(&src);
|
||||
Ok(PieceDrawer {
|
||||
red_crown,
|
||||
white_crown,
|
||||
})
|
||||
}
|
||||
|
||||
fn draw_piece(
|
||||
&self,
|
||||
ctx: &CanvasRenderingContext2d,
|
||||
xform: &Xform,
|
||||
cell: Rect,
|
||||
piece: Piece,
|
||||
) -> Result<(), JsValue> {
|
||||
ctx.save();
|
||||
let center = cell.center();
|
||||
let outer_radius = cell.width() * 0.3;
|
||||
let thickness = outer_radius * 0.5;
|
||||
let vertical_scale_factor = 0.7;
|
||||
{
|
||||
let p = Point::new(0.0, cell.center().y()) * xform;
|
||||
let xform = xform.clone()
|
||||
* Xform::xlate(-p.x(), -p.y())
|
||||
* Xform::scale(1.0, vertical_scale_factor)
|
||||
* Xform::xlate(p.x(), p.y());
|
||||
apply_xform(ctx, &xform)?;
|
||||
}
|
||||
|
||||
ctx.set_fill_style_str("#000");
|
||||
ctx.begin_path();
|
||||
|
||||
ctx.arc(
|
||||
center.x(),
|
||||
center.y() + thickness / 2.0,
|
||||
outer_radius,
|
||||
0.0,
|
||||
PI,
|
||||
)?;
|
||||
ctx.line_to(center.x() - outer_radius, center.y() - thickness / 2.0);
|
||||
ctx.arc(
|
||||
center.x(),
|
||||
center.y() - thickness / 2.0,
|
||||
outer_radius,
|
||||
PI,
|
||||
2.0 * PI,
|
||||
)?;
|
||||
ctx.line_to(center.x() + outer_radius, center.y() + thickness / 2.0);
|
||||
ctx.fill();
|
||||
|
||||
match piece.player() {
|
||||
Some(Player::Red) => {
|
||||
ctx.set_fill_style_str("#ff0000");
|
||||
}
|
||||
Some(Player::White) => {
|
||||
ctx.set_fill_style_str("#ffffff");
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
let radius = cell.width() * 0.275;
|
||||
ctx.begin_path();
|
||||
ctx.arc(
|
||||
center.x(),
|
||||
center.y() - thickness / 2.0,
|
||||
radius,
|
||||
0.0,
|
||||
2.0 * PI,
|
||||
)?;
|
||||
ctx.fill();
|
||||
ctx.restore();
|
||||
ctx.save();
|
||||
if let Some(player) = piece.player() {
|
||||
if piece.is_crowned() {
|
||||
{
|
||||
let p = Point::new(cell.center().x(), cell.center().y()) * xform;
|
||||
let f = 0.55;
|
||||
let xform = xform.clone()
|
||||
* Xform::xlate(-p.x(), -p.y())
|
||||
* Xform::scale(1.0, vertical_scale_factor)
|
||||
* Xform::scale(f, f)
|
||||
* Xform::xlate(p.x(), p.y());
|
||||
apply_xform(ctx, &xform)?;
|
||||
}
|
||||
let image = match player {
|
||||
Player::White => self.white_crown.clone(),
|
||||
Player::Red => self.red_crown.clone(),
|
||||
};
|
||||
ctx.draw_image_with_html_image_element_and_dw_and_dh(
|
||||
&image,
|
||||
cell.tl().x(),
|
||||
cell.tl().y() - thickness / 1.0,
|
||||
cell.width(),
|
||||
cell.height(),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
ctx.restore();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_xform(ctx: &CanvasRenderingContext2d, xform: &Xform) -> Result<(), JsValue> {
|
||||
ctx.set_transform(
|
||||
xform.xx(),
|
||||
xform.xy(),
|
||||
xform.yx(),
|
||||
xform.yy(),
|
||||
xform.tx(),
|
||||
xform.ty(),
|
||||
)
|
||||
}
|
||||
|
||||
fn from_ctx(ctx: &CanvasRenderingContext2d) -> Result<Xform, JsValue> {
|
||||
let m = ctx.get_transform()?;
|
||||
Ok(Xform::new(m.a(), m.c(), m.b(), m.d(), m.e(), m.f()))
|
||||
}
|
||||
|
||||
fn map_row(current_player: Player, row: usize) -> usize {
|
||||
match current_player {
|
||||
Player::White => DraughtsBoard::rows() - 1 - row,
|
||||
Player::Red => row,
|
||||
}
|
||||
}
|
||||
|
||||
trait Agent {}
|
||||
|
||||
struct App {
|
||||
game: DraughtsGame,
|
||||
board: Rect,
|
||||
screen: Rect,
|
||||
xform: Xform,
|
||||
selected_piece: Option<Position>,
|
||||
available_moves: Vec<Move>,
|
||||
main_player: Player,
|
||||
red_agent: Box<dyn Agent>,
|
||||
white_agent: Box<dyn Agent>,
|
||||
}
|
||||
|
||||
struct Human {}
|
||||
|
||||
impl Human {
|
||||
fn new() -> Human {
|
||||
Human {}
|
||||
}
|
||||
}
|
||||
|
||||
impl Agent for Human {}
|
||||
|
||||
impl Default for App {
|
||||
fn default() -> Self {
|
||||
let board = Rect::from_size(
|
||||
Point::new(100.0, 400.0),
|
||||
App::SQUARE_SIZE * DraughtsBoard::columns() as f64,
|
||||
App::SQUARE_SIZE * DraughtsBoard::rows() as f64,
|
||||
);
|
||||
App {
|
||||
game: DraughtsGame::default(),
|
||||
board,
|
||||
screen: Rect::new(Point::new(0.0, 0.0), Point::new(0.0, 0.0)),
|
||||
xform: Xform::default(),
|
||||
selected_piece: None,
|
||||
available_moves: Vec::new(),
|
||||
main_player: Player::White,
|
||||
red_agent: Box::new(Human::new()),
|
||||
white_agent: Box::new(Human::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl App {
|
||||
const SQUARE_SIZE: f64 = 100.0;
|
||||
fn draw(
|
||||
&mut self,
|
||||
ctx: &CanvasRenderingContext2d,
|
||||
piece_drawer: &PieceDrawer,
|
||||
) -> Result<(), JsValue> {
|
||||
ctx.clear_rect(0.0, 0.0, self.screen.width(), self.screen.height());
|
||||
let board = &self.board;
|
||||
let f = f64::min(
|
||||
self.screen.width() / board.width(),
|
||||
self.screen.height() / board.height(),
|
||||
);
|
||||
ctx.save();
|
||||
self.xform = Xform::default()
|
||||
* Xform::xlate(-board.center().x(), -board.center().y())
|
||||
* Xform::scale(f, f)
|
||||
* Xform::xlate(self.screen.center().x(), self.screen.center().y());
|
||||
apply_xform(ctx, &self.xform)?;
|
||||
// let board_center = board.center();
|
||||
// let screen_center = screen.center();
|
||||
// let xlate = -board_center;
|
||||
// ctx.restore();
|
||||
// ctx.translate(-xlate.x(), -xlate.y())?;
|
||||
// ctx.scale(f, f)?;
|
||||
// ctx.translate(screen_center.x(), screen_center.y())?;
|
||||
// console_log!("xform2: {}", from_ctx(ctx)?);
|
||||
let available_moves: Vec<Position> = self
|
||||
.available_moves
|
||||
.iter()
|
||||
.map(Move::get_end_position)
|
||||
.collect();
|
||||
for i in 0..DraughtsBoard::rows() {
|
||||
for j in 0..DraughtsBoard::columns() {
|
||||
let cell = Rect::new(
|
||||
Point::new(
|
||||
board.tl().x() + i as f64 * App::SQUARE_SIZE,
|
||||
board.tl().y() + j as f64 * App::SQUARE_SIZE,
|
||||
),
|
||||
Point::new(
|
||||
board.tl().x() + (i + 1) as f64 * App::SQUARE_SIZE,
|
||||
board.tl().y() + (j + 1) as f64 * App::SQUARE_SIZE,
|
||||
),
|
||||
);
|
||||
if (i + j) % 2 == 0 {
|
||||
ctx.set_fill_style_str("#cccc99");
|
||||
} else {
|
||||
ctx.set_fill_style_str("#666633");
|
||||
}
|
||||
ctx.fill_rect(cell.tl().x(), cell.tl().y(), cell.width(), cell.height());
|
||||
if (i + j) % 2 != 0 {
|
||||
let current_player = Player::White;
|
||||
let position = Position::from_index((map_row(current_player, j), i));
|
||||
let piece = self.game.piece_at(position);
|
||||
if piece.is_present() {
|
||||
piece_drawer.draw_piece(ctx, &self.xform, cell.clone(), piece)?;
|
||||
}
|
||||
if let Some(selected_piece) = self.selected_piece {
|
||||
if selected_piece == position {
|
||||
// console_log!("Selected: {}", selected_piece);
|
||||
ctx.save();
|
||||
ctx.set_shadow_blur(300.0);
|
||||
ctx.set_shadow_color("#ffffff");
|
||||
ctx.begin_path();
|
||||
ctx.move_to(cell.tl().x(), cell.tl().y());
|
||||
ctx.line_to(cell.br().x(), cell.tl().y());
|
||||
ctx.line_to(cell.br().x(), cell.br().y());
|
||||
ctx.line_to(cell.tl().x(), cell.br().y());
|
||||
ctx.line_to(cell.tl().x(), cell.tl().y());
|
||||
ctx.clip();
|
||||
ctx.set_stroke_style_str("#fff");
|
||||
ctx.set_line_width(4.0);
|
||||
ctx.begin_path();
|
||||
ctx.move_to(board.tl().x(), board.tl().y());
|
||||
ctx.line_to(board.br().x(), board.tl().y());
|
||||
ctx.line_to(board.br().x(), board.br().y());
|
||||
ctx.line_to(board.tl().x(), board.br().y());
|
||||
ctx.line_to(board.tl().x(), board.tl().y());
|
||||
ctx.move_to(cell.tl().x(), cell.tl().y());
|
||||
ctx.line_to(cell.br().x(), cell.tl().y());
|
||||
ctx.line_to(cell.br().x(), cell.br().y());
|
||||
ctx.line_to(cell.tl().x(), cell.br().y());
|
||||
ctx.line_to(cell.tl().x(), cell.tl().y());
|
||||
ctx.stroke();
|
||||
ctx.restore();
|
||||
}
|
||||
}
|
||||
if available_moves.contains(&position) {
|
||||
ctx.save();
|
||||
ctx.set_shadow_blur(300.0);
|
||||
ctx.set_shadow_color("#0000ff");
|
||||
ctx.begin_path();
|
||||
ctx.move_to(cell.tl().x(), cell.tl().y());
|
||||
ctx.line_to(cell.br().x(), cell.tl().y());
|
||||
ctx.line_to(cell.br().x(), cell.br().y());
|
||||
ctx.line_to(cell.tl().x(), cell.br().y());
|
||||
ctx.line_to(cell.tl().x(), cell.tl().y());
|
||||
ctx.clip();
|
||||
ctx.set_stroke_style_str("#0f0");
|
||||
ctx.set_line_width(4.0);
|
||||
ctx.begin_path();
|
||||
ctx.move_to(board.tl().x(), board.tl().y());
|
||||
ctx.line_to(board.br().x(), board.tl().y());
|
||||
ctx.line_to(board.br().x(), board.br().y());
|
||||
ctx.line_to(board.tl().x(), board.br().y());
|
||||
ctx.line_to(board.tl().x(), board.tl().y());
|
||||
ctx.move_to(cell.tl().x(), cell.tl().y());
|
||||
ctx.line_to(cell.br().x(), cell.tl().y());
|
||||
ctx.line_to(cell.br().x(), cell.br().y());
|
||||
ctx.line_to(cell.tl().x(), cell.br().y());
|
||||
ctx.line_to(cell.tl().x(), cell.tl().y());
|
||||
ctx.stroke();
|
||||
ctx.restore();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ctx.restore();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), JsValue> {
|
||||
let document = web_sys::window().unwrap().document().unwrap();
|
||||
let canvas = document
|
||||
.create_element("canvas")?
|
||||
.dyn_into::<web_sys::HtmlCanvasElement>()?;
|
||||
canvas.style().set_property("background-color", "black")?;
|
||||
document.body().unwrap().append_child(&canvas)?;
|
||||
// canvas.style().set_property("border", "solid")?;
|
||||
let window = web_sys::window().expect("should have a window in this context");
|
||||
let width = window.inner_width().unwrap().as_f64().unwrap();
|
||||
let height = window.inner_height().unwrap().as_f64().unwrap();
|
||||
canvas.set_width(width as u32);
|
||||
canvas.set_height(height as u32);
|
||||
let app = new_shared_mut_ref(App::default());
|
||||
let ctx = canvas
|
||||
.get_context("2d")?
|
||||
.unwrap()
|
||||
.dyn_into::<web_sys::CanvasRenderingContext2d>()?;
|
||||
let piece_drawer = new_shared_mut_ref(PieceDrawer::new(&document)?);
|
||||
{
|
||||
let piece_drawer = piece_drawer.clone();
|
||||
let screen = Rect::new(Point::new(0.0, 0.0), Point::new(width, height));
|
||||
app.borrow_mut().screen = screen;
|
||||
app.borrow_mut().draw(&ctx, &piece_drawer.borrow())?;
|
||||
}
|
||||
{
|
||||
let app = app.clone();
|
||||
let canvas = canvas.clone();
|
||||
let ctx = ctx.clone();
|
||||
let piece_drawer = piece_drawer.clone();
|
||||
let closure = Closure::<dyn FnMut()>::new(move || {
|
||||
let window = web_sys::window().expect("should have a window in this context");
|
||||
let width = window.inner_width().unwrap().as_f64().unwrap() as u32;
|
||||
let height = window.inner_height().unwrap().as_f64().unwrap() as u32;
|
||||
canvas.set_width(width);
|
||||
canvas.set_height(height);
|
||||
app.borrow_mut().screen = Rect::new(
|
||||
Point::new(0.0, 0.0),
|
||||
Point::new(canvas.width() as f64, canvas.height() as f64),
|
||||
);
|
||||
app.borrow_mut().draw(&ctx, &piece_drawer.borrow()).unwrap();
|
||||
});
|
||||
let window = web_sys::window().expect("should have a window in this context");
|
||||
window.set_onresize(Some(closure.as_ref().unchecked_ref()));
|
||||
closure.forget();
|
||||
}
|
||||
|
||||
{
|
||||
let app = app.clone();
|
||||
let piece_drawer = piece_drawer.clone();
|
||||
let mouse_click_handler = Closure::wrap(Box::new(move |event: MouseEvent| {
|
||||
let mut app = app.borrow_mut();
|
||||
let xform = app.xform.invert();
|
||||
let board = &app.board;
|
||||
let position = Point::new(event.x() as f64, event.y() as f64) * &xform - board.tl();
|
||||
if position.x() > 0.0
|
||||
&& position.x() < board.width()
|
||||
&& position.y() > 0.0
|
||||
&& position.y() < board.height()
|
||||
{
|
||||
let col = f64::floor(position.x() / App::SQUARE_SIZE) as usize;
|
||||
let row = f64::floor(position.y() / App::SQUARE_SIZE) as usize;
|
||||
let row = map_row(app.main_player, row);
|
||||
if (row + col) % 2 == 0 {
|
||||
let position = Position::new(row as u8, col as u8).unwrap();
|
||||
let selected_move = app
|
||||
.available_moves
|
||||
.iter()
|
||||
.find(|mv| mv.get_end_position() == position)
|
||||
.map(Move::clone);
|
||||
if app.selected_piece.is_some() && selected_move.is_some() {
|
||||
let mv = selected_move.unwrap();
|
||||
app.game.check_and_apply_move(&mv).unwrap();
|
||||
app.selected_piece = None;
|
||||
app.available_moves.clear();
|
||||
} else {
|
||||
app.selected_piece = Some(position);
|
||||
let mut available_moves = Vec::<Move>::new();
|
||||
for mv in app.game.moves_for_piece(position) {
|
||||
available_moves.push(mv);
|
||||
}
|
||||
app.available_moves = available_moves;
|
||||
// console_log!("Clicked at: ({}, {})", col, row);
|
||||
}
|
||||
} else {
|
||||
app.selected_piece = None;
|
||||
app.available_moves.clear();
|
||||
}
|
||||
} else {
|
||||
app.selected_piece = None;
|
||||
app.available_moves.clear();
|
||||
}
|
||||
app.draw(&ctx, &piece_drawer.borrow()).unwrap();
|
||||
}) as Box<dyn FnMut(MouseEvent)>);
|
||||
canvas.set_onclick(Some(mouse_click_handler.as_ref().unchecked_ref()));
|
||||
mouse_click_handler.forget();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
use heapless::Vec;
|
||||
use rdraught::{DraughtsBoard, Piece, Position};
|
||||
use rdraught::draughts::{DraughtsBoard, Piece};
|
||||
use rdraught::position::Position;
|
||||
|
||||
fn main() {
|
||||
let board = DraughtsBoard::default();
|
||||
|
@@ -1,297 +0,0 @@
|
||||
use core::mem::MaybeUninit;
|
||||
use core::result::Result;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Error {
|
||||
BufferIsFull,
|
||||
BufferIsEmpty,
|
||||
BufferHasZeroSize,
|
||||
}
|
||||
|
||||
pub struct CircularBuffer<T: Sized, const SIZE: usize> {
|
||||
buffer: [MaybeUninit<T>; SIZE],
|
||||
front: usize,
|
||||
rear: usize,
|
||||
fill: usize,
|
||||
}
|
||||
|
||||
impl<T: Sized, const SIZE: usize> CircularBuffer<T, SIZE> {
|
||||
pub fn new() -> CircularBuffer<T, SIZE> {
|
||||
CircularBuffer {
|
||||
buffer: [const { MaybeUninit::uninit() }; SIZE],
|
||||
front: 0,
|
||||
rear: 0,
|
||||
fill: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, element: T) -> Result<(), Error> {
|
||||
if self.len() == SIZE {
|
||||
Err(Error::BufferIsFull)
|
||||
} else {
|
||||
self.buffer[self.rear].write(element);
|
||||
self.rear = (self.rear + 1) % SIZE;
|
||||
self.fill += 1;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_evict(&mut self, element: T) -> Option<T> {
|
||||
let result = if self.len() == SIZE {
|
||||
self.pop_front()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
self.buffer[self.rear].write(element);
|
||||
self.rear = (self.rear + 1) % SIZE;
|
||||
self.fill += 1;
|
||||
result
|
||||
}
|
||||
|
||||
pub fn pop_front(&mut self) -> Option<T> {
|
||||
if self.len() == 0 {
|
||||
None
|
||||
} else {
|
||||
let mut result = MaybeUninit::uninit();
|
||||
core::mem::swap(&mut result, &mut self.buffer[self.front]);
|
||||
self.front = (self.front + 1) % SIZE;
|
||||
self.fill -= 1;
|
||||
Some(unsafe { result.assume_init() })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_back(&mut self) -> Option<T> {
|
||||
if self.len() == 0 {
|
||||
None
|
||||
} else {
|
||||
let mut result = MaybeUninit::uninit();
|
||||
core::mem::swap(&mut result, &mut self.buffer[self.rear - 1]);
|
||||
self.rear = (self.rear - 1) % SIZE;
|
||||
self.fill -= 1;
|
||||
Some(unsafe { result.assume_init() })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.fill
|
||||
}
|
||||
|
||||
pub fn capacity(&self) -> usize {
|
||||
SIZE
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len() == 0
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl DoubleEndedIterator<Item = &T> {
|
||||
CircularBufferIteratorRef::<T, SIZE> {
|
||||
cb: self,
|
||||
count: 0,
|
||||
count_back: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T> {
|
||||
CircularBufferIteratorMutRef::<T, SIZE> {
|
||||
cb: self,
|
||||
count: 0,
|
||||
count_back: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn first(&self) -> Option<&T> {
|
||||
if self.len() > 0 {
|
||||
unsafe { Some(self.buffer[self.front].assume_init_ref()) }
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn last(&self) -> Option<&T> {
|
||||
if self.len() > 0 {
|
||||
unsafe { Some(self.buffer[self.rear - 1].assume_init_ref()) }
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CircularBufferIterator<T: Sized, const SIZE: usize> {
|
||||
buffer: CircularBuffer<T, SIZE>,
|
||||
}
|
||||
|
||||
impl<T: Sized, const SIZE: usize> Iterator for CircularBufferIterator<T, SIZE> {
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.buffer.pop_front()
|
||||
}
|
||||
type Item = T;
|
||||
}
|
||||
|
||||
impl<T: Sized, const SIZE: usize> IntoIterator for CircularBuffer<T, SIZE> {
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
CircularBufferIterator { buffer: self }
|
||||
}
|
||||
|
||||
type IntoIter = CircularBufferIterator<T, SIZE>;
|
||||
type Item = T;
|
||||
}
|
||||
|
||||
pub struct CircularBufferIteratorRef<'a, T: Sized, const SIZE: usize> {
|
||||
cb: &'a CircularBuffer<T, SIZE>,
|
||||
count: usize,
|
||||
count_back: usize,
|
||||
}
|
||||
|
||||
impl<'a, T: Sized, const SIZE: usize> Iterator for CircularBufferIteratorRef<'a, T, SIZE> {
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.count + self.count_back == self.cb.len() {
|
||||
None
|
||||
} else {
|
||||
let i = (self.cb.front + self.count) % SIZE;
|
||||
let result = Some(unsafe { self.cb.buffer[i].assume_init_ref() });
|
||||
self.count += 1;
|
||||
result
|
||||
}
|
||||
}
|
||||
type Item = &'a T;
|
||||
}
|
||||
|
||||
impl<'a, T: Sized, const SIZE: usize> DoubleEndedIterator
|
||||
for CircularBufferIteratorRef<'a, T, SIZE>
|
||||
{
|
||||
fn next_back(&mut self) -> Option<Self::Item> {
|
||||
if self.count_back == self.cb.len() {
|
||||
None
|
||||
} else {
|
||||
let i = (SIZE + self.cb.rear - 1 - self.count_back) % SIZE;
|
||||
let result = Some(unsafe { self.cb.buffer[i].assume_init_ref() });
|
||||
self.count_back += 1;
|
||||
result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Sized, const SIZE: usize> IntoIterator for &'a CircularBuffer<T, SIZE> {
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
CircularBufferIteratorRef {
|
||||
cb: self,
|
||||
count: 0,
|
||||
count_back: 0,
|
||||
}
|
||||
}
|
||||
|
||||
type IntoIter = CircularBufferIteratorRef<'a, T, SIZE>;
|
||||
type Item = &'a T;
|
||||
}
|
||||
|
||||
pub struct CircularBufferIteratorMutRef<'a, T: Sized, const SIZE: usize> {
|
||||
cb: &'a mut CircularBuffer<T, SIZE>,
|
||||
count: usize,
|
||||
count_back: usize,
|
||||
}
|
||||
|
||||
impl<'a, T: Sized, const SIZE: usize> Iterator for CircularBufferIteratorMutRef<'a, T, SIZE> {
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.count + self.count_back == self.cb.len() {
|
||||
None
|
||||
} else {
|
||||
let result = unsafe {
|
||||
let i = (self.cb.front + self.count) % SIZE;
|
||||
Some(core::mem::transmute::<&mut T, &'a mut T>(
|
||||
self.cb.buffer[i].assume_init_mut(),
|
||||
))
|
||||
};
|
||||
self.count += 1;
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
type Item = &'a mut T;
|
||||
}
|
||||
|
||||
impl<'a, T: Sized, const SIZE: usize> DoubleEndedIterator
|
||||
for CircularBufferIteratorMutRef<'a, T, SIZE>
|
||||
{
|
||||
fn next_back(&mut self) -> Option<Self::Item> {
|
||||
if self.count + self.count_back == self.cb.len() {
|
||||
None
|
||||
} else {
|
||||
let result = unsafe {
|
||||
let i = (SIZE + self.cb.rear - 1 - self.count_back) % SIZE;
|
||||
Some(core::mem::transmute::<&mut T, &'a mut T>(
|
||||
self.cb.buffer[i].assume_init_mut(),
|
||||
))
|
||||
};
|
||||
self.count_back += 1;
|
||||
result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Sized, const SIZE: usize> IntoIterator for &'a mut CircularBuffer<T, SIZE> {
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
CircularBufferIteratorMutRef {
|
||||
cb: self,
|
||||
count: 0,
|
||||
count_back: 0,
|
||||
}
|
||||
}
|
||||
|
||||
type IntoIter = CircularBufferIteratorMutRef<'a, T, SIZE>;
|
||||
type Item = &'a mut T;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
const SZ: usize = 10;
|
||||
let mut cb = CircularBuffer::<i32, SZ>::new();
|
||||
for i in 0..SZ {
|
||||
let result = cb.push(i as i32);
|
||||
assert_eq!(true, result.is_ok());
|
||||
}
|
||||
|
||||
//Check the buffer is full
|
||||
assert_eq!(SZ, cb.len());
|
||||
|
||||
//The buffer contains the elements that we've pushed
|
||||
for (index, elem) in (&cb).into_iter().enumerate() {
|
||||
assert_eq!(index as i32, *elem);
|
||||
}
|
||||
let result = cb.push(10);
|
||||
//Cannot push in a full buffer
|
||||
assert_eq!(true, result.is_err());
|
||||
|
||||
//Check push_evict returns the first element
|
||||
let result = cb.push_evict(10);
|
||||
assert_eq!(Some(0), result);
|
||||
|
||||
//Check pop_front returns the second element
|
||||
let second = cb.pop_front();
|
||||
assert_eq!(Some(1), second);
|
||||
//Check the buffer is not full
|
||||
assert_eq!(SZ - 1, cb.len());
|
||||
|
||||
//Check pop_front returns the third element
|
||||
let second = cb.pop_front();
|
||||
|
||||
assert_eq!(Some(2), second);
|
||||
//Check the buffer is not full
|
||||
assert_eq!(SZ - 2, cb.len());
|
||||
|
||||
//Replace all the elements with their index
|
||||
for (n, i) in cb.iter_mut().enumerate() {
|
||||
*i = n as i32;
|
||||
}
|
||||
for (n, i) in cb.iter().enumerate() {
|
||||
assert_eq!(n as i32, *i);
|
||||
}
|
||||
for (n, i) in cb.iter().rev().enumerate() {
|
||||
assert_eq!((cb.len() - n - 1) as i32, *i);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,10 +1,9 @@
|
||||
use super::circular_buffer::CircularBuffer;
|
||||
use super::constants::{BITS_PER_POSITION, POSITIONS, POSITIONS_PER_ROW};
|
||||
use super::movement::{LoggedMove, Move, MoveDirection};
|
||||
use super::movement::{Move, MoveDirection};
|
||||
use super::position::Position;
|
||||
use crate::piece::Piece;
|
||||
use crate::player::Player;
|
||||
use heapless::Vec;
|
||||
use heapless::{HistoryBuffer, Vec};
|
||||
|
||||
pub trait RectangularBoard {
|
||||
fn rows() -> usize;
|
||||
@@ -13,7 +12,7 @@ pub trait RectangularBoard {
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub struct DraughtsBoard {
|
||||
data: [u8; (POSITIONS * BITS_PER_POSITION).div_ceil(8)],
|
||||
data: [u8; (POSITIONS * BITS_PER_POSITION + 7) / 8],
|
||||
}
|
||||
|
||||
impl DraughtsBoard {
|
||||
@@ -39,23 +38,23 @@ impl DraughtsBoard {
|
||||
let index = offset_bits / 8;
|
||||
let remainder = offset_bits % 8;
|
||||
let mut value = 0;
|
||||
value |= (self.data[index] >> remainder) & 7;
|
||||
value = value | ((self.data[index] >> remainder) & 7);
|
||||
if remainder > 5 {
|
||||
let written_bits = 8 - remainder;
|
||||
value |= (self.data[index + 1] & (7 >> written_bits)) << written_bits
|
||||
value = value | ((self.data[index + 1] & (7 >> written_bits)) << written_bits)
|
||||
}
|
||||
Piece::from_repr(value as usize).unwrap()
|
||||
}
|
||||
|
||||
pub fn new<T: FnMut(Position) -> Piece>(mut cb: T) -> DraughtsBoard {
|
||||
let mut result = DraughtsBoard {
|
||||
data: [0u8; (POSITIONS * BITS_PER_POSITION).div_ceil(8)],
|
||||
data: [0u8; (POSITIONS * BITS_PER_POSITION + 7) / 8],
|
||||
};
|
||||
for i in 0..DraughtsBoard::rows() {
|
||||
for j in 0..DraughtsBoard::columns() {
|
||||
if (i + j) % 2 == 0 {
|
||||
let position = Position::new(i as u8, j as u8).unwrap();
|
||||
let piece = cb(position);
|
||||
let piece = cb(position.clone());
|
||||
result.set(position, piece);
|
||||
}
|
||||
}
|
||||
@@ -89,7 +88,7 @@ impl DraughtsBoard {
|
||||
}
|
||||
|
||||
fn check_move_valid(&self, mv: &Move) -> Result<(), Error> {
|
||||
let start = mv.get_start_position();
|
||||
let start = mv.start_position();
|
||||
if mv.is_movement() {
|
||||
let mut move_is_possible = false;
|
||||
for possible_move in self.moves_for_piece(start, false) {
|
||||
@@ -121,70 +120,8 @@ impl DraughtsBoard {
|
||||
}
|
||||
}
|
||||
|
||||
fn undo_move(&mut self, mv: &LoggedMove) {
|
||||
let end_pos = mv.get_end_position();
|
||||
let p = self.get(end_pos);
|
||||
//Revert promotion if any
|
||||
let initial_piece = if mv.pawn_promoted() {
|
||||
match p.player() {
|
||||
Some(Player::White) => Piece::SimpleWhitePawn,
|
||||
Some(Player::Red) => Piece::SimpleRedPawn,
|
||||
None => Piece::NoPiece,
|
||||
}
|
||||
} else {
|
||||
p
|
||||
};
|
||||
self.set(end_pos, Piece::NoPiece);
|
||||
self.set(mv.start_position(), initial_piece);
|
||||
if mv.is_capture() {
|
||||
let captured_position = (mv.start_position() + mv.get_end_position()) / (2, 2);
|
||||
let captured_piece = match p.player().unwrap() {
|
||||
Player::Red => {
|
||||
if mv.crowned_captured() {
|
||||
Piece::CrownedWhitePawn
|
||||
} else {
|
||||
Piece::SimpleWhitePawn
|
||||
}
|
||||
}
|
||||
Player::White => {
|
||||
if mv.crowned_captured() {
|
||||
Piece::CrownedRedPawn
|
||||
} else {
|
||||
Piece::SimpleRedPawn
|
||||
}
|
||||
}
|
||||
};
|
||||
self.set(captured_position, captured_piece);
|
||||
}
|
||||
}
|
||||
|
||||
fn redo_move(&mut self, mv: &LoggedMove) {
|
||||
let start = mv.start_position();
|
||||
let piece = self.get_piece(&start);
|
||||
let final_piece = if mv.pawn_promoted() {
|
||||
match piece.player() {
|
||||
Some(Player::Red) => Piece::CrownedRedPawn,
|
||||
Some(Player::White) => Piece::CrownedWhitePawn,
|
||||
None => Piece::NoPiece,
|
||||
}
|
||||
} else {
|
||||
piece
|
||||
};
|
||||
if mv.is_movement() {
|
||||
let end = mv.get_end_position();
|
||||
self.set(start, Piece::NoPiece);
|
||||
self.set(end, final_piece);
|
||||
} else {
|
||||
let end = mv.get_end_position();
|
||||
let captured_pos = (start + end) / (2, 2);
|
||||
self.set(start, Piece::NoPiece);
|
||||
self.set(end, final_piece);
|
||||
self.set(captured_pos, Piece::NoPiece);
|
||||
};
|
||||
}
|
||||
|
||||
fn apply_move(&mut self, mv: &Move) -> Result<(), Error> {
|
||||
let start = mv.get_start_position();
|
||||
let start = mv.start_position();
|
||||
let piece = self.get_piece(&start);
|
||||
if let Piece::NoPiece = piece {
|
||||
Err(Error::InvalidMove)
|
||||
@@ -192,7 +129,7 @@ impl DraughtsBoard {
|
||||
let player = piece.player().unwrap();
|
||||
if mv.is_movement() {
|
||||
let end = mv.get_end_position();
|
||||
// Make sure the move ends in a valid position
|
||||
// Make sure the move ends in a vlid position
|
||||
if !DraughtsBoard::is_position_valid(end) {
|
||||
return Err(Error::InvalidMove);
|
||||
}
|
||||
@@ -213,8 +150,7 @@ impl DraughtsBoard {
|
||||
let piece_at_destination = self.get(mv.get_end_position());
|
||||
// Make sure there is no piece at destination
|
||||
if let Piece::NoPiece = piece_at_destination {
|
||||
let captured_pos = (start + end) / (2, 2);
|
||||
let captured = self.get(captured_pos);
|
||||
let captured = self.get((end + start) / (2, 2));
|
||||
// Make sure there is a piece to be captured
|
||||
if let Piece::NoPiece = captured {
|
||||
return Err(Error::InvalidMove);
|
||||
@@ -224,7 +160,7 @@ impl DraughtsBoard {
|
||||
if captured_piece_player != player {
|
||||
self.set(start, Piece::NoPiece);
|
||||
self.set(end, piece);
|
||||
self.set(captured_pos, Piece::NoPiece);
|
||||
self.set((end + start) / (2, 2), Piece::NoPiece);
|
||||
} else {
|
||||
return Err(Error::InvalidMove);
|
||||
}
|
||||
@@ -318,6 +254,12 @@ pub struct DraughtsGame {
|
||||
next_move: Player,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SerializedDraughtsGame {
|
||||
data: Vec<u8, 24>,
|
||||
next_move: Player,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Error {
|
||||
WrongPlayer,
|
||||
@@ -402,22 +344,8 @@ impl DraughtsGame {
|
||||
}
|
||||
}
|
||||
|
||||
fn undo_move(&mut self, mv: &LoggedMove) {
|
||||
self.board.undo_move(mv);
|
||||
if !mv.is_capture() || !mv.multi_capture() {
|
||||
self.next_turn();
|
||||
}
|
||||
}
|
||||
|
||||
fn redo_move(&mut self, mv: &LoggedMove) {
|
||||
self.board.redo_move(mv);
|
||||
if !mv.is_capture() || !mv.multi_capture() {
|
||||
self.next_turn();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_and_apply_move(&mut self, mv: &Move) -> Result<(), Error> {
|
||||
let start = mv.get_start_position();
|
||||
pub fn apply_move(&mut self, mv: &Move) -> Result<(), Error> {
|
||||
let start = mv.start_position();
|
||||
let piece = self.board.get_piece(&start);
|
||||
if let Some(player) = piece.player() {
|
||||
if mv.is_movement() {
|
||||
@@ -427,7 +355,8 @@ impl DraughtsGame {
|
||||
} else {
|
||||
return Err(Error::WrongPlayer);
|
||||
}
|
||||
} else if self.next_move == player {
|
||||
} else {
|
||||
if self.next_move == player {
|
||||
self.board.apply_move(mv)?;
|
||||
// Check if more captures are available for the current piece
|
||||
if self
|
||||
@@ -441,6 +370,7 @@ impl DraughtsGame {
|
||||
self.next_turn();
|
||||
} else {
|
||||
return Err(Error::WrongPlayer);
|
||||
}
|
||||
};
|
||||
let end = mv.get_end_position();
|
||||
//Promote pawns that reach the last row
|
||||
@@ -510,7 +440,7 @@ impl DraughtsGame {
|
||||
depth: u32,
|
||||
analyzed_moves: &mut usize,
|
||||
) -> f32 {
|
||||
self.check_and_apply_move(mv).unwrap();
|
||||
self.apply_move(mv).unwrap();
|
||||
if depth != 0 {
|
||||
let mut best_score = None;
|
||||
for mv in self.available_moves() {
|
||||
@@ -589,6 +519,68 @@ impl DraughtsGame {
|
||||
}
|
||||
}
|
||||
|
||||
impl SerializedDraughtsGame {
|
||||
fn serialize(position: Position, piece: Piece) -> u8 {
|
||||
let p = position.row() * 4 + position.col() / 2;
|
||||
p << 3 | piece as u8
|
||||
}
|
||||
|
||||
fn deserialize(byte: u8) -> Result<(Position, Piece), Error> {
|
||||
let index = byte >> 3;
|
||||
let row = index >> 2;
|
||||
let col = ((index - (row << 2)) % 4) * 2 + (row % 2);
|
||||
let pos = Position::new(row, col)?;
|
||||
Ok((
|
||||
pos,
|
||||
Piece::from_repr((byte & 7) as usize).ok_or(Error::InvalidPiece)?,
|
||||
))
|
||||
}
|
||||
|
||||
fn from(
|
||||
DraughtsGame { board, next_move }: &DraughtsGame,
|
||||
) -> Result<SerializedDraughtsGame, Error> {
|
||||
let mut data = Vec::<u8, 24>::new();
|
||||
for i in 0..DraughtsBoard::rows() {
|
||||
for j in 0..DraughtsBoard::columns() {
|
||||
let p = Position::new(i as u8, j as u8)?;
|
||||
let piece = board.get_piece(&p);
|
||||
if piece != Piece::NoPiece {
|
||||
data.push(Self::serialize(p, piece)).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(SerializedDraughtsGame {
|
||||
data,
|
||||
next_move: *next_move,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DraughtsGame> for SerializedDraughtsGame {
|
||||
fn from(DraughtsGame { board, next_move }: DraughtsGame) -> Self {
|
||||
let mut data = Vec::<u8, 24>::new();
|
||||
for i in 0..DraughtsBoard::rows() {
|
||||
for j in 0..DraughtsBoard::columns() {
|
||||
let p = Position::new(i as u8, j as u8).unwrap();
|
||||
let piece = board.get_piece(&p);
|
||||
if piece != Piece::NoPiece {
|
||||
data.push(Self::serialize(p, piece)).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
SerializedDraughtsGame {
|
||||
data,
|
||||
next_move: next_move,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<DraughtsGame> for SerializedDraughtsGame {
|
||||
fn into(self) -> DraughtsGame {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MoveIterator<'a> {
|
||||
board: &'a DraughtsBoard,
|
||||
position: Position,
|
||||
@@ -655,7 +647,11 @@ impl<'a> Iterator for MoveIterator<'a> {
|
||||
}
|
||||
_ => {
|
||||
if piece.player() != piece_at_next_position.player() {
|
||||
let capture = Move::capture(self.position, direction);
|
||||
let capture = Move::capture(
|
||||
self.position,
|
||||
direction,
|
||||
piece_at_next_position.is_crowned(),
|
||||
);
|
||||
let next_position = capture.get_end_position();
|
||||
if DraughtsBoard::is_position_valid(next_position) {
|
||||
let piece_at_end_position = self.board.get(next_position);
|
||||
@@ -673,109 +669,57 @@ impl<'a> Iterator for MoveIterator<'a> {
|
||||
}
|
||||
|
||||
pub struct RDraughtApplication {
|
||||
initial_state: DraughtsGame,
|
||||
game: DraughtsGame,
|
||||
moves: CircularBuffer<LoggedMove, 128>,
|
||||
moves: HistoryBuffer<Move, 128>,
|
||||
cursor: usize,
|
||||
}
|
||||
|
||||
impl RDraughtApplication {
|
||||
pub fn new(game: DraughtsGame) -> RDraughtApplication {
|
||||
RDraughtApplication {
|
||||
initial_state: game.clone(),
|
||||
game,
|
||||
moves: CircularBuffer::<LoggedMove, 128>::new(),
|
||||
moves: HistoryBuffer::<Move, 128>::new(),
|
||||
cursor: 0usize,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn can_undo(&self) -> bool {
|
||||
self.moves.len() > self.cursor
|
||||
}
|
||||
|
||||
pub fn can_redo(&self) -> bool {
|
||||
self.cursor > 0
|
||||
}
|
||||
|
||||
pub fn undo(&mut self) -> Result<(), Error> {
|
||||
let mut it = self.moves.iter().rev();
|
||||
let mut count = 0;
|
||||
loop {
|
||||
let mv = it.next();
|
||||
if let Some(mv) = mv {
|
||||
if count == self.cursor {
|
||||
self.game.undo_move(&mv);
|
||||
self.cursor += 1;
|
||||
break;
|
||||
}
|
||||
count += 1;
|
||||
} else {
|
||||
return Err(Error::InvalidMove);
|
||||
}
|
||||
let mut new_state = self.initial_state.clone();
|
||||
if self.cursor > 0 && !self.moves.is_empty() {
|
||||
self.cursor -= 1;
|
||||
for mv in &self.moves[0..self.cursor] {
|
||||
new_state.apply_move(mv)?;
|
||||
}
|
||||
self.game = new_state;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::InvalidMove)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn redo(&mut self) -> Result<(), Error> {
|
||||
if self.cursor == 0 {
|
||||
Err(Error::InvalidMove)
|
||||
} else {
|
||||
let mut it = self.moves.iter().rev();
|
||||
let mut count = 0;
|
||||
loop {
|
||||
let mv = it.next();
|
||||
if let Some(mv) = mv {
|
||||
if count + 1 == self.cursor {
|
||||
self.game.redo_move(&mv);
|
||||
self.cursor -= 1;
|
||||
break;
|
||||
}
|
||||
count += 1;
|
||||
} else {
|
||||
return Err(Error::InvalidMove);
|
||||
}
|
||||
let mut new_state = self.initial_state.clone();
|
||||
if self.cursor < self.moves.len() {
|
||||
for mv in &self.moves[0..self.cursor] {
|
||||
new_state.apply_move(mv)?;
|
||||
}
|
||||
self.game = new_state;
|
||||
self.cursor += 1;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::InvalidMove)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn apply_move(&mut self, mv: Move) -> Result<(), Error> {
|
||||
while self.cursor > 0 {
|
||||
self.moves.pop_back();
|
||||
self.cursor -= 1;
|
||||
self.game.apply_move(&mv)?;
|
||||
if self.moves.len() == self.moves.capacity() {
|
||||
self.initial_state.apply_move(self.moves.first().unwrap())?;
|
||||
}
|
||||
let initial_piece = self.game.piece_at(mv.get_start_position());
|
||||
let end_position = mv.get_end_position();
|
||||
let crowned_capture = if let Move::Capture {
|
||||
start: _,
|
||||
direction: _,
|
||||
} = mv
|
||||
{
|
||||
self.game
|
||||
.piece_at((mv.get_start_position() + end_position) / (2, 2))
|
||||
.is_crowned()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let next_player_move = self.game.next_move;
|
||||
self.game.check_and_apply_move(&mv)?;
|
||||
let final_piece = self.game.piece_at(end_position);
|
||||
let is_pawn_promoted =
|
||||
initial_piece != final_piece && !initial_piece.is_crowned() && final_piece.is_crowned();
|
||||
let logged_move = match mv {
|
||||
Move::Movement { start, direction } => {
|
||||
LoggedMove::movement(start, direction, is_pawn_promoted)
|
||||
}
|
||||
Move::Capture { start, direction } => {
|
||||
let multi_capture = self.game.next_move == next_player_move;
|
||||
LoggedMove::capture(
|
||||
start,
|
||||
direction,
|
||||
is_pawn_promoted,
|
||||
crowned_capture,
|
||||
multi_capture,
|
||||
)
|
||||
}
|
||||
};
|
||||
self.moves.push_evict(logged_move);
|
||||
self.moves.write(mv);
|
||||
self.cursor += 1;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -798,57 +742,42 @@ mod std {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use std::*;
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(feature = "std")]
|
||||
mod tests {
|
||||
extern crate std;
|
||||
use crate::{LoggedMove, MoveDirection};
|
||||
|
||||
use super::{DraughtsBoard, Piece, Position};
|
||||
use super::{
|
||||
DraughtsBoard, DraughtsGame, Move, MoveDirection, Piece, Player, Position,
|
||||
SerializedDraughtsGame,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
#[test]
|
||||
fn test_undo_redo() {
|
||||
let pieces = [
|
||||
(Position::new(0, 4), Piece::CrownedWhitePawn),
|
||||
(Position::new(1, 3), Piece::SimpleRedPawn),
|
||||
(Position::new(5, 5), Piece::SimpleWhitePawn),
|
||||
(Position::new(4, 4), Piece::SimpleRedPawn),
|
||||
];
|
||||
|
||||
let map = pieces
|
||||
.into_iter()
|
||||
.map(|(pos, piece)| (pos.unwrap(), piece))
|
||||
.collect::<HashMap<Position, Piece>>();
|
||||
let board = DraughtsBoard::new(|p| match map.get(&p) {
|
||||
None => Piece::NoPiece,
|
||||
Some(piece) => *piece,
|
||||
});
|
||||
|
||||
let moves = [
|
||||
LoggedMove::capture(
|
||||
Position::new(0, 4).unwrap(),
|
||||
MoveDirection::NW,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
),
|
||||
LoggedMove::movement(Position::new(0, 4).unwrap(), MoveDirection::NE, false),
|
||||
LoggedMove::capture(
|
||||
Position::new(5, 5).unwrap(),
|
||||
MoveDirection::SW,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
),
|
||||
LoggedMove::movement(Position::new(4, 4).unwrap(), MoveDirection::NW, false),
|
||||
];
|
||||
for mv in moves {
|
||||
let mut board_clone = board.clone();
|
||||
board_clone.redo_move(&mv);
|
||||
assert_ne!(board, board_clone);
|
||||
board_clone.undo_move(&mv);
|
||||
assert_eq!(board, board_clone);
|
||||
fn test_serialize_deserialize_piece() {
|
||||
for (pos, piece) in [
|
||||
(Position::new(4u8, 2u8), Piece::SimpleRedPawn),
|
||||
(Position::new(0u8, 6u8), Piece::CrownedRedPawn),
|
||||
(Position::new(1u8, 7u8), Piece::SimpleWhitePawn),
|
||||
(Position::new(2u8, 4u8), Piece::CrownedWhitePawn),
|
||||
(Position::new(3u8, 5u8), Piece::SimpleRedPawn),
|
||||
(Position::new(6u8, 2u8), Piece::CrownedRedPawn),
|
||||
(Position::new(7u8, 7u8), Piece::SimpleWhitePawn),
|
||||
(Position::new(7u8, 1u8), Piece::CrownedWhitePawn),
|
||||
(Position::new(6u8, 6u8), Piece::SimpleRedPawn),
|
||||
(Position::new(4u8, 6u8), Piece::CrownedRedPawn),
|
||||
(Position::new(6u8, 6u8), Piece::SimpleWhitePawn),
|
||||
(Position::new(4u8, 6u8), Piece::CrownedWhitePawn),
|
||||
] {
|
||||
let pos = pos.unwrap();
|
||||
let serialized = SerializedDraughtsGame::serialize(pos, piece);
|
||||
let (deserialized_pos, deserialized_piece) =
|
||||
SerializedDraughtsGame::deserialize(serialized).unwrap();
|
||||
assert_eq!(piece, deserialized_piece);
|
||||
assert_eq!(pos, deserialized_pos);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -894,7 +823,7 @@ mod tests {
|
||||
#[cfg(feature = "std")]
|
||||
mod ai_tests {
|
||||
extern crate std;
|
||||
use super::{DraughtsGame, Move, MoveDirection, Piece, Player, Position};
|
||||
use super::{DraughtsBoard, DraughtsGame, Move, MoveDirection, Piece, Player, Position};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
@@ -939,7 +868,7 @@ mod ai_tests {
|
||||
assert_eq!(
|
||||
Some(Move::movement(
|
||||
Position::new(6, 2).unwrap(),
|
||||
MoveDirection::SW,
|
||||
MoveDirection::SW
|
||||
)),
|
||||
best_move
|
||||
);
|
||||
|
@@ -1,6 +1,7 @@
|
||||
#![no_std]
|
||||
#[macro_use]
|
||||
extern crate strum;
|
||||
|
||||
mod circular_buffer;
|
||||
mod constants;
|
||||
pub mod draughts;
|
||||
mod movement;
|
||||
@@ -9,7 +10,7 @@ mod player;
|
||||
mod position;
|
||||
|
||||
pub use draughts::{DraughtsBoard, DraughtsGame, Error, RDraughtApplication, RectangularBoard};
|
||||
pub use movement::{LoggedMove, Move, MoveDirection};
|
||||
pub use movement::Move;
|
||||
pub use piece::Piece;
|
||||
pub use player::Player;
|
||||
pub use position::Position;
|
||||
|
@@ -21,64 +21,26 @@ impl MoveDirection {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compute_end_position(
|
||||
start_position: Position,
|
||||
direction: MoveDirection,
|
||||
is_capture: bool,
|
||||
) -> Position {
|
||||
if is_capture {
|
||||
let direction = direction;
|
||||
match direction {
|
||||
MoveDirection::NE => start_position + (2, 2),
|
||||
MoveDirection::SE => start_position + (-2, 2),
|
||||
MoveDirection::SW => start_position + (-2, -2),
|
||||
MoveDirection::NW => start_position + (2, -2),
|
||||
}
|
||||
} else {
|
||||
match direction {
|
||||
MoveDirection::NE => start_position + (1, 1),
|
||||
MoveDirection::SE => start_position + (-1, 1),
|
||||
MoveDirection::SW => start_position + (-1, -1),
|
||||
MoveDirection::NW => start_position + (1, -1),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct LoggedMove {
|
||||
pub struct Move {
|
||||
data: u16,
|
||||
}
|
||||
|
||||
impl LoggedMove {
|
||||
pub fn movement(start: Position, dir: MoveDirection, is_pawn_promoted: bool) -> LoggedMove {
|
||||
impl Move {
|
||||
pub fn movement(start: Position, dir: MoveDirection) -> Move {
|
||||
let mut result = <Position as Into<u8>>::into(start) as u16;
|
||||
result |= (dir as u16) << 5;
|
||||
if is_pawn_promoted {
|
||||
result |= 1u16 << 8;
|
||||
}
|
||||
LoggedMove { data: result }
|
||||
Move { data: result }
|
||||
}
|
||||
|
||||
pub fn capture(
|
||||
start: Position,
|
||||
dir: MoveDirection,
|
||||
is_pawn_promoted: bool,
|
||||
crowned_capture: bool,
|
||||
multi_capture: bool,
|
||||
) -> LoggedMove {
|
||||
pub fn capture(start: Position, dir: MoveDirection, crowned_capture: bool) -> Move {
|
||||
let mut result = <Position as Into<u8>>::into(start) as u16;
|
||||
result |= (dir as u16) << 5;
|
||||
result |= 1u16 << 7;
|
||||
if is_pawn_promoted {
|
||||
if crowned_capture {
|
||||
result |= 1u16 << 8;
|
||||
}
|
||||
if crowned_capture {
|
||||
result |= 1u16 << 9;
|
||||
}
|
||||
if multi_capture {
|
||||
result |= 1u16 << 10;
|
||||
}
|
||||
LoggedMove { data: result }
|
||||
Move { data: result }
|
||||
}
|
||||
|
||||
pub fn start_position(&self) -> Position {
|
||||
@@ -97,173 +59,63 @@ impl LoggedMove {
|
||||
!self.is_capture()
|
||||
}
|
||||
|
||||
pub fn pawn_promoted(&self) -> bool {
|
||||
pub fn crowned_captured(&self) -> bool {
|
||||
(self.data & 256) != 0
|
||||
}
|
||||
|
||||
pub fn crowned_captured(&self) -> bool {
|
||||
(self.data & 512) != 0
|
||||
}
|
||||
|
||||
pub fn multi_capture(&self) -> bool {
|
||||
(self.data & 1024) != 0
|
||||
}
|
||||
|
||||
pub fn get_end_position(&self) -> Position {
|
||||
compute_end_position(
|
||||
self.start_position().clone(),
|
||||
self.direction(),
|
||||
self.is_capture(),
|
||||
)
|
||||
if self.is_capture() {
|
||||
let start = self.start_position();
|
||||
let direction = self.direction();
|
||||
match direction {
|
||||
MoveDirection::NE => start + (2, 2),
|
||||
MoveDirection::SE => start + (-2, 2),
|
||||
MoveDirection::SW => start + (-2, -2),
|
||||
MoveDirection::NW => start + (2, -2),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
pub enum Move {
|
||||
Movement {
|
||||
start: Position,
|
||||
direction: MoveDirection,
|
||||
},
|
||||
Capture {
|
||||
start: Position,
|
||||
direction: MoveDirection,
|
||||
},
|
||||
}
|
||||
|
||||
impl Move {
|
||||
pub fn movement(start: Position, direction: MoveDirection) -> Move {
|
||||
Move::Movement { start, direction }
|
||||
} else {
|
||||
let start = self.start_position();
|
||||
let direction = self.direction();
|
||||
match direction {
|
||||
MoveDirection::NE => start + (1, 1),
|
||||
MoveDirection::SE => start + (-1, 1),
|
||||
MoveDirection::SW => start + (-1, -1),
|
||||
MoveDirection::NW => start + (1, -1),
|
||||
}
|
||||
pub fn capture(start: Position, direction: MoveDirection) -> Move {
|
||||
Move::Capture { start, direction }
|
||||
}
|
||||
|
||||
pub fn is_movement(&self) -> bool {
|
||||
match self {
|
||||
Move::Movement {
|
||||
start: _,
|
||||
direction: _,
|
||||
} => true,
|
||||
Move::Capture {
|
||||
start: _,
|
||||
direction: _,
|
||||
} => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_capture(&self) -> bool {
|
||||
!self.is_movement()
|
||||
}
|
||||
|
||||
pub fn get_start_position(&self) -> Position {
|
||||
match self {
|
||||
Move::Movement {
|
||||
start,
|
||||
direction: _,
|
||||
} => start.clone(),
|
||||
Move::Capture {
|
||||
start,
|
||||
direction: _,
|
||||
} => start.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_end_position(&self) -> Position {
|
||||
compute_end_position(
|
||||
self.get_start_position(),
|
||||
self.get_direction(),
|
||||
self.is_capture(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get_direction(&self) -> MoveDirection {
|
||||
match self {
|
||||
Move::Movement {
|
||||
start: _,
|
||||
direction,
|
||||
} => direction.clone(),
|
||||
Move::Capture {
|
||||
start: _,
|
||||
direction,
|
||||
} => direction.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{LoggedMove, MoveDirection, Piece};
|
||||
use super::{Move, MoveDirection};
|
||||
use crate::Position;
|
||||
|
||||
struct MoveData {
|
||||
piece: Piece,
|
||||
start: Position,
|
||||
dir: MoveDirection,
|
||||
capture: bool,
|
||||
crowned_capture: bool,
|
||||
pawn_promoted: bool,
|
||||
multi_capture: bool,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_into() {
|
||||
let move_datas = [
|
||||
MoveData {
|
||||
piece: Piece::SimpleRedPawn,
|
||||
let move_datas = [MoveData {
|
||||
start: Position::new(0, 0).unwrap(),
|
||||
dir: super::MoveDirection::NE,
|
||||
capture: false,
|
||||
crowned_capture: false,
|
||||
pawn_promoted: false,
|
||||
multi_capture: false,
|
||||
},
|
||||
MoveData {
|
||||
piece: Piece::SimpleWhitePawn,
|
||||
start: Position::new(3, 5).unwrap(),
|
||||
dir: super::MoveDirection::SW,
|
||||
capture: false,
|
||||
crowned_capture: false,
|
||||
pawn_promoted: false,
|
||||
multi_capture: false,
|
||||
},
|
||||
MoveData {
|
||||
piece: Piece::SimpleWhitePawn,
|
||||
start: Position::new(6, 6).unwrap(),
|
||||
dir: super::MoveDirection::NW,
|
||||
capture: false,
|
||||
crowned_capture: false,
|
||||
pawn_promoted: true,
|
||||
multi_capture: false,
|
||||
},
|
||||
MoveData {
|
||||
piece: Piece::SimpleWhitePawn,
|
||||
start: Position::new(5, 5).unwrap(),
|
||||
dir: super::MoveDirection::NE,
|
||||
capture: true,
|
||||
crowned_capture: true,
|
||||
pawn_promoted: true,
|
||||
multi_capture: true,
|
||||
},
|
||||
];
|
||||
}];
|
||||
|
||||
for md in move_datas {
|
||||
let mv = if md.capture {
|
||||
LoggedMove::capture(
|
||||
md.start,
|
||||
md.dir,
|
||||
md.pawn_promoted,
|
||||
md.crowned_capture,
|
||||
md.multi_capture,
|
||||
)
|
||||
Move::capture(md.start, md.dir, md.crowned_capture)
|
||||
} else {
|
||||
LoggedMove::movement(md.start, md.dir, md.pawn_promoted)
|
||||
Move::movement(md.start, md.dir)
|
||||
};
|
||||
assert_eq!(md.start, mv.start_position());
|
||||
assert_eq!(md.dir, mv.direction());
|
||||
assert_eq!(md.capture, mv.is_capture());
|
||||
assert_eq!(md.crowned_capture, mv.crowned_captured());
|
||||
assert_eq!(md.pawn_promoted, mv.pawn_promoted());
|
||||
assert_eq!(md.multi_capture, mv.multi_capture());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -117,9 +117,9 @@ impl Mul<(i32, i32)> for Position {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Position> for u8 {
|
||||
fn from(pos: Position) -> Self {
|
||||
(8 - 1 - pos.row()) * (POSITIONS_PER_ROW as u8) + pos.col() / 2
|
||||
impl Into<u8> for Position {
|
||||
fn into(self) -> u8 {
|
||||
(8 - 1 - self.row()) * (POSITIONS_PER_ROW as u8) + self.col() / 2
|
||||
}
|
||||
}
|
||||
|
||||
|