-
Notifications
You must be signed in to change notification settings - Fork 22
Proposal Lloyd Page #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LloydPage
wants to merge
2
commits into
CPRO-Session1:master
Choose a base branch
from
LloydPage:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| 0 | ||
| 4000 | ||
| 0 | ||
| 0 | ||
| Democracy | ||
| $ |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| /*Lloyd Page*/ | ||
| /*Nation sim with optimized sorts and text based commands*/ | ||
| #include<stdio.h> | ||
| #include<stdlib.h> | ||
| #include<string.h> | ||
| #include<unistd.h>//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;i<count;i++) | ||
| printf("%s",*(changes+i)); | ||
| int choice; | ||
| while(1) | ||
| { | ||
| int N=3; | ||
| char *line=malloc(N); | ||
| printf("1.Edit government type\n2.Edit economic policies\n3.Edit social policies\n4.Edit military\n5.sort government file\n6.save\n7.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<10&&choice>0) | ||
| 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<len;i++) | ||
| { | ||
| fprintf(out,"%s",*(changes+i)); | ||
| } | ||
| fclose(out); | ||
| }//creation of save file | ||
| int lines(int count,FILE*in)//gets the number of lines within the file | ||
| { | ||
| rewind(in); | ||
| int len=0; | ||
| for(int i=0;i<count;i++) | ||
| if(fgetc(in)=='\n') | ||
| len++; | ||
| return len; | ||
| } | ||
| void init(char**change,FILE*in,int size) | ||
| { | ||
| rewind(in); | ||
| for(int i=0;i<size;i++) | ||
| { | ||
| int N=3; | ||
| char *line=malloc(N); | ||
| fgets(line, N,in); | ||
| size_t last=strlen(line); | ||
| while(line[last-1]!='\n') | ||
| { | ||
| N*=2; | ||
| line=realloc(line,N); | ||
| fgets(line+last,N/2,in); | ||
| last=strlen(line); | ||
| } | ||
| *(change+i)=malloc(strlen(line)); | ||
| *(change+i)=line; | ||
| }//stores each line from in into string array | ||
| fclose(in); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| Lloyd Page | ||
| To run using a preset save file, use save.txt when prompted by the program | ||
| Due to an inability to successfully initialize the input file before noon on the due date, I was unable to implement the sort function, and any functionality beyond the government type. I ended up using about 6-7 hours trying to resolve this issue. | ||
| www.gnu.org/software/libc/manual/html_node/Deleting-Files.html to delete the temporary file created in the saveFile function | ||
| www.gnu.org/software/libc/manual/html_node/Testing-File-Access.html to check if input file exists, run in the main due to previously listed issue | ||
| www.exforsys.com/tutorials/c-language/file-management-in-c.html used to find file functions, which were needed for File I/O in this program. | ||
| sort functions, if they had been implemented would have been pulled from java. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| 0 | ||
| 4000 | ||
| 0 | ||
| 0 | ||
| Autocracy |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an interesting take on a simulator, and I like that you're using your knowledge of sorting algorithms. Go for it.