2 changed files with 187 additions and 0 deletions
@ -0,0 +1,10 @@ |
|||||||
|
GCC=/usr/bin/gcc
|
||||||
|
FLAGS=
|
||||||
|
TARGET=adv
|
||||||
|
OBJ=adv.o
|
||||||
|
|
||||||
|
$(TARGET): $(OBJ) |
||||||
|
$(GCC) $(FLAGS) $(OBJ) -o $(TARGET)
|
||||||
|
|
||||||
|
$(OBJ): $(TARGET).c |
||||||
|
$(GCC) $(FLAGS) -c $(TARGET).c
|
||||||
@ -0,0 +1,177 @@ |
|||||||
|
#include <stdio.h> |
||||||
|
#include <string.h> |
||||||
|
|
||||||
|
#define _DEBUGGING 0 |
||||||
|
|
||||||
|
#define _NORTH 8 |
||||||
|
#define _EAST 4 |
||||||
|
#define _SOUTH 2 |
||||||
|
#define _WEST 1 |
||||||
|
|
||||||
|
#define _FORWARD 8 |
||||||
|
#define _RIGHT 4 |
||||||
|
#define _BACKWARD 2 |
||||||
|
#define _LEFT 1 |
||||||
|
|
||||||
|
#define _NEWLINE 10 |
||||||
|
#define _SPACEBAR 32 |
||||||
|
|
||||||
|
/* Ett äventyrligt test
|
||||||
|
|
||||||
|
27/8-2022 - initial program. Chopping up input |
||||||
|
9/10-2022 - actual program. Added parsing of input, move & help command, and idea of exits and directions |
||||||
|
*/ |
||||||
|
|
||||||
|
char *roomName[16] = {"hallen", "ett vardagsrum", "Rum 2", "Rum 3", |
||||||
|
"Rum 4", "Rum 5", "Rum 6", "Rum 7", |
||||||
|
"Rum 8", "Rum 9", "Rum 10", "Rum 11", |
||||||
|
"Rum 12", "Rum 13", "Rum 14", "Rum 15"}; |
||||||
|
char *roomDesc[16] = { "Det ligger en ful matta på golvet. \nDet luktar ättika..",
|
||||||
|
"En TV står och brusar bredvid en bokhylla.\nTapeterna på väggen ramlar nästan av."}; |
||||||
|
short int roomExits[16] = {15,15,15,15, /* Binär information om utgångar. 1111 (15) = alla riktningar, osv */ |
||||||
|
15,15,15,15, /* 1000 - north, 0100 - east, 0010 - south, 0001 - west */ |
||||||
|
15,15,15,15, |
||||||
|
15,15,15,15}; |
||||||
|
|
||||||
|
short int commandParts; |
||||||
|
char command[5][16]; |
||||||
|
|
||||||
|
short int currentRoom; |
||||||
|
short int looking; /* 0 - north, 1 - south, 2- west, 3 - east */ |
||||||
|
|
||||||
|
int main () { |
||||||
|
startup(); |
||||||
|
|
||||||
|
while(1) { |
||||||
|
enterRoom(); |
||||||
|
prompt(); |
||||||
|
parseCommand(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
int startup () { |
||||||
|
printf("***************************\n"); |
||||||
|
printf("* E T T Ä V E N T Y R *\n"); |
||||||
|
printf("***************************\n\n\n"); |
||||||
|
|
||||||
|
printf("Du står utanför ett hus och sparkar in dörren.\nDen går itu och du går in...\n\n"); |
||||||
|
looking = _NORTH; /* Utgå från rum 13 och titta norrut */ |
||||||
|
currentRoom = 13; |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
int enterRoom () { |
||||||
|
printf("- - Du är i %s\n\n", roomName[currentRoom]); |
||||||
|
printf("%s \n\n", roomDesc[currentRoom]); |
||||||
|
} |
||||||
|
|
||||||
|
int prompt () { |
||||||
|
char input[40]; // Stoppa allt i variabeln input
|
||||||
|
|
||||||
|
printf("Vad vill du göra?\n"); |
||||||
|
fgets(input,40,stdin); |
||||||
|
printf("\n"); |
||||||
|
|
||||||
|
commandParts=0; |
||||||
|
int i=0; |
||||||
|
int ltr=0; // Position för bokstaven i ordet
|
||||||
|
|
||||||
|
while (i<sizeof(input)) { |
||||||
|
switch (input[i]) { |
||||||
|
|
||||||
|
case _NEWLINE: // Ta bort \n, terminera nuvarande variabel med NULL, avsluta insamling av ord
|
||||||
|
command[commandParts][ltr] = 0; |
||||||
|
i=sizeof(input); |
||||||
|
break; |
||||||
|
|
||||||
|
case _SPACEBAR: // Upptäck mellanslag, terminera nuvarande variabel med NULL, påbörja nytt ord
|
||||||
|
command[commandParts][ltr] = 0; |
||||||
|
commandParts++; |
||||||
|
ltr=0; |
||||||
|
break; |
||||||
|
|
||||||
|
default: // Lägg bokstaven till befintligt ord
|
||||||
|
command[commandParts][ltr] = input[i]; |
||||||
|
ltr++; |
||||||
|
break; |
||||||
|
} |
||||||
|
i++; |
||||||
|
} |
||||||
|
|
||||||
|
/* debug stuff */ |
||||||
|
if(_DEBUGGING){ |
||||||
|
printf("Hittade %d ord:\n", commandParts+1); |
||||||
|
int x=0; |
||||||
|
while (x<=commandParts) { |
||||||
|
printf("%s\n",command[x]); |
||||||
|
x++; |
||||||
|
} |
||||||
|
} |
||||||
|
/* end of debugging stuff */ |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
/* parseCommand expects the command array to contain something */ |
||||||
|
/* Look at the first word and call appropriate function */ |
||||||
|
int parseCommand() { |
||||||
|
if (strcmp(command[0], "hjälp") == 0) { /* HJÄLP */ |
||||||
|
help(); |
||||||
|
} else if (strcmp(command[0], "gå") == 0) { /* GÅ */ |
||||||
|
commandMove(); |
||||||
|
} else { /* unknown commands */ |
||||||
|
printf("- - ! HUH? (skriv hjälp för tillgängliga kommandon)\n\n"); |
||||||
|
} |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
/* gå command - depends on the command array */ |
||||||
|
int commandMove () { |
||||||
|
short int direction; |
||||||
|
if (strcmp(command[1], "framåt") == 0) { |
||||||
|
direction = _FORWARD; |
||||||
|
} else if (strcmp(command[1], "bakåt") == 0) { |
||||||
|
direction = _BACKWARD; |
||||||
|
} else if (strcmp(command[1], "vänster") == 0) { |
||||||
|
direction = _LEFT; |
||||||
|
} else if (strcmp(command[1], "höger") == 0) { |
||||||
|
direction = _RIGHT; |
||||||
|
} else { |
||||||
|
printf("-- ! HUH? gå framåt / bakåt / vänster / höger\n\n"); |
||||||
|
return 1; |
||||||
|
} |
||||||
|
|
||||||
|
if(canigo(direction)) { |
||||||
|
printf("Du går %s...\n\n", command[1]); |
||||||
|
switch(direction) { |
||||||
|
case _FORWARD: |
||||||
|
currentRoom=currentRoom-4; |
||||||
|
break; |
||||||
|
case _BACKWARD: |
||||||
|
currentRoom=currentRoom+4; |
||||||
|
break; |
||||||
|
case _LEFT: |
||||||
|
currentRoom--; |
||||||
|
break; |
||||||
|
case _RIGHT: |
||||||
|
currentRoom++; |
||||||
|
break; |
||||||
|
} |
||||||
|
} else { |
||||||
|
printf("Du kan inte gå %s\n\n", command[1]); |
||||||
|
return 1; |
||||||
|
} |
||||||
|
return 0;
|
||||||
|
} |
||||||
|
|
||||||
|
/* I framtiden ska denna funktion kika på roomExits utifrån önskad riktning */ |
||||||
|
int canigo(int direction) { /* 0 - forward, 1 - backward, 2 - left, 3 - right */ |
||||||
|
/* Got en bitwise jämförelse av önskad riktning och roomExit här. Glöm inte att omvandla riktning till ett passande tal (1,2,4,8) */ |
||||||
|
return 1; |
||||||
|
} |
||||||
|
|
||||||
|
int help() { |
||||||
|
printf("- - ! Tillgängliga kommandon: \ngå (framåt / bakåt / vänster / höger) \n\n- - ! SLUT PÅ HJÄLP\n"); |
||||||
|
return 0; |
||||||
|
} |
||||||
Loading…
Reference in new issue