Here is the code I have so far:
#include <iostream>
#include <cstdlib>
#define DIM 10
using namespace std;
char grid[DIM][DIM];
int dimen1=0, dimen2=0;
int Dimen1=3, Dimen2=8;
void Error() // An error message used when the player reaches the limits of the grid
{
system("clear");
cout << "ERROR!" << endl;
system("pause");
}
void botRun() // The game's "Monster" pseudo-randomly chooses a direction to move
{
int randnum;
randnum=rand()%4+1;
char *cptr=&grid[Dimen1][Dimen2];
const char xtrvrbl='M', xtrvrbl2='.';
switch(randnum)
{
case 1: // monster moves upwards
if(Dimen1==0)
{
break;
}
else
*cptr=xtrvrbl2;
--Dimen1;
cptr=&grid[Dimen1][Dimen2];
*cptr=xtrvrbl;
break;
case 2: // monster moves downwards
if(Dimen1==9)
{
break;
}
else
*cptr=xtrvrbl2;
++Dimen1;
cptr=&grid[Dimen1][Dimen2];
*cptr=xtrvrbl;
break;
case 3: // monster moves left
if(Dimen2==0)
{
break;
}
else
*cptr=xtrvrbl2;
--Dimen2;
cptr=&grid[Dimen1][Dimen2];
*cptr=xtrvrbl;
break;
case 4: // monster moves right
if(Dimen2==9)
{
break;
}
else
*cptr=xtrvrbl2;
++Dimen2;
cptr=&grid[Dimen1][Dimen2];
*cptr=xtrvrbl;
break;
}
}
void drawGrid()
{
char move;
char *cptr=&grid[dimen1][dimen2]; // This pointer points to the dimentions that the Player "P" is positioned.
const char xtrvrbl='P', xtrvrbl2='.'; // xtrvrbl=extravariable holds 'P' (Player).
int dimenDraw1=0, dimenDraw2=0; // These variables draw the grid.
system("clear");
while(dimenDraw1<=9 && dimenDraw2<=9) // This cycle draws the grid on the console.
{
if(dimenDraw2==9)
{
cout << grid[dimenDraw1][dimenDraw2];
cout << endl;
dimenDraw2=0;
++dimenDraw1;
}
else
{
cout << grid[dimenDraw1][dimenDraw2];
++dimenDraw2;
}
}
cout << endl << "Type Here!:";
cin >> move; // The player chooses the direction of the 'P' using the -W- -S- -A- -D-
switch(move)
{
case 'w': // player moves upwards
if(dimen1==0)
{
Error();
break;
}
else
*cptr=xtrvrbl2;
--dimen1;
cptr=&grid[dimen1][dimen2];
*cptr=xtrvrbl;
break;
case 's': // player moves downwards
if(dimen1==9)
{
Error();
break;
}
else
*cptr=xtrvrbl2;
++dimen1;
cptr=&grid[dimen1][dimen2];
*cptr=xtrvrbl;
break;
case 'a': // player moves left
if(dimen2==0)
{
Error();
break;
}
else
*cptr=xtrvrbl2;
--dimen2;
cptr=&grid[dimen1][dimen2];
*cptr=xtrvrbl;
break;
case 'd': // player moves right
if(dimen2==9)
{
Error();
break;
}
else
*cptr=xtrvrbl2;
++dimen2;
cptr=&grid[dimen1][dimen2];
*cptr=xtrvrbl;
break;
}
botRun();
drawGrid();
}
void InicGrid()
{
int dimen1=0, dimen2=0, n=0;
while(dimen1<=9 && dimen2<=9)
{
if(dimen2==9)
{
grid[dimen1][dimen2]='.';
dimen2=0;
++dimen1;
}
else
{
grid[dimen1][dimen2]='.';
++dimen2;
}
}
grid[0][0]='P';
grid[3][8]='M';
grid[9][9]='T';
drawGrid();
}
int main()
{
InicGrid();
}
What's Done:
- Player can move the letter 'P' around the grid
- The Monster ('M') can move around the board using the rand() function
- Both the Player and Monster can't move outside the 10x10 grid
What's not done:
- Any form of interaction between the Player and the Monster and the Player and the Treasure
- (Can't think of anything else at the moment)
Bugs:
- When 'P' passes nearby 'M' it sometimes dissapears, but it will show up again when it moves
So what do you think of the program.