Commit 8e533737 authored by elijah vasquez's avatar elijah vasquez 🦍

idkanymore

parent 99d556d8
No preview for this file type
// Structs and functions placed here // Structs and functions placed here
// Pokemon List // Pokemon List
typedef struct PokemonNode typedef struct PokemonNode // typedef struct to create alias for data type
{ { // in combination with struct for struct type
char name[20]; char name[20]; // kept at 20 as pokemon names don't exceed higher than value
char type[20]; char type[20];
char ability[30]; char ability[30];
struct PokemonNode *next; struct PokemonNode *next; // used for traversing singly linked list from pokemon
} PokemonNode; } PokemonNode;
// Player List // Player List
typedef struct PlayerNode typedef struct PlayerNode // Player node will include name, pokemon owned and is placed
{ { // in an array
char name[50]; char name[50];
int totalPokemon; int totalPokemon; // total number of pokemon owned by players
PokemonNode* PokemonArray[20]; //Array of players PokemonNode* PokemonArray[20]; // Array of players
struct PlayerNode *next; struct PlayerNode *next; // used for traversing singly linked list
} PlayerNode; } PlayerNode;
// Pokedex structure // Pokedex structure
typedef struct Pokedex typedef struct Pokedex // To house the header nodes to start single linked lists
{ {
PokemonNode *pokeHead; PokemonNode* pokeHead; // pokemon node used to create head pointer variable
PlayerNode *playerHead; PlayerNode* playerHead; //player node used to create head pointer variable
} Pokedex; } Pokedex;
// Pokemon List functions // Pokemon List
PokemonNode* NewPokemonNode(char name[20], char type[20], char ability[30]); PokemonNode* NewPokemonNode(char name[20], char type[20], char ability[30]);
void AddPokemonToList(Pokedex* pokedex, char name[20], char type[20], char ability[30]); void AddPokemonToList(Pokedex* pokedex, char name[20], char type[20], char ability[30]);
PokemonNode* FindPokemon(Pokedex* pokedex, char name[20]); PokemonNode* FindPokemon(Pokedex* pokedex, char name[20]);
// Player List functions // Player List
PlayerNode* NewPlayerNode(char name[50]); // Create new node, return pointer to node PlayerNode* NewPlayerNode(char name[50]); // Create new node, return pointer to node
void AddPlayerToList(Pokedex* pokedex, char name[50]); // Check if name already exists in Player list. void AddPlayerToList(Pokedex* pokedex, char name[50]); // Check if name already exists in Player list.
PlayerNode* FindPlayer(Pokedex* pokedex, char name[50]); // Searches Player list for name. PlayerNode* FindPlayer(Pokedex* pokedex, char name[50]); // Searches Player list for name.
void AddPokemonToPlayer(Pokedex* player, Pokedex* pokemon, char playerName[50], char pokemonName[20]); // Adds Pokemon to player's Pokemon list void AddPokemonToPlayer(Pokedex* player, Pokedex* pokemon, char playerName[50], char pokemonName[20]); // Adds Pokemon to player's Pokemon list
// Additional functions for the pokedex // Additional
// Pokemon details functs // Pokemon details
void DisplayPokemonDetails(Pokedex* pokedex, char name[20]); // Output details of pokemon void DisplayPokemonDetails(Pokedex* pokedex, char name[20]); // Output details of pokemon
PokemonNode* ListPokemon(Pokedex* pokedex); //output list of names of all POKEMON in pokedex PokemonNode* ListPokemon(Pokedex* pokedex); //output list of names of all POKEMON in pokedex
// Pokemon players functs // Pokemon players
void DisplayPlayerDetails(Pokedex* pokedex, char name[50]); // output details of players, incl. list of names of pokemon owned void DisplayPlayerDetails(Pokedex* pokedex, char name[50]); // output details of players, incl. list of names of pokemon owned
PlayerNode * ListPlayers(Pokedex* pokedex); //output list of names of all PLAYERS in pokedex PlayerNode * ListPlayers(Pokedex* pokedex); //output list of names of all PLAYERS in pokedex
// Calling of functions and setting up nodes // Calling of functions and setting up nodes
#include "functions.h" #include "functions.h"
int main(void)
{ // Adding Pokemon to list int main(void)
{ // Adding Pokemon nodes to list
Pokedex* pokeHead = NewPokemonNode("blastoise", "water", "torrent"); //Creation of node to introduce head of single linked list Pokedex* pokeHead = NewPokemonNode("blastoise", "water", "torrent"); //Creation of node to introduce head of single linked list
AddPokemonToList(pokeHead, "dragapult", "dragon", "infiltrator"); AddPokemonToList(pokeHead, "dragapult", "dragon", "infiltrator");
AddPokemonToList(pokeHead, "volcarona", "fire", "flame body"); AddPokemonToList(pokeHead, "volcarona", "fire", "flame body");
AddPokemonToList(pokeHead, "regieleki", "electric", "transistor"); AddPokemonToList(pokeHead, "regieleki", "electric", "transistor");
// Adding player to player list // Adding player nodes to player list
Pokedex* playerHead = NewPlayerNode("armin"); Pokedex* playerHead = NewPlayerNode("armin");
AddPlayerToList(playerHead, "chase"); AddPlayerToList(playerHead, "chase");
AddPlayerToList(playerHead, "ziggy"); AddPlayerToList(playerHead, "ziggy");
// Adding pokemon to player // Linking pokemon with players
AddPokemonToPlayer(pokeHead, playerHead, "armin", "blastoise"); AddPokemonToPlayer(pokeHead, playerHead, "armin", "blastoise");
AddPokemonToPlayer(pokeHead, playerHead, "armin", "dragapult"); AddPokemonToPlayer(pokeHead, playerHead, "armin", "dragapult");
AddPokemonToPlayer(pokeHead, playerHead, "chase", "volcarona"); AddPokemonToPlayer(pokeHead, playerHead, "chase", "volcarona");
AddPokemonToPlayer(pokeHead, playerHead, "ziggy", "regieleki"); AddPokemonToPlayer(pokeHead, playerHead, "ziggy", "regieleki");
// To display all of both pokemon and players // Displays and lists all pokemon and players present in program
ListPokemon(pokeHead); ListPokemon(pokeHead);
ListPlayers(playerHead); ListPlayers(playerHead);
...@@ -35,3 +35,4 @@ int main(void) ...@@ -35,3 +35,4 @@ int main(void)
DisplayPokemonDetails(pokeHead, "volcarona"); DisplayPokemonDetails(pokeHead, "volcarona");
DisplayPokemonDetails(pokeHead, "regieleki"); DisplayPokemonDetails(pokeHead, "regieleki");
} }
// Main file, RUN FROM HERE!!!
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -5,16 +6,16 @@ ...@@ -5,16 +6,16 @@
#include "liststructure.c" #include "liststructure.c"
// Details of Pokemon // Details of Pokemon
void DisplayPokemonDetails(Pokedex* pokedex, char name[50]) { void DisplayPokemonDetails(Pokedex* pokedex, char name[20]) {
PokemonNode* headPoke = pokedex; PokemonNode* headPoke = pokedex;
while (headPoke != NULL) { while (headPoke != NULL) {
if (strcmp(headPoke->name, name) == 0) { if (strcmp(headPoke -> name, name) == 0) {
printf("\nPOKEMON DETAILS\nname: %s\n", headPoke->name); printf("\nPOKEMON DETAILS\nname: %s\n", headPoke->name);
printf("type: %s\n", headPoke->type); printf("type: %s\n", headPoke->type);
printf("primary ability: %s\n", headPoke->ability); printf("primary ability: %s\n", headPoke->ability);
return; return;
} }
headPoke = headPoke->next; headPoke = headPoke -> next;
} }
} }
...@@ -53,8 +54,8 @@ PlayerNode* ListPlayers(Pokedex* pokedex) { ...@@ -53,8 +54,8 @@ PlayerNode* ListPlayers(Pokedex* pokedex) {
return playerHead; return playerHead;
} }
// Creates a new pokemon node and returns a pointer to said node // Create new pokemon node, return pointer to it
PokemonNode* NewPokemonNode(char name[50], char type[50], char ability[50]) { PokemonNode* NewPokemonNode(char name[20], char type[20], char ability[30]) {
PokemonNode* newPoke = NULL; PokemonNode* newPoke = NULL;
newPoke = malloc(sizeof(PokemonNode)); newPoke = malloc(sizeof(PokemonNode));
...@@ -67,9 +68,8 @@ PokemonNode* NewPokemonNode(char name[50], char type[50], char ability[50]) { ...@@ -67,9 +68,8 @@ PokemonNode* NewPokemonNode(char name[50], char type[50], char ability[50]) {
return newPoke; return newPoke;
} }
// Checks if Pokemon name already exists, if not a new node is created // If pokemon exists check
void AddPokemonToList(Pokedex* pokedex, char name[20], char type[20], void AddPokemonToList(Pokedex* pokedex, char name[20], char type[20], char ability[50]) {
char ability[50]) {
PokemonNode* ptr = pokedex; PokemonNode* ptr = pokedex;
bool exists = false; bool exists = false;
while (ptr->next != NULL) { while (ptr->next != NULL) {
...@@ -86,8 +86,8 @@ void AddPokemonToList(Pokedex* pokedex, char name[20], char type[20], ...@@ -86,8 +86,8 @@ void AddPokemonToList(Pokedex* pokedex, char name[20], char type[20],
} }
// Searches Pokemon List for name, if the name is not found NULL is returned // Searches Pokemon List for name, if the name is not found NULL is returned
PokemonNode *FindPokemon(Pokedex *pokedex, char name[20]) { PokemonNode* FindPokemon(Pokedex* pokedex, char name[20]) {
PokemonNode *pointer = pokedex; PokemonNode* pointer = pokedex;
while (pointer != NULL) { while (pointer != NULL) {
if (strcmp(pointer->name, name) == 0) { if (strcmp(pointer->name, name) == 0) {
return pointer; return pointer;
...@@ -98,76 +98,21 @@ PokemonNode *FindPokemon(Pokedex *pokedex, char name[20]) { ...@@ -98,76 +98,21 @@ PokemonNode *FindPokemon(Pokedex *pokedex, char name[20]) {
} }
// Creates a new player node and returns a pointer to said node // Creates a new player node and returns a pointer to said node
PlayerNode *NewPlayerNode(char name[50]) { PlayerNode* NewPlayerNode(char name[50]) {
PlayerNode *new_player = NULL; PlayerNode* playerNew = NULL;
new_player = malloc(sizeof(PlayerNode)); playerNew = malloc(sizeof(PlayerNode));
if (new_player != NULL) { if (playerNew != NULL) {
strcpy(new_player->name, name); strcpy(playerNew->name, name);
new_player->totalPokemon = 0; playerNew->totalPokemon = 0;
new_player->next = NULL; playerNew->next = NULL;
} }
return new_player; return playerNew;
} }
// Checks if Player name already exists, if not a new node is created // Checks if Player name already exists, if not a new node is created
void AddPlayerToList(Pokedex *pokedex, char name[50]) { void AddPlayerToList(Pokedex* pokedex, char name[50]) {
PlayerNode *pointer = pokedex; PlayerNode* ptr = pokedex;
bool already_exists = false;
while (pointer->next != NULL) {
if (strcmp(pointer->name, name) == 0) {
already_exists = true;
break;
}
pointer = pointer->next;
}
if (already_exists == true) {
return;
}
pointer->next = NewPlayerNode(name);
}
// Searches Player List for name, if the name is not found NULL is returned
PlayerNode *FindPlayer(Pokedex *pokedex, char name[50]) {
PlayerNode *pointer = pokedex;
while (pointer != NULL) {
if (strcmp(pointer->name, name) == 0) {
return pointer;
}
pointer = pointer->next;
}
return NULL;
}
// Adds the Pokemon to a players list and updates the counter
void AddPokemonToPlayer(Pokedex *player, Pokedex *pokemon, char playerName[50], char pokemonName[20]) {
PlayerNode *playerptr = FindPlayer(player, playerName);
PokemonNode *pokemonptr = FindPokemon(pokemon, pokemonName);
if (playerptr->totalPokemon < 50) {
playerptr->PokemonArray[playerptr->totalPokemon] =
pokemonptr;
playerptr->totalPokemon++;
}
}
// Creates a new pokemon node and returns a pointer to said node
PokemonNode* NewPokemonNode(char name[50], char type[50], char ability[50]) {
PokemonNode* newPoke = NULL;
newPoke = malloc(sizeof(PokemonNode));
if (newPoke != NULL) {
strcpy(newPoke->name, name);
strcpy(newPoke->type, type);
strcpy(newPoke->ability, ability);
newPoke->next = NULL;
}
return newPoke;
}
// Checks if Pokemon name already exists, if not a new node is created
void AddPokemonToList(Pokedex* pokedex, char name[20], char type[20],
char ability[50]) {
PokemonNode* ptr = pokedex;
bool exists = false; bool exists = false;
while (ptr->next != NULL) { while (ptr->next != NULL) {
if (strcmp(ptr->name, name) == 0) { if (strcmp(ptr->name, name) == 0) {
...@@ -179,70 +124,29 @@ void AddPokemonToList(Pokedex* pokedex, char name[20], char type[20], ...@@ -179,70 +124,29 @@ void AddPokemonToList(Pokedex* pokedex, char name[20], char type[20],
if (exists == true) { if (exists == true) {
return; return;
} }
ptr->next = NewPokemonNode(name, type, ability); ptr->next = NewPlayerNode(name);
}
// Searches Pokemon List for name, if the name is not found NULL is returned
PokemonNode *FindPokemon(Pokedex *pokedex, char name[20]) {
PokemonNode *pointer = pokedex;
while (pointer != NULL) {
if (strcmp(pointer->name, name) == 0) {
return pointer;
}
pointer = pointer->next;
}
return NULL;
}
// Creates a new player node and returns a pointer to said node
PlayerNode *NewPlayerNode(char name[50]) {
PlayerNode *new_player = NULL;
new_player = malloc(sizeof(PlayerNode));
if (new_player != NULL) {
strcpy(new_player->name, name);
new_player->totalPokemon = 0;
new_player->next = NULL;
}
return new_player;
}
// Checks if Player name already exists, if not a new node is created
void AddPlayerToList(Pokedex *pokedex, char name[50]) {
PlayerNode *pointer = pokedex;
bool already_exists = false;
while (pointer->next != NULL) {
if (strcmp(pointer->name, name) == 0) {
already_exists = true;
break;
}
pointer = pointer->next;
}
if (already_exists == true) {
return;
}
pointer->next = NewPlayerNode(name);
} }
// Searches Player List for name, if the name is not found NULL is returned // Searches Player List for name, if the name is not found NULL is returned
PlayerNode *FindPlayer(Pokedex *pokedex, char name[50]) { PlayerNode* FindPlayer(Pokedex* pokedex, char name[50]) {
PlayerNode *pointer = pokedex; PlayerNode* ptr = pokedex;
while (pointer != NULL) { while (ptr != NULL) {
if (strcmp(pointer->name, name) == 0) { if (strcmp(ptr->name, name) == 0) {
return pointer; return ptr;
} }
pointer = pointer->next; ptr = ptr->next;
} }
return NULL; return NULL;
} }
// Adds the Pokemon to a players list and updates the counter //ARRAY SECTION
void AddPokemonToPlayer(Pokedex *player, Pokedex *pokemon, char playerName[50], char pokemonName[20]) { // Add pokemon to player array, updates increment
PlayerNode *playerptr = FindPlayer(player, playerName); void AddPokemonToPlayer(Pokedex* player, Pokedex* poke, char playerName[50], char pokemonName[20]) {
PokemonNode *pokemonptr = FindPokemon(pokemon, pokemonName); PlayerNode* playerptr = FindPlayer(player, playerName);
PokemonNode* pokeptr = FindPokemon(poke, pokemonName);
if (playerptr->totalPokemon < 50) { if (playerptr->totalPokemon < 50) {
playerptr->PokemonArray[playerptr->totalPokemon] = playerptr->PokemonArray[playerptr->totalPokemon] =
pokemonptr; pokeptr;
playerptr->totalPokemon++; playerptr->totalPokemon++;
} }
} }
File added
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