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
}
}
pub fn eq (&self, other: &BrdIdx) -> bool {
*self == *other
}
}
impl Display for BrdIdx {

View File

@ -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);

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";
///////////////////
@ -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,28 +73,47 @@ 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;
@ -113,13 +134,13 @@ function process_canvas_click(cell_coord) {
case Moveable.WrongTeamSrc:
break;
}
}
}
game.draw();
from = null;
to = null;
clicks = [];
// from = null;
// to = null;
current_state = GameState.HUMAN_TURN.THINKING;
break;