Add reset button

This commit is contained in:
2022-01-05 22:10:04 +01:00
parent 0e8eb7b767
commit c7f82769e3
4 changed files with 32 additions and 1 deletions

View File

@@ -79,5 +79,11 @@ def choose_move():
return flask.jsonify(move)
@app.route("/reset/", methods=["POST"])
def reset():
(state_str,) = ask_engine("reset")
return flask.jsonify(parse_state(state_str))
if __name__ == "__main__":
app.run(debug=True, host="127.0.0.1", port=3000)

View File

@@ -31,14 +31,18 @@ class Backend {
getAvailableMoves(position) {
return post_json("http://localhost:3000/get_moves/", position);
}
reset() {
return post_json("http://localhost:3000/reset/");
}
}
class Chess {
constructor() {
this.backend = new Backend();
this.canvas = new visuals.Canvas();
this.button = document.createElement("button");
this.autoplay = false;
this.button = document.createElement("button");
this.button.innerHTML = "Autoplay Off";
this.button.onclick = () => {
this.autoplay = !this.autoplay;
@@ -47,6 +51,15 @@ class Chess {
];
};
document.body.appendChild(this.button);
this.resetButton = document.createElement("button");
this.resetButton.innerHTML = "Reset";
this.resetButton.onclick = () => {
this.backend.reset();
this.updateState();
};
document.body.appendChild(this.resetButton);
this.configVis = new visuals.ConfigVis(this.backend.getConfig());
this.configVis.draw(this.canvas);
this.activeSquares = new visuals.ActiveSquares();

View File

@@ -13,6 +13,9 @@ impl Engine {
board,
}
}
pub fn reset(&mut self) {
self.board.reset()
}
pub fn get_state(&self) -> &board::Board {
&self.board
}

View File

@@ -230,6 +230,14 @@ impl Ui {
Err("choose_move takes 1 arg".to_owned())
}
}
fn reset(&mut self, args: &[&str]) -> Result<String, String> {
if let [] = args {
self.engine.reset();
Ok(format!("{}", self.engine.get_state()))
} else {
Err("reset doesn't take args".to_owned())
}
}
fn handle_command(&mut self, s: &str) -> Result<String, String> {
let mut cmd = s.split(',');
// There will be at least an empty string => otherwise panic
@@ -241,6 +249,7 @@ impl Ui {
"make_move" => self.make_move(&args),
"set_state" => self.set_state(&args),
"choose_move" => self.choose_move(&args),
"reset" => self.reset(&args),
"" => Err("No command given".to_owned()),
_ => Err("Invalid command".to_owned()),
}