2D Array Maze Game

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 9648 Views - Last Post: 15 April 2014 - 09:08 AM Rate Topic: -----

#1 ThePie69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

2D Array Maze Game

Posted 06 December 2012 - 06:56 PM

I have a maze game program that i'm working on here. New to the forums also just started all this and I'm stuck in the movement part where the void functions are. I no that the voids are getting the x and y passed by reference but I don;t really understand that part or how to work it. If you can fill in the missing spots or give hints or anything that will be great, and thank you.

P.S. - I'm doing this in Visual studios 2010 so it might help to copy/paste code into there.


#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

//declare array for game board and fill it
     char gameBoard[20][20] =
     { {'-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-', '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-'}
     , {'|' , '#' , '#' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#', '#' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '-' , '-' , '-' , '-', '-' , '-' , '#' , '-' , '-' , '-' , '-' , '-' , '|'}
     , {'|' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '#' , '#' , '#', '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , 'O' , '|' , '#' , '|' , '-' , '-' , '#' , '|' , '-' , '|', '#' , '|' , '-' , '-' , '-' , '-' , '-' , '#' , '|'}
     , {'|' , '-' , '-' , '-' , '|' , '#' , '|' , '#' , '|' , '#' , '|', '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '#' , '#' , '|' , '#' , '|' , '#' , '#' , '#' , '|', '#' , '|' , '#' , '-' , '-' , '-' , '-' , '-' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '-' , '#' , '|' , '-', '#' , '|' , '#' , '|' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '#' , '#' , '|' , '#', '#' , '|' , '#' , '#' , '#' , '-' , '-' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '#' , '-' , '|' , '#', '#' , '|' , '-' , '-' , '-' , '|' , '|' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '#' , '|' , '#', '#' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '-' , '-' , '-' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '-' , '-' , '|' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '#' , '#' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '|' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '-' , '-' , '|' , '#' , '|' , '-' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '-' , '-' , '#' , '-' , '|'}
     , {'|' , '#' , '#' , '#' , '|' , '#' , '|' , '#' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '#' , '#' , '|' , '#', '|' , '#' , '|' , '-' , '-' , '-' , '-' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '#' , '|' , '#', '|' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '-' , '-' , '-' , '-' , '-' , '#', '|' , '#' , '|' , '-' , '-' , '-' , '-' , '#' , '|'}
     , {'|' , 'X' , '|' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#', '|' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-', '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-'}};

void up(int &, int &); // pass the players position to all 
void down(int &, int &); // functions by reference
void left(int &, int &);
void right(int &, int &);

int main()
{
	int playerX = 1; // variable to hold players x coord remember higher numbers move right
	int playerY = 18; // variable to hold players y coord remember higher numbers move down
	bool game = false; // bool to hold tell when the game is over

	// at start of program clear the screen and output the map
	system("cls"); //clear screen
	for(int i=0; i<20; i++) // start display map
	{
		for(int j=0; j<20; j++)
		{
			cout << gameBoard[i][j];
		}

		cout << endl;
	}  // end display map

	// You need to create a main game loop, anything that your game needs to do needs to take place
	// inside this loop
	// hint: You can use a do while loop, and it needs to include your display of the board, 
	// a prompt to the user, a function call and a check to see if the game is won
	do
	{
    // inside the loop clear the screen and display the map again to start
	system("cls");
	for(int i=0; i<20; i++)
	{
		for(int j=0; j<20; j++)
		{
			cout << gameBoard[i][j];
		}

		cout << endl;
	}
    
    // After the board is displayed tell the player their current location and ask which
    // way they want to go and get their input
    cout << "Your current location is (" << playerX << "," << playerY << ")" << endl;
	cout << "Which way to you want to go? (Up = U, Down = D, Left = L, Right = R)" << endl;
	char UserMove;
	cin >> UserMove;

    // Based off the input the user enters call one of the functions to move their player
    // in the desired direction
    UserMove = toupper(UserMove);
	
	if(UserMove == 'U')
	{
		void up();
	}
	else if(UserMove == 'D')
	{
		void down();
	}
	else if(UserMove == 'L')
	{
		void left();
	}
	else if(UserMove == 'R')
	{
		void right();
	}
	else
	{
		cout << "You entered a invailed character, try again" << endl;
		//cin >> UserMove;
	}
	
    // the last thing to do before the loop starts over is check if the player has won
    // ak the player position is the same as the end xy(1,4) or array[4][1]
	if(playerX == 1 && playerY == 4)
	{
		game = true;
	}

	} // end do while loop
	while(game == false);
	// outside the loop make sure to tell the player they won
	system("cls");
	cout << "YOU WIN!!!" << endl;


	system("pause");
	return 0;
} // end main


void up() // function that moves the player in the up direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' or the end 'O' not a wall
	// hint: use an If and check the next space == #

	// If the move is vaild you need to replace the current space with a # and
	// place the players peice into the new square and change their coords
	// hint: vertical movement is controled in the first array 
	
		
	
	// If the move is bad tell the user
	

} // end up()


void down() // function that moves the player in the down direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' not a wall
	// hint: use an If and check the next space == #
	

	// If the move is vaild you need to replace the current space with a # and
	// place the players peice into the new square
	// hint: vertical movement is controled in the first array 



	// If the move is bad tell the user


} // end down()

void left() // function that moves the player in the left direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' not a wall
	// hint: use an If and check the next space == #


	// If the move is vaild you need to replace the current space with a # and
	// place the players peice into the new square
	// hint: horizontal movement is controled in the second array 

   


	// If the move is bad tell the user



} // end left()

void right() // function that moves the player in the right direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' not a wall
    // hint: use an If and check the next space == #



	// If the move is vaild you need to replace the current space with a # and
    // place the players peice into the new square
    // hint: horizontal movement is controled in the second array

   


    // If the move is bad tell the user



} // end right()





Is This A Good Question/Topic? 0
  • +

Replies To: 2D Array Maze Game

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: 2D Array Maze Game

Posted 06 December 2012 - 07:05 PM

Welcome to the forum, ThePie69!

I can't comment on C++, but in C, this:
{'-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-', '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-'}



Is the same as this:

{"--------------------"}




You might the latter a LOT easier to use. ;)
Was This Post Helpful? 0
  • +
  • -

