Hi i am new to c++ and have begun looking into Classes.
i have this code below for a Simple maze game.
#include <iostream>
using namespace std;
void print(char maze[10][9])
{
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 9; j++)
{
cout << maze[i][j];
}
cout << endl;
}
}
int main()
{
char maze[][9] = { { '+', '-', '-', '-', '-', '-', '-', '-', '+' },
{ '|', ' ', ' ', ' ', '|', ' ', ' ', ' ', '|' },
{ '|', ' ', '|', ' ', ' ', ' ', '|', ' ', '|' },
{ '|', ' ', '+', '-', '-', '-', '+', ' ', '|' },
{ '|', '#', '|', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', '-', '+', ' ', '+', ' ', '+', '-', '|' },
{ '|', ' ', ' ', ' ', '|', ' ', '|', ' ', '|' },
{ '|', ' ', '|', ' ', '+', '-', '+', ' ', '|' },
{ '|', '*', '|', ' ', ' ', ' ', ' ', ' ', '|' },
{ '+', '-', '-', '-', '-', '-', '-', '-', '+' } };
int row = 8;
int col = 1;
bool gameOver = false;
while(!gameOver)
{
print(maze);
char move = ' ';
cout << "Move: ";
while(move != 'w' && move != 's' && move != 'a' && move != 'd' && move != 'q')
{
cin >> move;
}
if (move == 'w')
{
if (maze[row - 1][col] == ' ')
{
maze[row][col] = ' ';
row--;
maze[row][col] = '*';
}
else if (maze[row - 1][col] == '#')
{
gameOver = true;
}
}
else if (move == 's')
{
if (maze[row + 1][col] == ' ')
{
maze[row][col] = ' ';
row++;
maze[row][col] = '*';
}
else if (maze[row + 1][col] == '#')
{
gameOver = true;
}
}
else if (move == 'a')
{
if (maze[row][col - 1] == ' ')
{
maze[row][col] = ' ';
col--;
maze[row][col] = '*';
}
else if (maze[row][col - 1] == '#')
{
gameOver = true;
}
}
else if (move == 'd')
{
if (maze[row][col + 1] == ' ')
{
maze[row][col] = ' ';
col++;
maze[row][col] = '*';
}
else if (maze[row][col + 1] == '#')
{
gameOver = true;
}
}
else if (move == 'q')
{
gameOver = true;
}
else
{
cout << "Invalid Input" << endl;
}
}
return 0;
}
Instead of having all of this code in Main function Id like to put it as a class. Here is what i tried.
New mazefam.cpp file
#include <iostream>
#include "maze.h"
using namespace std;
void print(char maze[10][9])
{
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 9; j++)
{
cout << maze[i][j];
}
cout << endl;
}
}
int main()
{
maze mazenow;
mazenow.mazegame();
cout << "Hi world" << endl;
}
And the header file i created, maze.h
#include <iostream>
using namespace std;
class maze
{
public:
int mazegame(){
char maze[][9] = { { '+', '-', '-', '-', '-', '-', '-', '-', '+' },
{ '|', ' ', ' ', ' ', '|', ' ', ' ', ' ', '|' },
{ '|', ' ', '|', ' ', ' ', ' ', '|', ' ', '|' },
{ '|', ' ', '+', '-', '-', '-', '+', ' ', '|' },
{ '|', '#', '|', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', '-', '+', ' ', '+', ' ', '+', '-', '|' },
{ '|', ' ', ' ', ' ', '|', ' ', '|', ' ', '|' },
{ '|', ' ', '|', ' ', '+', '-', '+', ' ', '|' },
{ '|', '*', '|', ' ', ' ', ' ', ' ', ' ', '|' },
{ '+', '-', '-', '-', '-', '-', '-', '-', '+' } };
int row = 8;
int col = 1;
bool gameOver = false;
while(!gameOver)
{
print(maze);
char move = ' ';
cout << "Move: ";
while(move != 'w' && move != 's' && move != 'a' && move != 'd' && move != 'q')
{
cin >> move;
}
if (move == 'w')
{
if (maze[row - 1][col] == ' ')
{
maze[row][col] = ' ';
row--;
maze[row][col] = '*';
}
else if (maze[row - 1][col] == '#')
{
gameOver = true;
}
}
else if (move == 's')
{
if (maze[row + 1][col] == ' ')
{
maze[row][col] = ' ';
row++;
maze[row][col] = '*';
}
else if (maze[row + 1][col] == '#')
{
gameOver = true;
}
}
else if (move == 'a')
{
if (maze[row][col - 1] == ' ')
{
maze[row][col] = ' ';
col--;
maze[row][col] = '*';
}
else if (maze[row][col - 1] == '#')
{
gameOver = true;
}
}
else if (move == 'd')
{
if (maze[row][col + 1] == ' ')
{
maze[row][col] = ' ';
col++;
maze[row][col] = '*';
}
else if (maze[row][col + 1] == '#')
{
gameOver = true;
}
}
else if (move == 'q')
{
gameOver = true;
}
else
{
cout << "Invalid Input" << endl;
}
}
return 0;
}
}
And the Errors i get,
missing ; before using
and
'print' identifier not found
A noob question but help fixing this would be great :D I dont even know if what im trying to do makes sense, apologies if it doesnt
edit: restructured, i think its more correct now