Help : C++ 2D array maze game , moving objects .
Hi , I have got this maze game that I want to make ,
in this maze the '@' symbol should be able to move a character
in the grid , so this is basically like moving boulders game where you can
move object by using @ and pushing some characters in the maze .
But the problem now is that there is the code written how to move the @
symbol throughout the maze , however how do you actually put another character
in the maze and make it move by pushing it with @ symbol .
Can anybody help me out ?
-----------------------------------------
#include <iostream> //for output and input: cout <<, cin >>
#include <iomanip> //for output manipulators
#include <conio.h> //for getch()
#include <string> //for string
using namespace std;
#include "ConsoleUtils.h" //for Clrscr, Gotoxy, etc.
const int SIZEY(6); const int SIZEX(10); const char MOUSE('@'); const char TUNNEL(' '); const char WALL('#'); const int UP(72); const int DOWN(80); const int RIGHT(77); const int LEFT(75); const char QUIT('Q');
struct Item {
int x, y;
const char symbol;
};
int main()
{
void initialiseGame(char g[][SIZEX + 1], char m[][SIZEX + 1], Item& mouse);
void paintGame(const char g[][SIZEX + 1], string& mess);
bool wantsToQuit(int key);
bool isArrowKey(int k);
int getKeyPress();
void moveMouse(const char g[][SIZEX + 1], Item& mouse, int key, string& mess);
void updateGrid(char g[][SIZEX + 1], const char m[][SIZEX + 1], Item mouse);
void endProgram();
char grid[SIZEY + 1][SIZEX + 1]; char maze[SIZEY + 1][SIZEX + 1]; Item mouse = { (SIZEX / 2) + 1, (SIZEY / 2) + 1, MOUSE }; string message("LET'S START...");
Clrscr();
initialiseGame(grid, maze, mouse); paintGame(grid, message); int key(getKeyPress()); while (!wantsToQuit(key)) {
if (isArrowKey(key))
{
int dx(0), dy(0);
moveMouse(grid, mouse, key, message); updateGrid(grid, maze, mouse); }
else
message = "INVALID KEY!"; paintGame(grid, message); key = getKeyPress(); }
endProgram(); return 0;
}
void initialiseGame(char grid[][SIZEX + 1], char maze[][SIZEX + 1], Item& mouse)
{ void setInitialMazeStructure(char g[][SIZEX + 1]);
void setInitialMouseCoordinates(Item& mouse);
void updateGrid(char g[][SIZEX + 1], const char m[][SIZEX + 1], Item mouse);
setInitialMazeStructure(maze); setInitialMouseCoordinates(mouse); updateGrid(grid, maze, mouse); }
void setInitialMazeStructure(char maze[][SIZEX + 1])
{
char initialMaze[SIZEY + 1][SIZEX + 1]
= { { 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X' },
{ 'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ 'X', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ 'X', '#', ' ', ' ', '#', ' ', ' ', '#', ' ', ' ', '#' },
{ 'X', '#', ' ', ' ', '#', '#', ' ', '#', ' ', '#', '#' },
{ 'X', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ 'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } };
for (int row(1); row <= SIZEY; ++row) for (int col(1); col <= SIZEX; ++col) maze[row][col] = initialMaze[row][col];
}
void setInitialMouseCoordinates(Item& mouse)
{ mouse.y = 3; mouse.x = 5; }
void updateGrid(char grid[][SIZEX + 1], const char maze[][SIZEX + 1], Item mouse)
{ void setMaze(char g[][SIZEX + 1], const char m[][SIZEX + 1]);
void placeMouse(char g[][SIZEX + 1], Item mouse);
setMaze(grid, maze); placeMouse(grid, mouse); }
void setMaze(char grid[][SIZEX + 1], const char maze[][SIZEX + 1])
{ for (int row(1); row <= SIZEY; ++row) for (int col(1); col <= SIZEX; ++col) grid[row][col] = maze[row][col];
}
void placeMouse(char g[][SIZEX + 1], Item m)
{ g[m.y][m.x] = m.symbol;
}
void moveMouse(const char g[][SIZEX + 1], Item& m, int key, string& mess)
{ void setKeyDirection(int k, int& dx, int& dy);
int dx(0), dy(0);
setKeyDirection(key, dx, dy); switch (g[m.y + dy][m.x + dx])
{ case TUNNEL: m.y += dy; m.x += dx; break;
case WALL: cout << '\a'; mess = "CANNOT GO THERE!";
break;
}
}
void setKeyDirection(int key, int& dx, int& dy)
{ switch (key) {
case LEFT: dx = -1; dy = 0;
break;
case RIGHT: dx = +1; dy = 0;
break;
case UP: dx = 0;
dy = -1; break;
case DOWN: dx = 0;
dy = +1; break;
}
}
int getKeyPress()
{ int keyPressed;
keyPressed = getch(); while (keyPressed == 224) keyPressed = getch();
return(toupper(keyPressed)); }
bool isArrowKey(int key)
{ return ((key == LEFT) || (key == RIGHT) || (key == UP) || (key == DOWN));
}
bool wantsToQuit(int key)
{ return (toupper(key) == QUIT);
}
void paintGame(const char gd[][SIZEX + 1], string& mess)
{ void paintGrid(const char g[][SIZEX + 1]);
Clrscr();
SelectTextColour(clYellow);
Gotoxy(0, 0);
cout << "___MOUSE AND APPLES GAME___\n" << endl;
SelectBackColour(clWhite);
SelectTextColour(clRed);
Gotoxy(40, 0);
cout << "Pascale Vacher: March 14";
paintGrid(gd);
SelectBackColour(clRed);
SelectTextColour(clYellow);
Gotoxy(40, 3);
cout << "TO MOVE USE KEYBOARD ARROWS ";
Gotoxy(40, 4);
cout << "TO QUIT ENTER 'Q' ";
SelectBackColour(clBlack);
SelectTextColour(clWhite);
Gotoxy(40, 8);
cout << mess; mess = ""; }
void paintGrid(const char g[][SIZEX + 1])
{
SelectBackColour(clBlack);
SelectTextColour(clWhite);
Gotoxy(0, 2);
for (int row(1); row <= SIZEY; ++row) {
for (int col(1); col <= SIZEX; ++col) cout << g[row][col]; cout << endl;
}
}
void endProgram()
{
SelectBackColour(clRed);
SelectTextColour(clYellow);
Gotoxy(40, 8);
cout << "\n";
system("pause");
}