-
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;
}
-
Sorry; here's attachment:
-
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?
-
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.
-
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.
-
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.
-
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");
}
}
}