#3 ThePie69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

Re: 2D Array Maze Game

Posted 06 December 2012 - 07:07 PM

Its a array though so each item needs to be sepearate so when the player moves to a # symbol it will then be a X symbol and his old spot be a #. Its just easier with arrays that way.
Was This Post Helpful? 0
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: 2D Array Maze Game

Posted 06 December 2012 - 07:29 PM

View PostThePie69, on 06 December 2012 - 07:07 PM, said:

Its a array though so each item needs to be sepearate so when the player moves to a # symbol it will then be a X symbol and his old spot be a #. Its just easier with arrays that way.


What do you think I posted for you? I'll bet you were thinking of a string literal!

#include <stdio.h>

int main(void) {
   int i,j,k,len;
   char grid[22]={"---------------------"};

   for(i=0;i<20;i++) {
      if(i% 4 == 0)
         grid[i]='X';
   }
   
   printf("%s\n",grid);
   return 0;
}




Take it or leave it, it's your time to waste.
Was This Post Helpful? 0
  • +
  • -

#5 ThePie69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

Re: 2D Array Maze Game

Posted 06 December 2012 - 10:02 PM

Don't take it the wrong way, I did appretate the help, but the way the array is laid out has to stay that way. I can only change the code for this project. What I'm trying to do is code the void up function right now.
Was This Post Helpful? 0
  • +
  • -

#6 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: 2D Array Maze Game

Posted 06 December 2012 - 10:46 PM

View PostThePie69, on 06 December 2012 - 10:02 PM, said:

Don't take it the wrong way, I did appretate the help, but the way the array is laid out has to stay that way. I can only change the code for this project. What I'm trying to do is code the void up function right now.


I didn't mean to come off so snarky. The arrays would be identical unless you changed the size of them, or the number of hyphens in them.

But let's move on.

So what up with up()?
Unless the board is global in scope, this function will need to receive it as a parameter
Also, it will need to have access to the coord or cp (current position) of the player -
x and y (col and row).

