switching between levels/maps in a text based adventure game

This is a discussion on switching between levels/maps in a text based adventure game within the C++ Programming forums, part of the General Programming Boards category; Well, I have been self learning c++ for a while now and I decided to try and make a text ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    17

    switching between levels/maps in a text based adventure game

    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

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Code:
    if(map == 4){
    
    // code
    
    }
    
    // etc
    You are assigning values and checking if it succeeded. What you need to do is compare values.

    Assign value: =
    Compare values ==
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    17
    thanks very much, I feel stupid as the solution was really simple, I am getting a error again now I will edit this post if I can't fix it with in a hour or so.

    thanks again
    zidsal

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Well the first thing you need to do is decide whether you're writing in C or C++.

    Then have a look at this.
    A begginer writing a text based dungeon game.
    The point of this post (which the OP of the thread failed to grasp) is to separate the code and data. Notice how the code itself doesn't explicitly know anything about say moving "east". It's just a link from one element in a data structure to another element in a data structure.

    Rooms can be expanded to include
    - short and long descriptions
    - objects to pick up
    - objects which are initially hidden (for some reason)
    - NPCs (of various hostilities)
    - doors to other levels, as well as doors to other rooms.

    By putting all the complexity in the data, you simplify the code. Adding more rooms or more levels for example should have NO effect on your code if you've done a good job.

    if ( room == 1 && monster == 2 && weapon == 3 )
    is a sure sign of something going wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    21
    Code:
    // 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";
    }

    Code:
    cout <<"this is map " << map << endl;
    Then you can reuse it when needed by putting it in a function etc
    Last edited by cakey; 01-28-2008 at 01:33 PM.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    All your ifs will be true, because you use = instead of ==
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    One word: indent.
    I really hope you've done that and are just posting un-indented code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text adventure game GUI
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 11-07-2007, 05:34 PM
  2. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  3. OpenGL - 2d text in 3d game
    By mikeb1986 in forum C++ Programming
    Replies: 1
    Last Post: 03-22-2006, 12:24 PM
  4. Text based game..
    By MipZhaP in forum C++ Programming
    Replies: 8
    Last Post: 02-28-2005, 07:35 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21