Public Group

# Some code I've been working on

This topic is 2391 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been working on a program that is, in essence, a game. Basicly, you move the player (represented by 'P') through a 10x10 grid made up of dots ('.'). The objective of the game is to reach the treasure (represented by the letter 'T') without being killed by the monster (represented by the letter 'M') who moves throughout the board using a pseudo-random function. To move the player, use the (w, s, a, d) buttons and press Enter. Unfortunately I haven't found a way to move the 'P' without pressing Enter because I've been working on this program on Ubuntu and, for some reason, it wont let me use the getch() function.

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.

##### Share on other sites
getch() on *nix systems is usually available through the curses library or one of the curses alternatives.

• 10
• 18
• 14
• 18
• 15