Commit 7518d06b authored by a-j.towse's avatar a-j.towse

Working player functions

parent 21a92d6c
No preview for this file type
......@@ -30,10 +30,10 @@ typedef struct PokemonNode {
typedef struct PlayerNode {
char name[50];
int pokemonCount;
struct PokemonNode * pokemonArray[20];
struct PlayerNode *next;
struct PlayerNode *prev;
struct PokemonNode pokemonList[];
}PlayerNode;
//Define functions for PokemonNode struct
......@@ -42,18 +42,56 @@ void AddPokemonToList(struct Pokedex pokedex,char name[], char type[], char abil
struct PokemonNode * FindPokemon(struct Pokedex pokedex, char name[]);
//Define functions for PlayerNode struct
struct PlayerNode * NewPlayerNode(char name);
void AddPlayerToList(struct Pokedex *pokedex, char name);
struct PlayerNode * FindPlayer(struct Pokedex pokedex, char name);
struct PlayerNode * NewPlayerNode(char name[]);
void AddPlayerToList(struct Pokedex pokedex, char name[]);
struct PlayerNode * FindPlayer(struct Pokedex pokedex, char name[]);
//Define functions for other functionality
void AddPokemonToPlayer(struct Pokedex pokedex, char Playername, char pokemonName);
void DisplayPokemonDetails(struct Pokedex pokedex, char name);
void AddPokemonToPlayer(struct Pokedex pokedex, char playerName[], char pokemonName[]);
void DisplayPokemonDetails(struct Pokedex pokedex, char name[]);
void DiplayPlayerDetails(struct Pokedex pokedex, char name);
void ListPokemon(struct Pokedex pokedex);
void ListPlayers(struct Pokedex pokedex);
//Create NewPokemonNode function - returns variable of type PokemonNode
//main
int main (void) {
PokemonNode *PokemonHead = NULL; //Create head for DLL of pokemon
PlayerNode *PlayerHead = NULL; //Create head for DLL of players
Pokedex pokedex; //Create a pointer to a Pokedex structure, NULL
pokedex.ptrToPlayerHead = &PlayerHead; //Set pointer value head of Player List
pokedex.ptrToPokemonHead = &PokemonHead; //Set pointer value to head of Pokemon List
/*Tests for function implementation*/
//NewPokemonNode("Charmander","Fire","FireBallz");
AddPokemonToList(pokedex,"Charmander","Fire","FireBallz");
AddPokemonToList(pokedex,"Squirtle","Water","WaterBlast");
AddPokemonToList(pokedex,"Squirtle","Water","WaterBlast"); //Testing that it doesnt add a duplicate
AddPokemonToList(pokedex,"The grass one","Leaf","LeafSomething");
AddPokemonToList(pokedex,"Poki1","Leaf","LeafSomething");
AddPokemonToList(pokedex,"Poki2","Leaf","LeafSomething");
//printf("%p\n",FindPokemon(pokedex,"Charmander"));
ListPokemon(pokedex);
//NewPlayerNode("Joe");
AddPlayerToList(pokedex,"Joe");
AddPlayerToList(pokedex,"Josh");
AddPlayerToList(pokedex,"James");
//printf("%p\n",FindPlayer(pokedex,"James"));
ListPlayers(pokedex);
AddPokemonToPlayer(pokedex,"Joe","Charmander");
return 0;
}
//NewPokemonNode function - returns variable of type PokemonNode
struct PokemonNode * NewPokemonNode(char name[], char type[], char ability[] ) {
struct PokemonNode *newNode = NULL; //Create a pointer to a PokemonNode structure, NULL for safety
......@@ -70,7 +108,7 @@ struct PokemonNode * NewPokemonNode(char name[], char type[], char ability[] ) {
return newNode;
}
//Create AddPokemonToList function - returns void
//AddPokemonToList function - returns void
void AddPokemonToList(struct Pokedex pokedex,char name[], char type[], char ability[]){
PokemonNode *newNode = NewPokemonNode(name,type,ability); //Call NewPokemonNode and store ptr to new node in variable
......@@ -98,7 +136,7 @@ void AddPokemonToList(struct Pokedex pokedex,char name[], char type[], char abil
}
}
//Create Findpokemon function, returns pointer to the node if found, NULL (nil) if not found
//Findpokemon function - returns pointer to the node if found, NULL (nil) if not found
struct PokemonNode * FindPokemon(struct Pokedex pokedex, char name[]) {
PokemonNode **ptrHead = pokedex.ptrToPokemonHead; //Create pointer to pointer variable from pokedex pointer
......@@ -116,7 +154,7 @@ struct PokemonNode * FindPokemon(struct Pokedex pokedex, char name[]) {
return NULL;
}
//Create ListPokemon function, prints a list of pokemon names, returns void
//ListPokemon function - prints a list of pokemon names, returns void
void ListPokemon(Pokedex pokedex) {
PokemonNode **ptrHead = pokedex.ptrToPokemonHead; //Create pointer to pointer variable from pokedex pointer
......@@ -124,34 +162,105 @@ void ListPokemon(Pokedex pokedex) {
//Traverse list with temp until NULL
while (temp != NULL) {
printf("Name: %s\n",temp->name); //Print the name of the node that temp is currently pointing to
printf("Pokemon Name: %s\n",temp->name); //Print the name of the node that temp is currently pointing to
temp = temp->next; //temp increments to the next item in the list
}
}
int main (void) {
//NewPlayerNode function - returns variable of type PlayerNode
struct PlayerNode * NewPlayerNode(char name[]){
PokemonNode *PokemonHead = NULL; //Create head for DLL of pokemon
PlayerNode *PlayerHead = NULL; //Create head for DLL of players
struct PlayerNode *newNode = NULL; //Create a pointer to a PlayerNode structure, NULL for safety
newNode = malloc(sizeof(struct PlayerNode)); //Assign memory space for PlayerNode
Pokedex pokedex; //Create a pointer to a Pokedex structure, NULL
pokedex.ptrToPlayerHead = &PlayerHead; //Set pointer value head of Player List
pokedex.ptrToPokemonHead = &PokemonHead; //Set pointer value to head of Pokemon List
if (newNode != NULL) { //Ensure it exists
strcpy(newNode->name,name); //Set name
newNode->pokemonCount = 0; //Set pokemon count to 0
newNode->next = NULL; //Set next pointet to NULL
newNode->prev = NULL; //Set prev pointer to NULL
}
/*Tests for function implementation*/
return newNode;
}
//NewPokemonNode("Charmander","Fire","FireBallz");
//AddPlayerToList function - returns void
void AddPlayerToList(struct Pokedex pokedex, char name[]) {
AddPokemonToList(pokedex,"Charmander","Fire","FireBallz");
AddPokemonToList(pokedex,"Squirtle","Water","WaterBlast");
AddPokemonToList(pokedex,"Squirtle","Water","WaterBlast"); //Testing that it doesnt add a duplicate
AddPokemonToList(pokedex,"The grass one","Leaf","LeafSomething");
AddPokemonToList(pokedex,"Poki1","Leaf","LeafSomething");
AddPokemonToList(pokedex,"Poki2","Leaf","LeafSomething");
PlayerNode *newNode = NewPlayerNode(name); //Call NewPlayerNode and store ptr to new node in variable
PlayerNode **ptrHead = pokedex.ptrToPlayerHead; //Create pointer to pointer variable from pokedex pointer
PlayerNode *temp = *ptrHead; //Create temp pointer variable to store current node in list traversal
printf("%p\n",FindPokemon(pokedex,"Charmander"));
//Check that the node exists first
if (newNode != NULL) {
ListPokemon(pokedex);
//If the list is empty, point head to new node
if (temp == NULL) {
*ptrHead = newNode; //Point the head to the new node
newNode->next = NULL; //Set the new node next to NULL
return;
}
return 0;
if (FindPlayer(pokedex,newNode->name) != NULL) { //Call FindPokemon to check if the pokemon eixists in the list already
return;
}
*ptrHead = newNode; //Set pointer to head to point to newNode
newNode->next = temp; //Set newNode to point to old first item in list
newNode->prev = NULL; //Set the prev pointer to NULL as is first item in list
temp->prev = newNode; //Set temp to point to new first item in list (newNode)
}
}
//FindPlayer function - returns pointer to the node if found, NULL (nil) if not found
struct PlayerNode * FindPlayer(struct Pokedex pokedex, char name[]){
PlayerNode **ptrHead = pokedex.ptrToPlayerHead; //Create pointer to pointer variable from pokedex pointer
PlayerNode *temp = *ptrHead; //Create temp pointer variable to store current node in list traversal
//Traverse list with temp until NULL
while (temp != NULL) {
//string comparison of current node name and target name
if (strcmp(temp->name,name) == 0) {
return temp; //If string comparison is true (0) return address of current node
}
temp = temp->next; //temp increments to the next item in the list
}
return NULL;
}
void ListPlayers (Pokedex pokedex) {
PlayerNode **ptrHead = pokedex.ptrToPlayerHead; //Create pointer to pointer variable from pokedex pointer
PlayerNode *temp = *ptrHead; //Create temp pointer variable to store current node in list traversal
//Traverse list with temp until NULL
while (temp != NULL) {
printf("Player Name: %s\n",temp->name); //Print the name of the node that temp is currently pointing to
temp = temp->next; //temp increments to the next item in the list
}
}
void AddPokemonToPlayer(struct Pokedex pokedex, char playerName[], char pokemonName[]) {
PlayerNode *playerAdd = FindPlayer(pokedex,playerName);
PokemonNode *pokemonAdd = FindPokemon(pokedex,pokemonName);
if (playerAdd == NULL || pokemonAdd == NULL) { //Check that the player and pokemon exist
return;
}
for (int i = 0; 1 < playerAdd->pokemonCount; i++) {
if (playerAdd->pokemonArray[i] == pokemonAdd){
return;
}
}
playerAdd->pokemonArray[playerAdd->pokemonCount] = pokemonAdd;
playerAdd->pokemonCount++;
}
void DisplayPokemonDetails(struct Pokedex pokedex, char name[]) {
}
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