Compare commits
8 Commits
c11ab13f42
...
dev
Author | SHA1 | Date | |
---|---|---|---|
6d415468e0
|
|||
1348d8369f
|
|||
f4168c449b
|
|||
8e852759ba
|
|||
2483de7608
|
|||
00c9787c17
|
|||
01799ea4a0
|
|||
d326c615cd
|
12
Cargo.toml
12
Cargo.toml
@@ -1,5 +1,5 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = ["rdraught", "rdraught-cli", "rdraught-pi", "rdraught-ui"]
|
members = ["rdraught", "rdraught-cli", "rdraught-ui", "rdraught-w4"]
|
||||||
resolver = "3"
|
resolver = "3"
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
@@ -16,11 +16,13 @@ rdraught = { path = "rdraught", version = "0.1.0" }
|
|||||||
cairo-rs = "0.20"
|
cairo-rs = "0.20"
|
||||||
cairo-sys-rs = "0.20"
|
cairo-sys-rs = "0.20"
|
||||||
librsvg = "2.60"
|
librsvg = "2.60"
|
||||||
gtk4 = "0.9"
|
gtk4 = {version = "0.9", features = ["v4_10"] }
|
||||||
gdk4 = "0.9"
|
gdk4 = "0.9"
|
||||||
gio = "0.20.12"
|
gio = "0.20.12"
|
||||||
glib = "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"
|
||||||
|
|
||||||
#[patch.crates-io]
|
|
||||||
#cairo-rs = { git = "https://github.com/gtk-rs/gtk-rs-core.git", package = "cairo-rs", tag="0.20.12" }
|
|
||||||
#cairo-sys-rs = { git = "https://github.com/gtk-rs/gtk-rs-core.git", package = "cairo-sys-rs", tag="0.20.12" }
|
|
||||||
|
@@ -1,16 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "rdraught-pi"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2024"
|
|
||||||
authors = ["Walter Oggioni <oggioni.walter@gmail.com>"]
|
|
||||||
license = "MIT"
|
|
||||||
rust-version = "1.87"
|
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
name = "hello"
|
|
||||||
path = "src/hello.rs"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
panic-halt = "1.0"
|
|
||||||
libc = { version = "0.2", default-features = false }
|
|
||||||
|
|
@@ -1,17 +0,0 @@
|
|||||||
#![no_std]
|
|
||||||
#![no_main]
|
|
||||||
|
|
||||||
use libc::printf;
|
|
||||||
|
|
||||||
extern crate libc;
|
|
||||||
extern crate panic_halt;
|
|
||||||
|
|
||||||
#[link(name = "c", kind = "static")]
|
|
||||||
unsafe extern "C" {}
|
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
|
||||||
pub extern "C" fn main() {
|
|
||||||
unsafe {
|
|
||||||
printf("Hello world!!\n".as_ptr() as *const i8);
|
|
||||||
}
|
|
||||||
}
|
|
@@ -11,9 +11,17 @@ version.workspace = true
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
gtk4.workspace = true
|
gtk4.workspace = true
|
||||||
gdk4.workspace = true
|
gdk4.workspace = true
|
||||||
rdraught.workspace = true
|
rdraught = {workspace = true, features = ["std"]}
|
||||||
librsvg.workspace = true
|
librsvg.workspace = true
|
||||||
cairo-rs.workspace = true
|
cairo-rs.workspace = true
|
||||||
gio.workspace = true
|
gio.workspace = true
|
||||||
glib.workspace = true
|
glib.workspace = true
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "rdraught_ui"
|
||||||
|
crate-type = ["lib"]
|
||||||
|
bench = false
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "rdraught-ui"
|
||||||
|
path = "src/main.rs"
|
||||||
|
19
rdraught-ui/examples/ai_debugger.rs
Normal file
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)
|
||||||
|
}
|
12
rdraught-ui/src/final_dialog.rs
Normal file
12
rdraught-ui/src/final_dialog.rs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
use gtk4::{AlertDialog, Window, prelude::IsA};
|
||||||
|
use rdraught::Player;
|
||||||
|
|
||||||
|
pub(crate) async fn create_dialog<W: IsA<Window>>(window: W, winner: Player) {
|
||||||
|
let msg = match winner {
|
||||||
|
Player::Red => String::from("Red player wins"),
|
||||||
|
Player::White => String::from("White player wins"),
|
||||||
|
};
|
||||||
|
let info_dialog = AlertDialog::builder().modal(true).message(msg).build();
|
||||||
|
|
||||||
|
info_dialog.show(Some(&window));
|
||||||
|
}
|
62
rdraught-ui/src/greeting_dialog.rs
Normal file
62
rdraught-ui/src/greeting_dialog.rs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
use gtk4::{Align, Application, Box, CheckButton, Label, Orientation, Window, prelude::*};
|
||||||
|
|
||||||
|
use crate::types::SharedMutable;
|
||||||
|
use rdraught::Player;
|
||||||
|
|
||||||
|
pub(crate) fn create(application: &Application, current_player: SharedMutable<Player>) -> Window {
|
||||||
|
let label = Label::builder().label("Main player:").build();
|
||||||
|
let red_player_button = CheckButton::builder().label("Red").active(true).build();
|
||||||
|
let white_player_button = CheckButton::builder()
|
||||||
|
.label("White")
|
||||||
|
.active(false)
|
||||||
|
.group(&red_player_button)
|
||||||
|
.build();
|
||||||
|
let current_player_1 = current_player.clone();
|
||||||
|
red_player_button.connect_toggled(move |b| {
|
||||||
|
if b.is_active() {
|
||||||
|
current_player_1.set(Player::Red);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let current_player_2 = current_player.clone();
|
||||||
|
white_player_button.connect_toggled(move |b| {
|
||||||
|
if b.is_active() {
|
||||||
|
current_player_2.set(Player::White);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let main_player_selector = Box::builder()
|
||||||
|
.valign(Align::Center)
|
||||||
|
.halign(Align::Center)
|
||||||
|
.spacing(10)
|
||||||
|
.orientation(Orientation::Horizontal)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
main_player_selector.append(&label);
|
||||||
|
main_player_selector.append(&red_player_button);
|
||||||
|
main_player_selector.append(&white_player_button);
|
||||||
|
let layout = Box::builder()
|
||||||
|
.valign(Align::Center)
|
||||||
|
.halign(Align::Center)
|
||||||
|
.spacing(10)
|
||||||
|
.orientation(Orientation::Vertical)
|
||||||
|
.build();
|
||||||
|
layout.append(&main_player_selector);
|
||||||
|
layout.set_margin_top(10);
|
||||||
|
layout.set_margin_bottom(10);
|
||||||
|
layout.set_margin_start(10);
|
||||||
|
layout.set_margin_end(10);
|
||||||
|
let player_select_dialog = Window::builder()
|
||||||
|
.application(application)
|
||||||
|
.title("Rdraught")
|
||||||
|
.modal(true)
|
||||||
|
.child(&layout)
|
||||||
|
.visible(true)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
player_select_dialog.connect_close_request(move |window| {
|
||||||
|
if let Some(application) = window.application() {
|
||||||
|
application.remove_window(window);
|
||||||
|
}
|
||||||
|
glib::Propagation::Proceed
|
||||||
|
});
|
||||||
|
player_select_dialog
|
||||||
|
}
|
8
rdraught-ui/src/lib.rs
Normal file
8
rdraught-ui/src/lib.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
mod geo2d;
|
||||||
|
|
||||||
|
mod final_dialog;
|
||||||
|
mod greeting_dialog;
|
||||||
|
mod rdraught_application;
|
||||||
|
mod types;
|
||||||
|
|
||||||
|
pub use rdraught_application::run;
|
@@ -1,410 +1,8 @@
|
|||||||
use gdk4::cairo::{Context as CairoContext, Matrix, Rectangle};
|
use glib::ExitCode;
|
||||||
use gtk4::cairo::Error;
|
use rdraught::draughts::DraughtsGame;
|
||||||
use gtk4::{self as gtk, gdk::ffi::GDK_BUTTON_PRIMARY};
|
use rdraught_ui;
|
||||||
use gtk4::{DrawingArea, prelude::*};
|
|
||||||
use rdraught::draughts::{DraughtsBoard, DraughtsGame, Move, Piece, Player};
|
|
||||||
use rdraught::position::Position;
|
|
||||||
mod geo2d;
|
|
||||||
use core::f64::consts::PI;
|
|
||||||
use geo2d::Point;
|
|
||||||
use rsvg::SvgHandle;
|
|
||||||
use std::cell::{Cell, RefCell};
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
const SQUARE_SIZE: f64 = 1.0;
|
fn main() -> ExitCode {
|
||||||
|
let game = DraughtsGame::default();
|
||||||
const CROWN_RED: &[u8] = include_bytes!("crown_red.svg");
|
rdraught_ui::run(game)
|
||||||
const CROWN_WHITE: &[u8] = include_bytes!("crown_white.svg");
|
|
||||||
|
|
||||||
fn transform_point(p: &Point, m: &Matrix) -> Point {
|
|
||||||
let (x, y) = m.transform_point(p.x(), p.y());
|
|
||||||
Point::new(x, y)
|
|
||||||
}
|
|
||||||
|
|
||||||
trait AugmentedRect {
|
|
||||||
fn from_points(tl: Point, br: Point) -> Rectangle;
|
|
||||||
fn center(&self) -> Point;
|
|
||||||
fn tl(&self) -> Point;
|
|
||||||
fn br(&self) -> Point;
|
|
||||||
fn contains(&self, p: &Point) -> bool;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AugmentedRect for Rectangle {
|
|
||||||
fn center(&self) -> Point {
|
|
||||||
Point::new(
|
|
||||||
self.x() + self.width() / 2.0,
|
|
||||||
self.y() + self.height() / 2.0,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn tl(&self) -> Point {
|
|
||||||
Point::new(self.x(), self.y())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn br(&self) -> Point {
|
|
||||||
Point::new(self.x() + self.width(), self.y() + self.height())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_points(tl: Point, br: Point) -> Rectangle {
|
|
||||||
Rectangle::new(tl.x(), tl.y(), br.x() - tl.x(), br.y() - tl.y())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn contains(&self, p: &Point) -> bool {
|
|
||||||
self.x() < p.x()
|
|
||||||
&& self.x() + self.width() > p.x()
|
|
||||||
&& self.y() < p.y()
|
|
||||||
&& self.y() + self.height() > p.y()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw_piece(
|
|
||||||
cr: &CairoContext,
|
|
||||||
square: &Rectangle,
|
|
||||||
piece: Piece,
|
|
||||||
crown_red: &SvgHandle,
|
|
||||||
crown_white: &SvgHandle,
|
|
||||||
) -> Result<(), Error> {
|
|
||||||
if let Piece::NoPiece = piece {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
let center = square.center();
|
|
||||||
let outer_radius = square.width() * 0.3;
|
|
||||||
let vertical_scale_factor = 0.8;
|
|
||||||
let matrix = {
|
|
||||||
let mut m1 = Matrix::identity();
|
|
||||||
m1.translate(0.0, -(center.y() - outer_radius));
|
|
||||||
let mut m2 = Matrix::identity();
|
|
||||||
m2.scale(1.0, vertical_scale_factor);
|
|
||||||
let mut m3 = Matrix::identity();
|
|
||||||
m3.translate(0.0, center.y() - outer_radius * vertical_scale_factor);
|
|
||||||
Matrix::multiply(&Matrix::multiply(&m1, &m2), &m3)
|
|
||||||
};
|
|
||||||
cr.save()?;
|
|
||||||
cr.set_matrix(matrix);
|
|
||||||
cr.set_source_rgb(0.0, 0.0, 0.0);
|
|
||||||
let thickness = outer_radius * 0.3;
|
|
||||||
cr.arc(
|
|
||||||
center.x(),
|
|
||||||
center.y() + thickness / 2.0,
|
|
||||||
outer_radius,
|
|
||||||
0.0,
|
|
||||||
2.0 * PI,
|
|
||||||
);
|
|
||||||
cr.rectangle(
|
|
||||||
center.x() - outer_radius,
|
|
||||||
center.y() - thickness / 2.0,
|
|
||||||
outer_radius * 2.0,
|
|
||||||
thickness,
|
|
||||||
);
|
|
||||||
cr.arc(
|
|
||||||
center.x(),
|
|
||||||
center.y() - thickness / 2.0,
|
|
||||||
outer_radius,
|
|
||||||
0.0,
|
|
||||||
2.0 * PI,
|
|
||||||
);
|
|
||||||
cr.fill().unwrap();
|
|
||||||
let (color, crowned) = match piece {
|
|
||||||
Piece::NoPiece => return Ok(()),
|
|
||||||
Piece::SimpleRedPawn => ((1.0, 0.0, 0.0), false),
|
|
||||||
Piece::SimpleWhitePawn => ((1.0, 1.0, 1.0), false),
|
|
||||||
Piece::CrownedRedPawn => ((1.0, 0.0, 0.0), true),
|
|
||||||
Piece::CrownedWhitePawn => ((1.0, 1.0, 1.0), true),
|
|
||||||
};
|
|
||||||
let radius = square.width() * 0.275;
|
|
||||||
cr.set_source_rgb(color.0, color.1, color.2);
|
|
||||||
cr.arc(
|
|
||||||
center.x(),
|
|
||||||
center.y() - thickness / 2.0,
|
|
||||||
radius,
|
|
||||||
0.0,
|
|
||||||
2.0 * PI,
|
|
||||||
);
|
|
||||||
cr.fill()?;
|
|
||||||
if crowned {
|
|
||||||
let renderer = match piece.player() {
|
|
||||||
Some(Player::Red) => rsvg::CairoRenderer::new(crown_red),
|
|
||||||
Some(Player::White) => rsvg::CairoRenderer::new(crown_white),
|
|
||||||
None => panic!("This should never happen"),
|
|
||||||
};
|
|
||||||
let m4 = {
|
|
||||||
let mut m1 = Matrix::identity();
|
|
||||||
m1.translate(-center.x(), -(center.y() - thickness / 1.0));
|
|
||||||
let mut m2 = Matrix::identity();
|
|
||||||
m2.scale(0.5, 0.5);
|
|
||||||
let mut m3 = Matrix::identity();
|
|
||||||
m3.translate(center.x(), center.y() - thickness / 1.0);
|
|
||||||
Matrix::multiply(&Matrix::multiply(&m1, &m2), &m3)
|
|
||||||
};
|
|
||||||
cr.set_matrix(Matrix::multiply(&matrix, &m4));
|
|
||||||
renderer
|
|
||||||
.render_document(
|
|
||||||
cr,
|
|
||||||
&cairo::Rectangle::new(
|
|
||||||
square.tl().x(),
|
|
||||||
square.tl().y(),
|
|
||||||
square.width(),
|
|
||||||
square.height(),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
cr.restore()?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_activate(application: >k::Application) {
|
|
||||||
// Initialize GTK before using any GTK functions.
|
|
||||||
if gtk::init().is_err() {
|
|
||||||
panic!("Failed to initialize GTK.");
|
|
||||||
}
|
|
||||||
// Create a new window.
|
|
||||||
let window = gtk::ApplicationWindow::builder()
|
|
||||||
.application(application)
|
|
||||||
.title("Rdraught")
|
|
||||||
.default_width(800)
|
|
||||||
.default_height(800)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
// Create a DrawingArea widget where we will draw the chessboard.
|
|
||||||
let drawing_area = Rc::new(RefCell::new(gtk::DrawingArea::new()));
|
|
||||||
// Add the drawing area to the window.
|
|
||||||
window.set_child(Some(drawing_area.borrow().as_ref() as &DrawingArea));
|
|
||||||
|
|
||||||
let draughts_game = Rc::new(RefCell::new(DraughtsGame::default()));
|
|
||||||
let selected_piece: Rc<Cell<Option<Position>>> = Rc::new(Cell::new(None));
|
|
||||||
let available_moves: Rc<RefCell<Vec<Move>>> = Rc::new(RefCell::new(Vec::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;
|
|
||||||
let board = Rectangle::from_points(Point::new(0.0, 0.0), Point::new(board_width, board_height));
|
|
||||||
let board_clone = board;
|
|
||||||
let crown_red_handle = {
|
|
||||||
let stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from_static(CROWN_RED));
|
|
||||||
|
|
||||||
rsvg::Loader::new()
|
|
||||||
.read_stream(
|
|
||||||
&stream,
|
|
||||||
None::<&gio::File>, // no base file as this document has no references
|
|
||||||
None::<&gio::Cancellable>, // no cancellable
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
};
|
|
||||||
let crown_white_handle = {
|
|
||||||
let stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from_static(CROWN_WHITE));
|
|
||||||
|
|
||||||
rsvg::Loader::new()
|
|
||||||
.read_stream(
|
|
||||||
&stream,
|
|
||||||
None::<&gio::File>, // no base file as this document has no references
|
|
||||||
None::<&gio::Cancellable>, // no cancellable
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
};
|
|
||||||
let xform = Rc::<RefCell<Matrix>>::new(RefCell::new(Matrix::identity()));
|
|
||||||
// Set the "draw" function of the drawing area. This callback is called
|
|
||||||
// whenever GTK needs to redraw this widget (for example, on first display or when resized).
|
|
||||||
{
|
|
||||||
let draughts_game = draughts_game.clone();
|
|
||||||
let xform = xform.clone();
|
|
||||||
let selected_piece = selected_piece.clone();
|
|
||||||
let board_clone = board;
|
|
||||||
let available_moves = available_moves.clone();
|
|
||||||
let get_square_for_position = move |position: &Position, xform: &Matrix| -> Rectangle {
|
|
||||||
let square_size = SQUARE_SIZE;
|
|
||||||
|
|
||||||
let p1 = Point::new(
|
|
||||||
(position.col() as f64) * square_size,
|
|
||||||
((8 - 1 - position.row()) as f64) * square_size,
|
|
||||||
);
|
|
||||||
let p2 = &p1 + &Point::new(square_size, square_size);
|
|
||||||
let square = Rectangle::from_points(&board_clone.tl() + &p1, &board_clone.tl() + &p2);
|
|
||||||
let tl = transform_point(&square.tl(), xform);
|
|
||||||
let br = transform_point(&square.br(), xform);
|
|
||||||
Rectangle::new(
|
|
||||||
f64::min(tl.x(), br.x()),
|
|
||||||
f64::min(tl.y(), br.y()),
|
|
||||||
f64::abs(tl.x() - br.x()),
|
|
||||||
f64::abs(tl.y() - br.y()),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
drawing_area
|
|
||||||
.borrow_mut()
|
|
||||||
.set_draw_func(move |_widget, cr, width, height| {
|
|
||||||
let screen = Rectangle::from_points(
|
|
||||||
Point::new(0.0, 0.0),
|
|
||||||
Point::new(width as f64, height as f64),
|
|
||||||
);
|
|
||||||
let f = f64::min(
|
|
||||||
screen.width() / board.width(),
|
|
||||||
screen.height() / board.height(),
|
|
||||||
);
|
|
||||||
let screen_center = screen.center();
|
|
||||||
let board_center = board.center();
|
|
||||||
let mut xform = xform.borrow_mut();
|
|
||||||
*xform = Matrix::multiply(
|
|
||||||
&Matrix::multiply(
|
|
||||||
&Matrix::new(1.0, 0.0, 0.0, 1.0, -board_center.x(), -board_center.y()),
|
|
||||||
&Matrix::new(f, 0.0, 0.0, f, 0.0, 0.0),
|
|
||||||
),
|
|
||||||
&Matrix::new(1.0, 0.0, 0.0, 1.0, screen_center.x(), screen_center.y()),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Loop over rows and columns to draw each chessboard cell.
|
|
||||||
for row in 0..DraughtsBoard::rows() {
|
|
||||||
for col in 0..DraughtsBoard::columns() {
|
|
||||||
let position = Position::new((8 - row - 1) as u8, col as u8);
|
|
||||||
let square = get_square_for_position(&position, &xform);
|
|
||||||
// Alternate colors based on the sum of row and column indices.
|
|
||||||
if (row + col) % 2 == 0 {
|
|
||||||
cr.set_source_rgb(0.8, 0.8, 0.6);
|
|
||||||
} else {
|
|
||||||
cr.set_source_rgb(0.4, 0.4, 0.2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw and fill the square.
|
|
||||||
cr.rectangle(
|
|
||||||
square.tl().x(),
|
|
||||||
square.tl().y(),
|
|
||||||
square.width(),
|
|
||||||
square.height(),
|
|
||||||
);
|
|
||||||
cr.fill().unwrap();
|
|
||||||
draw_piece(
|
|
||||||
cr,
|
|
||||||
&square,
|
|
||||||
draughts_game.borrow().piece_at(position),
|
|
||||||
&crown_red_handle,
|
|
||||||
&crown_white_handle,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if let Some(selected_postion) = selected_piece.get() {
|
|
||||||
let square = get_square_for_position(&selected_postion, &xform);
|
|
||||||
cr.save().unwrap();
|
|
||||||
cr.new_path();
|
|
||||||
cr.move_to(square.tl().x(), square.tl().y());
|
|
||||||
cr.line_to(square.tl().x(), square.br().y());
|
|
||||||
cr.line_to(square.br().x(), square.br().y());
|
|
||||||
cr.line_to(square.br().x(), square.tl().y());
|
|
||||||
cr.line_to(square.tl().x(), square.tl().y());
|
|
||||||
cr.clip();
|
|
||||||
cr.new_path();
|
|
||||||
cr.set_source_rgb(0.0, 0.0, 1.0);
|
|
||||||
cr.set_line_width((square.width() + square.height()) * 0.05);
|
|
||||||
cr.move_to(square.tl().x(), square.tl().y());
|
|
||||||
cr.line_to(square.tl().x(), square.br().y());
|
|
||||||
cr.line_to(square.br().x(), square.br().y());
|
|
||||||
cr.line_to(square.br().x(), square.tl().y());
|
|
||||||
cr.line_to(square.tl().x(), square.tl().y());
|
|
||||||
cr.stroke().unwrap();
|
|
||||||
cr.restore().unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
let am = available_moves.borrow();
|
|
||||||
if !am.is_empty() {
|
|
||||||
for mv in am.iter() {
|
|
||||||
let square = get_square_for_position(&mv.get_end_position(), &xform);
|
|
||||||
cr.save().unwrap();
|
|
||||||
cr.new_path();
|
|
||||||
cr.move_to(square.tl().x(), square.tl().y());
|
|
||||||
cr.line_to(square.tl().x(), square.br().y());
|
|
||||||
cr.line_to(square.br().x(), square.br().y());
|
|
||||||
cr.line_to(square.br().x(), square.tl().y());
|
|
||||||
cr.line_to(square.tl().x(), square.tl().y());
|
|
||||||
cr.clip();
|
|
||||||
cr.new_path();
|
|
||||||
cr.set_source_rgb(0.0, 1.0, 0.0);
|
|
||||||
cr.set_line_width((square.width() + square.height()) * 0.05);
|
|
||||||
cr.move_to(square.tl().x(), square.tl().y());
|
|
||||||
cr.line_to(square.tl().x(), square.br().y());
|
|
||||||
cr.line_to(square.br().x(), square.br().y());
|
|
||||||
cr.line_to(square.br().x(), square.tl().y());
|
|
||||||
cr.line_to(square.tl().x(), square.tl().y());
|
|
||||||
cr.stroke().unwrap();
|
|
||||||
cr.restore().unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
let gesture = gtk::GestureClick::new();
|
|
||||||
|
|
||||||
// Set the gestures button to the right mouse button (=3)
|
|
||||||
gesture.set_button(GDK_BUTTON_PRIMARY as u32);
|
|
||||||
|
|
||||||
// Assign your handler to an event of the gesture (e.g. the `pressed` event)
|
|
||||||
{
|
|
||||||
let drawing_area = drawing_area.clone();
|
|
||||||
let available_moves = available_moves.clone();
|
|
||||||
gesture.connect_pressed(move |gesture, _, x, y| {
|
|
||||||
gesture.set_state(gtk::EventSequenceState::Claimed);
|
|
||||||
let xform = xform.borrow();
|
|
||||||
let inverse = {
|
|
||||||
let mut m = *xform;
|
|
||||||
m.invert();
|
|
||||||
m
|
|
||||||
};
|
|
||||||
let p = transform_point(&Point::new(x, y), &inverse);
|
|
||||||
if board_clone.contains(&p) {
|
|
||||||
let p = &p - &board_clone.tl();
|
|
||||||
// println!("Point: {:?}", p);
|
|
||||||
let position = Position::new(
|
|
||||||
8 - 1 - (p.y() / SQUARE_SIZE) as u8,
|
|
||||||
(p.x() / SQUARE_SIZE) as u8,
|
|
||||||
);
|
|
||||||
// println!("Selected position: {:?}", position);
|
|
||||||
let mut draughts_game = draughts_game.borrow_mut();
|
|
||||||
let piece = draughts_game.piece_at(position);
|
|
||||||
let mut am = available_moves.borrow_mut();
|
|
||||||
let mut move_applied = false;
|
|
||||||
if !am.is_empty() {
|
|
||||||
for mv in am.iter() {
|
|
||||||
if mv.get_end_position() == position {
|
|
||||||
draughts_game.apply_move(mv).unwrap();
|
|
||||||
move_applied = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if move_applied {
|
|
||||||
selected_piece.set(None);
|
|
||||||
am.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !move_applied {
|
|
||||||
match piece.player() {
|
|
||||||
Some(Player::Red) => selected_piece.set(Some(position)),
|
|
||||||
Some(Player::White) => selected_piece.set(Some(position)),
|
|
||||||
None => selected_piece.set(None),
|
|
||||||
}
|
|
||||||
am.clear();
|
|
||||||
if piece.player().is_none() {
|
|
||||||
selected_piece.set(None)
|
|
||||||
} else {
|
|
||||||
selected_piece.set(Some(position));
|
|
||||||
for mv in draughts_game.moves_for_piece(position) {
|
|
||||||
am.push(mv);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
selected_piece.set(None);
|
|
||||||
}
|
|
||||||
drawing_area.borrow_mut().queue_draw();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// Assign the gesture to the treeview
|
|
||||||
drawing_area.borrow_mut().add_controller(gesture);
|
|
||||||
window.present();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
// Create a new application with the builder pattern
|
|
||||||
let app = gtk::Application::builder()
|
|
||||||
.application_id("net.woggioni.rdraught")
|
|
||||||
.build();
|
|
||||||
app.connect_activate(on_activate);
|
|
||||||
// Run the application
|
|
||||||
app.run();
|
|
||||||
}
|
}
|
||||||
|
551
rdraught-ui/src/rdraught_application.rs
Normal file
551
rdraught-ui/src/rdraught_application.rs
Normal file
@@ -0,0 +1,551 @@
|
|||||||
|
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, DrawingArea, prelude::*};
|
||||||
|
use rdraught::{
|
||||||
|
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 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");
|
||||||
|
|
||||||
|
fn transform_point(p: &Point, m: &Matrix) -> Point {
|
||||||
|
let (x, y) = m.transform_point(p.x(), p.y());
|
||||||
|
Point::new(x, y)
|
||||||
|
}
|
||||||
|
|
||||||
|
trait AugmentedRect {
|
||||||
|
fn from_points(tl: Point, br: Point) -> Rectangle;
|
||||||
|
fn center(&self) -> Point;
|
||||||
|
fn tl(&self) -> Point;
|
||||||
|
fn br(&self) -> Point;
|
||||||
|
fn contains(&self, p: &Point) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AugmentedRect for Rectangle {
|
||||||
|
fn center(&self) -> Point {
|
||||||
|
Point::new(
|
||||||
|
self.x() + self.width() / 2.0,
|
||||||
|
self.y() + self.height() / 2.0,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tl(&self) -> Point {
|
||||||
|
Point::new(self.x(), self.y())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn br(&self) -> Point {
|
||||||
|
Point::new(self.x() + self.width(), self.y() + self.height())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_points(tl: Point, br: Point) -> Rectangle {
|
||||||
|
Rectangle::new(tl.x(), tl.y(), br.x() - tl.x(), br.y() - tl.y())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn contains(&self, p: &Point) -> bool {
|
||||||
|
self.x() < p.x()
|
||||||
|
&& self.x() + self.width() > p.x()
|
||||||
|
&& self.y() < p.y()
|
||||||
|
&& self.y() + self.height() > p.y()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_piece(
|
||||||
|
cr: &CairoContext,
|
||||||
|
square: &Rectangle,
|
||||||
|
piece: Piece,
|
||||||
|
crown_red: &SvgHandle,
|
||||||
|
crown_white: &SvgHandle,
|
||||||
|
) -> Result<(), cairo::Error> {
|
||||||
|
if let Piece::NoPiece = piece {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
cr.save()?;
|
||||||
|
let center = square.center();
|
||||||
|
let outer_radius = square.width() * 0.3;
|
||||||
|
let vertical_scale_factor = 0.8;
|
||||||
|
let matrix = {
|
||||||
|
let mut m1 = Matrix::identity();
|
||||||
|
m1.translate(0.0, -(center.y() - outer_radius));
|
||||||
|
let mut m2 = Matrix::identity();
|
||||||
|
m2.scale(1.0, vertical_scale_factor);
|
||||||
|
let mut m3 = Matrix::identity();
|
||||||
|
m3.translate(0.0, center.y() - outer_radius * vertical_scale_factor);
|
||||||
|
Matrix::multiply(&Matrix::multiply(&m1, &m2), &m3)
|
||||||
|
};
|
||||||
|
cr.set_matrix(Matrix::multiply(&matrix, &cr.matrix()));
|
||||||
|
cr.set_source_rgb(0.0, 0.0, 0.0);
|
||||||
|
let thickness = outer_radius * 0.3;
|
||||||
|
cr.arc(
|
||||||
|
center.x(),
|
||||||
|
center.y() + thickness / 2.0,
|
||||||
|
outer_radius,
|
||||||
|
0.0,
|
||||||
|
2.0 * PI,
|
||||||
|
);
|
||||||
|
cr.rectangle(
|
||||||
|
center.x() - outer_radius,
|
||||||
|
center.y() - thickness / 2.0,
|
||||||
|
outer_radius * 2.0,
|
||||||
|
thickness,
|
||||||
|
);
|
||||||
|
cr.arc(
|
||||||
|
center.x(),
|
||||||
|
center.y() - thickness / 2.0,
|
||||||
|
outer_radius,
|
||||||
|
0.0,
|
||||||
|
2.0 * PI,
|
||||||
|
);
|
||||||
|
cr.fill().unwrap();
|
||||||
|
let (color, crowned) = match piece {
|
||||||
|
Piece::NoPiece => return Ok(()),
|
||||||
|
Piece::SimpleRedPawn => ((1.0, 0.0, 0.0), false),
|
||||||
|
Piece::SimpleWhitePawn => ((1.0, 1.0, 1.0), false),
|
||||||
|
Piece::CrownedRedPawn => ((1.0, 0.0, 0.0), true),
|
||||||
|
Piece::CrownedWhitePawn => ((1.0, 1.0, 1.0), true),
|
||||||
|
};
|
||||||
|
let radius = square.width() * 0.275;
|
||||||
|
cr.set_source_rgb(color.0, color.1, color.2);
|
||||||
|
cr.arc(
|
||||||
|
center.x(),
|
||||||
|
center.y() - thickness / 2.0,
|
||||||
|
radius,
|
||||||
|
0.0,
|
||||||
|
2.0 * PI,
|
||||||
|
);
|
||||||
|
cr.fill()?;
|
||||||
|
cr.restore()?;
|
||||||
|
if crowned {
|
||||||
|
let renderer = match piece.player() {
|
||||||
|
Some(Player::Red) => rsvg::CairoRenderer::new(crown_red),
|
||||||
|
Some(Player::White) => rsvg::CairoRenderer::new(crown_white),
|
||||||
|
None => panic!("This should never happen"),
|
||||||
|
};
|
||||||
|
let m4 = {
|
||||||
|
let mut m1 = Matrix::identity();
|
||||||
|
m1.translate(-center.x(), -(center.y() - thickness / 1.0));
|
||||||
|
let mut m2 = Matrix::identity();
|
||||||
|
m2.scale(0.5, 0.5);
|
||||||
|
let mut m3 = Matrix::identity();
|
||||||
|
m3.translate(center.x(), center.y() - thickness / 1.0);
|
||||||
|
Matrix::multiply(&Matrix::multiply(&m1, &m2), &m3)
|
||||||
|
};
|
||||||
|
cr.set_matrix(Matrix::multiply(
|
||||||
|
&Matrix::multiply(&matrix, &m4),
|
||||||
|
&cr.matrix(),
|
||||||
|
));
|
||||||
|
renderer
|
||||||
|
.render_document(
|
||||||
|
cr,
|
||||||
|
&cairo::Rectangle::new(
|
||||||
|
square.tl().x(),
|
||||||
|
square.tl().y(),
|
||||||
|
square.width(),
|
||||||
|
square.height(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_score_bar(
|
||||||
|
cr: &CairoContext,
|
||||||
|
board: &Rectangle,
|
||||||
|
draughts_game: &DraughtsGame,
|
||||||
|
current_player: Player,
|
||||||
|
) {
|
||||||
|
fn modulate_score(relative_score: f64) -> f64 {
|
||||||
|
let x = relative_score;
|
||||||
|
1.0 - (f64::atan(8.0 * x - 4.0) / f64::atan(4.0) / 2.0 + 0.5)
|
||||||
|
}
|
||||||
|
let score_bar = Rectangle::new(
|
||||||
|
board.tl().x() - board.width() / 10.0,
|
||||||
|
board.tl().y(),
|
||||||
|
board.width() / 16.0,
|
||||||
|
board.height(),
|
||||||
|
);
|
||||||
|
let num_rects = 40usize;
|
||||||
|
let spacing = board.height() / 200.0;
|
||||||
|
let rect_height = (board.height() - spacing * (num_rects - 1) as f64) / (num_rects as f64);
|
||||||
|
let score_percentage = modulate_score(draughts_game.relative_score(current_player) as f64);
|
||||||
|
let threshold = (score_percentage * num_rects as f64) as usize;
|
||||||
|
let tl = score_bar.tl();
|
||||||
|
cr.save().unwrap();
|
||||||
|
cr.set_line_width(spacing / 4.0);
|
||||||
|
for i in 0..threshold {
|
||||||
|
match current_player {
|
||||||
|
Player::White => cr.set_source_rgb(1.0, 0.0, 0.0),
|
||||||
|
Player::Red => cr.set_source_rgb(1.0, 1.0, 1.0),
|
||||||
|
}
|
||||||
|
cr.rectangle(
|
||||||
|
tl.x(),
|
||||||
|
tl.y() + i as f64 * (rect_height + spacing),
|
||||||
|
score_bar.width(),
|
||||||
|
rect_height,
|
||||||
|
);
|
||||||
|
cr.fill().unwrap();
|
||||||
|
cr.set_source_rgb(0.0, 0.0, 0.0);
|
||||||
|
cr.rectangle(
|
||||||
|
tl.x(),
|
||||||
|
tl.y() + i as f64 * (rect_height + spacing),
|
||||||
|
score_bar.width(),
|
||||||
|
rect_height,
|
||||||
|
);
|
||||||
|
cr.stroke().unwrap();
|
||||||
|
}
|
||||||
|
for i in threshold..num_rects {
|
||||||
|
match current_player {
|
||||||
|
Player::White => cr.set_source_rgb(1.0, 1.0, 1.0),
|
||||||
|
Player::Red => cr.set_source_rgb(1.0, 0.0, 0.0),
|
||||||
|
}
|
||||||
|
cr.rectangle(
|
||||||
|
tl.x(),
|
||||||
|
tl.y() + i as f64 * (rect_height + spacing),
|
||||||
|
score_bar.width(),
|
||||||
|
rect_height,
|
||||||
|
);
|
||||||
|
cr.fill().unwrap();
|
||||||
|
cr.set_source_rgb(0.0, 0.0, 0.0);
|
||||||
|
cr.rectangle(
|
||||||
|
tl.x(),
|
||||||
|
tl.y() + i as f64 * (rect_height + spacing),
|
||||||
|
score_bar.width(),
|
||||||
|
rect_height,
|
||||||
|
);
|
||||||
|
cr.stroke().unwrap();
|
||||||
|
}
|
||||||
|
cr.restore().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_game_window(
|
||||||
|
rd: SharedMutableRef<RDraughtApplication>,
|
||||||
|
application: &Application,
|
||||||
|
current_player: Player,
|
||||||
|
) {
|
||||||
|
// Create a new window.
|
||||||
|
let window = gtk::ApplicationWindow::builder()
|
||||||
|
.application(application)
|
||||||
|
.title("Rdraught")
|
||||||
|
.default_width(800)
|
||||||
|
.default_height(800)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
|
||||||
|
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;
|
||||||
|
let board = Rectangle::from_points(Point::new(0.0, 0.0), Point::new(board_width, board_height));
|
||||||
|
let board_with_bar = Rectangle::from_points(
|
||||||
|
Point::new(-board_width / 10.0, 0.0),
|
||||||
|
Point::new(board_width, board_height),
|
||||||
|
);
|
||||||
|
let board_clone = board;
|
||||||
|
let crown_red_handle = {
|
||||||
|
let stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from_static(CROWN_RED));
|
||||||
|
rsvg::Loader::new()
|
||||||
|
.read_stream(
|
||||||
|
&stream,
|
||||||
|
None::<&gio::File>, // no base file as this document has no references
|
||||||
|
None::<&gio::Cancellable>, // no cancellable
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
};
|
||||||
|
let crown_white_handle = {
|
||||||
|
let stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from_static(CROWN_WHITE));
|
||||||
|
|
||||||
|
rsvg::Loader::new()
|
||||||
|
.read_stream(
|
||||||
|
&stream,
|
||||||
|
None::<&gio::File>, // no base file as this document has no references
|
||||||
|
None::<&gio::Cancellable>, // no cancellable
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
};
|
||||||
|
let xform = new_shared_mut_ref(Matrix::identity());
|
||||||
|
// Set the "draw" function of the drawing area. This callback is called
|
||||||
|
// whenever GTK needs to redraw this widget (for example, on first display or when resized).
|
||||||
|
{
|
||||||
|
let rd = rd.clone();
|
||||||
|
let xform = xform.clone();
|
||||||
|
let selected_piece = selected_piece.clone();
|
||||||
|
let available_moves = available_moves.clone();
|
||||||
|
drawing_area.set_draw_func(move |_widget, cr, width, height| {
|
||||||
|
let screen = Rectangle::from_points(
|
||||||
|
Point::new(0.0, 0.0),
|
||||||
|
Point::new(width as f64, height as f64),
|
||||||
|
);
|
||||||
|
let f = f64::min(
|
||||||
|
screen.width() / board_with_bar.width(),
|
||||||
|
screen.height() / board_with_bar.height(),
|
||||||
|
);
|
||||||
|
let screen_center = screen.center();
|
||||||
|
let board_center = board_with_bar.center();
|
||||||
|
let mut xform = xform.borrow_mut();
|
||||||
|
*xform = Matrix::multiply(
|
||||||
|
&Matrix::multiply(
|
||||||
|
&Matrix::new(1.0, 0.0, 0.0, 1.0, -board_center.x(), -board_center.y()),
|
||||||
|
&Matrix::new(f, 0.0, 0.0, f, 0.0, 0.0),
|
||||||
|
),
|
||||||
|
&Matrix::new(1.0, 0.0, 0.0, 1.0, screen_center.x(), screen_center.y()),
|
||||||
|
);
|
||||||
|
cr.set_matrix(*xform);
|
||||||
|
|
||||||
|
// Loop over rows and columns to draw each chessboard cell.
|
||||||
|
for row in 0..DraughtsBoard::rows() {
|
||||||
|
for col in 0..DraughtsBoard::columns() {
|
||||||
|
let position = match current_player {
|
||||||
|
Player::Red => Position::new(row as u8, (8 - 1 - col) as u8),
|
||||||
|
Player::White => Position::new((8 - 1 - row) as u8, col as u8),
|
||||||
|
};
|
||||||
|
let square = Rectangle::new(
|
||||||
|
col as f64 * SQUARE_SIZE,
|
||||||
|
row as f64 * SQUARE_SIZE,
|
||||||
|
SQUARE_SIZE,
|
||||||
|
SQUARE_SIZE,
|
||||||
|
);
|
||||||
|
cr.save().unwrap();
|
||||||
|
// Alternate colors based on the sum of row and column indices.
|
||||||
|
if (row + col) % 2 == 0 {
|
||||||
|
cr.set_source_rgb(0.8, 0.8, 0.6);
|
||||||
|
} else {
|
||||||
|
cr.set_source_rgb(0.4, 0.4, 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw and fill the square.
|
||||||
|
cr.rectangle(
|
||||||
|
square.tl().x(),
|
||||||
|
square.tl().y(),
|
||||||
|
square.width(),
|
||||||
|
square.height(),
|
||||||
|
);
|
||||||
|
cr.fill().unwrap();
|
||||||
|
draw_piece(
|
||||||
|
cr,
|
||||||
|
&square,
|
||||||
|
position.map_or(Piece::NoPiece, |p| rd.borrow().game().piece_at(p)),
|
||||||
|
&crown_red_handle,
|
||||||
|
&crown_white_handle,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
cr.restore().unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(selected_position) = selected_piece.get() {
|
||||||
|
let screen_position = match current_player {
|
||||||
|
Player::White => (8 - 1 - selected_position.row(), selected_position.col()),
|
||||||
|
Player::Red => (selected_position.row(), 8 - 1 - selected_position.col()),
|
||||||
|
};
|
||||||
|
let square = Rectangle::new(
|
||||||
|
screen_position.1 as f64 * SQUARE_SIZE,
|
||||||
|
screen_position.0 as f64 * SQUARE_SIZE,
|
||||||
|
SQUARE_SIZE,
|
||||||
|
SQUARE_SIZE,
|
||||||
|
);
|
||||||
|
cr.save().unwrap();
|
||||||
|
cr.new_path();
|
||||||
|
cr.move_to(square.tl().x(), square.tl().y());
|
||||||
|
cr.line_to(square.tl().x(), square.br().y());
|
||||||
|
cr.line_to(square.br().x(), square.br().y());
|
||||||
|
cr.line_to(square.br().x(), square.tl().y());
|
||||||
|
cr.line_to(square.tl().x(), square.tl().y());
|
||||||
|
cr.clip();
|
||||||
|
cr.new_path();
|
||||||
|
cr.set_source_rgb(0.0, 0.0, 1.0);
|
||||||
|
cr.set_line_width((square.width() + square.height()) * 0.035);
|
||||||
|
cr.move_to(square.tl().x(), square.tl().y());
|
||||||
|
cr.line_to(square.tl().x(), square.br().y());
|
||||||
|
cr.line_to(square.br().x(), square.br().y());
|
||||||
|
cr.line_to(square.br().x(), square.tl().y());
|
||||||
|
cr.line_to(square.tl().x(), square.tl().y());
|
||||||
|
cr.stroke().unwrap();
|
||||||
|
cr.restore().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
let am = available_moves.borrow();
|
||||||
|
if !am.is_empty() {
|
||||||
|
for mv in am.iter() {
|
||||||
|
let end_pos = mv.get_end_position();
|
||||||
|
let screen_position = match current_player {
|
||||||
|
Player::White => (8 - 1 - end_pos.row(), end_pos.col()),
|
||||||
|
Player::Red => (end_pos.row(), 8 - 1 - end_pos.col()),
|
||||||
|
};
|
||||||
|
let square = Rectangle::new(
|
||||||
|
screen_position.1 as f64 * SQUARE_SIZE,
|
||||||
|
screen_position.0 as f64 * SQUARE_SIZE,
|
||||||
|
SQUARE_SIZE,
|
||||||
|
SQUARE_SIZE,
|
||||||
|
);
|
||||||
|
cr.save().unwrap();
|
||||||
|
cr.new_path();
|
||||||
|
cr.move_to(square.tl().x(), square.tl().y());
|
||||||
|
cr.line_to(square.tl().x(), square.br().y());
|
||||||
|
cr.line_to(square.br().x(), square.br().y());
|
||||||
|
cr.line_to(square.br().x(), square.tl().y());
|
||||||
|
cr.line_to(square.tl().x(), square.tl().y());
|
||||||
|
cr.clip();
|
||||||
|
cr.new_path();
|
||||||
|
cr.set_source_rgb(0.0, 1.0, 0.0);
|
||||||
|
cr.set_line_width((square.width() + square.height()) * 0.035);
|
||||||
|
cr.move_to(square.tl().x(), square.tl().y());
|
||||||
|
cr.line_to(square.tl().x(), square.br().y());
|
||||||
|
cr.line_to(square.br().x(), square.br().y());
|
||||||
|
cr.line_to(square.br().x(), square.tl().y());
|
||||||
|
cr.line_to(square.tl().x(), square.tl().y());
|
||||||
|
cr.stroke().unwrap();
|
||||||
|
cr.restore().unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
draw_score_bar(cr, &board, rd.borrow().game(), current_player);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
let gesture = gtk::GestureClick::new();
|
||||||
|
|
||||||
|
// Set the gestures button to the right mouse button (=3)
|
||||||
|
gesture.set_button(GDK_BUTTON_PRIMARY as u32);
|
||||||
|
|
||||||
|
// Assign your handler to an event of the gesture (e.g. the `pressed` event)
|
||||||
|
{
|
||||||
|
let drawing_area = drawing_area.clone();
|
||||||
|
let available_moves = available_moves.clone();
|
||||||
|
let window = window.clone();
|
||||||
|
gesture.connect_pressed(move |gesture, _, x, y| {
|
||||||
|
gesture.set_state(gtk::EventSequenceState::Claimed);
|
||||||
|
if let Some(winner) = rd.borrow().game().winner() {
|
||||||
|
MainContext::default()
|
||||||
|
.spawn_local(final_dialog::create_dialog(window.clone(), winner));
|
||||||
|
} else {
|
||||||
|
let xform = xform.borrow();
|
||||||
|
let inverse = {
|
||||||
|
let mut m = *xform;
|
||||||
|
m.invert();
|
||||||
|
m
|
||||||
|
};
|
||||||
|
let p = transform_point(&Point::new(x, y), &inverse);
|
||||||
|
if board_clone.contains(&p) {
|
||||||
|
let p = &p - &board_clone.tl();
|
||||||
|
// println!("Point: {:?}", p);
|
||||||
|
let position = match current_player {
|
||||||
|
Player::White => Position::new(
|
||||||
|
(8.0 - (p.y() / SQUARE_SIZE)) as u8,
|
||||||
|
(p.x() / SQUARE_SIZE) as u8,
|
||||||
|
),
|
||||||
|
Player::Red => Position::new(
|
||||||
|
(p.y() / SQUARE_SIZE) as u8,
|
||||||
|
(8.0 - p.x() / SQUARE_SIZE) as u8,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
println!("Selected position: {:?}", position);
|
||||||
|
let piece = {
|
||||||
|
let rd = rd.borrow();
|
||||||
|
let draughts_game = rd.game();
|
||||||
|
position
|
||||||
|
.clone()
|
||||||
|
.map_or(Piece::NoPiece, |it| draughts_game.piece_at(it))
|
||||||
|
// println!("Selected piece: {:?}", piece);
|
||||||
|
};
|
||||||
|
let am = available_moves.replace(Vec::new());
|
||||||
|
let mut move_applied = false;
|
||||||
|
if !am.is_empty() {
|
||||||
|
if let Ok(pos) = position {
|
||||||
|
for mv in am.into_iter() {
|
||||||
|
if mv.get_end_position() == pos {
|
||||||
|
let mut rd_app = rd.borrow_mut();
|
||||||
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
move_applied = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if move_applied {
|
||||||
|
selected_piece.set(None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !move_applied {
|
||||||
|
let position = position.ok();
|
||||||
|
match piece.player() {
|
||||||
|
Some(Player::Red) => selected_piece.set(position),
|
||||||
|
Some(Player::White) => selected_piece.set(position),
|
||||||
|
None => selected_piece.set(None),
|
||||||
|
}
|
||||||
|
if piece.player().is_none() {
|
||||||
|
selected_piece.set(None)
|
||||||
|
} else {
|
||||||
|
let mut am = available_moves.borrow_mut();
|
||||||
|
selected_piece.set(position);
|
||||||
|
if let Some(position) = position {
|
||||||
|
for mv in rd.borrow().game().moves_for_piece(position) {
|
||||||
|
am.push(mv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
selected_piece.set(None);
|
||||||
|
}
|
||||||
|
drawing_area.queue_draw();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Assign the gesture to the treeview
|
||||||
|
drawing_area.add_controller(gesture);
|
||||||
|
window.present();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_activate(application: &Application, game: DraughtsGame) {
|
||||||
|
// Initialize GTK before using any GTK functions.
|
||||||
|
if gtk::init().is_err() {
|
||||||
|
panic!("Failed to initialize GTK.");
|
||||||
|
}
|
||||||
|
|
||||||
|
let current_player = new_shared_mut(Player::Red);
|
||||||
|
let dialog = greeting_dialog::create(application, current_player.clone());
|
||||||
|
let application = application.clone();
|
||||||
|
dialog.connect_close_request(move |w| {
|
||||||
|
application.remove_window(w);
|
||||||
|
let rd = new_shared_mut_ref(RDraughtApplication::new(game.clone()));
|
||||||
|
create_game_window(rd.clone(), &application, current_player.get());
|
||||||
|
Propagation::Proceed
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(game: DraughtsGame) -> ExitCode {
|
||||||
|
// Create a new application with the builder pattern
|
||||||
|
let app = Application::builder()
|
||||||
|
.application_id("net.woggioni.rdraught")
|
||||||
|
.build();
|
||||||
|
app.connect_activate(move |it| on_activate(it, game.clone()));
|
||||||
|
app.run()
|
||||||
|
}
|
12
rdraught-ui/src/types.rs
Normal file
12
rdraught-ui/src/types.rs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
use std::cell::{Cell, RefCell};
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
pub(crate) type SharedMutable<T> = Rc<Cell<T>>;
|
||||||
|
pub(crate) type SharedMutableRef<T> = Rc<RefCell<T>>;
|
||||||
|
pub(crate) fn new_shared_mut_ref<T>(obj: T) -> SharedMutableRef<T> {
|
||||||
|
Rc::new(RefCell::new(obj))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn new_shared_mut<T>(obj: T) -> SharedMutable<T> {
|
||||||
|
Rc::new(Cell::new(obj))
|
||||||
|
}
|
19
rdraught-w4/Cargo.toml
Normal file
19
rdraught-w4/Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
[package]
|
||||||
|
name = "rdraught-w4"
|
||||||
|
authors.workspace = true
|
||||||
|
edition.workspace = true
|
||||||
|
homepage.workspace = true
|
||||||
|
license.workspace = true
|
||||||
|
repository.workspace = true
|
||||||
|
rust-version.workspace = true
|
||||||
|
version.workspace = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
wasm4.workspace = true
|
||||||
|
wasm4-sys.workspace = true
|
||||||
|
rdraught.workspace = true
|
||||||
|
|
||||||
|
[package.metadata.docs.rs]
|
||||||
|
all-features = true
|
||||||
|
default-target = "wasm32-unknown-unknown"
|
||||||
|
targets = []
|
45
rdraught-w4/src/main.rs
Normal file
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);
|
||||||
|
// }
|
||||||
|
// }
|
@@ -13,7 +13,9 @@ name = "rdraught"
|
|||||||
crate-type = ["lib"]
|
crate-type = ["lib"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
heapless = "0.8"
|
heapless.workspace = true
|
||||||
|
strum.workspace = true
|
||||||
|
strum_macros.workspace = true
|
||||||
|
|
||||||
[dev-dependencies]
|
[features]
|
||||||
rand = "0.9"
|
std = []
|
||||||
|
@@ -15,7 +15,7 @@ fn main() {
|
|||||||
pieces.push(Piece::CrownedRedPawn).unwrap();
|
pieces.push(Piece::CrownedRedPawn).unwrap();
|
||||||
|
|
||||||
board
|
board
|
||||||
.pieces(&pieces)
|
.pieces(pieces)
|
||||||
.for_each(|pos| println!("({}, {}): {:?}", pos.row(), pos.col(), board[pos]));
|
.for_each(|pos| println!("({}, {}): {:?}", pos.row(), pos.col(), board[pos]));
|
||||||
// println!("{:?}", board[Position::new(0, 0)]);
|
// println!("{:?}", board[Position::new(0, 0)]);
|
||||||
|
|
||||||
|
@@ -1,131 +0,0 @@
|
|||||||
use core::iter::Enumerate;
|
|
||||||
use core::iter::Flatten;
|
|
||||||
use core::ops::Index;
|
|
||||||
use core::ops::IndexMut;
|
|
||||||
|
|
||||||
use super::position::Position;
|
|
||||||
|
|
||||||
pub trait RectangularBoard {
|
|
||||||
fn rows() -> usize;
|
|
||||||
fn columns() -> usize;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub(crate) struct Board<TYPE, const ROWS: usize, const COLUMNS: usize> {
|
|
||||||
data: [[TYPE; COLUMNS]; ROWS],
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<TYPE, const ROWS: usize, const COLUMNS: usize> RectangularBoard
|
|
||||||
for Board<TYPE, ROWS, COLUMNS>
|
|
||||||
{
|
|
||||||
fn rows() -> usize {
|
|
||||||
ROWS
|
|
||||||
}
|
|
||||||
|
|
||||||
fn columns() -> usize {
|
|
||||||
COLUMNS
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<TYPE, const ROWS: usize, const COLUMNS: usize> Default for Board<TYPE, ROWS, COLUMNS>
|
|
||||||
where
|
|
||||||
TYPE: Default,
|
|
||||||
{
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new(|_| TYPE::default())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<TYPE, const ROWS: usize, const COLUMNS: usize> IntoIterator for Board<TYPE, ROWS, COLUMNS> {
|
|
||||||
type Item = (usize, usize, TYPE);
|
|
||||||
|
|
||||||
type IntoIter = BoardIterator<TYPE, ROWS, COLUMNS>;
|
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
|
||||||
BoardIterator {
|
|
||||||
it: self.data.into_iter().flatten().enumerate(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct BoardIteratorRef<'a, TYPE, const ROWS: usize, const COLUMNS: usize> {
|
|
||||||
it: Enumerate<Flatten<core::slice::Iter<'a, [TYPE; COLUMNS]>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, TYPE, const ROWS: usize, const COLUMNS: usize> Iterator
|
|
||||||
for BoardIteratorRef<'a, TYPE, ROWS, COLUMNS>
|
|
||||||
where
|
|
||||||
TYPE: Clone + Copy,
|
|
||||||
{
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
self.it
|
|
||||||
.next()
|
|
||||||
.map(|(index, value)| (index / COLUMNS, index % COLUMNS, *value))
|
|
||||||
}
|
|
||||||
type Item = (usize, usize, TYPE);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, TYPE, const ROWS: usize, const COLUMNS: usize> IntoIterator
|
|
||||||
for &'a Board<TYPE, ROWS, COLUMNS>
|
|
||||||
where
|
|
||||||
TYPE: Clone + Copy,
|
|
||||||
{
|
|
||||||
fn into_iter(self) -> BoardIteratorRef<'a, TYPE, ROWS, COLUMNS> {
|
|
||||||
BoardIteratorRef {
|
|
||||||
it: (self.data).iter().flatten().enumerate(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type IntoIter = BoardIteratorRef<'a, TYPE, ROWS, COLUMNS>;
|
|
||||||
type Item = (usize, usize, TYPE);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<TYPE, const ROWS: usize, const COLUMNS: usize> Board<TYPE, ROWS, COLUMNS> {
|
|
||||||
pub(crate) fn new<INITIALIZER>(mut cb: INITIALIZER) -> Self
|
|
||||||
where
|
|
||||||
INITIALIZER: FnMut((usize, usize)) -> TYPE,
|
|
||||||
{
|
|
||||||
let values: [[TYPE; COLUMNS]; ROWS] =
|
|
||||||
core::array::from_fn(|i| core::array::from_fn(|j| cb((i, j))));
|
|
||||||
Board { data: values }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn iter(&self) -> BoardIteratorRef<'_, TYPE, ROWS, COLUMNS> {
|
|
||||||
BoardIteratorRef {
|
|
||||||
it: (self.data).iter().flatten().enumerate(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct BoardIterator<TYPE, const ROWS: usize, const COLUMNS: usize> {
|
|
||||||
it: Enumerate<Flatten<core::array::IntoIter<[TYPE; COLUMNS], ROWS>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<TYPE, const ROWS: usize, const COLUMNS: usize> Iterator
|
|
||||||
for BoardIterator<TYPE, ROWS, COLUMNS>
|
|
||||||
{
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
self.it
|
|
||||||
.next()
|
|
||||||
.map(|(index, value)| (index / COLUMNS, index % COLUMNS, value))
|
|
||||||
}
|
|
||||||
type Item = (usize, usize, TYPE);
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<TYPE, const ROWS: usize, const COLUMNS: usize> Index<Position> for Board<TYPE, ROWS, COLUMNS> {
|
|
||||||
type Output = TYPE;
|
|
||||||
|
|
||||||
fn index(&self, position: Position) -> &Self::Output {
|
|
||||||
let index = position.to_index();
|
|
||||||
&self.data[index.1][index.0]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<TYPE, const ROWS: usize, const COLUMNS: usize> IndexMut<Position>
|
|
||||||
for Board<TYPE, ROWS, COLUMNS>
|
|
||||||
{
|
|
||||||
fn index_mut(&mut self, position: Position) -> &mut Self::Output {
|
|
||||||
let index = position.to_index();
|
|
||||||
&mut self.data[index.1][index.0]
|
|
||||||
}
|
|
||||||
}
|
|
3
rdraught/src/constants.rs
Normal file
3
rdraught/src/constants.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
pub const BITS_PER_POSITION: usize = 3;
|
||||||
|
pub const POSITIONS: usize = 32;
|
||||||
|
pub const POSITIONS_PER_ROW: usize = 4;
|
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,16 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
mod board;
|
#[macro_use]
|
||||||
pub mod draughts;
|
extern crate strum;
|
||||||
pub mod position;
|
|
||||||
|
|
||||||
//use draughts::DraughtsBoard;
|
mod constants;
|
||||||
//use draughts::Piece;
|
pub mod draughts;
|
||||||
|
mod movement;
|
||||||
|
mod piece;
|
||||||
|
mod player;
|
||||||
|
mod position;
|
||||||
|
|
||||||
|
pub use draughts::{DraughtsBoard, DraughtsGame, Error, RDraughtApplication, RectangularBoard};
|
||||||
|
pub use movement::Move;
|
||||||
|
pub use piece::Piece;
|
||||||
|
pub use player::Player;
|
||||||
|
pub use position::Position;
|
||||||
|
121
rdraught/src/movement.rs
Normal file
121
rdraught/src/movement.rs
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
use crate::position::Position;
|
||||||
|
|
||||||
|
use crate::{Piece, Player, draughts::Error};
|
||||||
|
use strum_macros::FromRepr;
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, FromRepr)]
|
||||||
|
pub enum MoveDirection {
|
||||||
|
NE = 0,
|
||||||
|
SE = 1,
|
||||||
|
SW = 2,
|
||||||
|
NW = 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MoveDirection {
|
||||||
|
pub fn is_forward(&self, piece: Piece) -> Result<bool, Error> {
|
||||||
|
match piece.player() {
|
||||||
|
Some(Player::Red) => Ok(matches!(self, Self::SE) || matches!(self, Self::SW)),
|
||||||
|
Some(Player::White) => Ok(matches!(self, Self::NE) || matches!(self, Self::NW)),
|
||||||
|
None => Err(Error::NoPiece),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub struct Move {
|
||||||
|
data: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
Move { data: result }
|
||||||
|
}
|
||||||
|
|
||||||
|
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 crowned_capture {
|
||||||
|
result |= 1u16 << 8;
|
||||||
|
}
|
||||||
|
Move { data: result }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn start_position(&self) -> Position {
|
||||||
|
Position::from((self.data & 31) as u8)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn direction(&self) -> MoveDirection {
|
||||||
|
MoveDirection::from_repr(((self.data & 96) >> 5) as usize).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_capture(&self) -> bool {
|
||||||
|
(self.data & 128) != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_movement(&self) -> bool {
|
||||||
|
!self.is_capture()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn crowned_captured(&self) -> bool {
|
||||||
|
(self.data & 256) != 0
|
||||||
|
}
|
||||||
|
pub fn get_end_position(&self) -> Position {
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
} 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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::{Move, MoveDirection};
|
||||||
|
use crate::Position;
|
||||||
|
|
||||||
|
struct MoveData {
|
||||||
|
start: Position,
|
||||||
|
dir: MoveDirection,
|
||||||
|
capture: bool,
|
||||||
|
crowned_capture: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_from_into() {
|
||||||
|
let move_datas = [MoveData {
|
||||||
|
start: Position::new(0, 0).unwrap(),
|
||||||
|
dir: super::MoveDirection::NE,
|
||||||
|
capture: false,
|
||||||
|
crowned_capture: false,
|
||||||
|
}];
|
||||||
|
|
||||||
|
for md in move_datas {
|
||||||
|
let mv = if md.capture {
|
||||||
|
Move::capture(md.start, md.dir, md.crowned_capture)
|
||||||
|
} else {
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
35
rdraught/src/piece.rs
Normal file
35
rdraught/src/piece.rs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
use crate::player::Player;
|
||||||
|
use strum_macros::FromRepr;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone, Copy, FromRepr)]
|
||||||
|
pub enum Piece {
|
||||||
|
NoPiece = 0,
|
||||||
|
SimpleRedPawn = 1,
|
||||||
|
CrownedRedPawn = 3,
|
||||||
|
SimpleWhitePawn = 2,
|
||||||
|
CrownedWhitePawn = 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Piece {
|
||||||
|
pub fn player(&self) -> Option<Player> {
|
||||||
|
if self.is_present() && (*self as u32) % 2 == 1 {
|
||||||
|
Some(Player::Red)
|
||||||
|
} else if self.is_present() && (*self as u32) % 2 == 0 {
|
||||||
|
Some(Player::White)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_present(&self) -> bool {
|
||||||
|
*self != Piece::NoPiece
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_crowned(&self) -> bool {
|
||||||
|
(*self as u32) > 2
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn can_move_backward(&self) -> bool {
|
||||||
|
self.is_crowned()
|
||||||
|
}
|
||||||
|
}
|
5
rdraught/src/player.rs
Normal file
5
rdraught/src/player.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
|
pub enum Player {
|
||||||
|
White = 0,
|
||||||
|
Red = 1,
|
||||||
|
}
|
@@ -1,9 +1,12 @@
|
|||||||
|
use crate::Error;
|
||||||
use core::fmt::Display;
|
use core::fmt::Display;
|
||||||
use core::ops::Add;
|
use core::ops::Add;
|
||||||
use core::ops::Div;
|
use core::ops::Div;
|
||||||
use core::ops::Mul;
|
use core::ops::Mul;
|
||||||
use core::ops::Sub;
|
use core::ops::Sub;
|
||||||
|
|
||||||
|
use crate::constants::POSITIONS_PER_ROW;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
pub struct Position((u8, u8));
|
pub struct Position((u8, u8));
|
||||||
|
|
||||||
@@ -22,8 +25,12 @@ impl Position {
|
|||||||
Position((index.0 as u8, index.1 as u8))
|
Position((index.0 as u8, index.1 as u8))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(row: u8, column: u8) -> Position {
|
pub fn new(row: u8, column: u8) -> Result<Position, Error> {
|
||||||
Position((row, column))
|
if (row + column) % 2 == 0 {
|
||||||
|
Ok(Position((row, column)))
|
||||||
|
} else {
|
||||||
|
Err(Error::InvalidPosition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn row(&self) -> u8 {
|
pub fn row(&self) -> u8 {
|
||||||
@@ -109,3 +116,46 @@ impl Mul<(i32, i32)> for Position {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Into<u8> for Position {
|
||||||
|
fn into(self) -> u8 {
|
||||||
|
(8 - 1 - self.row()) * (POSITIONS_PER_ROW as u8) + self.col() / 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<u8> for Position {
|
||||||
|
fn from(n: u8) -> Self {
|
||||||
|
// println!("{}", n);
|
||||||
|
let row = 8 - 1 - n / (POSITIONS_PER_ROW as u8);
|
||||||
|
let offset = if row % 2 == 0 { 0u8 } else { 1u8 };
|
||||||
|
let col = n % (POSITIONS_PER_ROW as u8) * 2 + offset;
|
||||||
|
Position::new(row, col).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::Position;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_from_into() {
|
||||||
|
let positions = [
|
||||||
|
Position::new(4, 6),
|
||||||
|
Position::new(0, 4),
|
||||||
|
Position::new(1, 3),
|
||||||
|
Position::new(7, 1),
|
||||||
|
Position::new(2, 2),
|
||||||
|
Position::new(5, 3),
|
||||||
|
Position::new(3, 7),
|
||||||
|
Position::new(6, 0),
|
||||||
|
Position::new(7, 7),
|
||||||
|
];
|
||||||
|
|
||||||
|
for p in positions {
|
||||||
|
let p = p.unwrap();
|
||||||
|
let n = <Position as Into<u8>>::into(p);
|
||||||
|
let p2 = <Position as From<u8>>::from(n);
|
||||||
|
assert_eq!(p, p2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user