Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Proposal.txt
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.
Copy link
Copy Markdown
Contributor

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.

6 changes: 6 additions & 0 deletions default.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
0
4000
0
0
Democracy
$
Binary file added nationSim
Binary file not shown.
220 changes: 220 additions & 0 deletions nationsim.c
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);
}
7 changes: 7 additions & 0 deletions readme.txt
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.
5 changes: 5 additions & 0 deletions save.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
0
4000
0
0
Autocracy