Commit dfafbdea authored by stephanie.smith's avatar stephanie.smith

Replace Pokedex.c

parent ff198193
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// structs to hold addresses for future reference
struct Player {
char name[20];
int PokemonCount;
struct Pokemon *array[20];
struct Player *next;
};
struct Pokemon {
char name[20];
char type[20];
char ability[20];
struct Pokemon *next;
};
struct Pokedex {
struct Player *playerList;
struct Pokemon *pokemonList;
};
// POKEMON FUNCTIONS///////////////////////////////////////////////
//function to add new pokemon to Pokemon struct using new_node
struct Pokemon *NewPokemonNode(char name[20], char ability[20], char type[20]) {
struct Pokemon *new_node = NULL;
new_node = malloc(sizeof(struct Pokemon));
if (new_node != NULL) {
strcpy(new_node->name, name);
strcpy(new_node->ability, ability);
strcpy(new_node->type, type);
new_node->next = NULL;
}
return new_node;
}
//function to add Pokemon called in main to PokemonList
void AddPokemonToList(struct Pokemon *PokemonList, char name[20], char ability[20], char type[20]) {
struct Pokemon *NewNode = NewPokemonNode(name, ability, type);
struct Pokemon *temp = PokemonList;
while (temp->next != NULL) {
temp = temp->next;
}
if (temp->next == NULL) {
temp->next = NewNode;
return;
}
return;
}
//finds pokemon in PokemonList called in main and returned as temp
struct Pokemon *FindPokemon(struct Pokemon *PokemonList, char name[20]) {
struct Pokemon *temp = PokemonList;
int value;
while (temp->next != NULL) {
value = strcmp(name, temp->name);
if (value == 0) {
return temp;
}
if (name == temp->name) {
return temp;
}
temp = temp->next;
}
if (temp->next == NULL) {
value = strcmp(name, temp->name);
if (value == 0) {
return temp;
}
}
return temp;
}
// PLAYER FUNCTIONS//////////////////////////////////////////////////
//functions the same Pokemon but converted for player use
struct Player *NewPlayerNode(char name[20]) {
struct Player *new_node = NULL;
new_node = malloc(sizeof(struct Player));
if (new_node != NULL) {
strcpy(new_node->name, name);
new_node->next = NULL;
}
return new_node;
}
void AddPlayerToList(struct Player *PlayerList, char name[20]) {
struct Player *NewNode = NewPlayerNode(name);
struct Player *temp = PlayerList;
while (temp->next != NULL) {
temp = temp->next;
}
if (temp->next == NULL) {
temp->next = NewNode;
return;
}
return;
}
struct Player *FindPlayer(struct Player *PlayerList, char name[20]) {
struct Player *temp = PlayerList;
int value;
while (temp->next != NULL) {
value = strcmp(name, temp->name);
if (value == 0) {
return temp;
}
if (name == temp->name) {
return temp;
}
temp = temp->next;
}
if (temp->next == NULL) {
value = strcmp(name, temp->name);
if (value == 0) {
return temp;
}
}
return temp;
}
// DISPLAY///////////////////////////////////////////////////////////
//'FindPokemon' goes through the Pokemon List and prints each pokemon and details when called in main
void DisplayPokemonDetails(struct Pokemon *PokemonList, char name[20]) {
printf("Found Pokemon in Pokedex:\n");
struct Pokemon *foundpokemon = FindPokemon(PokemonList, name);
printf("Name - %s\n", foundpokemon->name);
printf("Ability - %s\n", foundpokemon->ability);
printf("Type - %s\n", foundpokemon->type);
}
// LIST ALL//////////////////////////////////////////////////////////
//uses pointers to find the end of list and stop printing when pointer reaches end of list
void ListPokemon(struct Pokemon *PokemonList) {
printf("Pokemon in Pokedex:\n");
struct Pokemon *temp = PokemonList;
while (temp->next != NULL) {
printf("Name - %s\n", temp->name);
temp = temp->next;
}
if (temp->next == NULL) {
printf("Name - %s\n", temp->name);
return;
}
}
void ListPlayer(struct Player *PlayerList) {
printf("Player in Pokedex:\n");
struct Player *temp = PlayerList;
while (temp->next != NULL) {
printf("Name - %s\n", temp->name);
temp = temp->next;
}
if (temp->next == NULL) {
printf("Name - %s\n", temp->name);
return;
}
}
// ADD POKEMON TO PLAYER/////////////////////////////////////////////
//calls functions from above, each player gets a pokemon assigned to it
void AddPokemonToPlayer(struct Pokemon *PokemonList, struct Player *PlayerList,
char playername[20], char pokename[20]) {
struct Pokemon *foundpokemon = FindPokemon(PokemonList, pokename);
struct Player *foundplayer = FindPlayer(PlayerList, playername);
for (int x = 0; x < 20; x = x + 1) {
if (foundplayer->array[x] == NULL) {
foundplayer->array[x] = foundpokemon;
break;
} else if (foundplayer->array[x]) {
foundplayer->array[x + 1] = foundpokemon;
break;
}
};
}
// PLAYER DETAILS//////////////////////////////////////////////////
void DisplayPlayerDetails(struct Player *PlayerList, char name[20]) {
printf("Pokemon that belongs to player: \n");
struct Player *findplayer = FindPlayer(PlayerList, name);
struct Pokemon *poke;
for (int x = 0; x < 20; ++x) {
poke = findplayer->array[x];
if (poke != NULL) {
printf("Name - %s\n", poke->name);
} else {
continue;
}
}
}
// MAIN/////////////////////////////////////////////////////////////
int main() {
//functions are called in order to run and display details requested
// POKEMON CALLING FUNCTIONS////////////////////////////////////////
struct Pokedex *StartList = NULL;
struct Pokemon *Growlithe = NULL;
Growlithe = malloc(sizeof(struct Pokemon));
if (Growlithe != NULL) {
strcpy(Growlithe->name, "Growlithe");
strcpy(Growlithe->ability, "Fire Ball");
strcpy(Growlithe->type, "Fire");
Growlithe->next = NULL;
}
struct Pokemon *PokemonList = StartList->pokemonList;
PokemonList = NULL;
if (PokemonList == NULL) {
PokemonList = Growlithe;
}
AddPokemonToList(PokemonList, "Pikatchu", "Shock", "Electric");
AddPokemonToList(PokemonList, "Milotic", "Tail Whip", "Electric");
FindPokemon(PokemonList, "Pikatchu");
// PLAYER CALLING FUNCTION///////////////////////////////////////////
struct Player *Ash = NULL;
Ash = malloc(sizeof(struct Player));
if (Ash != NULL) {
strcpy(Ash->name, "Ash");
Ash->next = NULL;
}
struct Player *PlayerList = StartList->playerList;
PlayerList = NULL;
if (PlayerList == NULL) {
PlayerList = Ash;
}
AddPlayerToList(PlayerList, "Brett");
AddPlayerToList(PlayerList, "Dawn");
FindPlayer(PlayerList, "Ash");
// DISPLAY/////////////////////////////////////////////////////////
DisplayPokemonDetails(PokemonList, "Pikatchu");
ListPokemon(PokemonList);
ListPlayer(PlayerList);
AddPokemonToPlayer(PokemonList, PlayerList, "Ash", "Pikatchu");
AddPokemonToPlayer(PokemonList, PlayerList, "Ash", "Growlithe");
DisplayPlayerDetails(PlayerList, "Ash");
return 0;
}
\ No newline at end of file
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