Commit f8e06ee2 authored by jinny.wilkin's avatar jinny.wilkin

Final Commit - All functions working as expected, comments added and

report connected
parent c05dd23e
No preview for this file type
No preview for this file type
......@@ -5,8 +5,10 @@
#include <conio.h>
#include <ctype.h>
//needed for populateDex()
#define MAXCHAR 1000
//Create and define the variables for all of the structs
typedef struct Pokemon{
int id, height, weight;
char name[12], species[22], type1[9], type2[9], ability[14], evolveTo[12], evolveFrom[12], dimorphic[6];
......@@ -19,10 +21,13 @@ typedef struct Trainers{
} Trainers;
typedef struct Pokedex{
Pokemon pokemon[721];
Trainers trainers[5];
Pokemon pokemon[1000];
Trainers trainers[10];
int totalPokemon;
int totalTrainers;
} Pokedex;
//I'm only really using one struct, but there's nothing stopping multiple being made if needed or wanted - I call this one here
Pokedex mainDex;
int populateDex(){
......@@ -31,50 +36,56 @@ int populateDex(){
int n = 1;
char *holder;
//open the pokedex reference file in read only mode
fp = fopen("pokedexREF.csv", "r");
int read = 0;
int records = 0;
//While it isn't the end of the file, scan the line in and fill in all of the variables in that order. I'm using specific size char arrays instead of open ended %s flags
while (!feof(fp)){
read = fscanf(fp, "%d,%11[^,],%8[^,],%8[^,],%13[^,],%d,%d,%5[^,],%11[^,],%11[^,],%21[^\n]\n",
&mainDex.pokemon[records].id, mainDex.pokemon[records].name, mainDex.pokemon[records].type1, mainDex.pokemon[records].type2, mainDex.pokemon[records].ability, &mainDex.pokemon[records].height,
&mainDex.pokemon[records].weight, mainDex.pokemon[records].dimorphic, mainDex.pokemon[records].evolveTo, mainDex.pokemon[records].evolveFrom, mainDex.pokemon[records].species);
&mainDex.pokemon[mainDex.totalPokemon].id, mainDex.pokemon[mainDex.totalPokemon].name, mainDex.pokemon[mainDex.totalPokemon].type1, mainDex.pokemon[mainDex.totalPokemon].type2, mainDex.pokemon[mainDex.totalPokemon].ability, &mainDex.pokemon[mainDex.totalPokemon].height,
&mainDex.pokemon[mainDex.totalPokemon].weight, mainDex.pokemon[mainDex.totalPokemon].dimorphic, mainDex.pokemon[mainDex.totalPokemon].evolveTo, mainDex.pokemon[mainDex.totalPokemon].evolveFrom, mainDex.pokemon[mainDex.totalPokemon].species);
//make sure that all 11 records were correctly read and input before moving on
if (read == 11){
records++;
mainDex.totalPokemon++;
}
//return an error if that didn't work
if(read != 11){
printf("File Format Error");
printf("\n%d records read.\n", records);
printf("\n%d records read.\n", mainDex.totalPokemon);
return 1;
}
}
//close up the file to preserve memory
fclose(fp);
printf("\n%d pokemon records read.\n", records);
//confirm that the file was read fully
printf("\n%d pokemon records read.\n", mainDex.totalPokemon);
}
int generateDex(int trainerID){
int i = 0;
mainDex.trainers[trainerID].totalOwned = 0;
//This could be the full array, but as the rand function skews upwards and I'm not using a UI, I've limited it to 300 purely for ease of reading
//Did have this working with %721, but had one generate at about 600 Pokemon and the list was far too long to be any use
int numMon = rand() % 300;
//printf("%d\n", numMon);
while(i <= numMon){
while(mainDex.trainers[trainerID].totalOwned <= numMon){
//printf("%s\n", mainDex.pokemon[rand() % 721].name);
mainDex.trainers[trainerID].owned[i] = mainDex.pokemon[rand() % 721];
i++;
mainDex.trainers[trainerID].owned[mainDex.trainers[trainerID].totalOwned] = mainDex.pokemon[rand() % mainDex.totalPokemon];
mainDex.trainers[trainerID].totalOwned++;
}
return numMon;
}
void printDex(int trainerID){
int i;
printf("\n%s owns %d Pokemon. Their full PC is as follows:\n", mainDex.trainers[trainerID].name, mainDex.trainers[trainerID].totalOwned+1);
printf("\n%s owns %d Pokemon. Their full PC is as follows:\n", mainDex.trainers[trainerID].name, mainDex.trainers[trainerID].totalOwned);
//for every entry in the trainer's owned array, return that pokemon's name and the order that the owner has them
for(i=0;i<=mainDex.trainers[trainerID].totalOwned; i++){
printf("%d. %s\n", i+1, mainDex.trainers[trainerID].owned[i].name);
}
......@@ -82,19 +93,22 @@ void printDex(int trainerID){
}
void printPokemon(int pokemonID){
printf("The Pokemon you are looking for is %s, the %s.\n", mainDex.pokemon[pokemonID].name, mainDex.pokemon[pokemonID].species);
printf("The Pokemon you are looking for is %s, the %s\n", mainDex.pokemon[pokemonID].name, mainDex.pokemon[pokemonID].species);
//isspace is helpful to identify empty csv cells - returns 1 if true and 0 if false so can be used as boolean value
if(isspace(mainDex.pokemon[pokemonID].type2[0])){
printf("%s is a %s type, ", mainDex.pokemon[pokemonID].name, mainDex.pokemon[pokemonID].type1);
}
else{
printf("%s has types %s and %s, ", mainDex.pokemon[pokemonID].name, mainDex.pokemon[pokemonID].type1, mainDex.pokemon[pokemonID].type2);
}
printf("it is %.2fm tall and %.2fkg in weight and has the ability %s. ", ((float)mainDex.pokemon[pokemonID].height/10), ((float)mainDex.pokemon[pokemonID].weight/10), mainDex.pokemon[pokemonID].ability);
//Did not realise until it was way too late that the values in the csv were a factor of 10 out of what they were meant to be - convert to float here and output the correct units
printf("it is %.2fm tall and %.2fkg in weight and has the ability %s ", ((float)mainDex.pokemon[pokemonID].height/10), ((float)mainDex.pokemon[pokemonID].weight/10), mainDex.pokemon[pokemonID].ability);
if(mainDex.pokemon[pokemonID].dimorphic == "TRUE"){
printf("This Pokemon is sexually dimorphic, which means that the males and females of this species look different! ");
}else{
printf("This Pokemon isn't sexually dimorphic, which means that the males and females of this species look the same! ");
}
//this whole section is just checking for the evolution columns
if(!isspace(mainDex.pokemon[pokemonID].evolveFrom[0]) && !isspace(mainDex.pokemon[pokemonID].evolveTo[0])){
printf("%s evolved from %s, and will evolve into %s.", mainDex.pokemon[pokemonID].name, mainDex.pokemon[pokemonID].evolveFrom, mainDex.pokemon[pokemonID].evolveTo);
}
......@@ -107,16 +121,18 @@ void printPokemon(int pokemonID){
else{
printf("%s has no known evolutions.", mainDex.pokemon[pokemonID].name);
}
printf("\n");
printf("\n"); //really miss print that automatically leaves a new line at the end of each statement.
}
char* stripSpace(char* string){
//much of this function is not my code, but instead taken from Sai Gowtham's article on Reactgo https://reactgo.com/c-program-remove-spaces-in-string/
//added some additional string manipulation but otherwise left as is to get the most out of the function
char final[100];
int c = 0, d = 0;
while(string[c] != '\0')
{
string[c] = tolower(string[c]);
string[c] = tolower(string[c]); //this is mine, to make sure that all the letters are lower case
if(!(string[c] == ' ' && string[c+1] == ' ')){
final[d] = string[c];
d++;
......@@ -124,8 +140,9 @@ char* stripSpace(char* string){
c++;
}
string[d] = '\0';
string[strcspn(string, "\n")] = 0;
string[0] = toupper(string[0]);
string[strcspn(string, "\n")] = 0; //also added this to remove the new line characters
string[0] = toupper(string[0]); //and lastly, needed the first character to be upper case again
//While making the string entirely lower and then the first character upper is fairly redundant in my testing, I like to make sure that only my terrible spelling will cause an error
//printf("\n%s<-ends here", string);
return string;
}
......@@ -133,26 +150,27 @@ char* stripSpace(char* string){
int identify(){
int choice = 0;
int arrayLength;
//One of two ways to cancel this loop and end the program
while(choice!='6'){
printf("\nHow would you like to identify this Pokemon?\n1. By Name\t\t2. By ID\n3. By Primary Type\t4. By Secondary Type\n5. Trainer Info\t\t6. Exit\n\nEnter Your Choice: ");
choice = getch();
printf("%d", choice);
switch(choice){
choice = getch(); //the next key pressed is the input, no enter required
//printf("%d", choice);
switch(choice){ //takes the input and picks the relevant case, like a very fancy if-else loop
case '1':
printf("\nEnter the Pokemon's Name: ");
char str[13];
fgets(str, 13, stdin);
stripSpace(str);
stripSpace(str); //sanitise the input
int count = -1;
int comparitor = 1;
char nameStr[12];
while(comparitor != 0 && count <=720){
while(comparitor != 0 && count <=mainDex.totalPokemon){
count++;
strcpy(nameStr, mainDex.pokemon[count].name);
comparitor = strcmp(str,nameStr);
comparitor = strcmp(str,nameStr); //strcmp returns 0 if there are no differences, which is annoying in the sense of making logic work
}
if(!comparitor){
if(!comparitor){ //though it fortunately means that the while loop and this if statement can never run at the same time
printPokemon(count);
}
else{
......@@ -163,26 +181,29 @@ int identify(){
printf("\nEnter the Pokemon's ID: ");
int pokeID;
scanf("%d", &pokeID);
if(1 <= pokeID && pokeID < 722){
if(1 <= pokeID && pokeID < mainDex.totalPokemon){
printPokemon(pokeID-1);
}
else{
printf("That value is out of range of this Pokedex, please try a value between 1 and 721\n");
printf("That value is out of range of this Pokedex, please try a value between 1 and %d\n", mainDex.totalPokemon);
}
break;
case '3':
//case 3 and 4 are functionally very similar, the exception being what variable they compare
printf("\nEnter Pokemon Type: ");
fgets(str, 13, stdin);
stripSpace(str);
count = 0;
int output = 0;
while(count < 721){
//took me a while to realise that I needed the negative to get the matches
if(!strcmp(str, mainDex.pokemon[count].type1)){
printf("\n%d. %s", mainDex.pokemon[count].id, mainDex.pokemon[count].name);
output++;
}
count++;
}
//the output variable is fairly redundant but it means that the program knows if the user spelled fire with two r's (again)
if(!output){
printf("We couldn't find any with that type, did you type it correctly?\n");
}
......@@ -205,49 +226,254 @@ int identify(){
}
break;
case '5':
arrayLength = sizeof(mainDex.trainers)/sizeof(mainDex.trainers[0]);
arrayLength = mainDex.totalTrainers; //C's method of .length() - take the total memory size of the array and divide it by the size of one...
printf("\nWe have records for %d trainers.", arrayLength);
count = 0;
while(count <arrayLength){
while(count <arrayLength){ //the reason I needed array length
printf("\n%d. %s", count+1 , mainDex.trainers[count].name);
count++;
}
//much of this code is similar to case 2
printf("\nWhich would you like to know about? ");
int trainerID;
scanf("%d", &trainerID);
trainerID--;
if(trainerID > 0 && trainerID <= arrayLength){
int trainerID = 0;
trainerID = getch();
trainerID = trainerID-49; //It'd be more helpful for trainerID to be an Int, but the conversion isn't working and I'm not sure why
//so to fix that, I'm taking away 49 to make the number codes equal to what I want them to be (1=0etc) and converting manually
//printf("%d", trainerID);
if(trainerID >= 0 && trainerID <= arrayLength){
printDex(trainerID);
}
else{
printf("\nWe do not have any records for that trainer, please enter a value between 1 and %d", arrayLength);
}
break;
case '6':
case '6': //two ways out of this loop - pressing the key should return back to main, but on the off chance it doesn't it also ends the loop, and that returns to main too
printf("\nExitting Pokedex...");
return 0;
default:
default: //on the off chance someone thinks that g is a number
printf("\n\nInvalid Selection, please try again!\n");
}
}
return 0;
}
void newTrainer(char* name){
strcpy(mainDex.trainers[mainDex.totalTrainers].name, name);
mainDex.trainers[mainDex.totalTrainers].totalOwned = generateDex(mainDex.totalTrainers);
mainDex.totalTrainers++;
}
int newPokemon(){
char tempStr[21];
printf("\nEnter the Pokemon's Name: ");
fgets(tempStr, 11, stdin);
stripSpace(tempStr);
int count = -1;
int comparitor = 0;
char nameStr[12];
while(comparitor != 1 && count <=mainDex.totalPokemon){
count++;
strcpy(nameStr, mainDex.pokemon[count].name);
comparitor = strcmp(tempStr,nameStr); //strcmp returns 0 if there are no differences, which is annoying in the sense of making logic work
}
if(comparitor){ //though it fortunately means that the while loop and this if statement can never run at the same time
mainDex.pokemon[mainDex.totalPokemon].id = mainDex.totalPokemon + 1;
strcpy(mainDex.pokemon[mainDex.totalPokemon].name, tempStr);
printf("\nWhat is the species? Type in only the first section, as the Pokemon title will be added automatically!\n");
fgets(tempStr, 13, stdin);
strcpy(mainDex.pokemon[mainDex.totalPokemon].species, tempStr);
int typeChoice = 0;
int exitClause = 0;
while(exitClause != 1){
printf("\nHow many types does the Pokemon have?\n");
typeChoice = getch();
switch(typeChoice){
case '1':
printf("\nWhat is the type?\n");
fgets(tempStr, 7, stdin);
stripSpace(tempStr);
strcpy(mainDex.pokemon[mainDex.totalPokemon].type1, tempStr);
exitClause = 1;
break;
case '2':
printf("\nWhat is the first type?\n");
fgets(tempStr, 7, stdin);
stripSpace(tempStr);
strcpy(mainDex.pokemon[mainDex.totalPokemon].type1, tempStr);
printf("\nWhat is the second type?\n");
fgets(tempStr, 7, stdin);
getchar();
stripSpace(tempStr);
strcpy(mainDex.pokemon[mainDex.totalPokemon].type2, tempStr);
exitClause = 1;
break;
default:
printf("\nThere can only be either 1 or 2 types on a Pokemon.\n");
}
}
printf("\nWhat is the Pokemon's Ability?\n");
fgets(tempStr, 12, stdin);
strcpy(mainDex.pokemon[mainDex.totalPokemon].ability, tempStr);
printf("\nHow tall is the Pokemon in meters?\n");
float tempFl;
scanf("%f", &tempFl);
tempFl *= 10.0f;
tempFl = (int)tempFl;
mainDex.pokemon[mainDex.totalPokemon].height = tempFl;
printf("\nHow heavy is the Pokemon in kilograms?\n");
scanf("%f", &tempFl);
getchar();
tempFl *= 10.0f;
tempFl = (int)tempFl;
mainDex.pokemon[mainDex.totalPokemon].weight = tempFl;
printf("\nDo the males and females of this Pokemon have any physical differences?\n");
getchar();
fgets(tempStr, 5, stdin);
getchar();
for(int i = 0; i < 5; i++){
toupper(tempStr[i]);
}
strcpy(mainDex.pokemon[mainDex.totalPokemon].dimorphic, tempStr);
int evolutions = 0;
exitClause = 0;
while(exitClause != 1){
printf("\nDoes the Pokemon have any evolutions?\n1. No Evolution\t\t2. Can Evolve\n3. Evolves From Something\t\t4. Is A Middle Evolution\n");
evolutions = getch();
switch(evolutions){
case '1':
strcpy(mainDex.pokemon[mainDex.totalPokemon].evolveFrom, " ");
strcpy(mainDex.pokemon[mainDex.totalPokemon].evolveTo, " ");
exitClause = 1;
break;
case '2':
printf("\nWhat does it evolve into?\n");
fgets(tempStr, 11, stdin);
stripSpace(tempStr);
strcpy(mainDex.pokemon[mainDex.totalPokemon].evolveTo, tempStr);
strcpy(mainDex.pokemon[mainDex.totalPokemon].evolveFrom, " ");
exitClause = 1;
break;
case '3':
printf("\nWhat does this evolve from?\n");
fgets(tempStr, 11, stdin);
stripSpace(tempStr);
strcpy(mainDex.pokemon[mainDex.totalPokemon].evolveFrom, tempStr);
strcpy(mainDex.pokemon[mainDex.totalPokemon].evolveTo, " ");
exitClause = 1;
break;
case '4':
printf("\nWhat does this evolve from?\n");
fgets(tempStr, 11, stdin);
stripSpace(tempStr);
strcpy(mainDex.pokemon[mainDex.totalPokemon].evolveFrom, tempStr);
printf("\nWhat does this evolve from?\n");
fgets(tempStr, 11, stdin);
stripSpace(tempStr);
strcpy(mainDex.pokemon[mainDex.totalPokemon].evolveFrom, tempStr);
exitClause = 1;
break;
default:
printf("\nThat was not one of the options.\n");
}
}
mainDex.totalPokemon++;
}
else{
printf("\nSorry, that Pokemon already exists in the Pokedex.\n");
}
return 0;
}
int addOwned(int trainerID, int pokemonID){
mainDex.trainers[trainerID].owned[mainDex.trainers[trainerID].totalOwned] = mainDex.pokemon[pokemonID-1];
mainDex.trainers[trainerID].totalOwned++;
return 0;
}
int adminPanel(){
int choice = 0;
int arrayLength;
while(choice!='4'){
printf("\nWhat would you like to do?\n1. Add Trainer\t\t2. Add Pokemon\n3. Add Pokemon to Trainer\t4. Exit\n\nEnter Your Choice: ");
choice = getch();
switch(choice){
case '1': ;
char name[50];
printf("What is the character's name: ");
fgets(name, 49, stdin);
stripSpace(name);
strcpy(mainDex.trainers[mainDex.totalTrainers].name, name);
mainDex.trainers[mainDex.totalTrainers].totalOwned = 0;
mainDex.totalTrainers++;
break;
case '2':
newPokemon();
break;
case '3':;
int newPokeID;
int trainerID;
printf("\nWhat Trainer are we adding to?\n");
arrayLength = mainDex.totalTrainers;
int count = 0;
while(count <arrayLength){ //the reason I needed array length
printf("\n%d. %s", count+1 , mainDex.trainers[count].name);
count++;
}
printf("\n");
trainerID = getch();
trainerID = trainerID-49;
if(trainerID >= 0 && trainerID <= arrayLength){
printf("\nWhat Pokemon (by ID Number) have they caught?\n");
scanf("%d", &newPokeID);
if(0 < newPokeID && newPokeID < mainDex.totalPokemon){
addOwned(trainerID, newPokeID);
}
else{
printf("That value is out of range of this Pokedex.");
}
}else{
printf("That value is out of range of this Pokedex.");
}
break;
case '4':
printf("Exiting Admin Mode...");
return 0;
}
return 0;
}
}
int frontMenu(){
int choice = 0;
while(choice!='3'){
printf("\nWhat would you like to do?\n1. Pokedex\t\t2. Admin Mode\n3. Exit\n\nEnter Your Choice: ");
choice = getch();
switch(choice){
case '1':
identify();
break;
case '2':
adminPanel();
break;
case '3':
printf("\nClosing Program...");
return 0;
}
}
}
int main(void){
mainDex.totalPokemon = 0;
mainDex.totalTrainers = 0;
printf("Loading Pokedex, please wait...");
populateDex();
strcpy(mainDex.trainers[0].name, "Ash Ketchum");
mainDex.trainers[0].totalOwned = generateDex(0);
strcpy(mainDex.trainers[1].name, "Brock Harrison");
mainDex.trainers[1].totalOwned = generateDex(1);
strcpy(mainDex.trainers[2].name, "Misty Williams");
mainDex.trainers[2].totalOwned = generateDex(2);
strcpy(mainDex.trainers[3].name, "Elizabeth Keen");
mainDex.trainers[3].totalOwned = generateDex(3);
strcpy(mainDex.trainers[4].name, "Dawn Hikari");
mainDex.trainers[4].totalOwned = generateDex(4);
identify();
newTrainer("Ash Ketchum");
newTrainer("Brock Harrison");
newTrainer("Misty Williams");
newTrainer("Elizabeth Keen");
newTrainer("Dawn Hikari");
frontMenu();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment