I have got this maze game where you have to use the @ symbol to push the o symbols , and it works, but for one little problem : when I try to move the @ symbol then the o symbols moves in wrong strange directions. I think perhaps the coordinates are not put right ( maybe the dy dx are not put right ) but I am not sure can you guys help me out on this one ?
Since you need to see the entire code I shall just show the entire code : ( I know the code is kind of long , but I cannot find any other way to show my problem in the code )
-----------------------------------------------------
#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(12); const int SIZEX(20); const char MOUSE('@'); const char TUNNEL(' '); const char WALL('#'); const char APPLE('o'); const char STORAGE('+'); const int UP(72); const int DOWN(80); const int RIGHT(77); const int LEFT(75); const char QUIT('Q'); const int MAXAPPLES(6);
struct Item {
int x, y;
const char symbol;
};
struct AppleArray
{
int x,y;
char symbol;
};
int main()
{
int step(0); int storedApps(0); void initialiseGame(char g[][SIZEX + 1], char m[][SIZEX + 1], Item& mouse,
AppleArray apples[]);
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, int&
step, int& storedApps, AppleArray apples[]);
void updateGrid(char g[][SIZEX + 1], const char m[][SIZEX + 1], Item mouse,
AppleArray apples[]);
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..."); AppleArray apples[MAXAPPLES];
apples[0].x = 6;
apples[0].y = 3;
apples[0].symbol = APPLE;
apples[1].x = 6;
apples[1].y = 5;
apples[1].symbol = APPLE;
apples[2].x = 6;
apples[2].y = 8;
apples[2].symbol = APPLE;
apples[3].x = 3;
apples[3].y = 8;
apples[3].symbol = APPLE;
apples[4].x = 8;
apples[4].y = 4;
apples[4].symbol = APPLE;
apples[5].x = 8;
apples[5].y = 5;
apples[5].symbol = APPLE;
Clrscr();
initialiseGame(grid, maze, mouse, apples); paintGame(grid, message); int key(getKeyPress()); while (!wantsToQuit(key)) {
if (isArrowKey(key))
{
int dx(0), dy(0);
moveMouse(grid, mouse, key, message, step, storedApps, apples); updateGrid(grid, maze, mouse, apples); }
else
message = "INVALID KEY!"; paintGame(grid, message); key = getKeyPress(); }
endProgram(); return 0;
}
void initialiseGame(char grid[][SIZEX + 1], char maze[][SIZEX + 1], Item& mouse,
AppleArray apples[])
{ void setInitialMazeStructure(char g[][SIZEX + 1]);
void setInitialMouseCoordinates(Item& mouse);
void setInitialAppleCoordinates(AppleArray apples[]);
void updateGrid(char g[][SIZEX + 1], const char m[][SIZEX + 1], Item mouse,
AppleArray apples[]);
setInitialMazeStructure(maze); setInitialMouseCoordinates(mouse); updateGrid(grid, maze, mouse, apples); }
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', '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 = 6; }
void updateGrid(char grid[][SIZEX + 1], const char maze[][SIZEX + 1], Item mouse,
AppleArray apples[])
{ void setMaze(char g[][SIZEX + 1], const char m[][SIZEX + 1]);
void placeMouse(char g[][SIZEX + 1], Item mouse);
void placeApples(char g[][SIZEX + 1], AppleArray apples[]);
setMaze(grid, maze); placeMouse(grid, mouse); placeApples(grid, apples); }
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 placeApples(char g[][SIZEX + 1], AppleArray apples[])
{
for (int i(0); i < MAXAPPLES; ++i) {
g[apples[i].y][apples[i].x] = apples[i].symbol;
}
}
void moveMouse(const char g[][SIZEX + 1], Item& m, int key, string& mess, int& step,
int& storedApps, AppleArray apples[])
{
void setKeyDirection(int k, int& dx, int& dy);
bool moveApple(const char g[][SIZEX + 1], int, int, string&, int&, int&,
AppleArray a[], Item& m);
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; ++step; break;
case WALL: cout << '\a'; mess = "CANNOT GO THERE!";
break;
case APPLE:
if (moveApple(g, dx, dy, mess, step, storedApps, apples, m))
{
m.y += dy; m.x += dx; ++step; }
else
{
cout << '\a'; mess = "CANNOT GO THERE!";
}
}
}
bool moveApple(const char g[][SIZEX + 1], int dx, int dy, string& mess, int& step,
int& storedApps, AppleArray a[] , Item& m)
{
bool hasMoved = false;
int i(0);
while ((a[i].y != (m.y + dy)) && (a[i].x != (m.x + dx)))
{
switch (g[a[i].y + dy][a[i].x + dx])
{
case TUNNEL: a[i].y += dy; a[i].x += dx; hasMoved = true;
break;
case APPLE:
case WALL: cout << '\a'; mess = "CANNOT GO THERE!";
break;
case STORAGE:
a[i].y += dy;
a[i].x += dx;
hasMoved = true;
++storedApps;
break;
}
++i;
}
return hasMoved;
}
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 << "P V: 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");
}</string></conio.h></iomanip></iostream>