Created
May 8, 2023 19:00
-
-
Save sjdonado/b4f3ec5f1de30cb082ee48d0477d32b4 to your computer and use it in GitHub Desktop.
Tower of Hanoi in P5.js + WASM - Dev.to
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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