Well, I have been self learning c++ for a while now and I decided to try and make a text based adventure game, my efforts are going well including the addition of randomized combat but I have a small problem.
To get a player from 1 map/level to another in my game I have made a function called gamemap(). All this function dose is check to see what a variable is =equal to with a bunch of if statements and then displays text (which will at the later stage of my project contain the levels. If your not following me here is an example
if (map = 1)
{
map stuff here
}
if (map = 2)
{
map stuff here
}
then in my main section of the game I have to commands if you press "n" it will raise the variable by 1 and if you press "south" it will lower the variable by 1. In the main game I have a big do while loop which contains the commands you can do and it also calls on function gamemap() which will display the map. The whole code for the project can be viewed here.
Code:
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
using namespace std;
//global vairables
int map = 0; // sets the map variabile to 0
int rand_0toN1(int n);
// makes a function to randomise
// this creates the function for the combat
int combat()
{
int hitpoints;
int meanzpoints;
int n;
int i;
do { // starts the do while loop
for (i=1; 1<=n; i++) { // starts the for loop
srand(time(NULL)); // set a seed for a random num then generate it
// causes npc damage, this is generates from 1-10
meanzpoints = rand_0toN1(10) + 1;
cout << "you hurt meanz for " << meanzpoints << " " <<"hitpoints\n";
//causes player damage, this is generates from 1-10
hitpoints = rand_0toN1(10) + 1;
cout << "meanz hurts you for " << hitpoints << " " <<"hitpoints\n";
if (meanzpoints >= 4 )
{ //begins the if npc died condition
cout << "you have killed the evil meanz\n";
i++;
break;
} //end the if npc condition
else if (hitpoints >= 4 )
{//begins the if player died conditon
cout << "you have died\n";
i++;
break;
}//ends the if player died conditon
}// ends the for loop
} while ((meanzpoints < 0 ) or (hitpoints < 0)); // ends the do while loop
} // ends the function
int gamemap()
{ // make the map function
//cheak to make sure you dont go below map 1
if (map > 1)
{
cout << "you cant go south\n";
}
// map 1
if (map = 1)
{
cout << "this is map 1\n";
}
// map 2
if (map = 2)
{
cout << "this is map 2\n";
}
// map 3
if (map = 3)
{
cout << "this is map 3\n";
}
// map 4
if (map = 4)
{
cout << "this is map 4\n";
}
//cheak to make sure you dont go above map 4
if (map > 4)
{
cout << "you cant go north\n" ;
}
}// ends the map function
/////////////////////////////////////////////////////////////////////////////
///////////////////Main Game Loop////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
int main()
{ // starts the main game loop
char name[11];
char welcome[100];
char dir[2];
int n;
int i;
int x =0;
// this is the welcome message
cout << "enter you name:";
cin.getline(name, 11);
strcpy(welcome, " welcome to my game ");
strcat(welcome, name);
cout << welcome << endl;
//this is the map
cout << "you arrive in a land with out Meanz the first think you notice is how peacefull the lands have become" << endl;
cout << "you see a monster that looks alot like Meanz and you decide to attack it!\n";
// starts combat for hero!
combat();// calls the combat function.
// if meanz was killed continue on to the game if he didn't the game should end.
cout << "you manage to pass the evil meanz monster! you are now free to travel this land as you please ";
// starts the map loop the most important part of the program
do
{
cout << "type'n' to go north and 's' to go south!\n";
fgets( dir, 2, stdin );
scanf( "%s", dir );
if (dir[0] == 'n')
{
map + 1;
}
if (dir[0] == 's')
{
map - 1;
}
if (dir[0] == 'e')
{
break;
}
//load the map
gamemap();
} while (x < 1);
// this is the message to show the program has ended
End:
cout << "press any key to end the program:";
getchar();
return 0;
}// ends the main game loop
/////////////////////////////////////////////////////////////////////////////
//////////////////////////Game functions/////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// creates the random number function usefull for combat and items like fishing, woodcutting etc
int rand_0toN1(int n) {
return rand() % n;
}
The problem with the code is when you press n or south it will not print out what the map you are on contains it prints out the information for all the maps. e.g.
if I press north and go to room 1 it should print out the message "this is map 1" but instead it prints out the message;
this is map 1
this is map 2
this is map 3
this is map 4
any help on this subject would really be helpful, also if you spot me doing anything wrong and slow with the code please feel free to point out, I would like it very much if you didn't just copy the code make the changes and print it on the forum as I will learn nothing from it, I would rather prefer a small hint then a huge block of code posted.
thanks very much
zidsal