|
help me with this Maze game code
hi people! i've been coding this for the last 3 days, and the codes compiles and runs, but when i have to move the player in the array the program player moves to any direction (like random lol) i have to make it move the right direction , and also i need some help with the getplayerposition() , dont know how if make a seqsearch or two nested fors and check, any help will be apreciated.
const int Maxrow = 45;
const int Maxcol = 17;
struct Player
{
int posX, posY;
};
const int MAX_SIZE = 4;
struct Monster
{
string name;
int PosX, posY;
};
enum Status {CONTINUE, WON, TOUCHED_MONS};
enum MonsPos {NORTH, NEAST, EAST , SEAST, SOUTH, SWEST, WEST, NWEST};
// Prototypes
void init(ifstream& mapFile ,char board[Maxrow][Maxcol]);
void show(const char board[Maxrow][Maxcol]);
char getSelection();
void processSelection(char selection, char board[Maxrow][Maxcol],
Status& gameStatus, Player& player);
void moveUp(char board[Maxrow][Maxcol], Status& gameStatus, Player& player);
void moveDown(char board[Maxrow][Maxcol], Status& gameStatus, Player& player);
void moveLeft(char board[Maxrow][Maxcol], Status& gameStatus, Player& player);
void moveRight(char board[Maxrow][Maxcol], Status& gameStatus, Player& player);
void getPlayerPosition(char board[Maxrow][Maxcol], Player& player);
int main()
{
ifstream fileMap;
string mapLocation;
Player jugador;
char menuSelection;
char gameBoard[Maxrow][Maxcol];
int row = 0, column = 0;
int jugXpos, jugYpos;
Status gameStatus = CONTINUE;
cout << "Entre la localizacion del archivo" <<endl;
cin >> mapLocation;
fileMap.open("c:/map.txt");
init(fileMap ,gameBoard);
getPlayerPosition(gameBoard, jugador);
do
{
show(gameBoard);
menuSelection = getSelection();
processSelection(menuSelection, gameBoard,
gameStatus, jugador);
} while (gameStatus == CONTINUE);
system("cls");
if (gameStatus == WON)
cout << "You WON!" << endl;
else if (gameStatus == TOUCHED_MONS)
cout << "You LOST! You had touched a monster" << endl;
system("pause");
return 0;
}
void init(ifstream& mapFile ,char board[Maxrow][Maxcol])
// *********************************************************************
// *********************************************************************
{
for (int m=0; m<Maxrow; m++)
for (int n=0; n<Maxcol; n++)
mapFile.get(board[m][n]);
}
void show(const char board[Maxrow][Maxcol])
// *********************************************************************
// *********************************************************************
{
system("cls");
for (int m=0; m<Maxrow; m++)
for (int n=0; n<Maxcol; n++)
cout << board[m][n];
}
char getSelection()
// *********************************************************************
// *********************************************************************
{
char selection;
cout << endl << "(U)p, (D)own, (L)eft, (R)ight" << endl;
cout << "Selection: ?";
cin >> selection;
return toupper(selection);
}
void processSelection(char selection, char board[Maxrow][Maxcol],
Status& gameStatus, Player& player)
// *********************************************************************
// *********************************************************************
{
switch (selection)
{
case 'U' : moveUp(board, gameStatus, player);
break;
case 'D' : moveDown(board, gameStatus, player);
break;
case 'L' : moveLeft(board, gameStatus, player);
break;
case 'R' : moveRight(board, gameStatus, player);
break;
default : cout << "Wrong choice!" << endl;
system("pause");
}
}
void moveUp(char board[Maxrow][Maxcol], Status& gameStatus, Player& player)
// *********************************************************************
//
//
//
// *********************************************************************
{
board[player.posX ][player.posY] = ' ';
player.posX--;
if (board[player.posX - 1][player.posY] == '*')
cout <<"oops! una pared" <<endl;
else if (player.posX - 1 < 0)
gameStatus = WON;
else
{
board[player.posX][player.posY] = 'P';
}
}
void moveDown(char board[Maxrow][Maxcol], Status& gameStatus, Player& player)
// *********************************************************************
//
//
// ***************************************************************
{
board[player.posX][player.posY] = '-';
player.posX++;
if (player.posX + 1 == Maxrow)
gameStatus = WON;
else if (board[player.posX + 1][player.posY] == '*')
cout <<"oops! una pared" <<endl;
else
{
board[player.posX][player.posY] = 'P';
}
}
void moveLeft(char board[Maxrow][Maxcol], Status& gameStatus, Player& player)
// *********************************************************************
// nesecito saber como mover el jugador en el tablero
//
// *********************************************************************
{
board[player.posX][player.posY] = ' ';
player.posY--;
if (player.posX - 1 < 0)
gameStatus = WON;
else if (board[player.posX][player.posY - 1] == '*')
cout <<"oops una pared" << endl;
else
{
board[player.posX][player.posY] = 'P';
}
}
void moveRight(char board[Maxrow][Maxcol], Status& gameStatus, Player& player)
// *********************************************************************
// *********************************************************************
{
board[player.posX][player.posY] = '-';
player.posX++;
if (player.posY + 1 == Maxcol)
gameStatus = WON;
else if (board[player.posX][player.posY + 1] == '*')
cout <<"oops una pared" << endl;
else
{
board[player.posX][player.posY] = 'P';
}
}
void getPlayerPosition(char board[Maxrow][Maxcol], Player& player)
{
player.posX = 17;
player.posY = 11;
}
|