From 378cd45e112996f2053694732fb1cef7ee8bc252 Mon Sep 17 00:00:00 2001 From: Lloyd Page Date: Sun, 10 Jul 2016 03:39:08 -0400 Subject: [PATCH 1/2] commit Proposal.txt --- Proposal.txt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Proposal.txt diff --git a/Proposal.txt b/Proposal.txt new file mode 100644 index 0000000..1d5ef88 --- /dev/null +++ b/Proposal.txt @@ -0,0 +1,4 @@ +Lloyd Page +A nation simulator, in which the user must manage the govermental functions and policies, including military type,size of military, economic incetives/policies, social policies, etc through text-based input. To allow for more efficient management, the user will have the capacity to use an algorithm to determine the most efficient sorting algorithm given the input data, and how it is sorted. User data will be stored in a .txt file. +Functions will be provided by sorting algorithms, along with conditional statements, and loops. Structures will be used to keep nation data in a single data type, with a dynamic data structure(probably a linked-list) to store items inside the structure efficiently, and without worrying about size. Strings will be used to santize inputs, and for menu selection/input +I have done many simulations/games, however this is a genre I am yet to cover, so to have a fresh experience, I chose to do a nation simulator. I also wanted to do something somewhat practical, so creating an optimizer for sorting algorithms is something that I can use further down the road without many problems. From e673414eba8f04f678f9b4f12d77aa9e3fc4ebaf Mon Sep 17 00:00:00 2001 From: Lloyd Page Date: Fri, 15 Jul 2016 16:17:40 -0400 Subject: [PATCH 2/2] commit default.txt commit nationSim commit nationsim.c commit readme.txt commit save.txt --- default.txt | 6 ++ nationSim | Bin 0 -> 13728 bytes nationsim.c | 220 ++++++++++++++++++++++++++++++++++++++++++++++++++++ readme.txt | 7 ++ save.txt | 5 ++ 5 files changed, 238 insertions(+) create mode 100644 default.txt create mode 100755 nationSim create mode 100644 nationsim.c create mode 100644 readme.txt create mode 100644 save.txt diff --git a/default.txt b/default.txt new file mode 100644 index 0000000..30a2634 --- /dev/null +++ b/default.txt @@ -0,0 +1,6 @@ +0 +4000 +0 +0 +Democracy +$ diff --git a/nationSim b/nationSim new file mode 100755 index 0000000000000000000000000000000000000000..b185ac3317d8674987040f91fe9130d8c07dbdc6 GIT binary patch literal 13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$XlcU literal 0 HcmV?d00001 diff --git a/nationsim.c b/nationsim.c new file mode 100644 index 0000000..8418756 --- /dev/null +++ b/nationsim.c @@ -0,0 +1,220 @@ +/*Lloyd Page*/ +/*Nation sim with optimized sorts and text based commands*/ +#include +#include +#include +#include//included to handle invalid input +typedef struct nation +{ + float militaryStrength; + float stability; + float expenses; + float revenue; + char* govType; +}nation; +int start(int); +int lines(int,FILE*); +void saveFile(char**,int); +void init(char**,FILE*,int); +void Run(char**,int,nation); +void govType(char**); +int main() +{ + int choice=start(choice); + if(choice==3) + return 0; + FILE* in; + char line[BUFSIZ]="default.txt"; + int x=0; + if(choice==2) + { + printf("Enter your save file name\n"); + if(fgets(line,sizeof(line),stdin)==NULL) + exit(1); + line[strlen(line)-1]='\0'; + } + x=access(line,R_OK); + if(x!=0) + strcpy(line,"default.txt"); + in=fopen(line,"r"); + rewind(in); + char c=fgetc(in); + int count=0; + while(c!='$') + { + count++; + c=fgetc(in); + } + count=lines(count,in); + char** changes; + changes=malloc(count*sizeof(char*)); + init(changes,in,count); + nation n; + n.militaryStrength=atoi(*(changes)); + n.stability=atoi(*(changes+1)); + n.expenses=atoi(*(changes+2)); + n.revenue=atoi(*(changes+3)); + n.govType=*(changes+4); + Run(changes,count,n); + return 0; +} +void Run(char**changes,int count,nation n) +{ + for(int i=0;i0) + break; + }//end of menu selection + switch(choice) + { + case 1: + govType(changes); + break; + case 2: +//Not implemented Epolicies(changes); + break; + case 3: +//Not implemented Spolicies(changes); + break; + case 4: +//Not implemented military(changes); + break; + case 5: +//Not implemented sort(changes); + break; + case 6: + saveFile(changes,count); + break; + default: + exit(0); + } + Run(changes,count,n); +} +void govType(char**changes) +{ + int choice; + while(1) + { + int N=3; + char *line=malloc(N); + printf("1.Democracy\n2.Autocracy\n3.Anarchy\n"); + fgets(line, N,stdin); + size_t last=strlen(line); + while(line[last-1]!='\n') + { + N*=2; + line=realloc(line,N); + fgets(line+last,N/2,stdin); + last=strlen(line); + } + if((sscanf(line,"%d",&choice))&&choice<4&&choice>0) + { + free(line); + break; + } + printf("Enter valid input\n"); + } + switch(choice) + { + case 1: + *(changes+4)="Democracy"; + break; + case 2: + *(changes+4)="Autocracy"; + break; + case 3: + *(changes+4)="Anarchy"; + break; + } +} +int start(int choice)//start menu +{ + while(1) + { + int N=3; + char *line=malloc(N); + printf("1.New Game\n2.Load save\n3.Quit\n"); + fgets(line, N,stdin); + size_t last=strlen(line); + while(line[last-1]!='\n') + { + N*=2; + line=realloc(line,N); + fgets(line+last,N/2,stdin); + last=strlen(line); + } + if((sscanf(line,"%d",&choice))&&choice<4&&choice>0) + { + free(line); + return choice; + } + printf("Enter valid input\n"); + } +} +void saveFile(char**changes,int len) +{ + int N=3; + char *line=malloc(N); + printf("Enter your save file name\n"); + fgets(line, N,stdin); + size_t last=strlen(line); + while(line[last-1]!='\n') + { + N*=2; + line=realloc(line,N); + fgets(line+last,N/2,stdin); + last=strlen(line); + }//grabs name of the writing file + line[strlen(line)-1]='\0'; + FILE*out=fopen(line,"w"); + for(int i=0;i