how to move character in maze

This is a discussion on how to move character in maze within the C++ Programming forums, part of the General Programming Boards category; My question is: I have to write a game that allows the user to navigate through a maze. I have ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    5

    how to move character in maze

    My question is:
    I have to write a game that allows the user to navigate through a maze. I have written most of the code but am stuck at this function:
    moveCharacter-move the user's character to a different location in the maze. The character can only move up, down, left and right and can quit.
    Now I think I have to use getch(), but I'm not sure how to write when the user is going up, down, left and right. Any suggestions? Thanks.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Here is a program I wrote a little while back. It's simulating an active interface with gotoxy(). It should solve your problem. I hope you are using a GCC compiler. Otherwise I could edit it for you.
    Sent from my iPadŽ

  3. #3
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Need more information please. Are you having trouble graphically moving the character? Getting the input to move the character(getch)? Testing for wall collision?
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    well heres what i have so far...

    Code:
    void moveCharacter()
            char x;
    	char charPos='*';
    	x= getch();
    
    	if(x=='w')
    	{
    		charPos -=1;
    	}
    * is the character, this is the code i wrote to when the user wants to go up..not sure if this is right.....not sure how to write for down, left and right

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    This is vague, but so is your post.

    Code:
    bool DoMove(int iYOffset,int iXOffset)
    {
      //Calculate new player position
      UINT iRow=Player.y+iYOffset;
      UINT iCol=Player.x+iXOffset;
    
      //Bounds check
      if (iRow<0 || iRow>maxrows || iCol<0 || iCol>maxcols) return false;
    
      //Compute new offset in maze
      UINT mazeoffset=iRow*mazewidth+iCol;
    
      //Bounds check
      if (mazeoffset>0 && mazeoffset<mazetotalsize)
      {
         //Check if maze cell is empty
         if (Maze[offset]==MAZE_EMPTY) 
         { 
           Maze[offset]=MAZE_PLAYER;
           return true;
         } else return false;
      } 
      return false;
    }
    Pass the desired offset to the function. Return true on success and false on error. MAZE_EMPTY can be 0 or any value you want. MAZE_PLAYER can be 1, 2 , etc. MAZE_WALL should be another value to represent a wall.

    Get the key input from the player, set the offsets and call DoMove() with the offsets.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe Comp Move help
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 09-24-2008, 11:05 AM
  2. get wide character and multibyte character value
    By George2 in forum C++ Programming
    Replies: 27
    Last Post: 01-27-2008, 04:10 AM
  3. Linked List (Base 21)
    By ATXFG in forum C Programming
    Replies: 3
    Last Post: 04-01-2007, 09:58 PM
  4. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 08:28 AM
  5. Replies: 5
    Last Post: 09-08-2001, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21