CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2014
    Posts
    3

    Cant make the maze work correctly with arrays

    I have to make a maze with arrays, but i cant seem to move down or up or at all. Any help would be greatly appreciated.
    Here is what I have so far


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

    using namespace std;

    void up(int &, int &);
    void down(int &, int &);
    void left(int &, int &);
    void right(int &, int &);

    int main()
    {

    char maze[30][20];
    ifstream inputFile;
    inputFile.open("E:/maze.txt");


    inputFile.close();

    int playerX = 1;
    int playerY = 0;
    bool game = false;


    system("cls");
    for (int i = 0; i<30; i++)
    {
    for (int j = 0; j<20; j++)
    {
    cout << maze[i][j];
    }

    cout << endl;
    }


    do
    {

    system("cls");
    for (int i = 0; i<30; i++)
    {
    for (int j = 0; j<20; j++)
    {
    cout << maze[i][j];
    }

    cout << endl;
    }


    cout << "You are currently at (" << playerX << "," << playerY << ")" << endl;
    cout << "Where would you like to move next? (Up = U, Down = D, Left = L, Right = R)" << endl;
    char UserMove;
    cin >> UserMove;


    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 << "Invalid, please choose another." << endl;
    system("pause");
    }


    if (playerX == 29 && playerY == 20)
    {
    game = true;
    }

    } while (game == false);

    system("cls");
    cout << "Winner!!" << endl;

    system("pause");
    return 0;
    }

    void up(int &playerX, int &playerY)
    {

    char maze[30][20];
    int prevplayerX = playerX;
    int prevplayerY = playerY;

    if (maze[playerX][playerY - 1] == '#')
    {
    playerY--;

    maze[prevplayerX][prevplayerY] = '#';
    }



    }

    void down(int &playerX, int &playerY)
    {


    char maze[30][20];
    int prevplayerX = playerX;
    int prevplayerY = playerY;

    if (maze[playerX][playerY - 1] == '#')
    {
    playerY++;

    maze[prevplayerX][prevplayerY] = '#';
    }



    }

    void left(int &playerX, int &playerY)
    {


    char maze[30][20];
    int prevplayerX = playerX;
    int prevplayerY = playerY;

    if (maze[playerX][playerY - 1] == '#')
    {
    playerY--;

    maze[prevplayerX][prevplayerY] = '#';
    }



    }

    void right(int &playerX, int &playerY)
    {

    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Cant make the maze work correctly with arrays

    Quote Originally Posted by Codedexter View Post
    I have to make a maze with arrays, but i cant seem to move down or up or at all. Any help would be greatly appreciated.
    Here is what I have so far
    ...
    First you have to use Code tags to make your code snippets readable. Compare what you posted with how it should look like:
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <fstream>
    #include <cmath>
    
    using namespace std;
    
    void up(int &, int &);
    void down(int &, int &);
    void left(int &, int &);
    void right(int &, int &);
    
    int main()
    {
    	char maze[30][20];
    	ifstream inputFile;
    	inputFile.open("E:/maze.txt");
    
    	inputFile.close();
    
    	int playerX = 1;
    	int playerY = 0;
    	bool game = false;
    
    	system("cls");
    	for (int i = 0; i<30; i++)
    	{
    		for (int j = 0; j<20; j++)
    		{
    			cout << maze[i][j];
    		}
    		cout << endl;
    	}
    	do
    	{
    		system("cls");
    		for (int i = 0; i<30; i++)
    		{
    			for (int j = 0; j<20; j++)
    			{
    				cout << maze[i][j];
    			}
    
    			cout << endl;
    		}
    
    		cout << "You are currently at (" << playerX << "," << playerY << ")" << endl;
    		cout << "Where would you like to move next? (Up = U, Down = D, Left = L, Right = R)" << endl;
    		char UserMove;
    		cin >> UserMove;
    
    		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 << "Invalid, please choose another." << endl;
    			system("pause");
    		}
    
    		if (playerX == 29 && playerY == 20)
    		{
    			game = true;
    		}
    
    	} while (game == false);
    
    	system("cls");
    	cout << "Winner!!" << endl;
    
    	system("pause");
    	return 0;
    }
    
    void up(int &playerX, int &playerY)
    {
    	char maze[30][20];
    	int prevplayerX = playerX;
    	int prevplayerY = playerY;
    
    	if (maze[playerX][playerY - 1] == '#')
    	{
    		playerY--;
    
    		maze[prevplayerX][prevplayerY] = '#';
    	}
    }
    
    void down(int &playerX, int &playerY)
    {
    	char maze[30][20];
    	int prevplayerX = playerX;
    	int prevplayerY = playerY;
    
    	if (maze[playerX][playerY - 1] == '#')
    	{
    		playerY++;
    
    		maze[prevplayerX][prevplayerY] = '#';
    	}
    }
    
    void left(int &playerX, int &playerY)
    {
    	char maze[30][20];
    	int prevplayerX = playerX;
    	int prevplayerY = playerY;
    
    	if (maze[playerX][playerY - 1] == '#')
    	{
    		playerY--;
    
    		maze[prevplayerX][prevplayerY] = '#';
    	}
    }
    
    void right(int &playerX, int &playerY)
    {
    }
    Second, you use the local arrays
    Code:
     	char maze[30][20];
    in your functions left, down, up. All these arrays are NOT initialized, therefore they contain some garbage. So why do you compare this garbage with something meaningful?
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Cant make the maze work correctly with arrays

    Hmm try to make a maze class and make the arrays member variables belonging to the class. That may help.
    ahoodin
    To keep the plot moving, that's why.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Cant make the maze work correctly with arrays

    You open a file, do nothing with it and then close it again? Does this file contain some info about the maze? If so then will need to read the data from the file and use it to initialize the maze array.

    Which brings me on to... You display the contents of the array maze without first initializing it - so the contents are unknown and are whatever happens to be at the memory address of maze each time the program is run. You also initially display it twice - once before the do loop and then again from within the do loop????

    The array maze defined in main() and also again in up(), down() and left() are different variables (although they have the same name). They do not refer to the same memory contents. See the link to scope in http://msdn.microsoft.com/en-us/library/k1f45exx.aspx

    There are various ways to have the up(), down() functions etc refer to the same array maze as used in main() but a good method would be to use a class as suggested by ahoodin in his post #3. The array maze would then be a private member of the class with up(), down() etc being public class functions.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Apr 2014
    Posts
    3

    Re: Cant make the maze work correctly with arrays

    Sorry im new at this :/ thanks for the help

  6. #6
    Join Date
    Apr 2014
    Posts
    3

    Re: Cant make the maze work correctly with arrays

    thanks so much 2kaud

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured