473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to make an object move in a maze

3
What c++ code can be used to make an 'X' move through a maze. I have some code, but I'm not sure where to go from there. I have divided the program into three files, A header file, a main file and a .cpp implementation file.

In my implementation file I have:


Expand|Select|Wrap|Line Numbers
  1. #include "Maze.h"
  2.  
  3. Maze::Maze()
  4. {
  5.  
  6. }
  7. void Maze::mazeTraversal(char maze[][COLS], int row, int col, int direction)
  8. {
  9.     enum Direction {DOWN, RIGHT, UP, LEFT};
  10.  
  11.     switch(option)
  12.     {
  13.     case DOWN:
  14.         cout<<LEFT<<endl;
  15.         break;
  16.     case RIGHT:
  17.         cout<<DOWN<<endl;
  18.         break;
  19.     case UP:
  20.         cout<<RIGHT<<endl;
  21.         break;
  22.     case LEFT:
  23.         cout<<UP<<endl;
  24.     }
  25. }
  26. void Maze::printMaze()
  27. {
  28.     cout<<"please press enter"<<endl;
  29.     cout<<validMove()<<endl;
  30.     cin.get();
  31.  
  32.  
  33. }
  34. Maze::~Maze()
  35. {
  36.  
  37. }
In my main.cpp I have:

Expand|Select|Wrap|Line Numbers
  1. #include "Maze.h"
  2.  
  3. int main()
  4. {
  5.  
  6.     int rowStart = 2;
  7.     int colStart = 0;
  8.     Maze mazeObj;
  9.     enum Direction{DOWN, RIGHT, UP, LEFT};
  10.  
  11.     char tempMaze [12][12] = 
  12.     {{'1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'},
  13.     {'1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1'},
  14.     {'0', '0', '1', '0', '1', '0', '1', '1', '1', '1', '0', '1'},
  15.     {'1', '1', '1', '0', '1', '0', '0', '0', '0', '1', '0', '1'},
  16.     {'1', '0', '0', '0', '0', '1', '1', '1', '0', '1', '0', '0'},
  17.     {'1', '1', '1', '1', '0', '1', '0', '1', '0', '1', '0', '1'},
  18.     {'1', '0', '0', '1', '0', '1', '0', '1', '0', '1', '0', '1'},
  19.     {'1', '1', '0', '1', '0', '1', '0', '1', '0', '1', '0', '1'},
  20.     {'1', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '1'},
  21.     {'1', '1', '1', '1', '1', '1', '0', '1', '1', '1', '0', '1'},
  22.     {'1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1'},
  23.     {'1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'} };
  24.  
  25.     mazeObj.mazeTraversal(tempMaze, rowStart, colStart, RIGHT);
  26. }
In my header file I have:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. #ifndef MAZE_H
  7. #define MAZE_H
  8. class Maze
  9. {
  10. private:
  11.     enum Direction {DOWN, RIGHT, UP, LEFT};
  12.     static const int ROWS = 12;
  13.     static const int COLS = 12;
  14. public:
  15.     Maze();
  16.     ~Maze();
  17.     void mazeTraversal(char maze [][ COLS ], int row, int col, int direction);
  18.     void printMaze(const char maze [][ COLS ] );
  19.     bool validMove(const char maze[] [COLS], int row, int col);
  20.     bool coordsAreEdge(int row, int col);
  21.  
  22. };
  23. #endif
  24.  
  25.  
Apr 29 '13 #1
1 3976
weaknessforcats
9,208 Expert Mod 8TB
First, you write a program to display your maze array.

Second, locate where in your array the object is to be located

Third, make a copy of your array. Always keep te original with no changes. It is your master.

Fourth, put an X in the copied array at the correct spot

Fifth, display the copied array.

To move to the next square, just make another copy of the master array, put your X on the copy and re-display the copy. You will see the X appear to move. Like a cartoon.

If you clear your screen so your displays always overlay one another you will see the X travel.

You may need to put a sleep in your loop displaying the copies to slow things down enough so your eye can follow the X.
Apr 30 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: serge calderara | last post by:
Dear all, I have define a suer type as follow Structure USER_PARAM_LIST Dim LastUser As String Dim SaveEntry As Boolean End Structure and a variable of that type
1
by: Frank Rizzo | last post by:
I have VS.NET 2003. For some reason the Object Browser window appears as one of the tabs for every project I open. Even if I just open VS.NET without any projects, it still appears. How can...
4
by: Barkster | last post by:
I don't usually create installers for my projects cause we have dotnet on all our machines and I normally just copy the exe and any required Dll's to the folder where I want to run. Normally I...
1
by: Lal - Luxor | last post by:
please help me. how to work object.move will work on ve.net regards lal
8
by: Michael Yanowitz | last post by:
Hello: Are there any tools to convert non-object-oriented code into object-oriented code? If not, perhaps something that I can pass in two (or more) classes and will create a base-class and...
1
by: vedran | last post by:
Hello, How can I move objects in directpython???
0
by: catchrohith | last post by:
Hi all I have a flash object im my webpage. When the close button associated with it is clicked it should automatically flows to a particular position on the screen. Again clicking on...
2
by: Lax | last post by:
Say we have a static external object (object defined outside of any block with static qualifier) in a file. We are telling the compiler that we intend that object to be accessed by functions in...
3
by: Andrej Tv | last post by:
Hi. I'm having troubles to come up with a way to assign an object's method to a DOM-node created by the object without hardcoding object's name. For example: function currancySwitcher(){ ...
1
by: Rajesh Kesari | last post by:
I have a parent form with 3 subforms- after entering data in subform 1 I want cursor to move to a control in subform 2 and likewise to sub3 afterwards; what is the best way of doing that; I tried...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.