?
Solved

maze game in C

Posted on 2007-08-01
10
Medium Priority
?
5,045 Views
Last Modified: 2012-06-21
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;
}
0
Comment
Question by:cipher007
10 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 19614221
So where are you stuck ?
0
 
LVL 85

Expert Comment

by:ozo
ID: 19614297
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 19614968
>> ** 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Author Comment

by:cipher007
ID: 19621894
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 19623012
>> 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
 
LVL 3

Author Comment

by:cipher007
ID: 19628285
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 19628314
What do you mean by "some code" ? Which part do you have a problem with ?
0
 
LVL 3

Author Comment

by:cipher007
ID: 19628529
well, the ghost (M) chasing the player (O)
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1500 total points
ID: 19630228
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
 
LVL 3

Author Comment

by:cipher007
ID: 19674328
Thank you all for the help :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question