added status text
This commit is contained in:
parent
0c839402ae
commit
5d3e20cf37
@ -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
|
||||
|
@ -39,11 +39,17 @@
|
||||
<p class="text-muted">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 <a href="https://en.wikipedia.org/wiki/Minimax">minimax</a> algorithm to create an AI player that can operate with reasonable performance as a result of Rust's compiled performance.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row p-1">
|
||||
<div class="row p-3">
|
||||
<div class="col-sm-12">
|
||||
<a href="doc/draught" class="btn btn-secondary">Docs</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row p-1">
|
||||
<div class="col-sm-12">
|
||||
<p hidden id="status-p"></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
47
www/index.js
47
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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user