Hi everyone. I need help with an assignment that calls for a puzzle. The puzzle has five levers, numbered 1 to 5. All the levers must be down to complete the puzzle. The program produces a random puzzle each time it is opened or a new game is played. It looks something like this...
v ^ ^ v v
1 2 3 4 5
In order to move a lever up or down, the lever preceding it must be up and the rest of the levers preceding that lever must be down. So in the example above 3 can go down because 2 is up and 1 is down. Then we put 2 down by switching 1 up. Then we switch 1 down and the puzzle is complete.
I am suppose to use arrays to loop through the players moves and see which moves are legal and which aren't. I'm not quite at that point yet though...
Here is what I have so far
#include<iostream>
#include<time.h>
using namespace std;
enum TLeverPos { LeverUp = '^', LeverDwn = 'v' };
int const TOTAL_NUMBER_OF_LEVERS = 5;
int GetInt(void);
void HoldScreen(void);
int TLeverMove(void);
void Randomize(void);
void GeneratePuzzle(char Lever[]);
TLeverPos TLeverFill(void);
int main(void)
{
// use a char array, as that what the enum uses.
char Lever[TOTAL_NUMBER_OF_LEVERS];
Randomize();
GeneratePuzzle(Lever);
// verify the random generation.
for ( int x = 0; x < TOTAL_NUMBER_OF_LEVERS; ++x )
{
cout << " " << Lever[x] << " ";
}
cout << endl << " 1 2 3 4 5" << endl;
int PlayerChoice = GetInt();
// verify the choice.
cout << "Player choice was: " << PlayerChoice << endl;
HoldScreen();
}
void Randomize(void)
{
srand(unsigned(time(NULL))); rand();
}
void GeneratePuzzle(char Lever[])
{
for ( int Index = 0 ; Index < TOTAL_NUMBER_OF_LEVERS ; ++Index )
{
Lever[Index] = TLeverFill();
}
}
TLeverPos TLeverFill(void)
{
TLeverPos Lever = LeverUp;
switch ( rand() % 2 )
{
case 0:
Lever = LeverUp;
break;
case 1:
Lever = LeverDwn;
break;
default:
cout << "hmm...." << endl;
break;
}
return Lever;
}
int GetInt( void )
{
int LeverChoice = 0;
do
{
cout << "Select a Lever, one to five, please. ";
cin >> LeverChoice;
HoldScreen();
}
while ( LeverChoice < 1 || LeverChoice > 5 );
return LeverChoice;
}
void HoldScreen(void)
{
cin.ignore(99,'\n');
}
int TLeverMove(void)
{
switch ( Lever[0] )
{
case LeverUp : return LeverDwn;
case LeverDwn : return LeverUp;
}
}
At this point I'm working on a switch for each move type, not too sure if I am doing it right.