#include #include const int MAP_SIZE = 9; const char* statepath = "w.state"; const char* map[] = { "wwwwwwwww", "w w", "w wwwww w", "w w w w w", "w w wdw w", "w w w w", "wdw wdw", "w d w", "wwwwwwwww" }; char pov[3][3]; typedef struct { int x; int y; char orientation; } Location; typedef struct { char name[10]; Location location; } Gamestate; Gamestate gamestate; void drawmap() { for (int i = 0; i < MAP_SIZE; i++) { for (int j = 0; j < MAP_SIZE; j++) { printf("%c", map[i][j]); } printf("\n"); } } void loadstate() { gamestate.name[0] = '\0'; FILE* file = fopen(statepath, "r+"); if (file) { fread(&gamestate, sizeof(Gamestate), 1, file); fclose(file); } } void savestate() { FILE* file = fopen(statepath, "w"); if (file) { fwrite(&gamestate, sizeof(Gamestate), 1, file); } fclose(file); } char charatpos(int x, int y) { if (x < 0 || y < 0 || x >= MAP_SIZE || y >= MAP_SIZE) { return ' '; } return map[8-y][x]; } void updatepov() { int x = gamestate.location.x; int y = gamestate.location.y; char orientation = gamestate.location.orientation; for (int frontoffset = 2; frontoffset >= 0; frontoffset--) { for (int rtoloffset = -1; rtoloffset < 2; rtoloffset++) { int newX = x, newY = y; switch (orientation) { case 'n': newX += rtoloffset; newY += frontoffset; break; case 's': newX -= rtoloffset; newY -= frontoffset; break; case 'e': newX += frontoffset; newY -= rtoloffset; break; case 'w': newX -= frontoffset; newY += rtoloffset; break; } pov[2 - frontoffset][1 + rtoloffset] = charatpos(newX, newY); } } } void forward() { if (pov[1][1] != 'w') { switch (gamestate.location.orientation) { case 'n': gamestate.location.y += 2; break; case 's': gamestate.location.y -= 2; break; case 'e': gamestate.location.x += 2; break; case 'w': gamestate.location.x -= 2; break; } } } void right() { switch (gamestate.location.orientation) { case 'n': gamestate.location.orientation = 'e'; break; case 's': gamestate.location.orientation = 'w'; break; case 'e': gamestate.location.orientation = 's'; break; case 'w': gamestate.location.orientation = 'n'; break; } } void left() { switch (gamestate.location.orientation) { case 'n': gamestate.location.orientation = 'w'; break; case 's': gamestate.location.orientation = 'e'; break; case 'e': gamestate.location.orientation = 'n'; break; case 'w': gamestate.location.orientation = 's'; break; } } void move(char movement) { switch (movement) { case 'f': forward(); break; case 'r': right(); break; case 'l': left(); break; } } void dumpstate() { printf("Name:%s\nx:%d\ny:%d\norientation:%c\n", gamestate.name, gamestate.location.x, gamestate.location.y, gamestate.location.orientation); } void init() { loadstate(); printf("test\n"); if (strlen(gamestate.name) == 0) { printf("Welcome.\nEnter your name: "); fgets(gamestate.name, sizeof(gamestate.name), stdin); gamestate.name[strcspn(gamestate.name, "\n")] = 0; gamestate.location.x = 1; gamestate.location.y = 1; gamestate.location.orientation = 'n'; } updatepov(); } void update(char command) { switch (command) { case 'f': case 'r': case 'l': move(command); updatepov(); break; case 'm': break; case 'd': break; default: break; } } char replacechar(char in, int y) { if (in == 'w') { return '#'; } else if (in == 'd') { return '*'; } return in; } void draw2() { char screen[10][10]; for (int i = 0; i < 10; i++) { memset(screen[i], ' ', 10); } if (pov[2][0] != ' ') { // wall on the left screen[0][0] = '\\'; screen[1][1] = '\\'; screen[9][0] = '/'; screen[8][1] = '/'; } else { screen[1][0] = '_'; screen[1][1] = '_'; screen[7][0] = '_'; screen[7][1] = '_'; screen[2][1] = '|'; screen[3][1] = '|'; screen[4][1] = '|'; screen[5][1] = '|'; screen[6][1] = '|'; screen[7][1] = '|'; } if (pov[2][2] != ' ') { // wall on the right screen[0][9] = '/'; screen[1][8] = '/'; screen[9][9] = '\\'; screen[8][8] = '\\'; } else { screen[1][9] = '_'; screen[1][8] = '_'; screen[7][9] = '_'; screen[7][8] = '_'; screen[2][8] = '|'; screen[3][8] = '|'; screen[4][8] = '|'; screen[5][8] = '|'; screen[6][8] = '|'; screen[7][8] = '|'; } if (pov[1][1] == ' ') { // front is free screen[3][4] = '_'; screen[3][5] = '_'; screen[5][4] = '_'; screen[5][5] = '_'; if (pov[0][0] != ' ') { screen[2][2] = '\\'; screen[3][3] = '\\'; screen[4][3] = '|'; screen[5][3] = '|'; screen[7][2] = '/'; screen[6][3] = '/'; } if (pov[0][2] != ' ') { screen[3][6] = '/'; screen[2][7] = '/'; screen[4][6] = '|'; screen[5][6] = '|'; screen[6][6] = '\\'; screen[7][7] = '\\'; } else { screen[2][8] = '|'; screen[3][8] = '|'; screen[4][8] = '|'; screen[5][8] = '|'; screen[6][8] = '|'; screen[7][8] = '|'; screen[3][6] = '_'; screen[3][7] = '_'; screen[5][6] = '_'; screen[5][7] = '_'; } } else { // front is blocked for (int i = 2; i < 8; i++) { screen[1][i] = '_'; screen[7][i] = '_'; } if (pov[2][0] != ' ') { // wall on the left for (int i = 2; i < 8; i++) screen[i][1] = '|'; } if (pov[2][2] != ' ') { // wall on the right for (int i = 2; i < 8; i++) screen[i][8] = '|'; } else { for (int i = 8; i < 10; i++) { screen[1][i] = '_'; screen[7][i] = '_'; } } } for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { printf("%c", screen[i][j]); } printf("\n"); } } void draw() { #ifdef _WIN32 system("cls"); #else system("clear"); #endif draw2(); printf("\n\n\n"); printf(" o=======o\n"); for (int y = 0; y < 3; y++) { printf(" | "); for (int x = 0; x < 3; x++) { if (y == 0 && pov[1][1] != ' ') { printf(" "); } else if (y == 2 && x == 1) { printf("^"); } else { printf("%c", replacechar(pov[y][x], y)); } } printf(" |\n"); } printf(" o=======o\n"); printf(" | %s |\n", gamestate.name); printf(" o=======o\n"); printf("\n\n"); } int main() { init(); char command = ' '; while (command != 'q') { update(command); draw(); command = getchar(); getchar(); } savestate(); return 0; }