NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dungeon.h"

/* Wir haben Ihnen die main, die Befehlsverarbeitung und zwei Hilfsfunktionen bereits
* vorgegeben. Sie müssen diese nicht übernehmen, können das aber tun. */

int main(int argc, char **argv){
spielstand st;
st.inventar = NULL;
int exit = 0;
if((exit = lade_spielstand(&st)) == 0){
if( argc == 2 ) exit = befehlsverarbeitung(argv[0], argv[1], &st);
else if (argc == 1) exit = befehlsverarbeitung(argv[0], NULL, &st);
else exit = 2;
}
if (exit == 0) exit = speichere_spielstand(st);
if(exit == 2){
printf(ERR_RAUM_NICHT_DA); //abfrage von Valgrind
}
liste_freigeben(st.inventar);
return exit;
}


int befehlsverarbeitung(char *befehl, char *gegenstand, spielstand *st){
int exit = 0;
if(strcmp(befehl, "./nimm") == 0){
if (gegenstand == NULL){
exit = 2;
fprintf(stderr, "Was moechtest Du nehmen?n");
}
else nimm(gegenstand, st);
} else if (strcmp(befehl, "./guck") == 0) exit = guck(st);
else if (strcmp(befehl, "./n") == 0) exit = gehe(NORD, st);
else if (strcmp(befehl, "./s") == 0) exit = gehe(SUED, st);
else if (strcmp(befehl, "./w") == 0) exit = gehe(WEST, st);
else if (strcmp(befehl, "./o") == 0) exit = gehe(OST, st);
else if (strcmp(befehl, "./inv") == 0) inv(st->inventar);
else{
exit = 2;
fprintf(stderr, "Der Befehl '%s' ist nicht bekannt.n", befehl);
}
return exit;
}

/* Implementieren Sie die folgenden Funktionen */

int lade_spielstand(spielstand *st){
char gegenstand[256];
FILE *load = fopen("spielstand", "r");
if(load == NULL){
fprintf(stderr, "Spielstand konnte nicht geladen werdenn");

return 2;
}
fscanf(load, "%255sn", st->aktueller_raum);
while(fscanf(load, "%255s ", gegenstand) == 1){
st->inventar = element_einfuegen(st->inventar, gegenstand);
}
if(strstr(st->aktueller_raum,"KEINRAUM")){
fclose(load);
return 2;
}
fclose(load);
return 0;
}

liste* element_einfuegen(liste *start, char *to_add){
liste *i = malloc(sizeof(liste)); //dynamisch Speicherallozieren
strcpy(i->item, to_add);
i->next = start; //verkettet die Liste
return i;
}

int speichere_spielstand(spielstand st){
liste *i = st.inventar;
FILE *save = fopen("spielstand", "w");
if(save == NULL){
fprintf(stderr, "Spielstand konnte nicht gespeichert werdenn");
return 2;
}
fprintf(save, "%sn", st.aktueller_raum);
for(; i != NULL;){
fprintf(save, "%s ", i->item);
i = i->next;
}
fclose(save);
return 0;
}

int gehe(richtung r, spielstand *st){
int i, j;
FILE *raumgehen = fopen(st->aktueller_raum, "r"); //Stream öffnen um die File das erstemal zu lesen
fscanf(raumgehen,"%*[^n]n"); //damit überspringe ich die Erste Zeile des Inputs
char raum[256], alles[1538], text[1024], himmelsrichtung[22];
richtung_to_string(r, himmelsrichtung);
for(i = 1; i < 5; i++){ //1, da r die Richtung in ENMUS ist. 1Nord 2Süd 3West 4Ost
fgets(alles, 1538, raumgehen); //Da ich keine Ahnung hatte, wie ich sonst erfahre, ob ein Text ein Tabulator hat, habe ich
//1528 (256Raum+256Wort+1023Text+2leerzeichen) Platz verwendet
if(i==r){
if(strstr(alles,"t")!=0) { //Finde das Tabulator zeichen, da danach das Item zum Nutzen und die Bedingung zum öffnen steht.
fclose(raumgehen); //Da ich wieder das File von anfang an Durchsuchen musste, habe ich das alte geschlossen und
FILE *raumgehen = fopen(st->aktueller_raum, "r"); //neu geöffnet. Das war die einzige Idee die ich hatte, es wieder von neu durchzulaufen
for(j = 0; j< r; j++){
fscanf(raumgehen, "%*[^n]n");
}
fscanf(raumgehen, "%sn", raum);
fscanf(raumgehen, "%sn", alles);
fgets(text, 1024, raumgehen);
if(in_liste(st->inventar, alles) == 1){
printf("%sn", text); //Text ausgabe der Bedingung zum gehen
printf(RAUMWECHSEL, st->aktueller_raum, himmelsrichtung, raum);
strcpy(st->aktueller_raum,raum);
fclose(raumgehen);
return 0;
}else{
printf(BENOETIGT_GEGENSTAND, alles); //Abbruch bedingung, wenn der Gegenstand fehlt
fclose(raumgehen);
return 2;
}
}else if(strstr(alles, "Wand")){ //Abbruch wenn gegen Wand gelaufen wird (strcmp hat nicht funktioniert(wohl t symbol),
//aber mit strstr schon)
printf(WAND);
fclose(raumgehen);
return 2;
}else if(strstr(alles, "Freiheit")){ //Beenden des Dungeon
printf(ENDLICH_FREI);
printf(SCHON_FREI);
fclose(raumgehen);
return 0;
}else{ //letzte else Bedingung, bei Verlassen ohne gebrauch von einem Gegenstand
printf(RAUMWECHSEL, st->aktueller_raum, himmelsrichtung, alles);
strcpy(st->aktueller_raum, alles);
fclose(raumgehen);
return 0;
}
}
}
fclose(raumgehen);
return 0;
}

