Main Topics
Browse All TopicsSimple maze game using C... Here's what I want to do. ( I am using Dev-C++ compiler)
** game map should be loaded from a file (in the following code, map is generated runtime), and its format should be something like the following.
WWWWWWWWWWWWWWWW
W W
W W
W $ $ W
W O W
W $ M W
W W
W M W
W M E W
WWWWWWWWWWWWWWWW
W = wall
O = moving character
$ = money
E = exit
M = monsters (when game is started these monsters should be chasing the O character)
** keys A,S,W and Z are used to move the character O (in the following code only key the down movement (key S) is coded.
** username and score should be visible and when game ends this info should be saved to a file (may be top 5 scorers)
==========================
#include <iostream.h>
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
char moveR;
char moveL;
char moveU;
char moveD;
char move;
int mx=1;
int my=1;
int i,j;
char direction;
const char up_key='w', down_key='s', left_key='a', right_key='d';
char keypress;
char matrix[100][100];
void gotoxy(short x, short y)
{
HANDLE hConsoleOutput;
COORD Cursor_an_Pos = {x,y};
hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h
}
void loadgrid()
{
for (i=0; i<30; i++)
{
for (j=0; j<30; j++)
{
matrix[i][j]= '.';
}
}
}
void printgrid()
{
for (i=0; i<30; i++)
{
for (j=0; j<30; j++)
{
printf("%c", matrix[i][j]);
}
printf("\n");
}
}
int main()
{
loadgrid();
printgrid();
while (mx>0 && my>0)
{
if (kbhit())
{
keypress=getch(); //keypress=(char)getchar()
if ((keypress == right_key) || (keypress == left_key) ||
(keypress == up_key) || (keypress == down_key))
direction = keypress;
if (direction == 's')
{
gotoxy(mx,my);
printf(".");
my=my+1;
gotoxy(mx,my);
printf("O");
gotoxy(mx,my);
}
if (direction == 'w')
{
gotoxy(mx,my);
printf(".");
my=my-1;
gotoxy(mx,my);
printf("O");
gotoxy(mx,my);
}
if (direction == 'a')
{
gotoxy(mx,my);
printf(".");
mx=mx-1;
gotoxy(mx,my);
printf("O");
gotoxy(mx,my);
}
if (direction == 'd')
{
gotoxy(mx,my);
printf(".");
mx=mx+1;
gotoxy(mx,my);
printf("O");
gotoxy(mx,my);
}
direction = ' ';
}
}
return 0;
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>> ** game map should be loaded from a file
I assume that this is where you're stuck ?
A good way of storing a map like this in a file, is to store it as ASCII the exact same way you showed it above.
For that, you'll need these functions :
fopen : http://www.cplusplus.com/r
fclose : http://www.cplusplus.com/r
fgets : http://www.cplusplus.com/r
and you would change your loadgrid function to use the above functions to load the map from the file. Something like this :
FILE *theMapFile = fopen("maze.map", "r");
char line[256] = { 0 };
int row = 0, col = 0;
while (fgets(line, sizeof(line), theMapFile)) {
int len = strlen(line);
if (line[len - 1] == '\n') {
line[--len] = '\0';
}
else {
/* PROBLEM READING FILE (line too long) !!! HANDLE THIS ERROR !!! */
}
len = (len < 100) ? len : 100;
for (col = 0; col < len; ++col) {
matrix[row][col] = line[col];
}
++row;
if (row >= 100) break;
}
fclose(theMapFile);
>> there are three monsters on the board and all of them should be chasing 'O'.
That's indeed a difficult part to get right. If you let the monsters go straight to the player character, then they will ultimately all end up in the same spot, chasing the player character. So, you'll need to add some randomness here and there, or even better, use AI for your monsters (so they can work together, and try to surround the player, etc.), although you might want to start with something simpler :)
>> and yes these monsters should be within the boundary (W)
You'll need some kind of collision detection for this. It will be very basic, since this is a 2D ASCII game. You just need to check whether the next move is gonna put you on a wall, and if so, deny the move.
For each iteration of the game, you'll have to decide where each ghost moves. You'll have to think of how you want them to move, and then implement that.
For example, in the original Pac-Man, there were 4 ghosts - each of them used a different tactic, so that they basically worked together :
- ghost 1 chased the player
- ghost 2 tried to cut off the player (by going to the next intersection the player is gonna pass)
- ghost 3 stayed around the center area, guarding that area, including the "side-tunnel"
- ghost 4 stayed in those areas where you hadn't been yet
If you want to put some intelligence in your ghosts, you could do something similar, like 1 chaser, one interceptor, and 1 that's guarding the exit.
You first have to come up with how you want them to act, and then it's very easy to implement. On each iteration :
- a chaser goes towards the player
- an interceptor goes towards the interception point
- a guard wanders around randomly in its guard area until it spots the player in that area
Business Accounts
Answer for Membership
by: avizitPosted on 2007-08-01 at 19:48:06ID: 19614221
So where are you stuck ?