From 5d3e20cf376cd5394b635e28589eeb46dd04226b Mon Sep 17 00:00:00 2001 From: andy Date: Fri, 9 Jul 2021 22:29:09 +0100 Subject: [PATCH] added status text --- src/game/mod.rs | 4 +++- www/index.html | 8 +++++++- www/index.js | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/game/mod.rs b/src/game/mod.rs index 61e4fbb..7abcf10 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -57,7 +57,7 @@ impl Game { } /// Attempt to make a move given a source and destination index - pub fn make_move(&mut self, from: BrdIdx, to: BrdIdx) { + pub fn make_move(&mut self, from: BrdIdx, to: BrdIdx) -> Moveable { let able = self.current.can_move(from, to); if let Moveable::Allowed = able { @@ -76,6 +76,8 @@ impl Game { // board has been changed, update player turn self.current.current_turn = self.current.current_turn.opponent(); + + able } /// Update board state with given move and push new board into current state diff --git a/www/index.html b/www/index.html index 6a09245..27eba6e 100644 --- a/www/index.html +++ b/www/index.html @@ -39,11 +39,17 @@

Working on an implementation of checkers in Rust WASM with a thin Js frontend, mainly as an exercise to learn Rust and to have a larger project in the language to fiddle with. The idea is to use the minimax algorithm to create an AI player that can operate with reasonable performance as a result of Rust's compiled performance.

-
+ + +
+
+ +
+
diff --git a/www/index.js b/www/index.js index 852fc01..be87e49 100644 --- a/www/index.js +++ b/www/index.js @@ -1,4 +1,4 @@ -import { Game, Board, BrdIdx, Painter, Team, init_wasm } from "draught"; +import { Game, Board, BrdIdx, Painter, Team, init_wasm, Moveable } from "draught"; import { memory } from "draught/draught_bg.wasm"; /////////////////// @@ -29,6 +29,8 @@ init_wasm(); // let board = new Board(BOARD_WIDTH, BOARD_HEIGHT, Team.Black); +const statusText = document.getElementById("status-p"); + let current_state = GameState.HUMAN_TURN.THINKING; let painter = new Painter(CANVAS_WIDTH, CANVAS_HEIGHT, "game-canvas"); // painter.draw(board); @@ -81,9 +83,43 @@ function process_canvas_click(cell_coord) { console.log("Your turn, first piece already picked, picking second"); to = cell_coord; - game.make_move(from, to); - game.draw(); + if(from.col == to.col && from.row == to.row){ + setStatusText("Move Cancelled"); + } else { + + let status = game.make_move(from, to); + + if (status == Moveable.Allowed) { + // game.draw(); + } else { + switch(status) { + case Moveable.Allowed: + break; + case Moveable.IllegalTrajectory: + break; + case Moveable.JumpingSameTeam: + break; + case Moveable.NoJumpablePiece: + break; + case Moveable.OccupiedDest: + break; + case Moveable.OutOfBounds: + break; + case Moveable.UnoccupiedSrc: + break; + case Moveable.Unplayable: + break; + case Moveable.WrongTeamSrc: + break; + } + } + + } + + game.draw(); + from = null; + to = null; current_state = GameState.HUMAN_TURN.THINKING; break; @@ -101,3 +137,8 @@ function getMousePos(canvas, evt) { }; } +function setStatusText(txt) { + statusText.hidden = false; + statusText.innerText = txt; +} +