add w
This commit is contained in:
parent
dc39569dec
commit
5654b06155
|
@ -0,0 +1 @@
|
|||
a.out
|
|
@ -0,0 +1,286 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void draw()
|
||||
{
|
||||
|
||||
#ifdef _WIN32
|
||||
system("cls");
|
||||
#else
|
||||
system("clear");
|
||||
#endif
|
||||
|
||||
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", pov[y][x]);
|
||||
}
|
||||
}
|
||||
printf(" |\n");
|
||||
}
|
||||
printf("o=======o\n");
|
||||
printf("| %s |\n", gamestate.name);
|
||||
printf("o=======o\n");
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
init();
|
||||
char command = ' ';
|
||||
while (command != 'q')
|
||||
{
|
||||
update(command);
|
||||
draw();
|
||||
command = getchar();
|
||||
getchar();
|
||||
}
|
||||
savestate();
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue