Thread: Console Game Problem

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    Console Game Problem

    This is a simple interface in my game. The aroow keys move the 'X' around on the screen inside of a maze. I had it working before, but when I changed the maze (it's in an array), it didn't work anymore. I changed all of the variables to fit it, but it doesn't work. Here's the code. Attached it the messed up output (Odd ASCII chars....):

    Code:
    #include "stdafx.h"
    #define _clrscr
    #include <monkey.h>
    
    
    typedef enum UPARAM {ZIP,UP,DOWN,LEFT,RIGHT,Q,A,Z,W,S,X};
    
    int COL_MAX = 47;
    int ROW_MAX = 10;
    int PLAYERX = 1;
    int PLAYERY = 12;
    char ITEM = 'O';
    char ItemMessage[50] = "You picked up an item!";
    char* message = new char[50];
    char MAP[10][47]    =  {" ____________________________________________ ",
    						"|   |      |X                                |",
    						"|   |      |_______________                  |",
    						"|   |      |>              |---_----------_  |",
    						"|   |                      | O |          |  |",
    						"|   |      |>     |        |   |          |  |",
    						"|   |      |      |        |                 |",
    						"|   |______|______|         ---------------  |",
    						"|                                            |",
    						" -------------------------------------------- "};
    
    void display();
    int ReadFiles();
    int MovePlayer(int x,int y);
    UPARAM PassParam(UPARAM Uparam);
    
    int main(int argc, char* argv[])
    {
    	message = " ";
    	char input;
    	char arrow;
    	UPARAM Uparam = ZIP;
    /////////////GAME PUMP/////////////GAME PUMP////////////////////
    /////////////GAME PUMP/////////////GAME PUMP////////////////////
    /////////////GAME PUMP/////////////GAME PUMP////////////////////
    	while(1)
    	{
    		//SHOW MAP SCREEN
    		display();
    		printf("\n");
    		//OUTPUT MESSAGE (BLANK IF THERE IS NOTHING TO SAY)
    		if(!strcmpi(message," "))
    		{
    		}
    		else
    		{
    		printf(message);
    		while(!kbhit());
    		}
    		message = " ";
    		input = getch();
    		//IF ARROW KEY...
    		if((int)input == -32)
    			{
    				arrow = getch();
    				switch(arrow)
    					{
    				case 'H':
    					{
    						Uparam = UP;
    					}break;
    				case 'P':
    					{
    						Uparam = DOWN;
    					}break;
    				case 'K':
    					{
    						Uparam = LEFT;
    					}break;
    				case 'M':
    					{
    						Uparam = RIGHT;
    					}break;
    					}
    
    				}
    		//IF OTHER THAN ARROW KEY...
    			else
    			{
    				switch(input)
    				{
    				case 'x':
    					{
    						return 0;
    					}break;
    					}
    			}
    		//SEND MESSAGE
    		PassParam(Uparam);
    		//CLEAR THE SCREEN
    		clrscr();
    		}
    /////////////END GAME PUMP/////////////END GAME PUMP////////////////////
    /////////////END GAME PUMP/////////////END GAME PUMP////////////////////
    /////////////END GAME PUMP/////////////END GAME PUMP////////////////////
    
    	return 0;
    }
    
    void display()
    {
        for(int loop2 = 0; loop2 < COL_MAX; loop2++)
        {
        for(int loop1 = 0; loop1 < ROW_MAX; loop1++)
        {
            char displayer;
            displayer = MAP[loop2][loop1];
            printf("%c",displayer);
            if(loop1 == (ROW_MAX - 1))
                printf("\n");
        }
        }
    }
    
    int MovePlayer(int x, int y)
    {
    	if(MAP[x][y] == ' ')
    	{
    		MAP[PLAYERX][PLAYERY] = ' ';
    		MAP[x][y] = 'X';
    		PLAYERX = x;
    		PLAYERY = y;
    	}
    	else if(MAP[x][y] == ITEM)
    	{
    		MAP[PLAYERX][PLAYERY] = ' ';
    		MAP[x][y] = 'X';
    		PLAYERX = x;
    		PLAYERY = y;
    		message = ItemMessage;
    	}
    	return 0;
    }
    
    UPARAM PassParam(UPARAM Uparam)
    {
    	switch(Uparam)
    	{
    	case UP:
    		{
    			MovePlayer(PLAYERX - 1,PLAYERY);
    		}break;
    	case DOWN:
    		{
    			MovePlayer(PLAYERX + 1,PLAYERY);
    		}break;
    	case LEFT:
    		{
    			MovePlayer(PLAYERX,PLAYERY - 1);
    		}break;
    	case RIGHT:
    		{
    			MovePlayer(PLAYERX,PLAYERY + 1);
    		}break;
    	default:
    		{
    		}break;
    	}
    	return ZIP;
    }
    
    int ReadFiles()
    {
    	char* COLMAX = new char[2];
    	char* ROWMAX = new char[2];
    
    	ifstream FileReader("C:\\Maze\\boarddimensions.txt");
    	FileReader>>COLMAX;
    	FileReader>>ROWMAX;
    	FileReader.close();
    	return 0;
    }

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Sorry; here's attachment:
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Is this a "fix all of my problems" thing? I NEED HELP! Here, I think my problem is centalized here:

    Code:
    void display()
    {
        for(int loop2 = 0; loop2 < COL_MAX; loop2++)
        {
        for(int loop1 = 0; loop1 < ROW_MAX; loop1++)
        {
            char displayer;
            displayer = MAP[loop2][loop1];
            printf("%c",displayer);
            if(loop1 == (ROW_MAX - 1))
                printf("\n");
        }
        }
    }
    
    //EARLIER DEFINED.....
    
    int COL_MAX = 47;
    int ROW_MAX = 10;
    int PLAYERX = 1;
    int PLAYERY = 12;
    char ITEM = 'O';
    char ItemMessage[50] = "You picked up an item!";
    char* message = new char[50];
    
    char MAP[10][47]    =  {" ____________________________________________ ",
    						"|   |      |X                                |",
    						"|   |      |_______________                  |",
    						"|   |      |>              |---_----------_  |",
    						"|   |                      | O |          |  |",
    						"|   |      |>     |        |   |          |  |",
    						"|   |      |      |        |                 |",
    						"|   |______|______|         ---------------  |",
    						"|                                            |",
    						" -------------------------------------------- "};
    Can ya make anything of this?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    it doesn't work
    Can you be a little bit more specific ?

    Does it compile ? Does it link ? Does it do what you want ?

    Please describe what it does and what it should do instead.




    Edit:
    Describing the problem often helps finding it. Your output isn't 10x47 but 47x10. Make sure your array declaration fits with your loops later. Therefore, use the defines in the array declaration as well. Change the COL/ROW in the loops.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    To be more specific, it compiles and links. When I run it, it gives me random ASCII stuff. I think I'm reading beyond my arrays.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    As I edited above: Your array is 10x47 but in your nested loops, you are reading it 47x10. Change the ROW/COL variables in your loop.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Ah! Alas it is solved! Thanks.

    Code:
    void display()
    {
        char displayer;
        for(int loop2 = 0; loop2 < ROW_MAX; loop2++)
        {
        for(int loop1 = 0; loop1 < COL_MAX; loop1++)
        {
            displayer = MAP[loop2][loop1];
            printf("%c",displayer);
            if(loop1 == (COL_MAX - 1))
                printf("\n");
        }
        }
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. console game
    By Prophet-ex in forum Game Programming
    Replies: 7
    Last Post: 01-04-2005, 09:41 AM
  2. Win32 tetris game problem
    By Finchie_88 in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2004, 09:15 AM
  3. Help with a simple game
    By jjj93421 in forum Game Programming
    Replies: 1
    Last Post: 03-28-2004, 03:52 PM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM