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 :D
Thanks.