From cd9ee4859f994087056eaf7247ebbbc6b7a81f56 Mon Sep 17 00:00:00 2001 From: andy Date: Sat, 10 Jul 2021 19:10:48 +0100 Subject: [PATCH] added eq BrdIdx method, changing to array of clicks --- src/board/mod.rs | 4 +++ src/game/mod.rs | 4 +++ www/index.js | 81 ++++++++++++++++++++++++++++++------------------ 3 files changed, 59 insertions(+), 30 deletions(-) diff --git a/src/board/mod.rs b/src/board/mod.rs index 80becce..62c9d71 100644 --- a/src/board/mod.rs +++ b/src/board/mod.rs @@ -133,6 +133,10 @@ impl BrdIdx { row, col } } + + pub fn eq (&self, other: &BrdIdx) -> bool { + *self == *other + } } impl Display for BrdIdx { diff --git a/src/game/mod.rs b/src/game/mod.rs index 7abcf10..c52145e 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -56,6 +56,10 @@ impl Game { self.current.current_turn } + pub fn current_cell_state(&self, idx: BrdIdx) -> Square { + self.current.cell(self.current.cell_idx(idx)) + } + /// Attempt to make a move given a source and destination index pub fn make_move(&mut self, from: BrdIdx, to: BrdIdx) -> Moveable { let able = self.current.can_move(from, to); diff --git a/www/index.js b/www/index.js index be87e49..7db00f5 100644 --- a/www/index.js +++ b/www/index.js @@ -1,4 +1,4 @@ -import { Game, Board, BrdIdx, Painter, Team, init_wasm, Moveable } from "draught"; +import { Game, Board, BrdIdx, Painter, Team, init_wasm, Moveable, SquareState } from "draught"; import { memory } from "draught/draught_bg.wasm"; /////////////////// @@ -35,6 +35,8 @@ let current_state = GameState.HUMAN_TURN.THINKING; let painter = new Painter(CANVAS_WIDTH, CANVAS_HEIGHT, "game-canvas"); // painter.draw(board); +let clicks = []; + let from = null; let to = null; @@ -71,55 +73,74 @@ function start_game() { } function process_canvas_click(cell_coord) { + + // if (game.current_cell_state(cell_coord).state == SquareState.Unplayable ) { + // from = null; + // to = null; + // current_state = GameState.HUMAN_TURN.THINKING; + // setStatusText("Unplayable Square!"); + // return; + // } + switch(current_state) { case GameState.HUMAN_TURN.THINKING: console.log("Your turn, first piece picked"); - from = cell_coord; + clicks.push(cell_coord); + + // from = cell_coord; current_state = GameState.HUMAN_TURN.FROM_SELECTED; break; case GameState.HUMAN_TURN.FROM_SELECTED: console.log("Your turn, first piece already picked, picking second"); - to = cell_coord; + clicks.push(cell_coord); - if(from.col == to.col && from.row == to.row){ + // to = cell_coord; + + if (clicks.length != 2) { + setStatusText(`Error: wrong number of clicks to process ${clicks.length}`); + console.error(`Error: wrong number of clicks to process ${clicks.length}`); + + return; + } + + // console.log(clicks[0].eq(clicks[1])); + + if (clicks[0].eq(clicks[1])) { setStatusText("Move Cancelled"); } else { - let status = game.make_move(from, to); + let status = game.make_move(clicks[0], clicks[1]); - 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; - } + 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; + clicks = []; + // from = null; + // to = null; current_state = GameState.HUMAN_TURN.THINKING; break;