Skip to content

Instantly share code, notes, and snippets.

@sjdonado
Created May 8, 2023 19:00
Show Gist options
  • Save sjdonado/b4f3ec5f1de30cb082ee48d0477d32b4 to your computer and use it in GitHub Desktop.
Save sjdonado/b4f3ec5f1de30cb082ee48d0477d32b4 to your computer and use it in GitHub Desktop.
Tower of Hanoi in P5.js + WASM - Dev.to
use gloo_utils::format::JsValueSerdeExt;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn get_moves(n: i32) -> JsValue {
fn move_tower(n: i32, from: i32, to: i32, aux: i32, moves: &mut Vec<String>) {
if n == 1 {
moves.push(format!("{}:{}", from, to));
} else {
move_tower(n - 1, from, aux, to, moves);
moves.push(format!("{}:{}", from, to));
move_tower(n - 1, aux, to, from, moves);
}
}
let mut moves = Vec::new();
move_tower(n, 0, 2, 1, &mut moves);
JsValue::from_serde(&moves).unwrap()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment