Thread: 2D array problem (within a class) Please help!

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    7

    2D array problem (within a class) Please help!

    Hi I was wondering if anyone can help me out on this little issue to make my program function, i've been stuck for days spending hours staring at the screen (SO FRUSTRATING).

    Basically the only thing i need help is, I need my character represented by the '/2' symbol to move.. for some reason he keeps resetting at the end of the loop. I did it in procedural programming (in a main.cpp file) and it worked perfectly i tried to put it in a class and it keeps resetting, I'll post the code of the class below and can someone identify the problem for me please or at least how to fix it because I've spent days on this problem?


    Code:
    void Easygame::drawmap(){
        const char HEIGHT=15; // these are the constants for the map dimensions!
        const char WIDTH=10;
    
        int moves_remaining=100;
        int playerxpos=2;
        int playerypos=2;
        lives=3;    
        char player='\1'; //creating the character player smiley face using ascii code /1 and <stdio.h> library
    
    
        system( "color 5A" ); // I thought I'd liven up the game by adding a splash of weird colours :) using the <stdlib.h> library
    
        cout << "YOU ARE PLAYING SUPAH EASY MODE" << endl;
        cout << endl;
        cout << "YOU ONLY HAVE "<< lives << " LIVES REMAINING!!" << endl;
    
    
    
        //declaring array
        unsigned char gamemap1[WIDTH][HEIGHT]={  // unsigned chars will be useful if i need more memory and because i know none of the values will be negative :) also this makes my code more effecient
        'W','W','W','W','W','W','W','W','W','W','W','W','W','W','W',
        'W',' ',' ',' ',' ',' ','W',' ',' ',' ',' ',' ',' ',' ','W',
        'W',' ',' ',' ',' ',' ','W','W','W',' ',' ',' ','W',' ','W',
        'W',' ',' ',' ',' ',' ','W',' ','W',' ','W',' ','W',' ','W',
        'W',' ',' ',' ',' ',' ',' ',' ',' ',' ','W',' ','W',' ','W',
        'W','W','W','W','W','W','W',' ','W',' ','W',' ','W',' ','W',
        'W',' ',' ',' ','W','W',' ',' ','W',' ','W',' ','W',' ','W',
        'W',' ','W','W','W',' ',' ','W','W',' ','W',' ','W',' ','W',
        'W',' ',' ',' ',' ',' ',' ',' ',' ',' ','W',' ',' ',' ','W',
        'W','W','W','W','W','W','W','W','W','W','W','W','W','W','W'
        };
    
        //Exit positions
        goal exit1;
        exit1.face='X';
        exit1.x=13;
        exit1.y=8;
    
        //enemys Structures!!!!
        enemys animal1; //animal is sooooo much easier to spell then elephant so it prevents a lot of mistakes
        animal1.face='\2';
        animal1.x=5;
        animal1.y=4;
    
        enemys animal2;
        animal2.face='\2';
        animal2.x=9;
        animal2.y=6;
    
    
        //putting stuff on the map.
        //putting the player in the map by using his starting position and assigning it to that position on the map!
        gamemap1[playerypos][playerxpos]=player;
    
        //putting mummys on map!!!
        gamemap1[animal1.y][animal1.x]=animal1.face; // assigning mummys position to the picture... which is another reason i used char.
        gamemap1[animal2.y][animal2.x]=animal2.face;
    
        //Creating the exit for the face!
        gamemap1[exit1.y][exit1.x]=exit1.face;
    
        //forloop for outputting array
        for(int col=0; col<WIDTH; col++){
            for(int row=0; row<HEIGHT; row++){        
                cout << gamemap1[col][row];
            }
            cout << endl;
        }
    
            //loop containing nested loop
        do{
            while(moves_remaining>0){
                cout << endl;
                cout << "The amount of moves you have remaining are: "<< moves_remaining << endl;            
                
                if(gamemap1[playerypos][playerxpos]!=(gamemap1[animal1.y][animal1.x]||gamemap1[animal2.y][animal2.x])){
                        lives--;
                    }
                
                
                cout << endl;
                cout << "Enter Command: ";
                char command=getch(); // set this for switch and allowing use have better WASD movement by assigning it to getch(); which uses the conio.h library.
            
                        switch(command){ //this is used instead of lots of if statements.
                            case 'w': //using the wasd keys and corresponding increments of the x and y position to move the player.
                            case 'W': //WASD has been used in switch so that captial or lowercase is both acceptable.
                                cout<< "W" << endl;
                                Sleep(195);//creates a time delay in ms, which player can see the key entered.
                                if(gamemap1[playerypos-1][playerxpos] !='W'){ //using an if statement and the != operator blocks user from passing through walls
                                        playerypos--;
                                }
                                break;
                            case 'a':
                            case 'A':
                                cout<< "A" << endl;
                                Sleep(150);
                                if(gamemap1[playerypos][playerxpos-1] !='W'){ //this is for wall
                                        playerxpos--;
                                }
                                break;
                            case 's':
                            case 'S':
                                cout<< "S" << endl;
                                Sleep(150);
                                if(gamemap1[playerypos+1][playerxpos] !='W'){
                                        playerypos++;
                                }
                                break;
                            case 'd':
                            case 'D':
                                cout<< "D" << endl;
                                Sleep(150);
                                if(gamemap1[playerypos][playerxpos+1] !='W'){
                                        playerxpos++;
                                }
                                break;
                            default: 
                            cout <<endl;
                            cout << "You have entered an invalid character please try again!!" << endl;
                            Sleep(2500); // this creates a time delay to show the message after a user input
                            break;
                        };    
                
                
                moves_remaining--;
                system("cls");
                    //forloop for outputting array
                    for(int col=0; col<WIDTH; col++){
                        for(int row=0; row<HEIGHT; row++){        
                            cout << gamemap1[col][row];
            }
            cout << endl;
        }
            };
    
            gameover();
    
        }while(restart==true);
    
    
    
    
    }

    This is within a class i created called easy game...

    The libraries and everything are fine it's just that the playerypos and xpos keep getting reset within the class..

    Any help/tips would be appreciated greatly as I am stumped *sigh*.

    Constructive criticism that's not related to the question is kewl too haha

    Thanks.
    Last edited by grasshopper101; 08-19-2013 at 08:07 AM.

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    OOHHH I'll try to explain it in a better question, within the switch case (which is within the loop) how do i modify playerxpos and playerypos so it loops again it will use the previously modified values instead of resetting playerxpos and playerypos

    ... I tried to use a pointer but failed miserably because remember this is within a class so it makes it more difficult :/

    I hope that cleared things up for anyone reading

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your player position variables aren't resetting. Your problem is that you're not updating the position of your character, just the position variables. You need to remove the character from it's previous position (before the position is updated) and then place it at it's new position (after the update).

    Another thing: this is probably not doing what you want:
    Code:
    if(gamemap1[playerypos][playerxpos]!=(gamemap1[animal1.y][animal1.x]||gamemap1[animal2.y][animal2.x])){
        lives--;
    }
    



    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Quote Originally Posted by oogabooga View Post
    Your player position variables aren't resetting. Your problem is that you're not updating the position of your character, just the position variables. You need to remove the character from it's previous position (before the position is updated) and then place it at it's new position (after the update).

    Another thing: this is probably not doing what you want:
    Code:
    if(gamemap1[playerypos][playerxpos]!=(gamemap1[animal1.y][animal1.x]||gamemap1[animal2.y][animal2.x])){
        lives--;
    }
    



    Hahaha thanks for the analysis dude.. But after 7 hours and days of staring at the screen I decided to take it out of the loop, figure out how to use a pointer to change the variable of the position, put it back into a for loop and baym! It worked, but Thanks for looking at it dude.. I really do appreciate that you took time out of your day to look at it!

    I'm just excited that i managed to figure it out by myself.

    Thanks again.


    /closethreadplease

  5. #5
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by grasshopper101 View Post
    /closethreadplease
    I don't think threads are closed per se here, and it wouldn't be a good idea anyway. You never know whether you might have missed something that someone else might pick up a few weeks down the road.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with loading array of class objects
    By McElmurry in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2010, 03:34 AM
  2. Replies: 3
    Last Post: 07-15-2010, 07:53 PM
  3. Array of Base(Abstract) Class Problem
    By Aramil in forum C++ Programming
    Replies: 10
    Last Post: 03-14-2008, 02:58 PM
  4. class problem (init. struct, itierator for 2d array)
    By avgprogamerjoe in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2007, 08:00 PM
  5. Problem with setting a character array in a class member.
    By swbluto in forum C++ Programming
    Replies: 4
    Last Post: 12-15-2006, 02:41 AM