• C

maze game in C

Simple 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(hConsoleOutput, Cursor_an_Pos);
}

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;
}
LVL 3
cipher007Asked:
Who is Participating?
 
Infinity08Commented:
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
0
 
avizitCommented:
So where are you stuck ?
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
Infinity08Commented:
>> ** 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/reference/clibrary/cstdio/fopen.html
    fclose : http://www.cplusplus.com/reference/clibrary/cstdio/fclose.html
    fgets : http://www.cplusplus.com/reference/clibrary/cstdio/fgets.html

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);
0
 
cipher007Author Commented:
Thanks for taking time to help.
The most difficult part is to code the monsters (M), those Ms should be chasing the 'O' character.

For example, there are three monsters on the board and all of them should be chasing 'O'.
and yes these monsters should be within the boundary (W)
0
 
Infinity08Commented:
>> 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.
0
 
cipher007Author Commented:
Just a basic AI would be OK,  like divinding the game board to 4 parts and if the 'O' is in top right part of the board just send all the ghosts to that part of the board and so on.

can I ask for some code, please,
0
 
Infinity08Commented:
What do you mean by "some code" ? Which part do you have a problem with ?
0
 
cipher007Author Commented:
well, the ghost (M) chasing the player (O)
0
 
cipher007Author Commented:
Thank you all for the help :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.