added eq BrdIdx method, changing to array of clicks

This commit is contained in:
andy 2021-07-10 19:10:48 +01:00
parent 5d3e20cf37
commit cd9ee4859f
3 changed files with 59 additions and 30 deletions

View File

@ -133,6 +133,10 @@ impl BrdIdx {
row, col row, col
} }
} }
pub fn eq (&self, other: &BrdIdx) -> bool {
*self == *other
}
} }
impl Display for BrdIdx { impl Display for BrdIdx {

View File

@ -56,6 +56,10 @@ impl Game {
self.current.current_turn 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 /// Attempt to make a move given a source and destination index
pub fn make_move(&mut self, from: BrdIdx, to: BrdIdx) -> Moveable { pub fn make_move(&mut self, from: BrdIdx, to: BrdIdx) -> Moveable {
let able = self.current.can_move(from, to); let able = self.current.can_move(from, to);

View File

@ -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"; 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"); let painter = new Painter(CANVAS_WIDTH, CANVAS_HEIGHT, "game-canvas");
// painter.draw(board); // painter.draw(board);
let clicks = [];
let from = null; let from = null;
let to = null; let to = null;
@ -71,28 +73,47 @@ function start_game() {
} }
function process_canvas_click(cell_coord) { 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) { switch(current_state) {
case GameState.HUMAN_TURN.THINKING: case GameState.HUMAN_TURN.THINKING:
console.log("Your turn, first piece picked"); console.log("Your turn, first piece picked");
from = cell_coord; clicks.push(cell_coord);
// from = cell_coord;
current_state = GameState.HUMAN_TURN.FROM_SELECTED; current_state = GameState.HUMAN_TURN.FROM_SELECTED;
break; break;
case GameState.HUMAN_TURN.FROM_SELECTED: case GameState.HUMAN_TURN.FROM_SELECTED:
console.log("Your turn, first piece already picked, picking second"); 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"); setStatusText("Move Cancelled");
} else { } 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) { switch(status) {
case Moveable.Allowed: case Moveable.Allowed:
break; break;
@ -113,13 +134,13 @@ function process_canvas_click(cell_coord) {
case Moveable.WrongTeamSrc: case Moveable.WrongTeamSrc:
break; break;
} }
}
} }
game.draw(); game.draw();
from = null; clicks = [];
to = null; // from = null;
// to = null;
current_state = GameState.HUMAN_TURN.THINKING; current_state = GameState.HUMAN_TURN.THINKING;
break; break;