Help for errors.



#include <string>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <conio.h>
#include <time.h>




using namespace std;

const int KEY_LEFT = 'a', KEY_RIGHT = 'd',KEY_UP = 'w', KEY_DOWN ='s';
const int HEIGHT =25, WIDTH = 40;
bool hasntWon = true;
int hP = 100;


enum DirectionX {Right,Left};
enum DirectionY {Up,Down};

struct Player {
char sprite;
int x;
int y;
};

struct Enemy{
char sprite;
int x;
int y;
DirectionX pastDirX;
DirectionY pastDirY;
};


unsigned char MoveEnemy(Enemy& enemy, unsigned char maze[HEIGHT][WIDTH], int i , int j , string& smove, bool& nc)
{
if (nc){

bool psbUp = false;
bool psbDown = false;
bool psbRight = false;
bool psbLeft = false;


if (maze[enemy.y][enemy.x+1] != '#' ){
psbRight = true;
}
if (maze[enemy.y][enemy.x-1] != '#' ){
psbLeft = true;
}
if (maze[enemy.y+1][enemy.x] != '#' ){
psbLeft = true;
}
if (maze[enemy.y-1][enemy.x] != '#') {
psbLeft = true;
}


enum Mv {up,right,down,left,NA};
Mv move = NA;


if (smove =="up"){
move = up;
}
else if (smove =="right"){
move = right;
}
else if (smove =="down"){
move = down;
}
else if (smove =="left"){
move = left;
}
else {
move = NA;
}


if (enemy.pastDirX == Right && !psbRight){
if(enemy.pastDirY == Up){
if (psbUp){
move = up;
}
else if (psbDown && move != up){
move = down;
}
else if (psbLeft){
move = left;
}
else {
move = down;
}
}
else{
if(psbDown){
move = down ;
}
else if(psbUp && move != down){
move = up;
}
else if (psbLeft){
move = left;
}
else{
move = up;
}

}

}
else if (enemy.pastDirX == Right && psbRight){
move = right;
}
else if (enemy.pastDirX == Left && !psbLeft){
if(enemy.pastDirY == Up) {
if (psbUp){
move = up;
}
else if(psbDown && move != up){
move = down;
}
else if (psbRight){
move = right;
}
else {
move = down;
}
}
else {
if(psbDown){
move = down;
}
else if (psbUp && move != down){
move = up;
}
else if (psbRight){
move = right;
}
else {
move = up;
}
}
}
else if (enemy.pastDirX == Left && psbLeft){
move = left;
}
else if(enemy.pastDirY == Up && !psbUp){
if (enemy.pastDirX == Right){
if (psbRight){
move = right;
}
else if (psbLeft && move != right){
move = left;
}
else if (psbDown){
move = down;
}
else {
move = left;
}
}

else {
if (psbLeft ){
move = left;
}
else if (psbRight && move != left){
move = right;
}
else if (psbDown){
move = down;
}
else {
move = right;
}

}
}

else if (enemy.pastDirY == Up && psbUp){
move = up;
}
else if (enemy.pastDirY == Down && !psbDown){
if (enemy.pastDirX == Right){
if (psbRight){
move = right;
}
else if (psbLeft && move != right){
move = left;
}
else if (psbUp){
move = up;
}
else {
move = left;
}
}
else {
if (psbLeft){
move = left;
}
else if (psbRight && move != left ){
move = right ;
}
else if (psbRight){
move = up;
}
else {
move = right ;
}
}
}
else if (enemy.pastDirY == Down && psbDown){
move = down;
}



switch (move) {
case up:
maze[enemy.y][enemy.x] = ' ';
enemy.pastDirY = Up;
smove = "up";
enemy.y--;
break ;
case right:
maze[enemy.y][enemy.x] = ' ';
enemy.pastDirX = Right;
smove = "right";
enemy.x++;
break ;
case down:
maze[enemy.y][enemy.x] = ' ';
enemy.pastDirY = Down;
smove = "down";
enemy.y++;
break ;
case left:
maze[enemy.y][enemy.x] = ' ';
enemy.pastDirX = Left;
smove = "left";
enemy.x--;
break ;
case NA:
smove = "NA";
break;
}
}

nc = false;
return maze[i][j];
}



