Last active
April 29, 2016 22:07
-
-
Save danish-rehman/8c85a08e3cbae1960b7dbcebe9000480 to your computer and use it in GitHub Desktop.
Tower of Hanoi
This file contains hidden or 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
| /* | |
| * Basic implementation of tower of hanoi | |
| * | |
| * Initial Board | |
| * 1 - - | |
| * 2 - - | |
| * 3 - - | |
| * | |
| * To win the board must be as below | |
| * - - 1 | |
| * - - 2 | |
| * - - 3 | |
| * | |
| * Input is given from the command line the following format | |
| * # Meaning move time from stack 0 to stack 1 | |
| * $ 0 1 | |
| * | |
| * Author: Danish Rehman | |
| */ | |
| #include <iostream> | |
| #include <cstring> | |
| #include <stdio.h> | |
| class GameOfHanoi{ | |
| private: | |
| int board[3][3]; | |
| int stack_top[3]; /*Maintains the index of top of the stack*/ | |
| void update(int from, int to); | |
| bool move_not_allowed(int from, int to); | |
| void print_board(); | |
| public: | |
| GameOfHanoi(); | |
| bool is_winner(); | |
| void move(int from, int to); | |
| }; | |
| GameOfHanoi::GameOfHanoi() | |
| { | |
| memset(board ,0, sizeof(board[0][0]) * 3 * 3); | |
| memset(stack_top ,0, sizeof(stack_top[0]) * 3 ); | |
| board[0][0] = 1; | |
| board[1][0] = 2; | |
| board[2][0] = 3; | |
| stack_top[0] = 0; | |
| stack_top[1] = 3; | |
| stack_top[2] = 3; | |
| } | |
| void GameOfHanoi::print_board() | |
| { | |
| for (int i = 0; i < 3; i++) { | |
| for (int j = 0; j < 3; j++) { | |
| if (board[i][j]) | |
| std::cout << board[i][j] << " "; | |
| else | |
| std::cout << "-" << " "; | |
| } | |
| std::cout << "\n"; | |
| } | |
| } | |
| bool GameOfHanoi::is_winner() | |
| { | |
| // Check last stack for the exact pattern | |
| for (int i = 0; i < 3; i++) | |
| { | |
| if (board[i][2] != i + 1) | |
| return false; | |
| } | |
| return true; | |
| } | |
| bool GameOfHanoi:: move_not_allowed(int from, int to) | |
| { | |
| /* | |
| * Check boundary conditions | |
| * Check if greater value tile is not being added over the lower value tile | |
| * in a given stack | |
| */ | |
| if (from < 0 || from > 2 || to < 0 || to > 2 or stack_top[from] == 3) | |
| return true; | |
| if (stack_top[to] == 3 || (board[stack_top[to]][to] > board[stack_top[from]][from])) | |
| return false; | |
| return true; | |
| } | |
| void GameOfHanoi::update(int from, int to) | |
| { | |
| /* | |
| * Move the tile and upate the respective | |
| * stack_top values | |
| */ | |
| int from_top = board[stack_top[from]][from]; | |
| board[stack_top[from]][from] = 0; | |
| stack_top[from]++; | |
| stack_top[to]--; | |
| board[stack_top[to]][to] = from_top; | |
| } | |
| void GameOfHanoi::move(int from, int to) | |
| { | |
| if (move_not_allowed(from, to)) { | |
| std::cout << "Can’t move there.\n"; | |
| return; | |
| } | |
| update(from, to); | |
| std::cout << "Piece moved and here is the board:\n"; | |
| print_board(); | |
| } | |
| int main() | |
| { | |
| int from, to; | |
| GameOfHanoi game; | |
| std::cout << "WELCOME TO THE GAME: TOWER OF HANOI:\n"; | |
| while (!game.is_winner()) { | |
| fflush (stdin); | |
| std::cout << "-----------------------------------------------------------------------\n"; | |
| std::cout << "Enter from and to stack (space separated)\n"; | |
| std::cout << "-----------------------------------------------------------------------\n"; | |
| std::cin >> from >> to; | |
| game.move(from, to); | |
| } | |
| std::cout << "YOU WIN.GAME OVER\n"; | |
| return 0; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
README
How to compile
g++ game_of_hanoi.cpp
How to run
./a.out
Sample run
Enter from and to stack (space separated)
0 1
Piece moved and here is the board:
2 - -
3 1 -
Enter from and to stack (space separated)