Com380 Project 1

Signed-off-by: Amrit Panesar <apanesar@4195tech.com>
This commit is contained in:
Amrit Panesar committed 2015-03-31 22:59:15 -07:00
1 parent 3aadf07077
commit f799583a3d
8 files changed
+560

No files matched your search

+20
View File
@@ -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
+89
View File
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{F439F87E-CA70-407F-9C1A-08BDE243C96C}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>Project1</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="Tower.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="main.h" />
<ClInclude Include="Tower.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Tower.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="main.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Tower.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
+116
View File
@@ -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<bool> (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;
}
+36
View File
@@ -0,0 +1,36 @@
#ifndef _tower_h
#define _tower_h
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
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<bool> hanoi;
};
#endif
+222
View File
@@ -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<Tower> 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<Tower> &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<Tower> &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
}
+42
View File
@@ -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 <Windows.h>
#elif __linux__
#include <unistd.h>
#include <term.h>
#endif
#ifndef _main_h
#define _main_h
/* just your run of the mill header file */
#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>
#include <vector>
#include "Tower.h"
void pause();
void sleep(int in);
void creepyLoop(string in);
void println(vector<Tower> &tvecs);
void ClearScreen();
int getInput(string prompt);
int movePieces(vector<Tower> &tvecs, int currentStack, int from, int to, int helper, int moveaccumulator);
string centerLine(string in);
string centerLine(int len);
#endif // !_main_h