Commit 209047bc authored by henry.dennett's avatar henry.dennett

pokedex

parents
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct sPerson{//struct for the trainer
char Name[50];//holds trainer name
char PokemonHas[255]; //holds the name of the pokemon a trainer has
int PokemonCount; //used for storing how many pokemon a trainer has
struct sPerson *nextInLineT;//
};
struct Pokemon{// struct for the pokemon
char PName[50];//holds pokemon name
char Type[50];//holds pokemon type
char ability[50];//holds pokemon ability
struct Pokemon *nextInLineP;
};
struct loopbreaker{
int LoopBreak;//used for stopping a loop in checkpokemon(), moveP and moveT
};
typedef struct loopbreaker loop ;
struct sPerson *getNewPerson(const char Name[50], const char PokemonHas[255]){
struct sPerson *newPerson = NULL;//creates a new pointer called new person
newPerson = malloc(sizeof(struct sPerson));//allocates memory for newperson
if (newPerson != NULL){
newPerson->nextInLineT = NULL;
strcpy(newPerson->Name, Name);//adds the users input for the trainer name to the list
strcpy(newPerson->PokemonHas, PokemonHas);//adds
printf("created new person at %p\n", newPerson);
}
else{
printf("Allocation Failure!! \n");//if adding a new person fails prints error message
}
return newPerson;//returns value back to the main()
}
void printPerson(const struct sPerson *person, const char *comment)
{
if(person == NULL){// if there are no trainers in the list it will state it is empty
printf("%s is NULL\n", comment);
}
else{//otherwise it prints out the trainer name, its place in memory and the memory location of the next person
printf("Name: %s|Pokemon: %s|Pokemon count: %d|address: %p|nextInLine: %p\n",person->Name,person->PokemonHas,person->PokemonCount,person,person->nextInLineT);
}
}
void PrintTList(const struct sPerson *list)
{
printf("printing List:\n");
const struct sPerson *t;//creates a pointer called 't'
t = list;//ta value is set to the the list
if(t == NULL)//runs when the list is empty
{
printf("List is empty\n");
}
else
{
while(t) //runs for the length of the list
{
printPerson(t, "t");//calls thr PrintPerson function
t = t->nextInLineT;//assignes the value of what is next in line to t
}
}
}
void CheckPokemon(const struct Pokemon *list,char *PokemonToBeAdded[255],loop *a)//functions the same as searching for a trainer
{
printf("printing List:\n");
const struct Pokemon *t;
const struct Pokemon *tt;
t = list;
tt = t;
if(t == NULL)
{
printf("List is empty\n");
}
else
{
while(t+1)
{
printf("\nloopbreak value: %d\n",a->LoopBreak);//unlike searching for a trainer it will not run another function here
if (a->LoopBreak == 1){
break;
}else{
moveP(t,"",PokemonToBeAdded,a);
tt =t;
t = t->nextInLineP;
}
}
}
}
void moveP(const struct Pokemon *pokemon, const char *comment, char *PokemonToBeAdded[255], loop *a)//functions the same as the trainer equivilent
{
char check[50]; strcpy(check, pokemon->PName);
if(pokemon == NULL){
printf("%s is NULL\n", comment);
printf("%s\n",check);
printf("%s\n",PokemonToBeAdded);
}
else if (strcmp(check, PokemonToBeAdded) ==0){
a->LoopBreak++;
}
else{
printf(" ");
}
}
void printSPerson(struct sPerson *person,const struct Pokemon* pokemon,const char *comment, char *searchT[50],loop *a)
{
int num = 0;//used for setting the value for the trainers pokemon count to zero due to a bug
char space[10] =" ";//used for adding a space to the users pokemon input
char dec[10];//decission variable
char fcheck[10] = "n";//used for adding a value to pokemonHas later in the code
char chex[10] = "Y";//used later to check if the user has entered the value of 'check'
char check[50]; strcpy(check, person->Name);//assigns the value of the nodes value 'Name' to 'check'
char PokemonIsAdded[255]; strcpy(PokemonIsAdded,person->PokemonHas);//assigns the value of 'PokemonHas' in the node
char PokemonToBeAdded[255];//for user input
if (person->PokemonCount>100){//this is a fix to a bug in the program when there are no input in pokemoncount it has a really high numerical value
person->PokemonCount=num;//this sets that value to zero
}
if (strcmp(PokemonIsAdded, fcheck)==0){//checks whether there is a match between 'fcheck' and PokemonIsAdded
//if there have been no pokemon added before the default value should be 'n'
//and if it is of the value of n then it assigns 'Pokemon: ' to pokemonHas in the list
strcpy(person->PokemonHas, "Pokemon: ");
strcpy(PokemonIsAdded,person->PokemonHas);
}
if(person == NULL){// if there are no trainers in the list it will state it is empty
printf("%s is NULL\n", comment);
}
else if (strcmp(check, searchT) ==0){//if the users input matches with the list (which it should be) runs code
printf("\nName:%s address:%p nextInLine:%p\n",person->Name,person,person->nextInLineT);//tells user the trainer they have chosen so they can see the details of them
printf("Would you like to add to this Trainer?\n");
printf("Y for yes and N for no: ");
gets(dec);//gets user input for decission
if (strcmp(dec,chex)==0){//if the user input is equal to the value 'Y' runs code bellow
printf("\nWhat is the name of the pokemon you want to add? ");
gets(PokemonToBeAdded);//takes user input for what pokemon they want to add
CheckPokemon(pokemon,PokemonToBeAdded,a);//runs function to check whether pokemon is in its list
if (a->LoopBreak == 1){//Loopbreak will have the value of one if the pokemon they want to add exists
printf("\nAdding %s\n",PokemonToBeAdded);//prints the name of the pokemon the user is adding
strcat(PokemonIsAdded,PokemonToBeAdded);//combines what the name of the pokemon the user wants to add to the what they already have
strcat(PokemonIsAdded,space);//adds a space to the end so if the user wants to add another pokemon they wont be all one word
strcpy(person->PokemonHas,PokemonIsAdded);//the value of 'pokemonUsAdded' is copied to the 'PokemonHas' variable in the trainer node
person->PokemonCount++;//increases the trainers pokemon count by one
printf(person->PokemonHas);//prints out all the pokemon the trainer currently has for confirmation
a->LoopBreak--;//sets 'LoopBreak' back to zero so it can be reused
}
else{//if loobreak is not the value of 1 that means the program was unable to find the pokemon the user wanted to add
printf("Pokemon input is not in pokedex\n");//thus it informs the user and goes back to the main loop
}
}
else{//if the user has not inputed a value then it outputs what the user has done and it will go back to the main loop
printf("You have chosen to not add a pokemon\n");
}
}
}
void move(const struct sPerson *person, const char *comment, char *searchT[50], loop *a)
{
char check[50]; strcpy(check, person->Name);//assigns the name of the person to the variable check
if(person == NULL){// if there are no trainers in the list it will state it is empty
printf("%s is NULL\n", comment);
printf("%s\n",check);
printf("%s\n",searchT);
}
else if (strcmp(check, searchT) ==0){//if there is a match then it incriments the
a->LoopBreak++;
}
else{//otherwise it prints a blank line
printf(" ");
}
}
void SearchTList(const struct sPerson *list,const struct Pokemon *pokemon,char searchT[50], loop *a)
{
printf("printing List:\n");
const struct sPerson *t;
const struct sPerson *tt;
t = list;
tt = t;//tt and t both share the same value right now
if(t == NULL)
{
printf("List is empty\n");
}
else
{
while(t+1)//needs to be plus one otherwise it wont run the loopbreakcheck properly
{
printf("\nloopbreak value: %d\n",a->LoopBreak);
if (a->LoopBreak == 1){//if the loopbreaker has the value of one
a->LoopBreak--;//brings the value of loopbreaker to 0 so it can sun again
printSPerson(tt,pokemon,"",searchT,a);//runs printSPerson
break;//breaks the while loop
}else{
move(t,"",searchT,a);
tt =t;//holds the value of the previous node
t = t->nextInLineT; //assigns the value of the next node to 't'
}
}
}
}
struct Pokemon *getNewPokemon(const char PName[50], char Type[50], char ability[50]){//functions the same to the trainer equivilent
struct Pokemon *newPokemon = NULL;
newPokemon = malloc(sizeof(struct Pokemon));
if (newPokemon != NULL){
newPokemon->nextInLineP = NULL;
strcpy(newPokemon->PName, PName);
strcpy(newPokemon->Type, Type);
strcpy(newPokemon->ability, ability);
printf("created new pokemon at %p\n", newPokemon);
}
else{
printf("Allocation Failure!! \n");
}
return newPokemon;
}
void printPokemon(const struct Pokemon *pokemon, const char *comment)//functions the same to the trainer equivilent
{
if(pokemon == NULL){
printf("%s is NULL\n", comment);
}
else{
printf("Pokemon Name: %s|Type: %s|ability: %s|address: %p|nextInLine: %p\n",pokemon->PName,pokemon->Type,pokemon->ability,pokemon,pokemon->nextInLineP);
}
}
void PrintPList(const struct Pokemon *list)//functions the same to the trainer equivilent
{
printf("printing List:\n");
const struct Pokemon *t;
t = list;
if(t == NULL)
{
printf("List is empty\n");
}
else
{
while(t)
{
printPokemon(t, "t");
t = t->nextInLineP;
}
}
}
void CleanUp(struct sPerson *list,struct Pokemon *listP){//function for freeing data from memory
struct sPerson *next;//creates pointer
struct Pokemon *nextP;
while(list)//frees all content of the trainer list
{
next = list->nextInLineT;//next as the next node to the current node
printf("Cleaning %s\n", list->Name);
free(list);// frees memory
list = next; //sets the next node as the curent node
}
while(listP)//frees content from pokemon list
{
nextP = listP->nextInLineP;
printf("Cleaning %s\n", list->Name);
free(listP);
listP = next;
}
}
int main(void)
{
printf("\n\n** START **\n\n");
struct sPerson *firstT = NULL;
struct sPerson *addedT = NULL;
struct Pokemon *firstP = NULL;
struct Pokemon *addedP = NULL;
loop a;
a.LoopBreak = 0;
loop *re =&a;
char command[64];
char Name[50];
char PokemonHas[255];
int PokemonCount =0;
char PName[50];
char Type[50];
char ability[50];
char searchT[50];
while(1)//runs the script constantly
{
printf("\n1|Enter 'q' for quit\n");
printf("2|Type 'printT' to print all Trainers\n");
printf("3|Type 'printP' to print all Pokemon\n");
printf("4|Type 'Pokemon' to add a Pokemon\n");
printf("5|Type searchT to search for a Trainer or add a pokemon to them\n");
printf("6|Type 'Trainer' to add a trainer\n");
printf(" |Enter a command: ");
fgets(command, 64, stdin);//users command is taken
if (strcmp("q\n", command) ==0)//if user enters 'q' the loop will break
{
printf("Quitting...\n");
break;
}
else if (strcmp("printT\n", command) == 0){//user entering 'printT' will run the PrintList function
printf("Printing...\n");
PrintTList(firstT);
}
else if(strcmp("printP\n", command) ==0){//user entering 'printP' will run PrintPList function
printf("Printing...\n");
PrintPList(firstP);
}
else if(strcmp("searchT\n", command) == 0){//user entering 'searchT' will run the SearchTList function
printf("Who would you like to search for/update?");
gets(searchT);//takes user input for what trainer they want the details for/update
printf("Searching...\n");
SearchTList(firstT,firstP,searchT,re);// pointers to the pokemon and trainer lists are passed through, who they want to search for and a pointer to the lookbreaker is passed also
}
else if (strcmp("Trainer\n", command) ==0){//if user enters 'Trainer' then the code bellow runs
printf("Enter Trainer name: ");
gets(Name);// user inputs trainer they want to add
printf("Adding %s\n", Name);
if (firstT == NULL)//if the list is empty
{
firstT = getNewPerson(Name,PokemonHas);//value of the result of the function getNewPerson is assigned to 'firstT'
if(firstT != NULL)//if it is not empty
{
addedT = firstT;//assigns the value of firstT to addedT
}
}else//otherwise if the list is not empty
{
addedT->nextInLineT = getNewPerson(Name,PokemonHas);//passes the values through the function getNewPerson and what is returned to assigned to the node next in line
if(addedT->nextInLineT != NULL)//if the next in line for 'addedT' is not empty
{
addedT = addedT->nextInLineT;//assign the next value in line of 'addedT' to addedT
}
}
}
else if (strcmp("Pokemon\n", command) ==0){//functions the same as adding a trainer accept for adding a pokemon
printf("Enter Pokemon name: ");
gets(PName);// user inputs trainer they want to search for
printf("Enter Pokemon Type: ");
gets(Type);
printf("Enter Pokemon ability: ");
gets(ability);
printf("Adding %s\n", PName);
if (firstP == NULL)
{
firstP = getNewPokemon(PName, Type, ability);
if(firstP != NULL)
{
addedP = firstP;
}
}
else
{
addedP->nextInLineP = getNewPokemon(PName, Type, ability);
if(addedP->nextInLineP != NULL)
{
addedP = addedP->nextInLineP;
}
}
}
}
CleanUp(firstT,firstP);//calls clear up function
firstT = NULL;
addedT = NULL;
return 0;
}
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