Hello, guys. I found useful stuff so far. I'd like to ask if someone can give me some ideas how to wandering guards or set switches to open certain doors in a maze game in c++? At the moment I am thinking just to add some bits, can someone give some ideas what is the easiest way how to add these features. Thanks, folks.

"Maze game" is pretty vague. Is it a go (North, South, East, West - choose one) type of maze game, is it a FPS type maze that one wanders around freely in, is it a birds-eye view of a maze that you have to work though, etc.

Just a normal 2D game, I mean, you walk around, you have doors and keys and you have to reach the end in order to complete the level. I am trying to figure out how to create a symbol moving around "patrolling" as guard.

width = 25;
	height = 12;
	maze = new string[height];
	maze[0]  = "XXXXXXXXXXXXXXXXXXXXXXXXX";
	maze[1]  = "XkX                +  eXX";
	maze[2]  = "X X   XXXXXXXXXXXXXXXXXXX";
	maze[3]  = "X XXX X            +   XX";
	maze[4]  = "X X   X  XX  X XXXXX   XX";
	maze[5]  = "X X XXXXXXXXXX X k X   XX";
	maze[6]  = "X X                X   XX";
	maze[7]  = "X XXXXXXXXXXXXXXXXXX+XXXX";
	maze[8]  = "X                    XkXX";
	maze[9]  = "XXXXXXXXXXXXX+XXXXXXXX XX";
	maze[10] = "Xs     k               XX";
	maze[11] = "XXXXXXXXXXXXXXXXXXXXXXXXX";

Pretty much this is the map.

You can add '#' character as a guard or some other character, and have it move for example from a left wall to right wall

Now I set it as a character, you cannot go further when you reach it but it is not moving. How can I make it moving, I mean it has to move itself, just bouncing between 2 walls?

You'd need to have it move left or right every few milliseconds using a timer system, then check what the square next to it is each iteration, if the next square is an X (wall I presume), turn around, or move in the opposite direction (+ or -) along the X or Y axis.

Cannot do it, grrrr, by the time you posted it, I started researching but couldn't find it... I found some articles for using a timer system but couldn't set the movement. Should I use it as a case

case '#':    //if it is a guard
		newx = player.getX();   //set newx to current x
		newy = player.getY();   //set newy to current y
                break;

Or should I create a septate class for it?

#include <iostream>
#include <string>
using namespace std;

#ifndef _Enemy_H
#define _Enemy_H

class Enemy
{
private:
		int x;
		int y;

public:
	Enemy();

	int getX();
	int getY();
	void setX(int);
	void setY(int);
};
#endif

Please forgive my ignorance but I am still learning, spending some time trying to understand some bits of the language.

Every update cycle you move the guard to the left, if it hits the wall, move it to the right, and repeat.
Thats all you need to do. Here is an example :

#include <iostream>
#include <ctime>
#include <string>
#include <vector>
using namespace std;


using namespace std;

void updateMap(std::vector<string>& map);
void displayMap(const std::vector<string>& map){
	for(int i = 0; i < map.size(); ++i) cout << map[i] << endl;
}
void clearScreen(const int N = 10){
	//crude way
	for(int i = 0; i < N; ++i) cout << "\n";
}
int main() {

	srand( time(0) );
	std::vector<string> maze;
	const char GUARD = '#';
	//update every x millisecond
	const int UPDATE_TIME_IN_MILLISECONDS = 1000;
	maze.push_back("XXXXXXXXXXXXXXXXXXXXXXXXX");
	maze.push_back("X                      XX");
	maze.push_back("X     XXXXXXXXXXXXXXXXXXX");
	maze.push_back("X                      XX");
	maze.push_back("X           #          XX");
	maze.push_back("X                      XX");
	maze.push_back("X                      XX");
	maze.push_back("X XXXXXXXXXXXXXXXXXXXXXXX");
	maze.push_back("X                      XX");
	maze.push_back("XXXXXXXXXXXXXXXXXXXXXX XX");
	maze.push_back("X                      XX");
	maze.push_back("XXXXXXXXXXXXXXXXXXXXXXXXX");

	long startTime = clock(); //start time in millisecond
	//game loop
	while(true){
		//check if we should update now
		if(clock() - startTime > UPDATE_TIME_IN_MILLISECONDS){
			updateMap(maze); //update guard position
			displayMap(maze);
			//clearScreen();  //use API method if you have it
			cin.get(); //inserted this so its easier for you to see and debug
			startTime = clock();
		}
		else{
			//wait it its time to update
		}
	}
	return 0;
}

void updateMap(std::vector<string>& map){
	static bool shouldGuardMoveLeft = true; //start out by moving left initially
	//just looking at the map, you would probably want ot calculate it in a class
	const int GUARD_POS_Y = 4;
	const char WALL = 'X';
	if(shouldGuardMoveLeft){
		string& str = map[GUARD_POS_Y]; //find the string that contains '#'
		string::size_type pos = str.find('#');
		if(pos > 0 && str[pos - 1] != WALL) std::swap(str[pos-1],str[pos]); //move left
		else{ //we hit a wall so stay still for this frame
			shouldGuardMoveLeft = false;
		}
	}
	else{ //move right
		string& str = map[GUARD_POS_Y]; //find the string that contains '#'
		string::size_type pos = str.find('#');
		if(pos < str.size() && str[pos + 1] != WALL) std::swap(str[pos],str[pos + 1]); //move right
		else{ //we hit a wall so stay still for this frame
			shouldGuardMoveLeft = true;
		}
	}
}

FYI the above is just to serve as an example and not as an implementation.

Member Avatar for iamthwee

Sounds like you might want to clear the screen as well as receive input continuously from the command prompt. All of which steer towards not portable code snippets depending on what OS you are on.

You using *nix or windows?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.