Pure Programmer
Blue Matrix


Cluster Map

Project: Tower of Hanoi

The [[Tower of Hanoi]] is a puzzle played with disks and pegs. The idea is to move a tower of disks from one peg to another subject to certain restrictions:

  1. Only one disk can be moved at a time
  2. Each move consists of taking the upper disk from one of the pegs and placing it on top of another stack or on an empty peg
  3. A larger disk can not be placed on a smaller disk

Write a program that uses a recursive function to solve the Tower of Hanoi puzzle with three pegs and any number of disks. Print out the configuration every 1, 5, 10 or other multiple of 5 moves depending on the number of moves it takes to solve so that about 7 moves of the solution are displayed. The number of moves (`m`) it takes to solve the puzzle depends on the number of disks (`n`) and is `m = 2^n - 1`

See [[Tower of Hanoi Algorithm]]
See [[Tower of Hanoi Animation]]

Output
$ rustc TowerOfHanoi.rs error: expected identifier, found keyword `move` --> TowerOfHanoi.rs:37:4 | 37 | fn move(source:isize, dest:isize) -> () { | ^^^^ expected identifier, found keyword | help: escape `move` to use it as an identifier | 37 | fn r#move(source:isize, dest:isize) -> () { | ++ error: expected one of `async`, `|`, or `||`, found `(` --> TowerOfHanoi.rs:70:7 | 70 | move(source, dest); | ^ expected one of `async`, `|`, or `||` error: expected one of `async`, `|`, or `||`, found `(` --> TowerOfHanoi.rs:73:7 | 73 | move(source, dest); | ^ expected one of `async`, `|`, or `||` error[E0530]: function parameters cannot shadow statics --> TowerOfHanoi.rs:68:10 | 10 | static mut numDisks:isize = 0; | ------------------------------ the static `numDisks` is defined here ... 68 | fn solve(numDisks:isize, source:isize, dest:isize, aux:isize) -> () { | ^^^^^^^^ cannot be named the same as a static error[E0425]: cannot find function `exit` in this scope --> TowerOfHanoi.rs:82:3 | 82 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | error[E0425]: cannot find value `Utils` in this scope --> TowerOfHanoi.rs:84:13 | 84 | numDisks = Utils.stoiWithDefault(args[1], 3); | ^^^^^ not found in this scope error[E0425]: cannot find function `max` in this scope --> TowerOfHanoi.rs:112:13 | 112 | printMod = max(printMod,1); | ^^^ not found in this scope | help: consider importing this function | 8 + use std::cmp::max; | error[E0277]: the type `[Vec<isize>]` cannot be indexed by `isize` --> TowerOfHanoi.rs:23:28 | 23 | let disk:isize = pegs[j][i]; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[Vec<isize>]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<Vec<isize>>` to implement `Index<isize>` error[E0277]: the type `[Vec<isize>]` cannot be indexed by `isize` --> TowerOfHanoi.rs:43:12 | 43 | if pegs[source][i] != 0 { | ^^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[Vec<isize>]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<Vec<isize>>` to implement `Index<isize>` error[E0277]: the type `[Vec<isize>]` cannot be indexed by `isize` --> TowerOfHanoi.rs:44:17 | 44 | disk = pegs[source][i]; | ^^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[Vec<isize>]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<Vec<isize>>` to implement `Index<isize>` error[E0277]: the type `[Vec<isize>]` cannot be indexed by `isize` --> TowerOfHanoi.rs:45:10 | 45 | pegs[source][i] = 0; | ^^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[Vec<isize>]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<Vec<isize>>` to implement `Index<isize>` error[E0277]: the type `[Vec<isize>]` cannot be indexed by `isize` --> TowerOfHanoi.rs:55:12 | 55 | if pegs[dest][i] != 0 { | ^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[Vec<isize>]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<Vec<isize>>` to implement `Index<isize>` error[E0277]: the type `[Vec<isize>]` cannot be indexed by `isize` --> TowerOfHanoi.rs:62:7 | 62 | pegs[dest][pos] = disk; | ^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[Vec<isize>]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<Vec<isize>>` to implement `Index<isize>` error[E0277]: the type `[Vec<isize>]` cannot be indexed by `isize` --> TowerOfHanoi.rs:97:9 | 97 | pegs[i] = positions; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[Vec<isize>]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<Vec<isize>>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> TowerOfHanoi.rs:104:12 | 104 | pegs[0][i] = i + 1; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields --> TowerOfHanoi.rs:109:31 | 109 | let solutionSteps:isize = (2.f64.powf(numDisks) - 1.f64) as isize; | ^^^ | help: if intended to be a floating point literal, consider adding a `0` after the period | 109 | let solutionSteps:isize = (2.0f64.powf(numDisks) - 1.f64) as isize; | + error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields --> TowerOfHanoi.rs:109:54 | 109 | let solutionSteps:isize = (2.f64.powf(numDisks) - 1.f64) as isize; | ^^^ | help: if intended to be a floating point literal, consider adding a `0` after the period | 109 | let solutionSteps:isize = (2.f64.powf(numDisks) - 1.0f64) as isize; | + error: aborting due to 17 previous errors Some errors have detailed explanations: E0277, E0425, E0530, E0610. For more information about an error, try `rustc --explain E0277`. $ rustc TowerOfHanoi.rs error: expected identifier, found keyword `move` --> TowerOfHanoi.rs:37:4 | 37 | fn move(source:isize, dest:isize) -> () { | ^^^^ expected identifier, found keyword | help: escape `move` to use it as an identifier | 37 | fn r#move(source:isize, dest:isize) -> () { | ++ error: expected one of `async`, `|`, or `||`, found `(` --> TowerOfHanoi.rs:70:7 | 70 | move(source, dest); | ^ expected one of `async`, `|`, or `||` error: expected one of `async`, `|`, or `||`, found `(` --> TowerOfHanoi.rs:73:7 | 73 | move(source, dest); | ^ expected one of `async`, `|`, or `||` error[E0530]: function parameters cannot shadow statics --> TowerOfHanoi.rs:68:10 | 10 | static mut numDisks:isize = 0; | ------------------------------ the static `numDisks` is defined here ... 68 | fn solve(numDisks:isize, source:isize, dest:isize, aux:isize) -> () { | ^^^^^^^^ cannot be named the same as a static error[E0425]: cannot find function `exit` in this scope --> TowerOfHanoi.rs:82:3 | 82 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | error[E0425]: cannot find value `Utils` in this scope --> TowerOfHanoi.rs:84:13 | 84 | numDisks = Utils.stoiWithDefault(args[1], 3); | ^^^^^ not found in this scope error[E0425]: cannot find function `max` in this scope --> TowerOfHanoi.rs:112:13 | 112 | printMod = max(printMod,1); | ^^^ not found in this scope | help: consider importing this function | 8 + use std::cmp::max; | error[E0277]: the type `[Vec<isize>]` cannot be indexed by `isize` --> TowerOfHanoi.rs:23:28 | 23 | let disk:isize = pegs[j][i]; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[Vec<isize>]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<Vec<isize>>` to implement `Index<isize>` error[E0277]: the type `[Vec<isize>]` cannot be indexed by `isize` --> TowerOfHanoi.rs:43:12 | 43 | if pegs[source][i] != 0 { | ^^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[Vec<isize>]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<Vec<isize>>` to implement `Index<isize>` error[E0277]: the type `[Vec<isize>]` cannot be indexed by `isize` --> TowerOfHanoi.rs:44:17 | 44 | disk = pegs[source][i]; | ^^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[Vec<isize>]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<Vec<isize>>` to implement `Index<isize>` error[E0277]: the type `[Vec<isize>]` cannot be indexed by `isize` --> TowerOfHanoi.rs:45:10 | 45 | pegs[source][i] = 0; | ^^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[Vec<isize>]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<Vec<isize>>` to implement `Index<isize>` error[E0277]: the type `[Vec<isize>]` cannot be indexed by `isize` --> TowerOfHanoi.rs:55:12 | 55 | if pegs[dest][i] != 0 { | ^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[Vec<isize>]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<Vec<isize>>` to implement `Index<isize>` error[E0277]: the type `[Vec<isize>]` cannot be indexed by `isize` --> TowerOfHanoi.rs:62:7 | 62 | pegs[dest][pos] = disk; | ^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[Vec<isize>]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<Vec<isize>>` to implement `Index<isize>` error[E0277]: the type `[Vec<isize>]` cannot be indexed by `isize` --> TowerOfHanoi.rs:97:9 | 97 | pegs[i] = positions; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[Vec<isize>]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<Vec<isize>>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> TowerOfHanoi.rs:104:12 | 104 | pegs[0][i] = i + 1; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields --> TowerOfHanoi.rs:109:31 | 109 | let solutionSteps:isize = (2.f64.powf(numDisks) - 1.f64) as isize; | ^^^ | help: if intended to be a floating point literal, consider adding a `0` after the period | 109 | let solutionSteps:isize = (2.0f64.powf(numDisks) - 1.f64) as isize; | + error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields --> TowerOfHanoi.rs:109:54 | 109 | let solutionSteps:isize = (2.f64.powf(numDisks) - 1.f64) as isize; | ^^^ | help: if intended to be a floating point literal, consider adding a `0` after the period | 109 | let solutionSteps:isize = (2.f64.powf(numDisks) - 1.0f64) as isize; | + error: aborting due to 17 previous errors Some errors have detailed explanations: E0277, E0425, E0530, E0610. For more information about an error, try `rustc --explain E0277`.

Solution