void up() // function that moves the player in the up direction
{
    // the first part of this function needs to check if the move is valid
    int or bool either one: isValid = 0; //assume it will be invalid - but it will be tested

    // make sure the space to be moved into is a '#' or the end 'O' not a wall
    
    // hint: use an If and check the next space == #
    if(cp.y - 1 =='#' && cp.y > 0 or cp.y-1=='O' && cp.y>0) {
       isValid=1;
       make the move and update the cp (cp is players current position) Your project may call it 
       "coords".
    }
    else {
       print Move is illegal, try again
    }

       
    // If the move is vaild you need to replace the current space with a # and
    // place the players peice into the new square and change their coords
    // hint: vertical movement is controled in the first array
     
    // If the move is bad tell the user
	     
} // end up()



This post has been edited by Adak: 06 December 2012 - 10:47 PM

Was This Post Helpful? 0
  • +
  • -

#7 ThePie69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

Re: 2D Array Maze Game

Posted 07 December 2012 - 11:07 AM

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

//declare array for game board and fill it
     char gameBoard[20][20] =
     { {'-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-', '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-'}
     , {'|' , '#' , '#' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#', '#' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '-' , '-' , '-' , '-', '-' , '-' , '#' , '-' , '-' , '-' , '-' , '-' , '|'}
     , {'|' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '#' , '#' , '#', '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , 'O' , '|' , '#' , '|' , '-' , '-' , '#' , '|' , '-' , '|', '#' , '|' , '-' , '-' , '-' , '-' , '-' , '#' , '|'}
     , {'|' , '-' , '-' , '-' , '|' , '#' , '|' , '#' , '|' , '#' , '|', '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '#' , '#' , '|' , '#' , '|' , '#' , '#' , '#' , '|', '#' , '|' , '#' , '-' , '-' , '-' , '-' , '-' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '-' , '#' , '|' , '-', '#' , '|' , '#' , '|' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '#' , '#' , '|' , '#', '#' , '|' , '#' , '#' , '#' , '-' , '-' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '#' , '-' , '|' , '#', '#' , '|' , '-' , '-' , '-' , '|' , '|' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '#' , '|' , '#', '#' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '-' , '-' , '-' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '-' , '-' , '|' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '#' , '#' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '|' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '-' , '-' , '|' , '#' , '|' , '-' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '-' , '-' , '#' , '-' , '|'}
     , {'|' , '#' , '#' , '#' , '|' , '#' , '|' , '#' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '#' , '#' , '|' , '#', '|' , '#' , '|' , '-' , '-' , '-' , '-' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '#' , '|' , '#', '|' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '-' , '-' , '-' , '-' , '-' , '#', '|' , '#' , '|' , '-' , '-' , '-' , '-' , '#' , '|'}
     , {'|' , 'X' , '|' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#', '|' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-', '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-'}};

void up(int &, int &); // pass the players position to all 
void down(int &, int &); // functions by reference
void left(int &, int &);
void right(int &, int &);

int main()
{
	int playerX = 1; // variable to hold players x coord remember higher numbers move right
	int playerY = 18; // variable to hold players y coord remember higher numbers move down
	bool game = false; // bool to hold tell when the game is over

	// at start of program clear the screen and output the map
	system("cls"); //clear screen
	for(int i=0; i<20; i++) // start display map
	{
		for(int j=0; j<20; j++)
		{
			cout << gameBoard[i][j];
		}

		cout << endl;
	}  // end display map

	// You need to create a main game loop, anything that your game needs to do needs to take place
	// inside this loop
	// hint: You can use a do while loop, and it needs to include your display of the board, 
	// a prompt to the user, a function call and a check to see if the game is won
	do
	{
    // inside the loop clear the screen and display the map again to start
	system("cls");
	for(int i=0; i<20; i++)
	{
		for(int j=0; j<20; j++)
		{
			cout << gameBoard[i][j];
		}

		cout << endl;
	}
    
    // After the board is displayed tell the player their current location and ask which
    // way they want to go and get their input
    cout << "Your current location is (" << playerX << "," << playerY << ")" << endl;
	cout << "Which way to you want to go? (Up = U, Down = D, Left = L, Right = R)" << endl;
	char UserMove;
	cin >> UserMove;

    // Based off the input the user enters call one of the functions to move their player
    // in the desired direction
    UserMove = toupper(UserMove);
	
	if(UserMove == 'U')
	{
		void up(int playerX, int playerY);
	}
	else if(UserMove == 'D')
	{
		void down();
	}
	else if(UserMove == 'L')
	{
		void left();
	}
	else if(UserMove == 'R')
	{
		void right();
	}
	else
	{
		cout << "You entered a invailed character, try again" << endl;
		//cin >> UserMove;
	}
	
    // the last thing to do before the loop starts over is check if the player has won
    // ak the player position is the same as the end xy(1,4) or array[4][1]
	if(playerX == 1 && playerY == 4)
	{
		game = true;
	}

	} // end do while loop
	while(game == false);
	// outside the loop make sure to tell the player they won
	system("cls");
	cout << "YOU WIN!!!" << endl;


	system("pause");
	return 0;
} // end main


void up(int playerX, int playerY) // function that moves the player in the up direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' or the end 'O' not a wall
	// hint: use an If and check the next space == #
	bool isValid = false;
	cout << "playerY = " << playerY;
	system("pause");
	if(playerY - 1 == '#' && playerY > 0 || playerY - 1 == 'O' && playerY > 0)
	{  
		isValid = true;  
	}
    else 
	{  
		//print Move is illegal, try again
	}   

	// If the move is vaild you need to replace the current space with a # and
	// place the players peice into the new square and change their coords
	// hint: vertical movement is controled in the first array 
	
		
	
	// If the move is bad tell the user
	

} // end up()


void down() // function that moves the player in the down direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' not a wall
	// hint: use an If and check the next space == #
	

	// If the move is vaild you need to replace the current space with a # and
	// place the players peice into the new square
	// hint: vertical movement is controled in the first array 



	// If the move is bad tell the user


} // end down()

void left() // function that moves the player in the left direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' not a wall
	// hint: use an If and check the next space == #


	// If the move is vaild you need to replace the current space with a # and
	// place the players peice into the new square
	// hint: horizontal movement is controled in the second array 

   


	// If the move is bad tell the user



} // end left()

void right() // function that moves the player in the right direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' not a wall
    // hint: use an If and check the next space == #



	// If the move is vaild you need to replace the current space with a # and
    // place the players peice into the new square
    // hint: horizontal movement is controled in the second array

   


    // If the move is bad tell the user



} // end right()



On lines 83ish and 124ish is what I dont understand. Is it wanting me to pass the varible playerX and playerY to void up? I'm I supposed to add something in my if statement when I call the up() function. And how can i get those playerX & Y coords to transfer to the void function.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,594
  • Joined: 16-October 07

Re: 2D Array Maze Game

Posted 07 December 2012 - 11:25 AM

This is not how you call a function, it's how you declare it:
if(UserMove == 'U') {
	void up(int playerX, int playerY);



You would call it as:
if(UserMove == 'U') {
	up(playerX, playerY);



I would use a struct, if you've gotten that far:
struct Player { int x, y; };
void up(Player &);



Or, better:
struct Player { 
	int x, y;
	void up();
};



Also, more functions. Something like bool moveForKey(char, int &, int &); would be nice.

Note, these two are not, exactly, identical:
char grid1[]="----";
char grid2[]={ '-', '-','-','-' };



The first is size 5, while the second is size 4. The first takes advantage of c-string syntax, but it will also tack a '\0' on the end.
Was This Post Helpful? 0
  • +
  • -

#9 ThePie69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

Re: 2D Array Maze Game

Posted 07 December 2012 - 12:15 PM

Thank you for your reply. We have not gotten to structs soI can't use them. I tried calling the function how you posted instead of declaring like I messed up on before
if(UserMove == 'U') {  
  up(playerX, playerY); 


But its saying that its being overloaded now. I'm using Visual Studios 2010 if you could take a minute and copy/paste that into there all my code below and see whats wrong with it that would be great. I'm trying to get it so when I type in U, the if statement will call the up funciton wich will see if the next space is a # and if move the X there and replace the old spot with a #. Then print out the new coords.

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

//declare array for game board and fill it
     char gameBoard[20][20] =
     { {'-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-', '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-'}
     , {'|' , '#' , '#' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#', '#' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '-' , '-' , '-' , '-', '-' , '-' , '#' , '-' , '-' , '-' , '-' , '-' , '|'}
     , {'|' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '#' , '#' , '#', '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , 'O' , '|' , '#' , '|' , '-' , '-' , '#' , '|' , '-' , '|', '#' , '|' , '-' , '-' , '-' , '-' , '-' , '#' , '|'}
     , {'|' , '-' , '-' , '-' , '|' , '#' , '|' , '#' , '|' , '#' , '|', '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '#' , '#' , '|' , '#' , '|' , '#' , '#' , '#' , '|', '#' , '|' , '#' , '-' , '-' , '-' , '-' , '-' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '-' , '#' , '|' , '-', '#' , '|' , '#' , '|' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '#' , '#' , '|' , '#', '#' , '|' , '#' , '#' , '#' , '-' , '-' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '#' , '-' , '|' , '#', '#' , '|' , '-' , '-' , '-' , '|' , '|' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '#' , '|' , '#', '#' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '-' , '-' , '-' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '-' , '-' , '|' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '#' , '#' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '|' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '-' , '-' , '|' , '#' , '|' , '-' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '-' , '-' , '#' , '-' , '|'}
     , {'|' , '#' , '#' , '#' , '|' , '#' , '|' , '#' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '#' , '#' , '|' , '#', '|' , '#' , '|' , '-' , '-' , '-' , '-' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '#' , '|' , '#', '|' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '-' , '-' , '-' , '-' , '-' , '#', '|' , '#' , '|' , '-' , '-' , '-' , '-' , '#' , '|'}
     , {'|' , 'X' , '|' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#', '|' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-', '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-'}};

void up(int &, int &); // pass the players position to all 
void down(int &, int &); // functions by reference
void left(int &, int &);
void right(int &, int &);

int main()
{
	int playerX = 1; // variable to hold players x coord remember higher numbers move right
	int playerY = 18; // variable to hold players y coord remember higher numbers move down
	bool game = false; // bool to hold tell when the game is over

	// at start of program clear the screen and output the map
	system("cls"); //clear screen
	for(int i=0; i<20; i++) // start display map
	{
		for(int j=0; j<20; j++)
		{
			cout << gameBoard[i][j];
		}

		cout << endl;
	}  // end display map

	// You need to create a main game loop, anything that your game needs to do needs to take place
	// inside this loop
	// hint: You can use a do while loop, and it needs to include your display of the board, 
	// a prompt to the user, a function call and a check to see if the game is won
	do
	{
    // inside the loop clear the screen and display the map again to start
	system("cls");
	for(int i=0; i<20; i++)
	{
		for(int j=0; j<20; j++)
		{
			cout << gameBoard[i][j];
		}

		cout << endl;
	}
    
    // After the board is displayed tell the player their current location and ask which
    // way they want to go and get their input
    cout << "Your current location is (" << playerX << "," << playerY << ")" << endl;
	cout << "Which way to you want to go? (Up = U, Down = D, Left = L, Right = R)" << endl;
	char UserMove;
	cin >> UserMove;

    // Based off the input the user enters call one of the functions to move their player
    // in the desired direction
    UserMove = toupper(UserMove);
	
	if(UserMove == 'U')
	{
		up(playerX, playerY);
	}
	else if(UserMove == 'D')
	{
		down();
	}
	else if(UserMove == 'L')
	{
		left();
	}
	else if(UserMove == 'R')
	{
		right();
	}
	else
	{
		cout << "You entered a invailed character, try again" << endl;
		system("pause");
	}
	
    // the last thing to do before the loop starts over is check if the player has won
    // ak the player position is the same as the end xy(1,4) or array[4][1]
	if(playerX == 1 && playerY == 4)
	{
		game = true;
	}

	} // end do while loop
	while(game == false);
	// outside the loop make sure to tell the player they won
	system("cls");
	cout << "YOU WIN!!!" << endl;


	system("pause");
	return 0;
} // end main


void up(int playerX, int playerY) // function that moves the player in the up direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' or the end 'O' not a wall
	// hint: use an If and check the next space == #
	int prevplayerX = playerX;
	int prevplayerY = playerY;
	
	if(gameBoard [playerX][playerY - 1] == '#')
	{
		playerY--;
		cout << playerX << "," << playerY << endl;
		gameBoard [prevplayerX][prevplayerY] = '#';
	}

	// If the move is vaild you need to replace the current space with a # and
	// place the players peice into the new square and change their coords
	// hint: vertical movement is controled in the first array 
	
		
	
	// If the move is bad tell the user
	

} // end up()


void down() // function that moves the player in the down direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' not a wall
	// hint: use an If and check the next space == #
/*	
		Just testing stuff here
	bool isValid = false;
	cout << "playerY = " << playerY;
	system("pause");
	if(playerY - 1 == '#' && playerY > 0 || playerY - 1 == 'O' && playerY > 0)
	{  
		isValid = true;  
	}
    else 
	{  
		cout << "Illegal Move" << endl;
	}  
*/
	// If the move is vaild you need to replace the current space with a # and
	// place the players peice into the new square
	// hint: vertical movement is controled in the first array 



	// If the move is bad tell the user


} // end down()

void left() // function that moves the player in the left direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' not a wall
	// hint: use an If and check the next space == #


	// If the move is vaild you need to replace the current space with a # and
	// place the players peice into the new square
	// hint: horizontal movement is controled in the second array 

   


	// If the move is bad tell the user



} // end left()

void right() // function that moves the player in the right direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' not a wall
    // hint: use an If and check the next space == #



	// If the move is vaild you need to replace the current space with a # and
    // place the players peice into the new square
    // hint: horizontal movement is controled in the second array

   


    // If the move is bad tell the user



} // end right()


Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,594
  • Joined: 16-October 07

Re: 2D Array Maze Game

Posted 07 December 2012 - 12:59 PM

You've got to be consistent. Computers are big on that.
// prototype
void up(int &, int &); // pass the players position to all 

// implementation does not match
void up(int playerX, int playerY) // function that moves the player in the up direction

// you want
void up(int &playerX, int &playerY)



Fix up the others, too.

Currently, none of your code is VS2010 specific, btw. Well, the nasty system calls make OS assumptions, but that's not VS's fault.
Was This Post Helpful? 0
  • +
  • -

#11 ThePie69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

Re: 2D Array Maze Game

Posted 07 December 2012 - 06:13 PM

So I changed the void function call with the & and now that has my position changing for the coords which is awsome! Thank you very much. All that I have to do now is get it so that the 'X' will move and its old spot will return to the '#' character afterwards. That i'm afraid I don't know how to do cause i'm not sure how to change the array. I've got some code there under up that I think is pretty good and then some commented out under the down function. And I just was saying that it was visual studios cause thats what we use for class.

Fully updated program below.

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

//declare array for game board and fill it
     char gameBoard[20][20] =
     { {'-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-', '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-'}
     , {'|' , '#' , '#' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#', '#' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '-' , '-' , '-' , '-', '-' , '-' , '#' , '-' , '-' , '-' , '-' , '-' , '|'}
     , {'|' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '#' , '#' , '#', '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , 'O' , '|' , '#' , '|' , '-' , '-' , '#' , '|' , '-' , '|', '#' , '|' , '-' , '-' , '-' , '-' , '-' , '#' , '|'}
     , {'|' , '-' , '-' , '-' , '|' , '#' , '|' , '#' , '|' , '#' , '|', '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '#' , '#' , '|' , '#' , '|' , '#' , '#' , '#' , '|', '#' , '|' , '#' , '-' , '-' , '-' , '-' , '-' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '-' , '#' , '|' , '-', '#' , '|' , '#' , '|' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '#' , '#' , '|' , '#', '#' , '|' , '#' , '#' , '#' , '-' , '-' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '#' , '-' , '|' , '#', '#' , '|' , '-' , '-' , '-' , '|' , '|' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '#' , '|' , '#', '#' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '-' , '-' , '-' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '-' , '-' , '|' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '#' , '#' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '|' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '-' , '-' , '|' , '#' , '|' , '-' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '-' , '-' , '#' , '-' , '|'}
     , {'|' , '#' , '#' , '#' , '|' , '#' , '|' , '#' , '#' , '|' , '#', '|' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '#' , '|' , '#' , '#' , '|' , '#', '|' , '#' , '|' , '-' , '-' , '-' , '-' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '#' , '#' , '|' , '#' , '#' , '|' , '#', '|' , '#' , '#' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'|' , '#' , '|' , '#' , '|' , '-' , '-' , '-' , '-' , '-' , '#', '|' , '#' , '|' , '-' , '-' , '-' , '-' , '#' , '|'}
     , {'|' , 'X' , '|' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '#', '|' , '#' , '|' , '#' , '#' , '#' , '#' , '#' , '|'}
     , {'-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-', '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-' , '-'}};

void up(int &, int &); // pass the players position to all 
void down(int &, int &); // functions by reference
void left(int &, int &);
void right(int &, int &);

int main()
{
	int playerX = 1; // variable to hold players x coord remember higher numbers move right
	int playerY = 18; // variable to hold players y coord remember higher numbers move down
	bool game = false; // bool to hold tell when the game is over

	// at start of program clear the screen and output the map
	system("cls"); //clear screen
	for(int i=0; i<20; i++) // start display map
	{
		for(int j=0; j<20; j++)
		{
			cout << gameBoard[i][j];
		}

		cout << endl;
	}  // end display map

	// You need to create a main game loop, anything that your game needs to do needs to take place
	// inside this loop
	// hint: You can use a do while loop, and it needs to include your display of the board, 
	// a prompt to the user, a function call and a check to see if the game is won
	do
	{
    // inside the loop clear the screen and display the map again to start
	system("cls");
	for(int i=0; i<20; i++)
	{
		for(int j=0; j<20; j++)
		{
			cout << gameBoard[i][j];
		}

		cout << endl;
	}
    
    // After the board is displayed tell the player their current location and ask which
    // way they want to go and get their input
    cout << "Your current location is (" << playerX << "," << playerY << ")" << endl;
	cout << "Which way to you want to go? (Up = U, Down = D, Left = L, Right = R)" << endl;
	char UserMove;
	cin >> UserMove;

    // Based off the input the user enters call one of the functions to move their player
    // in the desired direction
    UserMove = toupper(UserMove);
	
	if(UserMove == 'U')
	{
		up(playerX, playerY);
	}
	else if(UserMove == 'D')
	{
		down(playerX, playerY);
	}
	else if(UserMove == 'L')
	{
		left(playerX, playerY);
	}
	else if(UserMove == 'R')
	{
		right(playerX, playerY);
	}
	else
	{
		cout << "You entered a invailed character, try again" << endl;
		system("pause");
	}
	
    // the last thing to do before the loop starts over is check if the player has won
    // ak the player position is the same as the end xy(1,4) or array[4][1]
	if(playerX == 1 && playerY == 4)
	{
		game = true;
	}

	} // end do while loop
	while(game == false);
	// outside the loop make sure to tell the player they won
	system("cls");
	cout << "YOU WIN!!!" << endl;


	system("pause");
	return 0;
} // end main


void up(int &playerX, int &playerY) // function that moves the player in the up direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' or the end 'O' not a wall
	// hint: use an If and check the next space == #
	int prevplayerX = playerX;
	int prevplayerY = playerY;
	
	if(gameBoard [playerX][playerY - 1] == '#')
	{
		playerY--;
		//cout << playerX << "," << playerY << endl;
		gameBoard [prevplayerX][prevplayerY] = '#';
	}

	// If the move is vaild you need to replace the current space with a # and
	// place the players peice into the new square and change their coords
	// hint: vertical movement is controled in the first array 
	
		
	
	// If the move is bad tell the user
	

} // end up()


void down(int &playerX, int &playerY) // function that moves the player in the down direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' not a wall
	// hint: use an If and check the next space == #
/*	
		Just testing stuff here
	bool isValid = false;
	cout << "playerY = " << playerY;
	system("pause");
	if(playerY - 1 == '#' && playerY > 0 || playerY - 1 == 'O' && playerY > 0)
	{  
		isValid = true;  
	}
    else 
	{  
		cout << "Illegal Move" << endl;
	}  
*/
	// If the move is vaild you need to replace the current space with a # and
	// place the players peice into the new square
	// hint: vertical movement is controled in the first array 



	// If the move is bad tell the user


} // end down()

void left(int &playerX, int &playerY) // function that moves the player in the left direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' not a wall
	// hint: use an If and check the next space == #


	// If the move is vaild you need to replace the current space with a # and
	// place the players peice into the new square
	// hint: horizontal movement is controled in the second array 

   


	// If the move is bad tell the user



} // end left()

void right(int &playerX, int &playerY) // function that moves the player in the right direction
{
	// the first part of this function needs to check if the move is valid
	// make sure the space to be moved into is a '#' not a wall
    // hint: use an If and check the next space == #



	// If the move is vaild you need to replace the current space with a # and
    // place the players peice into the new square
    // hint: horizontal movement is controled in the second array

   


    // If the move is bad tell the user



} // end right()


Was This Post Helpful? 0
  • +
  • -

#12 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: 2D Array Maze Game

Posted 08 December 2012 - 12:37 AM

The way to do it is:



As you complete a successful move, you will update your position (x and y row and column, whatever you want to call them).

One heartbeat before you update your new position, you will restore your current (old) position, with whatever char was there, before you moved there. So you need to save that char.

So the move becomes:

1) is it legal?
2) if so, restore the old positions char
3) save the new positions char, in that same variable - it's now the oldchar.
4) update your x y position numbers
5) print your players char in the new position.

