diff --git a/.gitignore b/.gitignore index 57a1574..8676367 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +*.zip + ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. diff --git a/COM380/Project1/Project1.sln b/COM380/Project1/Project1.sln new file mode 100644 index 0000000..613db94 --- /dev/null +++ b/COM380/Project1/Project1.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Project1", "Project1\Project1.vcxproj", "{F439F87E-CA70-407F-9C1A-08BDE243C96C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F439F87E-CA70-407F-9C1A-08BDE243C96C}.Debug|Win32.ActiveCfg = Debug|Win32 + {F439F87E-CA70-407F-9C1A-08BDE243C96C}.Debug|Win32.Build.0 = Debug|Win32 + {F439F87E-CA70-407F-9C1A-08BDE243C96C}.Release|Win32.ActiveCfg = Release|Win32 + {F439F87E-CA70-407F-9C1A-08BDE243C96C}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/COM380/Project1/Project1/Project1.vcxproj b/COM380/Project1/Project1/Project1.vcxproj new file mode 100644 index 0000000..22f5567 --- /dev/null +++ b/COM380/Project1/Project1/Project1.vcxproj @@ -0,0 +1,89 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {F439F87E-CA70-407F-9C1A-08BDE243C96C} + Win32Proj + Project1 + + + + Application + true + v110 + Unicode + + + Application + false + v110 + true + Unicode + + + + + + + + + + + + + true + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + true + true + + + + + + + + + + + + + + \ No newline at end of file diff --git a/COM380/Project1/Project1/Project1.vcxproj.filters b/COM380/Project1/Project1/Project1.vcxproj.filters new file mode 100644 index 0000000..7f4999b --- /dev/null +++ b/COM380/Project1/Project1/Project1.vcxproj.filters @@ -0,0 +1,33 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/COM380/Project1/Project1/Tower.cpp b/COM380/Project1/Project1/Tower.cpp new file mode 100644 index 0000000..2829bbd --- /dev/null +++ b/COM380/Project1/Project1/Tower.cpp @@ -0,0 +1,116 @@ + +#include "Tower.h" + +int Tower::s_Discs = 0; + +Tower::Tower(int id) +{ + Tower(5, id); +} + +Tower::Tower(int num, int id) +{ + towerID = id; + s_Discs = num; + hanoi = vector (num); + for (int i = 0; i > num; i++) + { + hanoi.at(i) = false; + } +} + +Tower::~Tower() +{ + hanoi.clear(); +} + +int Tower::getdnum() +{ + return s_Discs; +} + +bool Tower::setAt(int i, bool in) +{ + if (i > s_Discs || i < 0) // yay input validation + { + return false; + } + else + { + hanoi.at(i) = in; + return true; + } +} + +bool Tower::isSetAt(int i) +{ + if (i > s_Discs || i < 0) // yay input validation + { + return false; + } + else + { + return hanoi.at(i); + } +} + +void Tower::setAll(bool in) +{ + for (int i = 0; i < s_Discs; i++) + { + hanoi.at(i) = in; + } +} + +void Tower::println(int num) +{ + string diff_spc3 = ""; + string diff_spc2 = ""; + for (int i = 0; i < s_Discs + 1; i++) + { + diff_spc2 += " "; + if (i < s_Discs -2) // magic number is -2 becuase 2 hours of testing says so + diff_spc3 += " "; + } + + if (num < s_Discs) + { + // tower lines + int diff = s_Discs - num; + string diff_spc = ""; + for (int i = 0; i < diff; i++) + { + diff_spc += " "; + } + if (hanoi.at(num) == true) + { + string out = ""; + char hold = ' '; + for (int i = 0; i <= num; i++) + { + hold = 220; // drawing lower halfblock + out += hold; + } + hold = 219; // drawing fullblock; + //hold = '|'; + cout << diff_spc << out << hold << out << diff_spc; + } + else + { + char hold = ' '; + hold = 219; // drawing fullblock; + //hold = '|'; + cout << diff_spc2 << hold << diff_spc2; + } + } + else + { + // last line is always tower id + cout << diff_spc3 << "Tower " << towerID << diff_spc3; + } +} + +int Tower::getTowerID() +{ + return towerID; +} \ No newline at end of file diff --git a/COM380/Project1/Project1/Tower.h b/COM380/Project1/Project1/Tower.h new file mode 100644 index 0000000..cbc6332 --- /dev/null +++ b/COM380/Project1/Project1/Tower.h @@ -0,0 +1,36 @@ + +#ifndef _tower_h +#define _tower_h + + #include + #include + #include + #include + + using namespace std; + + class Tower + { + public: + Tower(int); + Tower(int, int); + ~Tower(); + + bool isSetAt(int i); + bool setAt(int i, bool in); + + int getdnum(); + int getTowerID(); + + void println(int num); + void setAll(bool in = true); + + //friend ostream& operator<<(ostream& os, const Tower& tower); + + private: + int towerID; + static int s_Discs; + vector hanoi; + }; + +#endif \ No newline at end of file diff --git a/COM380/Project1/Project1/main.cpp b/COM380/Project1/Project1/main.cpp new file mode 100644 index 0000000..b9a2923 --- /dev/null +++ b/COM380/Project1/Project1/main.cpp @@ -0,0 +1,222 @@ + +/*********************************************** + * Project 1 - COM380 - S. Mayer + * By: Amrit Panesar - 77260 + * Toners of Hanoi + ***********************************************/ + +#include "main.h" +using namespace std; + +// Not unicode safe +// who wants to use silly wide characters anyways +int main(int argc, char* argv[]) +{ +#ifdef _WIN32 + + // Remember how things were when we started + HANDLE hstdout = GetStdHandle( STD_OUTPUT_HANDLE ); + CONSOLE_SCREEN_BUFFER_INFO csbi; + GetConsoleScreenBufferInfo( hstdout, &csbi ); + SetConsoleTextAttribute( hstdout, 0x1E ); + ClearScreen(); + +#endif // _WIN32 + + vector tvecs; + int towers = 3; + int discs = 5; + + cout << "Before we begin, how many discs would you like to solve for?" << endl; + int num = 0; + while (num < 3) + { + cout << "Please enter a number greater than three" << endl << ": "; + string hold = ""; + cin >> hold; + istringstream ss(hold); + ss >> num; + if (ss.fail()) + { + cout << "Error: invalid integer specified, please try again." << endl; + } + else if (num < 3) + { + cout << "Error: Please specify an integer greater than three" << endl; + num = 0; + } + } + cout << "Input accepted." << endl; + cout << "Creating "<< num << " discs." << endl; + discs = num; + + for (int i = 0; i < towers; i++) + { + tvecs.push_back(Tower(discs, i)); + } + tvecs.at(0).setAll(); + + cout << "Initial state: " << endl << endl; + println(tvecs); + if (discs > 11) + { + cout << endl; + cout << "Disclaimer: As you can see, with a disc count higher than 11, it looks crappy." << endl; + cout << "Please use the Console2 program (or Windows PowerShell) for a better experiance" << endl; + cout << endl; + } + pause(); + ClearScreen(); + creepyLoop("Towers of Hanoi"); + + int totalMoves = movePieces(tvecs, discs - 1, 0, 2, 1, 0); + + pause(); + ClearScreen(); + cout << "Final state: " << endl << endl; + println(tvecs); + + cout << "Finished with " << totalMoves << " moves." << endl; + + cout << endl << endl; + cout << "Thanks for playing!" << endl; + pause(); + +#ifdef _WIN32 + SetConsoleTextAttribute( hstdout, csbi.wAttributes); +#endif // _WIN32 + + return 0; +} + +void println(vector &tvecs) +{ + int discs = tvecs.at(0).getdnum(); + int towers = tvecs.size(); + for (int i = 0; i < discs + 1; i++) + { + for (int j = 0; j < towers; j++) + { + tvecs.at(j).println(i); + } + cout << endl; + } + cout << endl; +} + +int movePieces(vector &tvecs, int currentStack, int from, int to, int helper, int moveaccumulator) +{ + moveaccumulator++; + if (currentStack == 0) + { + tvecs.at(from).setAt(currentStack, false); + tvecs.at(to).setAt(currentStack, true); + println(tvecs); + } + else + { + moveaccumulator = movePieces(tvecs, currentStack - 1, from, helper, to, moveaccumulator); + tvecs.at(from).setAt(currentStack, false); + tvecs.at(to).setAt(currentStack, true); + println(tvecs); + moveaccumulator = movePieces(tvecs, currentStack - 1, helper, to, from, moveaccumulator); + } + return moveaccumulator; +} + +void creepyLoop(string input) +{ + int num = input.length() + 12; + string diffs = centerLine(num); + + cout << diffs << input << " "; + sleep(500); + cout << "."; + sleep(500); + cout << " "; + sleep(500); + cout << "."; + sleep(500); + cout << " "; + sleep(500); + cout << "."; + cout << endl << endl; +} + +string centerLine(string in) +{ + int len = in.length(); + return centerLine(len); +} + +string centerLine(int len) +{ + int diff = (len % 80)/2; + string strDiff = ""; + for (int i = 0; i < len; i++) + { + strDiff += " "; + } + return strDiff; +} + +void pause() +{ + cout << "Press any key to continue . . ."; + _getch(); // silly posix.. + cout << endl; +} + +void sleep(int in) +{ +#ifdef _WIN32 + Sleep(in); +#elif __linux__ + usleep(in); +#endif +} + +void ClearScreen() +{ +#ifdef _WIN32 + + HANDLE conOut = GetStdHandle( STD_OUTPUT_HANDLE ); + if (conOut == INVALID_HANDLE_VALUE) + return; + + DWORD count, cellCount; + + //Get the number of cells in the current buffer + CONSOLE_SCREEN_BUFFER_INFO csbi; + if (!GetConsoleScreenBufferInfo(conOut, &csbi)) + return; + + cellCount = csbi.dwSize.X *csbi.dwSize.Y; + + //Fill the entire buffer with spaces + COORD origin = { 0, 0 }; + if (!FillConsoleOutputCharacter(conOut, (TCHAR) ' ', cellCount, origin, &count)) + return; + + //Fill the entire buffer with the current colors and attributes + if (!FillConsoleOutputAttribute(conOut, csbi.wAttributes, cellCount, origin, &count)) + return; + + //Move the cursor to 0,0 + SetConsoleCursorPosition(conOut, origin); + +#elif __linux__ + + if (!cur_term) + { + int result; + setupterm (NULL, STDOUT_FILENO, &result); + + if (result <= 0) + return; + } + putp(tigetstr("clear" )); + +#endif +} + diff --git a/COM380/Project1/Project1/main.h b/COM380/Project1/Project1/main.h new file mode 100644 index 0000000..ccdd0c7 --- /dev/null +++ b/COM380/Project1/Project1/main.h @@ -0,0 +1,42 @@ +#ifdef _WIN32 + #ifndef VC_EXTRALEAN + #define VC_EXTRALEAN + // no clue what this does, but lets be EXTRALEAN + #endif // !VC_EXTRALEAN + + #ifndef WIN32_LEAN_AND_MEAN + // look how lean and mean we are! + #define WIN32_LEAN_AND_MEAN + #endif // !WIN32_LEAN_AND_MEAN + + #include +#elif __linux__ + #include + #include +#endif + +#ifndef _main_h + #define _main_h + /* just your run of the mill header file */ + + #include + #include + #include + #include + + #include + #include "Tower.h" + + void pause(); + void sleep(int in); + void creepyLoop(string in); + void println(vector &tvecs); + void ClearScreen(); + int getInput(string prompt); + + int movePieces(vector &tvecs, int currentStack, int from, int to, int helper, int moveaccumulator); + + string centerLine(string in); + string centerLine(int len); + +#endif // !_main_h