Add reset button
This commit is contained in:
@@ -79,5 +79,11 @@ def choose_move():
|
|||||||
return flask.jsonify(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__":
|
if __name__ == "__main__":
|
||||||
app.run(debug=True, host="127.0.0.1", port=3000)
|
app.run(debug=True, host="127.0.0.1", port=3000)
|
||||||
|
|||||||
@@ -31,14 +31,18 @@ class Backend {
|
|||||||
getAvailableMoves(position) {
|
getAvailableMoves(position) {
|
||||||
return post_json("http://localhost:3000/get_moves/", position);
|
return post_json("http://localhost:3000/get_moves/", position);
|
||||||
}
|
}
|
||||||
|
reset() {
|
||||||
|
return post_json("http://localhost:3000/reset/");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Chess {
|
class Chess {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.backend = new Backend();
|
this.backend = new Backend();
|
||||||
this.canvas = new visuals.Canvas();
|
this.canvas = new visuals.Canvas();
|
||||||
this.button = document.createElement("button");
|
|
||||||
this.autoplay = false;
|
this.autoplay = false;
|
||||||
|
|
||||||
|
this.button = document.createElement("button");
|
||||||
this.button.innerHTML = "Autoplay Off";
|
this.button.innerHTML = "Autoplay Off";
|
||||||
this.button.onclick = () => {
|
this.button.onclick = () => {
|
||||||
this.autoplay = !this.autoplay;
|
this.autoplay = !this.autoplay;
|
||||||
@@ -47,6 +51,15 @@ class Chess {
|
|||||||
];
|
];
|
||||||
};
|
};
|
||||||
document.body.appendChild(this.button);
|
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 = new visuals.ConfigVis(this.backend.getConfig());
|
||||||
this.configVis.draw(this.canvas);
|
this.configVis.draw(this.canvas);
|
||||||
this.activeSquares = new visuals.ActiveSquares();
|
this.activeSquares = new visuals.ActiveSquares();
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ impl Engine {
|
|||||||
board,
|
board,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pub fn reset(&mut self) {
|
||||||
|
self.board.reset()
|
||||||
|
}
|
||||||
pub fn get_state(&self) -> &board::Board {
|
pub fn get_state(&self) -> &board::Board {
|
||||||
&self.board
|
&self.board
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -230,6 +230,14 @@ impl Ui {
|
|||||||
Err("choose_move takes 1 arg".to_owned())
|
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> {
|
fn handle_command(&mut self, s: &str) -> Result<String, String> {
|
||||||
let mut cmd = s.split(',');
|
let mut cmd = s.split(',');
|
||||||
// There will be at least an empty string => otherwise panic
|
// There will be at least an empty string => otherwise panic
|
||||||
@@ -241,6 +249,7 @@ impl Ui {
|
|||||||
"make_move" => self.make_move(&args),
|
"make_move" => self.make_move(&args),
|
||||||
"set_state" => self.set_state(&args),
|
"set_state" => self.set_state(&args),
|
||||||
"choose_move" => self.choose_move(&args),
|
"choose_move" => self.choose_move(&args),
|
||||||
|
"reset" => self.reset(&args),
|
||||||
"" => Err("No command given".to_owned()),
|
"" => Err("No command given".to_owned()),
|
||||||
_ => Err("Invalid command".to_owned()),
|
_ => Err("Invalid command".to_owned()),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user