Every new position you move to, immediately becomes in your thinking, the old position.
Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,594
  • Joined: 16-October 07

Re: 2D Array Maze Game

Posted 08 December 2012 - 07:58 AM

Think about how graphic games work. Your maze is the background image. Conceptually, you draw the background first, then draw the animated elements over the top.

Think about how you draw your board. Do you need to change the board or simply allow for the current position of the player when you draw it?

e.g.
// magic numbers suck.  You should have something like this defined.
const int BOARD_WIDTH = 20;
const int BOARD_HEIGHT = 20;

char gameBoard[BOARD_HEIGHT][BOARD_WIDTH] ={
//...
// your draw loop; use var names that make sense
for (int y = 0; y<BOARD_HEIGHT; y++) {
	for (int x = 0; x<BOARD_WIDTH; x++) {
		// now, previously you just spat out the array value
		// instead, check against your mobile element first
		if (x==playerX && y=playerY) {
			// if were're in the right spot, print him instead
			cout << 'X';
		} else {
			// otherwise, just print the "background"
			cout << gameBoard[y][x];
		}
	}
	cout << endl;
}



I'd put your board draw in a function. Your main should be reasonably small.
Was This Post Helpful? 0
  • +
  • -

#14 ThePie69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 12

Re: 2D Array Maze Game

Posted 08 December 2012 - 06:08 PM

Thanks for the replys all of you. I have finished it and got it working. One last question I have is, is it possible to just type the letter for direction without having to press enter afterwards?
Was This Post Helpful? 0
  • +
  • -

#15 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: 2D Array Maze Game

Posted 08 December 2012 - 06:30 PM

Yes, but unbuffered input is not supported by the C standard. You need either:

1) To use a compiler which supports pdcurses, ncurses, curses, or conio.h. These support a broad assortment of compilers, and operating systems, from DOS to Windows, to Linux, and beyond.

2) Use something in the operating system, for this. Windows has an API for this, but I haven't used it.

The function you want to find is usually named getch(), (get char, no echo of the key pressed), or getche() (very good for entering passwords), (get char, with echo of the key pressed, going to the monitor).

Congrats on your program! ;)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2