diff --git a/Cargo.toml b/Cargo.toml index 31733ed..ba96a52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ rdraught = { path = "rdraught", version = "0.1.0" } cairo-rs = "0.20" cairo-sys-rs = "0.20" librsvg = "2.60" -gtk4 = "0.9" +gtk4 = {version = "0.9", features = ["v4_10"] } gdk4 = "0.9" gio = "0.20.12" glib = "0.20.12" diff --git a/rdraught-ui/Cargo.toml b/rdraught-ui/Cargo.toml index 8f4c043..8b317d5 100644 --- a/rdraught-ui/Cargo.toml +++ b/rdraught-ui/Cargo.toml @@ -17,3 +17,6 @@ cairo-rs.workspace = true gio.workspace = true glib.workspace = true +# [[bin]] +# name = "dialog_test" +# path = "src/greeting_dialog.rs" diff --git a/rdraught-ui/src/greeting_dialog.rs b/rdraught-ui/src/greeting_dialog.rs new file mode 100644 index 0000000..32788f7 --- /dev/null +++ b/rdraught-ui/src/greeting_dialog.rs @@ -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) -> 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 +} diff --git a/rdraught-ui/src/main.rs b/rdraught-ui/src/main.rs index 071ecc7..5451381 100644 --- a/rdraught-ui/src/main.rs +++ b/rdraught-ui/src/main.rs @@ -1,18 +1,22 @@ use gdk4::cairo::{Context as CairoContext, Matrix, Rectangle}; use gtk4::cairo::Error; +use gtk4::glib::Propagation; 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::position::Position; mod geo2d; use core::f64::consts::PI; use geo2d::Point; use rsvg::SvgHandle; -use std::cell::{Cell, RefCell}; -use std::rc::Rc; - +use std::thread; const SQUARE_SIZE: f64 = 1.0; +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_WHITE: &[u8] = include_bytes!("crown_white.svg"); @@ -186,11 +190,7 @@ fn draw_score_bar(cr: &CairoContext, board: &Rectangle, draughts_game: &Draughts cr.restore().unwrap(); } -fn on_activate(application: >k::Application) { - // Initialize GTK before using any GTK functions. - if gtk::init().is_err() { - panic!("Failed to initialize GTK."); - } +fn create_game_window(application: &Application, current_player: Player) { // Create a new window. let window = gtk::ApplicationWindow::builder() .application(application) @@ -200,13 +200,13 @@ fn on_activate(application: >k::Application) { .build(); // 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. - 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 selected_piece: Rc>> = Rc::new(Cell::new(None)); - let available_moves: Rc>> = Rc::new(RefCell::new(Vec::new())); + let draughts_game = new_shared_mut_ref(DraughtsGame::default()); + let selected_piece: SharedMutable> = new_shared_mut(None); + let available_moves = new_shared_mut_ref(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; @@ -238,8 +238,7 @@ fn on_activate(application: >k::Application) { ) .unwrap() }; - let current_player = Rc::::new(Player::Red); - let xform = Rc::>::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 // whenever GTK needs to redraw this widget (for example, on first display or when resized). { @@ -247,76 +246,107 @@ fn on_activate(application: >k::Application) { let xform = xform.clone(); let selected_piece = selected_piece.clone(); let available_moves = available_moves.clone(); - let current_player = current_player.clone(); - 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_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); + 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::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. - 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(); - cr.restore().unwrap(); + // 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::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. + 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(); + cr.restore().unwrap(); } - if let Some(selected_position) = selected_piece.get() { - let screen_position = match *current_player { - Player::White => { - Position::new(8 - 1 - selected_position.row(), selected_position.col()) - } - Player::Red => selected_position, + } + if let Some(selected_position) = selected_piece.get() { + 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.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 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, @@ -333,7 +363,7 @@ fn on_activate(application: >k::Application) { 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_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()); @@ -343,43 +373,9 @@ fn on_activate(application: >k::Application) { 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 => 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.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(); - } - } - draw_score_bar(cr, &board, &draughts_game.borrow()); - }); + } + draw_score_bar(cr, &board, &draughts_game.borrow()); + }); } let gesture = gtk::GestureClick::new(); @@ -402,7 +398,7 @@ fn on_activate(application: >k::Application) { if board_clone.contains(&p) { let p = &p - &board_clone.tl(); // println!("Point: {:?}", p); - let position = match *current_player { + let position = match current_player { Player::White => Position::new( (8.0 - (p.y() / SQUARE_SIZE)) as u8, (p.x() / SQUARE_SIZE) as u8, @@ -411,7 +407,7 @@ fn on_activate(application: >k::Application) { 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 piece = draughts_game.piece_at(position); // println!("Selected piece: {:?}", piece); @@ -421,9 +417,15 @@ fn on_activate(application: >k::Application) { for mv in am.iter() { if mv.get_end_position() == position { draughts_game.apply_move(mv).unwrap(); - // if let Some(mv) = draughts_game.get_best_move(10) { - // println!("Next best move: {:?}", mv); - // } + 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; break; } @@ -452,17 +454,33 @@ fn on_activate(application: >k::Application) { } else { selected_piece.set(None); } - drawing_area.borrow_mut().queue_draw(); + drawing_area.queue_draw(); }); } // Assign the gesture to the treeview - drawing_area.borrow_mut().add_controller(gesture); + drawing_area.add_controller(gesture); 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() { // Create a new application with the builder pattern - let app = gtk::Application::builder() + let app = Application::builder() .application_id("net.woggioni.rdraught") .build(); app.connect_activate(on_activate); diff --git a/rdraught-ui/src/types.rs b/rdraught-ui/src/types.rs new file mode 100644 index 0000000..a1d5208 --- /dev/null +++ b/rdraught-ui/src/types.rs @@ -0,0 +1,12 @@ +use std::cell::{Cell, RefCell}; +use std::rc::Rc; + +pub(crate) type SharedMutable = Rc>; +pub(crate) type SharedMutableRef = Rc>; +pub(crate) fn new_shared_mut_ref(obj: T) -> SharedMutableRef { + Rc::new(RefCell::new(obj)) +} + +pub(crate) fn new_shared_mut(obj: T) -> SharedMutable { + Rc::new(Cell::new(obj)) +} diff --git a/rdraught-w4/Cargo.toml b/rdraught-w4/Cargo.toml new file mode 100644 index 0000000..eb7040a --- /dev/null +++ b/rdraught-w4/Cargo.toml @@ -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 = [] diff --git a/rdraught-w4/src/main.rs b/rdraught-w4/src/main.rs new file mode 100644 index 0000000..25e723a --- /dev/null +++ b/rdraught-w4/src/main.rs @@ -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); +// } +// } diff --git a/rdraught/src/draughts.rs b/rdraught/src/draughts.rs index a45134b..94c1455 100644 --- a/rdraught/src/draughts.rs +++ b/rdraught/src/draughts.rs @@ -416,7 +416,13 @@ impl DraughtsGame { subject as f32 / (red + white) as f32 } - fn move_score(mut self, mv: &Move, player: Player, depth: u32) -> 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(); @@ -424,8 +430,9 @@ impl DraughtsGame { 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); + 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 { @@ -457,15 +464,21 @@ impl DraughtsGame { .flat_map(|pos| self.moves_for_piece(pos)) } - pub fn get_best_move(&self, max_depth: u32) -> Option { + pub fn get_best_move(&self, max_depth: u32) -> (Option, usize) { + let mut analyzed_moves = 0usize; let mut result: Option<(Move, f32)> = None; let available_moves = self.available_moves().count(); - match available_moves { + 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); + 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 { @@ -476,7 +489,8 @@ impl DraughtsGame { } result.map(|(mv, _)| mv) } - } + }; + (best_move, analyzed_moves) } } #[derive(Debug, PartialEq, Eq)]