Weird Bug with beginner dougeon crawler challenge

Hey guiys I've been learning to programme for a couple of weeks now and ive been working on the beginner challenges and I have a strange bug with the dungeon crawler exercise.

basically when I go to move the palyer out of bounds it oon the x axis its meant to reposition the player icon to the 0,0 position on the array but it doesnt it just randomly puts it elsewhere any suggestions to why this happens?


#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstdio>
using namespace std;



//variables
char board[10][10] = {{'.','.','.','.','.','.','.','.','.','.'},{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},{'.','.','.','.','.','.','.','.','.','.'},};

char player = 'P';
int playerPosX = 0;
int playerPosY = 0;
char playerMove;
int turn = 0;
int win = 0;
int gx = 0;
int gy = 0;

//functions
void drawBoard();
void setGoal();
void clrScreen();
void winCond();
void outOfBound();

//classes
class playerFunctions{
public:
void playerPos()
{
board[playerPosX][playerPosY] = player;
}


void playerMovement()
{


if( playerMove == 'w' || playerMove == 'W'){
playerPosX--;
board[playerPosX+1][playerPosY] = '.';
}else if( playerMove == 's' || playerMove == 'S'){
playerPosX++;
board[playerPosX-1][playerPosY] = '.';
}else if( playerMove == 'a' || playerMove == 'A'){
playerPosY--;
board[playerPosX][playerPosY+1] = '.';
}else if( playerMove == 'd' || playerMove == 'D'){
playerPosY++;
board[playerPosX][playerPosY-1] = '.';
}else{ cout << "that is not a valid selection" <<endl;
}

}
};


int main()
{

setGoal();

cout << "Welcome to the doungeon" << "\n\n";
cout << "The 'P' represents your position in the doungeon" <<endl;
cout << "The dots are blank spaces and the 'X' is your goal" << endl;
cout << "Use W A S D to move" << "\n\n";
playerFunctions plyob;
plyob.playerPos();
drawBoard();

while(win != 1){
cout << "\n\n" << "Which way would you like to move?" << endl;
cin >> playerMove;
plyob.playerMovement();
plyob.playerPos();
outOfBound();
plyob.playerPos();
clrScreen();
drawBoard();
winCond();
};
system("pause");
return 0;
}



void drawBoard()
{
for(int row = 0; row < 10; row++){
for(int col = 0; col < 10; col++){
cout << board[row][col] << " ";
}
cout <<endl;
}
}

void setGoal()
{

//set goal cords
srand(time(0));
gx = rand()% 10;
gy = rand()% 10;
board[gx][gy] = 'X';

}

void clrScreen()
{
for(int x = 0; x<20; x++){
cout << "\n";
}
}
//sets win when player is on X
void winCond()
{

if(playerPosX == gx && playerPosY == gy){
win = 1;
cout << "\nCongratulations you have won!" <<endl;
}
}
//Makes you lose if you go out of bounds
void outOfBound()
{
if(playerPosX < 0 || playerPosX > 9 || playerPosY < 0 || playerPosY > 9){
playerPosX = 0;
playerPosY = 0;
}
}

Hi,
Please use code tags, you will get more replies because it makes the code easier to read, does line numbers, indenting and keywords.

Hint: use the <> button on the format menu.

http://www.cplusplus.com/articles/z13hAqkS/


If you have compiler errors, then post them here verbatim, make sure the line numbers match with the code you posted.

However it looks like you have a runtime error.

The check for out of bounds needs to be made before movement on the board, otherwise the array will be accessed out of bounds.

Your class has very little in it, You could have a lot more there - like the move function for example.

Avoid global variables, they are bad unless you know what you are doing.

You can make use of the toupper function to halve the logic in your tests:

if( playerMove == 'W'){

Good Luck !!
Topic archived. No new replies allowed.