Compare commits
6 Commits
c11ab13f42
...
f4168c449b
Author | SHA1 | Date | |
---|---|---|---|
f4168c449b
|
|||
8e852759ba
|
|||
2483de7608
|
|||
00c9787c17
|
|||
01799ea4a0
|
|||
d326c615cd
|
@@ -1,5 +1,5 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = ["rdraught", "rdraught-cli", "rdraught-pi", "rdraught-ui"]
|
members = ["rdraught", "rdraught-cli", "rdraught-pi", "rdraught-ui", "rdraught-w4"]
|
||||||
resolver = "3"
|
resolver = "3"
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
@@ -16,10 +16,12 @@ 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"
|
||||||
|
|
||||||
#[patch.crates-io]
|
#[patch.crates-io]
|
||||||
#cairo-rs = { git = "https://github.com/gtk-rs/gtk-rs-core.git", package = "cairo-rs", tag="0.20.12" }
|
#cairo-rs = { git = "https://github.com/gtk-rs/gtk-rs-core.git", package = "cairo-rs", tag="0.20.12" }
|
||||||
|
@@ -17,3 +17,6 @@ cairo-rs.workspace = true
|
|||||||
gio.workspace = true
|
gio.workspace = true
|
||||||
glib.workspace = true
|
glib.workspace = true
|
||||||
|
|
||||||
|
# [[bin]]
|
||||||
|
# name = "dialog_test"
|
||||||
|
# path = "src/greeting_dialog.rs"
|
||||||
|
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::draughts::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
|
||||||
|
}
|
@@ -1,18 +1,23 @@
|
|||||||
use gdk4::cairo::{Context as CairoContext, Matrix, Rectangle};
|
use gdk4::cairo::{Context as CairoContext, Matrix, Rectangle};
|
||||||
use gtk4::cairo::Error;
|
use gtk4::cairo::Error;
|
||||||
|
use gtk4::glib::{MainContext, Propagation};
|
||||||
use gtk4::{self as gtk, gdk::ffi::GDK_BUTTON_PRIMARY};
|
use gtk4::{self as gtk, gdk::ffi::GDK_BUTTON_PRIMARY};
|
||||||
use gtk4::{DrawingArea, prelude::*};
|
use gtk4::{Application, DrawingArea, prelude::*};
|
||||||
use rdraught::draughts::{DraughtsBoard, DraughtsGame, Move, Piece, Player};
|
use rdraught::draughts::{DraughtsBoard, DraughtsGame, Move, Piece, Player};
|
||||||
use rdraught::position::Position;
|
use rdraught::position::Position;
|
||||||
mod geo2d;
|
mod geo2d;
|
||||||
use core::f64::consts::PI;
|
use core::f64::consts::PI;
|
||||||
use geo2d::Point;
|
use geo2d::Point;
|
||||||
use rsvg::SvgHandle;
|
use rsvg::SvgHandle;
|
||||||
use std::cell::{Cell, RefCell};
|
use std::thread;
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
const SQUARE_SIZE: f64 = 1.0;
|
const SQUARE_SIZE: f64 = 1.0;
|
||||||
|
|
||||||
|
mod final_dialog;
|
||||||
|
mod greeting_dialog;
|
||||||
|
mod types;
|
||||||
|
|
||||||
|
use types::{SharedMutable, SharedMutableRef, new_shared_mut, new_shared_mut_ref};
|
||||||
|
|
||||||
const CROWN_RED: &[u8] = include_bytes!("crown_red.svg");
|
const CROWN_RED: &[u8] = include_bytes!("crown_red.svg");
|
||||||
const CROWN_WHITE: &[u8] = include_bytes!("crown_white.svg");
|
const CROWN_WHITE: &[u8] = include_bytes!("crown_white.svg");
|
||||||
|
|
||||||
@@ -67,6 +72,7 @@ fn draw_piece(
|
|||||||
if let Piece::NoPiece = piece {
|
if let Piece::NoPiece = piece {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
|
cr.save()?;
|
||||||
let center = square.center();
|
let center = square.center();
|
||||||
let outer_radius = square.width() * 0.3;
|
let outer_radius = square.width() * 0.3;
|
||||||
let vertical_scale_factor = 0.8;
|
let vertical_scale_factor = 0.8;
|
||||||
@@ -79,8 +85,7 @@ fn draw_piece(
|
|||||||
m3.translate(0.0, center.y() - outer_radius * vertical_scale_factor);
|
m3.translate(0.0, center.y() - outer_radius * vertical_scale_factor);
|
||||||
Matrix::multiply(&Matrix::multiply(&m1, &m2), &m3)
|
Matrix::multiply(&Matrix::multiply(&m1, &m2), &m3)
|
||||||
};
|
};
|
||||||
cr.save()?;
|
cr.set_matrix(Matrix::multiply(&matrix, &cr.matrix()));
|
||||||
cr.set_matrix(matrix);
|
|
||||||
cr.set_source_rgb(0.0, 0.0, 0.0);
|
cr.set_source_rgb(0.0, 0.0, 0.0);
|
||||||
let thickness = outer_radius * 0.3;
|
let thickness = outer_radius * 0.3;
|
||||||
cr.arc(
|
cr.arc(
|
||||||
@@ -121,6 +126,7 @@ fn draw_piece(
|
|||||||
2.0 * PI,
|
2.0 * PI,
|
||||||
);
|
);
|
||||||
cr.fill()?;
|
cr.fill()?;
|
||||||
|
cr.restore()?;
|
||||||
if crowned {
|
if crowned {
|
||||||
let renderer = match piece.player() {
|
let renderer = match piece.player() {
|
||||||
Some(Player::Red) => rsvg::CairoRenderer::new(crown_red),
|
Some(Player::Red) => rsvg::CairoRenderer::new(crown_red),
|
||||||
@@ -136,7 +142,10 @@ fn draw_piece(
|
|||||||
m3.translate(center.x(), center.y() - thickness / 1.0);
|
m3.translate(center.x(), center.y() - thickness / 1.0);
|
||||||
Matrix::multiply(&Matrix::multiply(&m1, &m2), &m3)
|
Matrix::multiply(&Matrix::multiply(&m1, &m2), &m3)
|
||||||
};
|
};
|
||||||
cr.set_matrix(Matrix::multiply(&matrix, &m4));
|
cr.set_matrix(Matrix::multiply(
|
||||||
|
&Matrix::multiply(&matrix, &m4),
|
||||||
|
&cr.matrix(),
|
||||||
|
));
|
||||||
renderer
|
renderer
|
||||||
.render_document(
|
.render_document(
|
||||||
cr,
|
cr,
|
||||||
@@ -149,16 +158,55 @@ fn draw_piece(
|
|||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
cr.restore()?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_activate(application: >k::Application) {
|
fn draw_score_bar(
|
||||||
// Initialize GTK before using any GTK functions.
|
cr: &CairoContext,
|
||||||
if gtk::init().is_err() {
|
board: &Rectangle,
|
||||||
panic!("Failed to initialize GTK.");
|
draughts_game: &DraughtsGame,
|
||||||
|
current_player: Player,
|
||||||
|
) {
|
||||||
|
fn modulate_score(relative_score: f64) -> f64 {
|
||||||
|
let x = relative_score;
|
||||||
|
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 score_percentage = modulate_score(draughts_game.relative_score(current_player) as f64);
|
||||||
|
let tl = score_bar.tl();
|
||||||
|
cr.save().unwrap();
|
||||||
|
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(
|
||||||
|
score_bar.tl().x(),
|
||||||
|
score_bar.tl().y(),
|
||||||
|
score_bar.width(),
|
||||||
|
score_bar.height() * (1.0 - score_percentage),
|
||||||
|
);
|
||||||
|
cr.fill().unwrap();
|
||||||
|
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() + score_bar.height() * (1.0 - score_percentage),
|
||||||
|
score_bar.width(),
|
||||||
|
score_bar.height() * score_percentage,
|
||||||
|
);
|
||||||
|
cr.fill().unwrap();
|
||||||
|
cr.restore().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_game_window(application: &Application, current_player: Player) {
|
||||||
// Create a new window.
|
// Create a new window.
|
||||||
let window = gtk::ApplicationWindow::builder()
|
let window = gtk::ApplicationWindow::builder()
|
||||||
.application(application)
|
.application(application)
|
||||||
@@ -168,17 +216,21 @@ fn on_activate(application: >k::Application) {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Create a DrawingArea widget where we will draw the chessboard.
|
// Create a DrawingArea widget where we will draw the chessboard.
|
||||||
let drawing_area = Rc::new(RefCell::new(gtk::DrawingArea::new()));
|
let drawing_area = DrawingArea::new();
|
||||||
// Add the drawing area to the window.
|
// Add the drawing area to the window.
|
||||||
window.set_child(Some(drawing_area.borrow().as_ref() as &DrawingArea));
|
window.set_child(Some(&drawing_area));
|
||||||
|
|
||||||
let draughts_game = Rc::new(RefCell::new(DraughtsGame::default()));
|
let draughts_game = new_shared_mut_ref(DraughtsGame::default());
|
||||||
let selected_piece: Rc<Cell<Option<Position>>> = Rc::new(Cell::new(None));
|
let selected_piece: SharedMutable<Option<Position>> = new_shared_mut(None);
|
||||||
let available_moves: Rc<RefCell<Vec<Move>>> = Rc::new(RefCell::new(Vec::new()));
|
let available_moves = new_shared_mut_ref(Vec::<Move>::new());
|
||||||
// Get the allocation information for the widget.
|
// Get the allocation information for the widget.
|
||||||
let board_width = SQUARE_SIZE * DraughtsBoard::rows() as f64;
|
let board_width = SQUARE_SIZE * DraughtsBoard::rows() as f64;
|
||||||
let board_height = SQUARE_SIZE * DraughtsBoard::columns() 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 = 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 board_clone = board;
|
||||||
let crown_red_handle = {
|
let crown_red_handle = {
|
||||||
let stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from_static(CROWN_RED));
|
let stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from_static(CROWN_RED));
|
||||||
@@ -202,46 +254,25 @@ fn on_activate(application: >k::Application) {
|
|||||||
)
|
)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
};
|
};
|
||||||
let xform = Rc::<RefCell<Matrix>>::new(RefCell::new(Matrix::identity()));
|
let xform = new_shared_mut_ref(Matrix::identity());
|
||||||
// Set the "draw" function of the drawing area. This callback is called
|
// 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).
|
// whenever GTK needs to redraw this widget (for example, on first display or when resized).
|
||||||
{
|
{
|
||||||
let draughts_game = draughts_game.clone();
|
let draughts_game = draughts_game.clone();
|
||||||
let xform = xform.clone();
|
let xform = xform.clone();
|
||||||
let selected_piece = selected_piece.clone();
|
let selected_piece = selected_piece.clone();
|
||||||
let board_clone = board;
|
|
||||||
let available_moves = available_moves.clone();
|
let available_moves = available_moves.clone();
|
||||||
let get_square_for_position = move |position: &Position, xform: &Matrix| -> Rectangle {
|
drawing_area.set_draw_func(move |_widget, cr, width, height| {
|
||||||
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(
|
let screen = Rectangle::from_points(
|
||||||
Point::new(0.0, 0.0),
|
Point::new(0.0, 0.0),
|
||||||
Point::new(width as f64, height as f64),
|
Point::new(width as f64, height as f64),
|
||||||
);
|
);
|
||||||
let f = f64::min(
|
let f = f64::min(
|
||||||
screen.width() / board.width(),
|
screen.width() / board_with_bar.width(),
|
||||||
screen.height() / board.height(),
|
screen.height() / board_with_bar.height(),
|
||||||
);
|
);
|
||||||
let screen_center = screen.center();
|
let screen_center = screen.center();
|
||||||
let board_center = board.center();
|
let board_center = board_with_bar.center();
|
||||||
let mut xform = xform.borrow_mut();
|
let mut xform = xform.borrow_mut();
|
||||||
*xform = Matrix::multiply(
|
*xform = Matrix::multiply(
|
||||||
&Matrix::multiply(
|
&Matrix::multiply(
|
||||||
@@ -250,12 +281,22 @@ fn on_activate(application: >k::Application) {
|
|||||||
),
|
),
|
||||||
&Matrix::new(1.0, 0.0, 0.0, 1.0, screen_center.x(), screen_center.y()),
|
&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.
|
// Loop over rows and columns to draw each chessboard cell.
|
||||||
for row in 0..DraughtsBoard::rows() {
|
for row in 0..DraughtsBoard::rows() {
|
||||||
for col in 0..DraughtsBoard::columns() {
|
for col in 0..DraughtsBoard::columns() {
|
||||||
let position = Position::new((8 - row - 1) as u8, col as u8);
|
let position = match current_player {
|
||||||
let square = get_square_for_position(&position, &xform);
|
Player::White => Position::new((8 - row - 1) as u8, col as u8),
|
||||||
|
Player::Red => Position::new(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.
|
// Alternate colors based on the sum of row and column indices.
|
||||||
if (row + col) % 2 == 0 {
|
if (row + col) % 2 == 0 {
|
||||||
cr.set_source_rgb(0.8, 0.8, 0.6);
|
cr.set_source_rgb(0.8, 0.8, 0.6);
|
||||||
@@ -279,10 +320,22 @@ fn on_activate(application: >k::Application) {
|
|||||||
&crown_white_handle,
|
&crown_white_handle,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
cr.restore().unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(selected_postion) = selected_piece.get() {
|
if let Some(selected_position) = selected_piece.get() {
|
||||||
let square = get_square_for_position(&selected_postion, &xform);
|
let screen_position = match current_player {
|
||||||
|
Player::White => {
|
||||||
|
Position::new(8 - 1 - selected_position.row(), selected_position.col())
|
||||||
|
}
|
||||||
|
Player::Red => selected_position,
|
||||||
|
};
|
||||||
|
let square = Rectangle::new(
|
||||||
|
screen_position.col() as f64 * SQUARE_SIZE,
|
||||||
|
screen_position.row() as f64 * SQUARE_SIZE,
|
||||||
|
SQUARE_SIZE,
|
||||||
|
SQUARE_SIZE,
|
||||||
|
);
|
||||||
cr.save().unwrap();
|
cr.save().unwrap();
|
||||||
cr.new_path();
|
cr.new_path();
|
||||||
cr.move_to(square.tl().x(), square.tl().y());
|
cr.move_to(square.tl().x(), square.tl().y());
|
||||||
@@ -306,7 +359,17 @@ fn on_activate(application: >k::Application) {
|
|||||||
let am = available_moves.borrow();
|
let am = available_moves.borrow();
|
||||||
if !am.is_empty() {
|
if !am.is_empty() {
|
||||||
for mv in am.iter() {
|
for mv in am.iter() {
|
||||||
let square = get_square_for_position(&mv.get_end_position(), &xform);
|
let end_pos = mv.get_end_position();
|
||||||
|
let screen_position = match current_player {
|
||||||
|
Player::White => Position::new(8 - 1 - end_pos.row(), end_pos.col()),
|
||||||
|
Player::Red => end_pos,
|
||||||
|
};
|
||||||
|
let square = Rectangle::new(
|
||||||
|
screen_position.col() as f64 * SQUARE_SIZE,
|
||||||
|
screen_position.row() as f64 * SQUARE_SIZE,
|
||||||
|
SQUARE_SIZE,
|
||||||
|
SQUARE_SIZE,
|
||||||
|
);
|
||||||
cr.save().unwrap();
|
cr.save().unwrap();
|
||||||
cr.new_path();
|
cr.new_path();
|
||||||
cr.move_to(square.tl().x(), square.tl().y());
|
cr.move_to(square.tl().x(), square.tl().y());
|
||||||
@@ -327,6 +390,7 @@ fn on_activate(application: >k::Application) {
|
|||||||
cr.restore().unwrap();
|
cr.restore().unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
draw_score_bar(cr, &board, &draughts_game.borrow(), current_player);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
let gesture = gtk::GestureClick::new();
|
let gesture = gtk::GestureClick::new();
|
||||||
@@ -338,8 +402,13 @@ fn on_activate(application: >k::Application) {
|
|||||||
{
|
{
|
||||||
let drawing_area = drawing_area.clone();
|
let drawing_area = drawing_area.clone();
|
||||||
let available_moves = available_moves.clone();
|
let available_moves = available_moves.clone();
|
||||||
|
let window = window.clone();
|
||||||
gesture.connect_pressed(move |gesture, _, x, y| {
|
gesture.connect_pressed(move |gesture, _, x, y| {
|
||||||
gesture.set_state(gtk::EventSequenceState::Claimed);
|
gesture.set_state(gtk::EventSequenceState::Claimed);
|
||||||
|
if let Some(winner) = draughts_game.borrow().winner() {
|
||||||
|
MainContext::default()
|
||||||
|
.spawn_local(final_dialog::create_dialog(window.clone(), winner));
|
||||||
|
} else {
|
||||||
let xform = xform.borrow();
|
let xform = xform.borrow();
|
||||||
let inverse = {
|
let inverse = {
|
||||||
let mut m = *xform;
|
let mut m = *xform;
|
||||||
@@ -350,19 +419,34 @@ fn on_activate(application: >k::Application) {
|
|||||||
if board_clone.contains(&p) {
|
if board_clone.contains(&p) {
|
||||||
let p = &p - &board_clone.tl();
|
let p = &p - &board_clone.tl();
|
||||||
// println!("Point: {:?}", p);
|
// println!("Point: {:?}", p);
|
||||||
let position = Position::new(
|
let position = match current_player {
|
||||||
8 - 1 - (p.y() / SQUARE_SIZE) as u8,
|
Player::White => Position::new(
|
||||||
|
(8.0 - (p.y() / SQUARE_SIZE)) as u8,
|
||||||
(p.x() / SQUARE_SIZE) as u8,
|
(p.x() / SQUARE_SIZE) as u8,
|
||||||
);
|
),
|
||||||
|
Player::Red => {
|
||||||
|
Position::new((p.y() / SQUARE_SIZE) as u8, (p.x() / SQUARE_SIZE) as u8)
|
||||||
|
}
|
||||||
|
};
|
||||||
// println!("Selected position: {:?}", position);
|
// println!("Selected position: {:?}", position);
|
||||||
let mut draughts_game = draughts_game.borrow_mut();
|
let mut draughts_game = draughts_game.borrow_mut();
|
||||||
let piece = draughts_game.piece_at(position);
|
let piece = draughts_game.piece_at(position);
|
||||||
|
// println!("Selected piece: {:?}", piece);
|
||||||
let mut am = available_moves.borrow_mut();
|
let mut am = available_moves.borrow_mut();
|
||||||
let mut move_applied = false;
|
let mut move_applied = false;
|
||||||
if !am.is_empty() {
|
if !am.is_empty() {
|
||||||
for mv in am.iter() {
|
for mv in am.iter() {
|
||||||
if mv.get_end_position() == position {
|
if mv.get_end_position() == position {
|
||||||
draughts_game.apply_move(mv).unwrap();
|
draughts_game.apply_move(mv).unwrap();
|
||||||
|
let game_copy = draughts_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;
|
move_applied = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -391,17 +475,34 @@ fn on_activate(application: >k::Application) {
|
|||||||
} else {
|
} else {
|
||||||
selected_piece.set(None);
|
selected_piece.set(None);
|
||||||
}
|
}
|
||||||
drawing_area.borrow_mut().queue_draw();
|
drawing_area.queue_draw();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Assign the gesture to the treeview
|
// Assign the gesture to the treeview
|
||||||
drawing_area.borrow_mut().add_controller(gesture);
|
drawing_area.add_controller(gesture);
|
||||||
window.present();
|
window.present();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn on_activate(application: &Application) {
|
||||||
|
// 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);
|
||||||
|
create_game_window(&application, current_player.get());
|
||||||
|
Propagation::Proceed
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Create a new application with the builder pattern
|
// Create a new application with the builder pattern
|
||||||
let app = gtk::Application::builder()
|
let app = Application::builder()
|
||||||
.application_id("net.woggioni.rdraught")
|
.application_id("net.woggioni.rdraught")
|
||||||
.build();
|
.build();
|
||||||
app.connect_activate(on_activate);
|
app.connect_activate(on_activate);
|
||||||
|
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);
|
||||||
|
// }
|
||||||
|
// }
|
@@ -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,9 +1,9 @@
|
|||||||
use super::board::Board;
|
use super::board::Board;
|
||||||
use super::board::BoardIteratorRef;
|
|
||||||
use super::board::RectangularBoard;
|
use super::board::RectangularBoard;
|
||||||
use core::iter::{Filter, Map};
|
|
||||||
use core::ops::Index;
|
use core::ops::Index;
|
||||||
use core::ops::IndexMut;
|
use core::ops::IndexMut;
|
||||||
|
use core::result;
|
||||||
|
use heapless::BinaryHeap;
|
||||||
use heapless::Vec;
|
use heapless::Vec;
|
||||||
|
|
||||||
use super::position::Position;
|
use super::position::Position;
|
||||||
@@ -105,17 +105,14 @@ impl DraughtsBoard {
|
|||||||
Board::<Piece, 8, 8>::columns()
|
Board::<Piece, 8, 8>::columns()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pieces<'a, const PIECES: usize>(
|
pub fn pieces<const PIECES: usize>(
|
||||||
&'a self,
|
&self,
|
||||||
pieces: &'a Vec<Piece, PIECES>,
|
pieces: Vec<Piece, PIECES>,
|
||||||
) -> Map<
|
) -> impl Iterator<Item = Position> {
|
||||||
Filter<BoardIteratorRef<'a, Piece, 8, 8>, impl FnMut(&(usize, usize, Piece)) -> bool>,
|
|
||||||
impl FnMut((usize, usize, Piece)) -> Position,
|
|
||||||
> {
|
|
||||||
self.0
|
self.0
|
||||||
.iter()
|
.iter()
|
||||||
.filter(move |(_, _, piece)| {
|
.filter(move |(_, _, piece)| {
|
||||||
for p in pieces {
|
for p in &pieces {
|
||||||
if piece == p {
|
if piece == p {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -268,6 +265,9 @@ impl Default for DraughtsGame {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MoveRanking<const MAX_SIZE: usize> =
|
||||||
|
BinaryHeap<MoveHeapEntry, heapless::binary_heap::Max, MAX_SIZE>;
|
||||||
|
|
||||||
impl DraughtsGame {
|
impl DraughtsGame {
|
||||||
pub fn new() -> DraughtsGame {
|
pub fn new() -> DraughtsGame {
|
||||||
DraughtsGame::default()
|
DraughtsGame::default()
|
||||||
@@ -294,7 +294,7 @@ impl DraughtsGame {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut capture_available = false;
|
let mut capture_available = false;
|
||||||
for pos in self.board.pieces(&needle) {
|
for pos in self.board.pieces(needle) {
|
||||||
for _ in self.board.moves_for_piece(pos, true) {
|
for _ in self.board.moves_for_piece(pos, true) {
|
||||||
capture_available = true;
|
capture_available = true;
|
||||||
}
|
}
|
||||||
@@ -371,6 +371,155 @@ impl DraughtsGame {
|
|||||||
Err(Error::NoPiece)
|
Err(Error::NoPiece)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn score_for_player(&self, p: Player) -> u32 {
|
||||||
|
let md = MoveDirection::NE;
|
||||||
|
let mut score = 0u32;
|
||||||
|
let pieces = {
|
||||||
|
let mut v = Vec::<Piece, 2>::new();
|
||||||
|
match p {
|
||||||
|
Player::White => {
|
||||||
|
v.push(Piece::SimpleWhitePawn).unwrap();
|
||||||
|
v.push(Piece::CrownedWhitePawn).unwrap();
|
||||||
|
v
|
||||||
|
}
|
||||||
|
Player::Red => {
|
||||||
|
v.push(Piece::SimpleRedPawn).unwrap();
|
||||||
|
v.push(Piece::CrownedRedPawn).unwrap();
|
||||||
|
v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
for pos in self.board.pieces(pieces) {
|
||||||
|
let piece = self.board.get_piece(&pos);
|
||||||
|
if piece.is_crowned() {
|
||||||
|
score += 20;
|
||||||
|
} else {
|
||||||
|
score += 10;
|
||||||
|
if md.is_forward(piece).unwrap() {
|
||||||
|
score += pos.row() as u32;
|
||||||
|
} else {
|
||||||
|
score += DraughtsBoard::rows() as u32 - 1 - pos.row() as u32;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
score
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn relative_score(&self, p: Player) -> f32 {
|
||||||
|
let red = self.score_for_player(Player::Red);
|
||||||
|
let white = self.score_for_player(Player::White);
|
||||||
|
let subject = match p {
|
||||||
|
Player::White => white,
|
||||||
|
Player::Red => red,
|
||||||
|
};
|
||||||
|
subject as f32 / (red + white) as f32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_score(
|
||||||
|
mut self,
|
||||||
|
mv: &Move,
|
||||||
|
player: Player,
|
||||||
|
depth: u32,
|
||||||
|
analyzed_moves: &mut usize,
|
||||||
|
) -> f32 {
|
||||||
|
let pos = mv.get_start_position();
|
||||||
|
let piece = self.board.get_piece(&pos);
|
||||||
|
let moving_player = piece.player().unwrap();
|
||||||
|
self.apply_move(mv).unwrap();
|
||||||
|
if depth != 0 {
|
||||||
|
let mut best_score = None;
|
||||||
|
for mv in self.available_moves() {
|
||||||
|
*analyzed_moves += 1usize;
|
||||||
|
let clone = self.clone();
|
||||||
|
let score = clone.move_score(&mv, player, depth - 1, analyzed_moves);
|
||||||
|
if best_score.is_none() {
|
||||||
|
best_score = Some(score);
|
||||||
|
} else if let Some(bs) = best_score {
|
||||||
|
if bs < score {
|
||||||
|
best_score = Some(bs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
best_score.unwrap_or_else(|| self.relative_score(player))
|
||||||
|
} else {
|
||||||
|
self.relative_score(player)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn available_moves(&self) -> impl Iterator<Item = Move> {
|
||||||
|
let mut pieces = Vec::<Piece, 2>::new();
|
||||||
|
match self.next_move {
|
||||||
|
Player::White => {
|
||||||
|
pieces.push(Piece::SimpleWhitePawn).unwrap();
|
||||||
|
pieces.push(Piece::CrownedWhitePawn).unwrap();
|
||||||
|
}
|
||||||
|
Player::Red => {
|
||||||
|
pieces.push(Piece::SimpleRedPawn).unwrap();
|
||||||
|
pieces.push(Piece::CrownedRedPawn).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.board
|
||||||
|
.pieces(pieces)
|
||||||
|
.flat_map(|pos| self.moves_for_piece(pos))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_best_move(&self, max_depth: u32) -> (Option<Move>, usize) {
|
||||||
|
let mut analyzed_moves = 0usize;
|
||||||
|
let mut result: Option<(Move, f32)> = None;
|
||||||
|
let available_moves = self.available_moves().count();
|
||||||
|
let best_move = match available_moves {
|
||||||
|
0 => None,
|
||||||
|
1 => self.available_moves().next(),
|
||||||
|
_ => {
|
||||||
|
for mv in self.available_moves() {
|
||||||
|
let score = self.clone().move_score(
|
||||||
|
&mv,
|
||||||
|
self.next_move,
|
||||||
|
max_depth - 1,
|
||||||
|
&mut analyzed_moves,
|
||||||
|
);
|
||||||
|
if result.is_none() {
|
||||||
|
result = Some((mv, score));
|
||||||
|
} else if let Some((_, best_score)) = result {
|
||||||
|
if score > best_score {
|
||||||
|
result = Some((mv, score));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.map(|(mv, _)| mv)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
(best_move, analyzed_moves)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn winner(&self) -> Option<Player> {
|
||||||
|
if self.available_moves().next().is_none() {
|
||||||
|
Some(match self.next_move {
|
||||||
|
Player::White => Player::Red,
|
||||||
|
Player::Red => Player::White,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
struct MoveHeapEntry {
|
||||||
|
mv: Move,
|
||||||
|
score: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialOrd for MoveHeapEntry {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
|
||||||
|
Some(self.score.cmp(&other.score))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ord for MoveHeapEntry {
|
||||||
|
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
|
||||||
|
self.score.cmp(&other.score)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||||
|
Reference in New Issue
Block a user