arrays - C: Roguelike map is screwy -
i'm making roguelike game in c, , can't character move in way want to. made 2d char array character on @ point (x, y), drew array, , changed x , y values , redrew array upon input of direction in go (kind of zork graphics). isn't working out how planned. code explain more can:
/* game.h (header file globals) */ #define game_h char character = '@'; //char monster = '&'; int x = 2; int y = 2; /* beginning floor (will not implemented, testing movement) */ char floor[10][6] = { /* 219 = filled block, 32 = space */ 219, 219, 219, 219, 219, 219, 219, 219, 219, '\n', 219, 32, 32, 32, 32, 32, 32, 32, 219, '\n', 219, 32, 32, 32, 32, 32, 32, 32, 219, '\n', 219, 32, 32, 32, 32, 32, 32, 32, 219, '\n', 219, 219, 219, 219, 219, 219, 219, 219, 219, '\n'}; /* game.c (main file) */ #include <stdio.h> #include "game.h" int main(void){ system("cls"); floor[x][y] = character; printf("%s", floor); char move; redo: printf("\ntravel way?\n"); printf("a = left\ns = down\nd = up\nf = right\n\n>"); scanf("%s", &move); /* array oftentimes gets desroyed because newlines being overwritten assignments. if statements should have prevented this. why didn't they? */ if (move == 'a'){ /* left */ if (x < 1){ x = 1;} x--; floor[x][y] = character; floor[x+1][y] = ' '; system("cls"); printf("%s", floor); goto redo; } else if (move == 's'){ /* down (works, goes right. clones itself) */ if (y > 3){ y = 3;} /*bounds may wrong*/ y++; floor[x][y] = character; floor[x][y-1] = ' '; system("cls"); printf("%s", floor); goto redo; } else if (move == 'd'){ /* */ if (y < 1){ y = 1;} y--; floor[x][y] = character; floor[x][y+1] = ' '; system("cls"); printf("%s", floor); goto redo; } else if (move == 'f'){ /* right */ if (x > 7){ x = 7;} x++; floor[x][y] = character; floor[x-1][y] = ' '; system("cls"); printf("%s", floor); goto redo;} else { goto done; } done: return 0; }
can tell me i'm doing wrong?
note: map setup draft. i'm going doing differently once mechanics done, i'll planning move character along array in same way, rather have specific setup rather advice on how better/differently. however, relevant answers , source code show better implementation still useful , appreciated, since myself may doing totally wrong in first place, first roguelike game.
given code:
char character = '@'; int x = 2; int y = 2; char floor[10][6] = { /* 219 = filled block, 32 = space */ 219, 219, 219, 219, 219, 219, 219, 219, 219, '\n', 219, 32, 32, 32, 32, 32, 32, 32, 219, '\n', 219, 32, 32, 32, 32, 32, 32, 32, 219, '\n', 219, 32, 32, 32, 32, 32, 32, 32, 219, '\n', 219, 219, 219, 219, 219, 219, 219, 219, 219, '\n'}; /* game.c (main file) */ #include <stdio.h> #include "game.h" int main(void){ system("cls"); floor[x][y] = character; printf("%s", floor); /* print map , character */
there several problems. first, array not null terminated; not safe print that. there 5 * 10 = 50 characters in array, null terminated after all. didn't notice miscounting until later. if had sixth line of data, you'd have use:
printf("%.*s", (int)sizeof(floor), floor);
the other problem you've misunderstood array; 10 rows of 6 characters, not 6 rows of 10 characters.
char floor[10][6] = { /* 219 = filled block, 32 = space */ { 219, 219, 219, 219, 219, 219, }, { 219, 219, 219, '\n', 219, 32, }, { 32, 32, 32, 32, 32, 32, }, { 219, '\n', 219, 32, 32, 32, }, { 32, 32, 32, 32, 219, '\n', }, { 219, 32, 32, 32, 32, 32, }, { 32, 32, 219, '\n', 219, 219, }, { 219, 219, 219, 219, 219, 219, }, { 219, '\n' }, };
and, in fact, data short few characters too. however, appear ok because of way print it, if did bracketed initialization, you'd find had problems if put brackets around each line in original layout.
i'd tempted use names instead of numbers characters in initializatio:
enum { wall = 219, open = 32, endl = '\n' };
these uniformly 4 letters each, making more systematic layout in initializer.
note when place character @ 2,2, modifying array this:
char floor[10][6] = { /* 219 = filled block, 32 = space */ { 219, 219, 219, 219, 219, 219, }, { 219, 219, 219, '\n', 219, 32, }, { 32, 32, '@', 32, 32, 32, }, // change here! { 219, '\n', 219, 32, 32, 32, }, { 32, 32, 32, 32, 219, '\n', }, { 219, 32, 32, 32, 32, 32, }, { 32, 32, 219, '\n', 219, 219, }, { 219, 219, 219, 219, 219, 219, }, { 219, '\n' }, };
Comments
Post a Comment