Created
September 30, 2018 08:26
-
-
Save lavantien/40b3f5e41b26fb5ff3d18dfffdac3aa8 to your computer and use it in GitHub Desktop.
Tower of Hanoi game with the ability to choosing the number of discs.
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
// written by LaVanTien . You have all rights to use and modify this source code :) | |
// compiled with flag: -std=c++14 | |
#include <iostream> | |
#include <vector> | |
#include <cmath> | |
using namespace std; | |
static vector<int> a, b, c; // 3 towers, start at 'a' and end at 'c' | |
static int num; // number of discs | |
static int sum = 0; // number of steps | |
static void newGame() { | |
a.clear(); | |
b.clear(); | |
c.clear(); | |
if (num <= 0 || num > 15) { // maximum 15 discs, to protect user's computer :v | |
num = 2; | |
} | |
for (int i = num; i > 0; --i) { | |
a.push_back(i); | |
} | |
} | |
static void print() { // print step | |
int na = a.size(); | |
int nb = b.size(); | |
int nc = c.size(); | |
cout << "\n--"; | |
for (int i = 0; i < num; ++i) { | |
cout << "--"; | |
} | |
cout << "\nA: "; | |
if (na != 0) { | |
cout << a[0]; | |
} | |
for (int i = 1; i < na; ++i) { | |
cout << ',' << a[i]; | |
} | |
cout << '\n'; | |
cout << "B: "; | |
if (nb != 0) { | |
cout << b[0]; | |
} | |
for (int i = 1; i < nb; ++i) { | |
cout << ',' << b[i]; | |
} | |
cout << '\n'; | |
cout << "C: "; | |
if (nc != 0) { | |
cout << c[0]; | |
} | |
for (int i = 1; i < nc; ++i) { | |
cout << ',' << c[i]; | |
} | |
cout << '\n'; | |
cout << "--"; | |
for (int i = 0; i < num; ++i) { | |
cout << "--"; | |
} | |
cout << '\n'; | |
} | |
static unsigned long long preCalc() { | |
unsigned long long res = (unsigned long long)pow(2, num) - 1; | |
return res; | |
} | |
static void md(vector<int> &u, vector<int> &v) { // move disc | |
v.push_back(u.back()); | |
u.pop_back(); | |
++sum; | |
cout << "\nStep " << sum << ':'; | |
print(); | |
} | |
static void solve(const int n, vector<int> &u, vector<int> &v, vector<int> &t) { | |
if (n > 0) { // recursive calls | |
solve(n - 1, u, t, v); // first move n - 1 discs from A to B | |
md(u, v); // then move the last disc to C | |
solve(n - 1, t, v, u); // last, move all n - 1 discs from B to C | |
} // that's all the idea, pretty simple :) | |
} | |
static void playBot() { | |
cout << "\nOriginal state:"; | |
print(); | |
solve(num, a, c, b); | |
} | |
int main() { | |
cout << "_/\\_/\\_/\\_ \"Tower of Ha Noi\" Solver _/\\_/\\_/\\_\n"; | |
cout << "\nEnter number of disc: "; | |
cin >> num; | |
cout << "\nMinimum number of steps: " << preCalc() << '\n'; | |
cout << "Continue? (press any key to continue) "; | |
cin.get(); | |
cin.get(); | |
newGame(); | |
playBot(); | |
cout << "\nDone in " << sum << " steps.\n"; | |
cin.get(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment