From 5f832668129dfdb691b63e4b0ad90f694d3b978d Mon Sep 17 00:00:00 2001 From: KUchy Date: Tue, 10 Aug 2021 19:54:49 +0200 Subject: [PATCH] Hanoi tower --- CPP/miscelanious/hanoi/hanoi.cpp | 151 +++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/CPP/miscelanious/hanoi/hanoi.cpp b/CPP/miscelanious/hanoi/hanoi.cpp index e32c011..f4879d4 100644 --- a/CPP/miscelanious/hanoi/hanoi.cpp +++ b/CPP/miscelanious/hanoi/hanoi.cpp @@ -1,6 +1,157 @@ #include +#include +#include + +const int NUMBER_OF_FREE_STICKS = 3; + +void drawSpaces(int &numberOfPlates, int currentPlate) +{ + for(int i = 0; i < numberOfPlates - currentPlate; i++) + { + std::cout << " "; + } +} + +void drawFreeSticks(int &numberOfPlates) +{ + for(int i = 0; i < NUMBER_OF_FREE_STICKS; i++) + { + std::cout << "|"; + drawSpaces(numberOfPlates, 0); + std::cout << "|"; + drawSpaces(numberOfPlates, 0); + std::cout << "|" << std::endl; + } +} + +void drawRod(std::deque &rod, int ¤tLevel) +{ + unsigned int currentLevelUnsinged = currentLevel; + if(rod.size() > currentLevelUnsinged) + { + int j = rod[currentLevel]; + do + { + std::cout << "#"; + j--; + }while (j != 0); + }else std::cout << "|"; +} + +void drawRest(std::deque &firstRod, std::deque &secondRod, std::deque &thirdRod, int &numberOfPlates) +{ + for(int i = 0; i < numberOfPlates; i++) + { + drawRod(firstRod, i); + drawRod(secondRod, i); + drawRod(thirdRod, i); + std::cout << std::endl; + } +} + + +void drawTheTowers(std::deque &firstRod, std::deque &secondRod, std::deque &thirdRod, int &numberOfPlates) +{ + drawFreeSticks(numberOfPlates); + drawRest(firstRod, secondRod, thirdRod, numberOfPlates); +} + + + + +void printDeque(const std::deque deque) +{ + if(deque.size() != 0) + { + std::cout << "["; + for(unsigned int i = 0; i < deque.size(); i++) + { + std::cout << deque[i] << "; "; + } + std::cout << "]" << std::endl; + }else std::cout << "Deque is empty" << std::endl; +} + +void onlyLegalMove(std::deque &firstRod, std::deque &secondRod) +{ + if((!firstRod.empty() && firstRod.front() == 0) || (!secondRod.empty() && secondRod.front() == 0)) + { + printDeque(firstRod); + printDeque(secondRod); + } + + if(firstRod.empty() && secondRod.empty()) + { + std::cout << "BOTH EMPTY!" << std::endl; + }else if(secondRod.empty() || (!firstRod.empty() && firstRod.front() < secondRod.front()) ) + { + secondRod.push_front(firstRod.front()); + firstRod.pop_front(); + }else if(firstRod.empty() || (!secondRod.empty() && firstRod.front() > secondRod.front()) ) + { + firstRod.push_front(secondRod.front()); + secondRod.pop_front(); + }else + { + std::cout << "SOMETHING WENT TERRIBLY WRONG!" << std::endl; + printDeque(firstRod); + printDeque(secondRod); + } +} + +void doTheThingEven(std::deque &firstRod, std::deque &secondRod, std::deque &thirdRod) +{ + int i = 0; + do{ + onlyLegalMove(firstRod, secondRod); + i++; + onlyLegalMove(firstRod, thirdRod); + i++; + onlyLegalMove(secondRod, thirdRod); + i++; + }while(thirdRod.size() != 6); + printDeque(firstRod); + printDeque(secondRod); + printDeque(thirdRod); + std::cout << i << std::endl; +} + +void doTheThingOdd(std::deque &firstRod, std::deque &secondRod, std::deque &thirdRod) +{ + int i = 0; + do{ + onlyLegalMove(firstRod, thirdRod); + i++; + onlyLegalMove(firstRod, secondRod); + i++; + onlyLegalMove(secondRod, thirdRod); + i++; + }while(thirdRod.size() != 6); + printDeque(firstRod); + printDeque(secondRod); + printDeque(thirdRod); + std::cout << i << std::endl; +} + +/* +even: +AB AC BC +odd: +AC AB BC +*/ + int main() { + //int numberOfPlates = 6; + + std::deque firstRod, secondRod, thirdRod; + firstRod = {1, 2, 3, 4, 5, 6}; + secondRod = {}; + thirdRod = {}; + if(firstRod.size() % 2 == 0) doTheThingEven(firstRod, secondRod, thirdRod); + else doTheThingOdd(firstRod, secondRod, thirdRod); + + return 0; }