int main()
{
int x,y,h,i,j;
srand(time(NULL));

unsigned char maze [HEIGHT][WIDTH]={{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
{' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ',' ',' ','#','#',' ','#',' ',' ',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ','#'},
{'#','#','#','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ',' ','#',' ','#','#','#',' ','#','#','#',' ','#',' ','#','#','#',' ','#',' ','#',' ','#','#',' ','#'},
{'#',' ',' ',' ',' ','#',' ',' ',' ','#',' ','#',' ','#',' ','#','#',' ',' ',' ','#',' ',' ',' ',' ',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ',' ','#'},
{'#',' ','#','#',' ','#','#','#',' ','#',' ','#',' ','#',' ',' ',' ',' ','#',' ','#','#','#','#','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ',' ','#'},
{'#',' ','#',' ',' ','#',' ','#',' ','#',' ','#',' ','#','#','#','#','#','#',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ',' ','#',' ','#',' ',' ','#'},
{'#',' ','#',' ','#','#',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#','#','#',' ','#','#','#','#','#','#','#','#','#',' ','#',' ','#','#',' ','#'},
{'#',' ','#',' ','#',' ',' ','#',' ','#','#','#','#',' ','#','#','#',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ','#',' ','#','#',' ','#',' ','#',' ',' ',' ',' ','#',' ',' ',' ','#','#','#','#','#',' ','#',' ','#',' ','#',' ','#','#',' ','#','#',' ','#',' ','#','#'},
{'#',' ','#',' ',' ',' ',' ','#',' ','#','#','#','#',' ','#','#','#','#','#',' ',' ',' ','#',' ','#',' ','#',' ','#',' ','#','#',' ',' ','#',' ','#',' ',' ','#'},
{'#',' ','#','#','#','#',' ','#',' ',' ',' ',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ',' ',' ','#',' ','#',' ',' ','#',' ','#','#','#','#','#',' ','#'},
{'#',' ',' ',' ',' ','#',' ','#',' ','#','#','#','#',' ','#',' ','#',' ','#',' ','#',' ','#','#','#',' ','#',' ','#','#','#','#',' ',' ',' ','#',' ','#',' ','#'},
{'#',' ','#','#',' ',' ',' ','#',' ',' ',' ',' ','#',' ','#',' ','#',' ','#',' ',' ',' ',' ',' ','#',' ','#',' ',' ',' ',' ','#',' ','#',' ','#',' ',' ',' ','#'},
{'#',' ','#',' ',' ','#',' ','#',' ','#','#',' ','#',' ','#',' ','#',' ','#','#','#','#','#',' ','#',' ','#','#','#','#',' ','#',' ','#',' ','#','#','#','#','#'},
{'#',' ','#',' ','#','#',' ','#',' ','#',' ',' ',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ',' ',' #',' ','#',' ','#',' ',' ',' ','#',' ','#'},
{'#',' ','#',' ','#',' ',' ','#',' ','#','#','#','#','#','#',' ','#',' ','#','#',' ','#','#','#','#','#','#','#',' ','#',' ','#',' ',' ',' ','#',' ','#',' ','#'},
{'#',' ','#',' ','#',' ','#','#',' ','#',' ',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ','#','#','#','#','#',' ','#',' ','#'},
{'#',' ','#',' ','#',' ',' ','#',' ','#',' ','#','#','#','#','#','#','#',' ','#',' ','#','#','#','#','#',' ','#',' ','#',' ',' ',' ','#',' ','#',' ',' ',' ','#'},
{'#',' ','#','#','#','#','#','#',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ','#',' ',' ',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#','#','#','#','#'},
{'#',' ',' ',' ',' ','#',' ',' ',' ','#',' ','#',' ','#','#','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ',' ',' ','#',' ','#',' ','#',' ',' ',' ',' ',' ','#'},
{'#',' ','#','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#','#','#','#','#',' ',' ',' ','#',' ','#','#','#',' ','#'},
{'#',' ',' ','#',' ','#',' ','#',' ','#',' ','#',' ',' ',' ','#',' ','#',' ','#',' ','#',' ','#',' ',' ',' ',' ',' ','#','#','#',' ','#',' ','#',' ','#',' ','#'},
{'#',' ','#','#',' ','#',' ','#','#','#',' ','#','#','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#','#','#',' ','#',' ',' ',' ','#',' ','#',' ','#',' ','#'},
{'#',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#',' ','#',' ','#',' ',' ',' ',' ',' ','#',' ',' '},
{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
};


struct Player player;
player.sprite ='@';
player.x = 0;
player.y = 1;






string prevmove1 = "NA";
string prevmove2 = "NA";



while(hasntWon){


if (maze[player.y][player.x] == ' '){
maze[player.y][player.x] = player.sprite;
}



for (y=0;y<HEIGHT ; y++){
cout <<endl;
for (x = 0; x<WIDTH ; x++){
cout << maze[y][x];
}
}



char key = getch();

switch(key){
case KEY_LEFT :
if (maze[player.y][player.x-1] != '#'){
maze[player.y][player.x] = ' ';

player.x--;
}
break;
case KEY_UP :
if (maze[player.y-1][player.x] != '#'){
maze[player.y][player.x] = ' ';

player.y--;
}
break;
case KEY_DOWN :
if (maze[player.y+1][player.x] != '#'){
maze[player.y][player.x] = ' ';

player.y++;
}
break;
case KEY_RIGHT :
if (maze[player.y][player.x+1] != '#'){
maze[player.y][player.x] = ' ';

player.x++;
}
break;
case 'q':
exit(0);
break;
}

bool once1 = true;
bool once2 = true;
bool once3 = true;
bool once4 = true;


if (player.x == WIDTH -1){
cout <<endl<<endl<<"You have found the way out of the maze!" << endl<<endl;
system("PAUSE");
hasntWon = false;
}

system("CLS");
}

}

I need to complete this game programming but I couldn't do it. Please help for errors.
You need to check your array boundaries.
if (maze[player.y][player.x - 1] != '#') {
At the beginning palyer.y is 1 and player.x is 0 so you try to read maze[1][-1]
Topic archived. No new replies allowed.