Commit 8a433112 authored by sam.pople's avatar sam.pople

submission

parent afe94017
......@@ -4,11 +4,11 @@
# include "pokedexstructs.h"
//main file. Run from here.
//functions
void ListPokemon(Pokedex *pokedex)
void ListPokemon(Pokedex *pokedexpointer)
{
// This displays the list of pokemon.
//This assigns a node to be a head for the pokemon list
PokemonNode *current = pokedex->pokemonhead;
PokemonNode *current = pokedexpointer->pokemonhead;
printf("\nList of Pokemon:\n");
while (current != NULL)
{ //The current node is printed and then the next is assigned.
......@@ -18,10 +18,10 @@ void ListPokemon(Pokedex *pokedex)
}
//This displays the list of players
void ListPlayers(Pokedex* pokedex)
void ListPlayers(Pokedex* pokedexpointer)
{
//The first player item is made the head
PlayerNode* current = pokedex->playerhead;
PlayerNode* current = pokedexpointer->playerhead;
printf("\nList of Players:\n");
while (current != NULL)
{
......@@ -32,87 +32,88 @@ void ListPlayers(Pokedex* pokedex)
}
//This creates each Pokemon node
PokemonNode* NewPokemonNode(char name[20], char type[20], char ability[50])
PokemonNode* NewPokemonNode(char pokename[20], char poketype[20], char pokeability[50])
{
PokemonNode* addnode = malloc(sizeof(PokemonNode));
strcpy(addnode->name, name);
strcpy(addnode->type, type);
strcpy(addnode->ability, ability);
addnode->nextpokemon = NULL;
strcpy(addnode->name, pokename);
strcpy(addnode->type, poketype);
strcpy(addnode->ability, pokeability);
return addnode;
}
//This creates each Player node
PlayerNode *NewPlayerNode(char name[20])
PlayerNode *NewPlayerNode(char playername[20])
{
PlayerNode* addnode = malloc(sizeof(PlayerNode));
strcpy(addnode->name, name);
addnode->nextplayer = NULL;
addnode->numofpokemon = 0;
memset(addnode->PokemonArray, NULL, sizeof(addnode->PokemonArray));
strcpy(addnode->name, playername);
memset(addnode->PokemonArray, 0, sizeof(addnode->PokemonArray));
addnode->nextplayer = NULL;
return addnode;
}
//This finds the Pokemon in the Pokemon list
PokemonNode* FindPokemon(PokemonNode* head, char name[20])
PokemonNode* FindPokemon(PokemonNode* pokehead, char pokename[20])
{
PokemonNode *current = head;
PokemonNode *current = pokehead;
while (current != NULL)
{
//If the current node matches the searched for node,a pointer is returned to it or they move on to the next or return nothing
if (strcmp(name, current->name) == 0)
if (strcmp(pokename, current->name) != 0)
{
return current;
current = current->nextpokemon;
}
else
{
current = current->nextpokemon;
return current;
}
}
return 0;
}
//This searches the list for the specific player
PlayerNode* FindPlayer(PlayerNode* playerhead, char name[20])
PlayerNode* FindPlayer(PlayerNode* playerhead, char playername[20])
{
PlayerNode* current = playerhead;
while (current != NULL)
{
if (strcmp(name, current->name) == 0)
if (strcmp(playername, current->name) != 0)
{
return current;
current = current->nextplayer;
}
else
{
current = current->nextplayer;
return current;
}
}
return 0;
}
//This adds a new Pokemon to the Pokemon list
void AddPokemonToList(Pokedex* pokedex, char name[20], char type[20], char ability[50])
void AddPokemonToList(Pokedex* pokedexpointer, char pokename[20], char poketype[20], char pokeability[50])
{
if (FindPokemon(pokedex->pokemonhead, name) == 0)
if (FindPokemon(pokedexpointer->pokemonhead, pokename) != 0)
{
PokemonNode *current = NewPokemonNode(name, type, ability);
current->nextpokemon = pokedex->pokemonhead;
pokedex->pokemonhead = current;
printf("Pokemon %s added\n", name);
printf("Pokemon exists\n");
}
else
{
printf("Pokemon exists\n");
PokemonNode *current = NewPokemonNode(pokename, poketype, pokeability);
current->nextpokemon = pokedexpointer->pokemonhead;
pokedexpointer->pokemonhead = current;
printf("Pokemon %s added\n", pokename);
}
}
//This adds new players to the player list
void AddPlayerToList(Pokedex* pokedex, char name[20])
void AddPlayerToList(Pokedex* pokedexpointer, char playername[20])
{
if (FindPlayer(pokedex->playerhead, name) == 0)
if (FindPlayer(pokedexpointer->playerhead, playername) == 0)
{ //If the player isnt already in the list, a new node is created
PlayerNode* current = NewPlayerNode(name);
current->nextplayer = pokedex->playerhead;
pokedex->playerhead = current;
printf("Player %s Added\n", name);
PlayerNode* current = NewPlayerNode(playername);
current->nextplayer = pokedexpointer->playerhead;
pokedexpointer->playerhead = current;
printf("Player %s Added\n", playername);
}
else
{
......@@ -120,11 +121,11 @@ void AddPlayerToList(Pokedex* pokedex, char name[20])
}
}
//This assigns Pokemon to players
void AddPokemonToPlayer(Pokedex* pokedex, char pokemon[20], char player[20])
void AddPokemonToPlayer(Pokedex* pokedexpointer, char pokemon[20], char player[20])
{
PlayerNode* PlayerPointer = FindPlayer(pokedex->playerhead, player);
PokemonNode* PokemonPointer = FindPokemon(pokedex->pokemonhead, pokemon);
if ((PokemonPointer != 0) && (PlayerPointer != 0))
PokemonNode* PokemonPointer = FindPokemon(pokedexpointer->pokemonhead, pokemon);
PlayerNode* PlayerPointer = FindPlayer(pokedexpointer->playerhead, player);
if ((PokemonPointer != NULL) && (PlayerPointer != NULL))
{
for (int x=0; x <= PlayerPointer->numofpokemon; x++)
{
......@@ -137,19 +138,19 @@ void AddPokemonToPlayer(Pokedex* pokedex, char pokemon[20], char player[20])
}
else if (PlayerPointer->PokemonArray[x] == PokemonPointer)
{
printf("\n%s is already with %s.", PokemonPointer->name, PlayerPointer->name);
printf("\n%s is already with %s.\n", PokemonPointer->name, PlayerPointer->name);
break;
}
}
}
}
//This displays the details of the Pokemon, including their Name, type and primary ability.
void DisplayPokemonDetails(Pokedex* pokedex, char name[20])
void DisplayPokemonDetails(Pokedex* pokedexpointer, char pokename[20])
{
PokemonNode* current = FindPokemon(pokedex->pokemonhead, name);
if (current != 0)
PokemonNode* current = FindPokemon(pokedexpointer->pokemonhead, pokename);
if (current != NULL)
{
printf("\nName: %s \n", current->name);
printf("\nName: %s\n", current->name);
printf("Type: %s\n", current->type);
printf("Main ability: %s\n", current->ability);
}
......@@ -159,14 +160,14 @@ void DisplayPokemonDetails(Pokedex* pokedex, char name[20])
}
}
//This displays all the details of the current player node
void DisplayPlayerDetails(Pokedex* pokedex, char name[20])
void DisplayPlayerDetails(Pokedex* pokedexpointer, char playername[20])
{
PlayerNode* current = FindPlayer(pokedex->playerhead, name);
if (current != 0) {
PlayerNode* current = FindPlayer(pokedexpointer->playerhead, playername);
if (current != NULL) {
printf("\nName: %s\n", current->name);
printf("Number of Pokemon: %d\n", current->numofpokemon);
printf("Pokemon they have:\n");
if (current != 0)
if (current != NULL)
{
if (current->numofpokemon > 0)
{
......@@ -179,14 +180,19 @@ void DisplayPlayerDetails(Pokedex* pokedex, char name[20])
}
}
//testing
int main()
int main(void)
{ // this starts the Pokedex and gives it a head pokemon and player
Pokedex pokedexvalues;
PokemonNode* pokemonhead = NewPokemonNode("Charizard", "Fire", "Blaze");
PlayerNode* playerhead = NewPlayerNode("Alan");
PokemonNode* pokemonhead = NewPokemonNode("Charizard", "Fire", "Blaze");
Pokedex pokedexvalues;
Pokedex* pokedexvaluespointer = &pokedexvalues;
pokedexvalues.playerhead = playerhead;
pokedexvalues.pokemonhead = pokemonhead;
Pokedex* pokedexvaluespointer = &pokedexvalues;
//This adds Pokemon to the Pokedex list and sets their values
AddPokemonToList(pokedexvaluespointer, "Jigglypuff", "Fairy", "Cute Charm");
AddPokemonToList(pokedexvaluespointer, "Venusaur", "Grass", "Overgrow");
......
No preview for this file type
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