void inv(liste *start){
liste *i = start;
if(i == NULL){ //prüfen, ob das Inventar leer ist (NULL heißt Zeigt auf nichts)
printf(INVENTAR_LEER);
}else{
printf(INVENTAR_VOLL);
for(; i != NULL;){
printf("%s ", i->item);
i = i->next; //Rückgabe aller Werte, die gespeichert sind.
}
}
}

int nimm(char *gegenstand, spielstand *st){
int i;
FILE *raum = fopen(st->aktueller_raum, "r"); //voher zu Kompliziert gemacht, mit temporären Dateien.
char gegenstandlesen[256]; //da es Probleme mit Valgrind gab bei zu wenig frees nochmal komplett neu gemacht
for(i = 0; i < 5; i++){
fscanf(raum,"%*[^n]n");
}
int vorhanden = 0;
while(fscanf(raum,"%255s", gegenstandlesen) == 1) {
if(strcmp(gegenstandlesen, gegenstand)==0){
vorhanden = 1;
if(in_liste(st->inventar, gegenstand) == 0) {
st->inventar = element_einfuegen(st->inventar, gegenstand);
printf("Du nimmst eine(n) %s.n",gegenstand);
fclose(raum);
return 0;
}
}
}if(vorhanden==1){
printf(ERR_GEGENSTAND_IN_INVENTAR);
fclose(raum);
return 1;
}else{
printf(ERR_GEGENSTAND_NICHT_IN_RAUM);
fclose(raum);
return 1;
}
fclose(raum);
return 0;
}


int in_liste(liste *start, char *vergleich){
liste *i = start;
for(; i != NULL;){
if((strcmp(i->item, vergleich)) == 0){ //einfacher vergleich, ob etwas vorhaden ist. Wenn wörter gleich sind gibt strcmp 0 raus!!!! 0 = richtig
return 1;
}
i = i->next;
}
return 0;
}


int guck(spielstand *st){
int i;
char nswo[4][256]; //Array zum Speicher, was im norden süden westen osten ist
char beschreibung[1024]; //anfangs beschreibung im Input
char gegenstandlesen[256]; //gegenstände im Raum
FILE *raum = fopen(st->aktueller_raum, "r");
if(raum == NULL){
fprintf(stderr, "Raum konnte nicht geladen werdenn");
return 2;
}
fgets(beschreibung, 1024, raum);
for(i = 0; i < 4; i++){
fscanf(raum, "%255s[^n]n", nswo[i]);
fscanf(raum, "%*[^n]n");
}
printf(MEIN_STANDORT, st->aktueller_raum);
printf(beschreibung);
printf(GUCK_NORD, nswo[0]);
printf(GUCK_SUED, nswo[1]);
printf(GUCK_WEST, nswo[2]);
printf(GUCK_OST, nswo[3]);
while(fscanf(raum, "%255sn", gegenstandlesen) == 1){ //wenn lesen klappt, prüfen ob schon im Inventar!
if((in_liste(st->inventar, gegenstandlesen)) == 0){
printf("%s ", gegenstandlesen);
}
}
puts("");
fclose(raum);
return 0;
}

void liste_freigeben(liste *start){ //freigeben der Liste, von Tag 9 abgeguckt (Hundkatze aufgabe)
liste *i = start;
for(;i != NULL;){
liste *next = i;
i = i->next;
free(next);
}
}


void print_ausgang(int i, char* ort){
switch (i){
case 0: printf(GUCK_NORD, ort); break;
case 1: printf(GUCK_SUED, ort); break;
case 2: printf(GUCK_WEST, ort); break;
case 3: printf(GUCK_OST, ort); break;
}
}

void richtung_to_string(richtung r, char* r_string){
switch(r){
case NORD: strcpy(r_string, "Norden"); break;
case SUED: strcpy(r_string, "Süden"); break;
case WEST: strcpy(r_string, "Westen"); break;
case OST: strcpy(r_string, "Osten");
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.