-
September 27th, 2012, 08:49 PM
#1
[RESOLVED] Towers of Hanoi Game
So my program uses the list template as the towers and the values in them are disks. I used the push and pop methods with loops to display changing values due to user input.
Rules of the game:
- All disks are stacked into an initial tower.
- Disks range from smallest at the top to the biggest at the bottom.
- Only 1 disk can be moved to another tower per move.
- The disk you are moving to the next tower stacks onto possibly another disk, but the selected disk can't be bigger than the one in designated tower.
- 5. The game ends when the user successfully moves all the disks to another tower, in the range stated in rule #2.
Problems with my program:
- * add while loop that counts the remaining moves for player
- * fix displaying cout for the "lists"
- * fix push and pop logic statements
- * add if temp or dest tower size is = numDisk, you win
- * else if moves = 0, you lost
Code:
#include <list>
#include <queue>
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
template <typename T, size_t N>
inline
size_t arraysize( const T(&)[ N ] )
{
return N;
}
int main()
{
int numDisks;
char selTower, nxTower;
int count = 1;
queue<int>display;
cout << "o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o\n" <<
"o o o o o o o o o TOWERS OF HANOI GAME o o o o o o o o o o\n" <<
"o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o\n\n";
cout << "How many disks do you want to play with?\nSuggestions: 4 - Recommended, 5 - Intermediate ";
cin >> numDisks;
const int num = numDisks;
int disks[num];
for (int i = 0; i < numDisks; i++)
{
disks[i] = i+1;
//cout << disks[i] << endl;
}
//objectNameOf_List.classMem(position to insert, amount, input number)
list<int>initial (disks, disks+numDisks); //equiv. to (4, 1)
list<int>destination(numDisks);
list<int>temporary(numDisks);
while (1) //2^n - 1
{
cout << "\nChoose a tower:\n\n" <<
" [a] [b] [c]\n"
"Initial Destination Temporary\n"<<
" | | |\n" <<
" v v v\n\n";
cout << " | " << " " << " | " << " " << " | " << endl;
cout << " | " << " " << " | " << " " << " | " << endl;
for(int i = 0; i < (int)numDisks; i++)
cout << " " << initial.front()+i << " " << destination.front()+i << " " << temporary.front()+i << endl;
cout << "_________ _________ _________\n\n";
cin >> selTower;
cout << "\nSelect the tower for this top disk to move to. ";
cin >> nxTower;
if (selTower == 'a' && nxTower == 'b')
{
destination.push_front(initial.front());
destination.pop_back();
initial.pop_front();
}
else if (selTower == 'a' && nxTower == 'c')
{
temporary.push_front(initial.front());
temporary.pop_back();
initial.pop_front();
}
else if (selTower == 'b' && nxTower == 'a')
{
initial.push_front(destination.front());
initial.pop_back();
destination.pop_front();
}
else if (selTower == 'b' && nxTower == 'c')
{
temporary.push_front(destination.front());
temporary.pop_back();
destination.pop_front();
}
else if (selTower == 'c' && nxTower == 'a')
{
initial.push_front(temporary.front());
initial.pop_back();
temporary.pop_front();
}
else if (selTower == 'c' && nxTower == 'b')
{
destination.push_front(temporary.front());
destination.pop_back();
temporary.pop_front();
}
count++;
}
cout << "Ran out of moves. You lose!";
}
-
September 28th, 2012, 08:16 AM
#2
Re: Towers of Hanoi Game
Originally Posted by Lethargic
Problems with my program:
- * add while loop that counts the remaining moves for player
- * fix displaying cout for the "lists"
- * fix push and pop logic statements
- * add if temp or dest tower size is = numDisk, you win
- * else if moves = 0, you lost
OK, so what is your question?
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|