CDROM-Guide forums  

PDA

View Full Version : Bogged Down with C++ again....=(


   
Tealc
Oct 25, 2003, 03:33 PM
would appreciate any help, been at this since 1pm.....got the following:
my first problem, is that i have to make the map area 20x20 so if the player is on square 20,20 and clicks n to go north, an error message should display saying that s/he can't go any further
my second problem, is that when fatigue gets to 20 (after 20 movements) it is supposed to bring up a message saying that the player is too tired, which it does....but if n is input again it brings up the error message, as well as changing the coordinates (i dont want the player to move when fatigue is at 20)





#include <iostream>

using namespace std;
void main()

{
int x=6;
int y=6;


int max=20;

char action;
int iPlayerFatigue = 0;



char bp;
bp=7;


cout << "Hello and Welcome to lost island" << endl;



cout<< "Your current location is (" <<x<< "," <<y<< ")" << endl;
cout<<bp;





do
{
cout<<"What do you want to do?: " << endl; //outputs question
cin >> action; //user input
cout << ""<< endl;
switch (action)
{
case 't': cout << "You look around....there is nobody to talk to, try it next week." <<endl;
break;

case 'f': cout << "There is nobody to fight here." << endl;
break;

case 'p': cout << "There is nothing to pick up." <<endl;
break;

case 'd': cout << "You don't have anything to drop" <<endl;
break;

case 'n': cout << "Your current location is now (" <<x<< "," <<(y+1)<< ")" << endl;
y++;
iPlayerFatigue++;
break;

case 's': cout << "Your current location is now (" <<x<< "," <<(y-1)<< ")" << endl;
y--;
iPlayerFatigue++;
break;

case 'e': cout << "Your current location is now (" <<(x+1)<< "," <<y<< ")" << endl;
x++;
iPlayerFatigue++;
break;

case 'w': cout << "Your current location is now (" <<(x-1)<< "," <<y<< ")" << endl;
x--;
iPlayerFatigue++;
break;

case 'l' : cout << "Your fatigue is currently at " << iPlayerFatigue << endl;
break;

case 'r' : cout << "Your fatigue has been reduced by 5, it is now " <<(iPlayerFatigue = iPlayerFatigue - 5) << endl;
break;

case 'z' : cout << "You fall asleep..your fatigue is now 0 " << endl;
(iPlayerFatigue=0);
break;

case 'x': cout << "You are now leaving the game" << endl;
break;

default: cout << "Please enter a valid character" << endl;


}




if (iPlayerFatigue >=20)
switch (action)
{
case 'r' : cout << "Your fatigue has been reduced by 5, it is now " <<(iPlayerFatigue = iPlayerFatigue - 5) << endl;
break;

case 'z' : cout << "Your fatigue is now 0 " << endl;
(iPlayerFatigue=0);
break;

default : cout << "You are too tired, you should either rest (r) or sleep (z)" << endl;
break;
}


} while (action != 'x');


















}

orcus
Oct 25, 2003, 08:12 PM
Take the fatigue if statement and place it before the first switch statement and use the else on the entire switch statement that covers most of the options.

Your person keeps going because they can move before you check fatigue levels. So if you have if fatigue... use this switch statement else use this other one it should work fine.

Your person can walk off the board as you aren't placing any boundary limitations on your person when they move. Check to see what the co-ordinates are before just moving them.

Tealc
Oct 25, 2003, 08:29 PM
Thanks orcus much appreciated!! :) got the first prob sorted now :) but not sure how to set boundaries (need max and min values for x and y)at the moment, having a look round the net now...
cheers

Captain
Oct 26, 2003, 04:28 AM
For each of the four movements (n,s,e,w) add an if else statement inside the case statement.

So for north you would have:

case 'n':
if (y<20)
{
cout << "Your current location is now (" <<x<< "," <<(y+1)<< ")" << endl;
y++;
iPlayerFatigue++;
break;
}
else
{
cout << "You can't move any further north" << endl;
}


Do the same for each movement but change the if statement, so south would be if y>1 etc. Don't forget to change the error message too.

Hope that helps.

Tealc
Oct 26, 2003, 09:07 AM
thanks captain n' orcus, it all works perfectly i owe you one
its a tough language to pick up, only been doing it for 3-4 weeks but when you see some errors it seems obvious....its finding them thats the *****!
thanks again :)

Captain
Oct 26, 2003, 10:07 AM
When you compile it should tell you the line number of the error.

orcus
Oct 27, 2003, 04:30 AM
One alteration to Cappy's solution. Set a variable for the maximum size of your grid, that way you can change the size of the map by changing a variable instead of hardcoding the value inside if statements.

Just means you only have to change in one place instead of several.