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. 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 0000000..b185ac3 Binary files /dev/null and b/nationSim differ 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