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.

I'd recommend using a bool array rather than a char array; it tends to make calculations simpler and less error-prone if all you're dealing with is ones and zeroes. Adding a function which prints out the appropriate symbols would be quite trivial.

Also... your usage of switch statements doesn't seem right. For example, in the randomizing function, all you would need is a simple if() else() to determine whether the "coin-toss" makes the lever up or down. Lastly, you'll need a loop when checking if the move is valid, to ensure that all the levers proceeding the up-lever are actually down. The switch statement is unnecessary.

There's a number of other problems with your code, but these are the ones you should concentrate on right now.

Okay, I started using if-else statementsinstead of using the switch, but then I forgot that if the number of levers changes in the program, then these statements will become painfully long. Right?
I'm not sure how a loop would go though. For the fourth lever would it be something like this?

for ( Index = PlayerChoice - 3 ; Index < 1 ; ++Index )

>I forgot that if the number of levers changes in the program, then these statements
>will become painfully long. Right?

Nope. It seems to me that you're using the wrong tool for the job. switch() statements are useful when you need to branch into many different cases involving a single value equality. Since you're needing expandability, you should be looking at loops.

For example, here's how you would randomize a single lever:

if ( rand() % 2 == 1 )
{
  // lever up
}
else
{
  // lever down
}

Put that in a loop, and you're done.

>For the fourth lever would it be something like this?

Again, think expandability. Don't think of it in terms of the fourth lever, think of it in terms of the nth lever. In other words, make a function that takes a lever number as its argument.

bool MoveLever( int n )
{
  /*
   * First, loop through levers 0 through n-2, checking that they're all
   * down. Bail out of the function if any those levers aren't down
   * ( use return false; )
   *
   * Next, check that n-1 is UP. If not, bail out of the function.
   *
   * If we're still here, we obviously haven't bailed yet. Flip the lever.
   *
   * Return true; because the function was successful.
   */
}
This article has been dead for over six months. Start a new discussion instead.