GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Sep-2004, 08:09
manu83 manu83 is offline
New Member
 
Join Date: Sep 2004
Posts: 9
manu83 is on a distinguished road

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.

CPP / C++ / C Code:
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;
}
  #2  
Old 14-Sep-2004, 09:09
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
CPP / C++ / C Code:
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';
	}
}

I don't get this code. If you want the player to move up, why do you work with posX ? Shouldn't this be posY?

Regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 14-Sep-2004, 09:31
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by manu83
CPP / C++ / C Code:
void getPlayerPosition(char board[Maxrow][Maxcol], Player& player)
{

player.posX = 17;
player.posY = 11;
}
You just need the position of player, right? Well, then, just pass to this function Player& player and the addresses of 2 int variables in which to store the values:

CPP / C++ / C Code:
void getPlayerPosition(Player& player, int * posX, int * posY)
{
	*posX = player.posX;
	*posY = player.posY;
}

Or maybe you want (really bad ) to go through the whole array? No problem:

CPP / C++ / C Code:
void getPlayerPosition(char board[Maxrow][Maxcol], int * posX, int * posY)
{
	int i, j;
	bool bFound = false;
	for (i = 0; i < Maxrow; i++)
	{
		for(j = 0; j < Maxcol; j++)
		{
			if (  board[i][j] == 'P')
			{
				bFound = true;
			}
		}
		if ( bFound )
		{
			break;
		}
	}
	if ( bFound )
	{
		*posX = i;
		*posY = j;
	}
	else
	{
		*posX = -1; //     Or whatever suits you in case
		*posY = -1; //you didn't find what you were looking for
	}
}

Regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Formatting C / C++ code WaltP C Programming Language 1 07-Jan-2008 00:59
Thoughts on Software Piracy pcxgamer Computer Software Forum - Games 9 23-Apr-2004 07:36
Trying to create the game of life warny_maelstrom C Programming Language 10 21-Jan-2004 22:14
Tips for game troubleshooting pcxgamer Computer Software Forum - Games 0 02-Jan-2004 06:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:00.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.