Commit 17fedef6 authored by a-j.towse's avatar a-j.towse

Code split into header and code files

parent 61348d37
{
"files.associations": {
"pokedexfunctions.h": "c"
}
}
\ No newline at end of file
No preview for this file type
This diff is collapsed.
- Create a struct called Podedex with 2 pointers (head of pokemon list and head of player list)
- Create variable called pokedex of type 'Pokedex' (to pass into functions)
Pokemon List
- Create doubly linked list of pokemon
- create a stuct for each pokemon in the list called PokemonNode
^ This will store: name, type, primary ability (all strings) (Assume each has only one type+ability)
- Create functions:
^PokemonNode * NewPokemonNode(name,type,ability) - create node and return pointer to it
^void AddPokemonToList(*pokedex, name, type, ability) - IF pokemonName NOT IN pokemonList: create and add new node ELSE: do nothing
^PokemonNode * FindPokemon(pokedex, name) - searches pokemon list for name, returns pointer to names node, if found and NULL if not
Player List
- Create doubly linked list of players
- create a stuct for each player in the list called PlayerNode
^ This will store: player name, a count of pokemon owned, and array of pointers to the pokemon owned by the player
- Create functions
^PlayerNode * NewPlayerNode(name) - creates new node and returns pointer (pokemon count start at 0)
^void AddPlayerToList(*pokedex, name) - checks to seee if name exists in player list, if it doesnt then create node and add to list, if it does then do nothing
^PlayerNode * FindPlayer(pokedex, name) - searches the player list the name and returns a pointer to the node if it finds it, otherwise NULL
- Create functions:
^void AddPokemonToPlayer(pokedex, player name, pokemon name) -adds pokemon to players list (if not already there) and increments the pokemon count by 1
^PokemonNode * PokemonArray[20] as part of player node - stores pointers to the nodes in Pokemon list for the pokemon that the player has captured
^void DisplayPokemonDetails(pokedex, name) - outputs details of name to the screen
^void DiplayPlayerDetails(pokedex, name) - outputs details of name to the screen, including list of names of pokemon owned
^void ListPokemon(pokedex) - output list of names of all pokemon in the pokedex
^void ListPlayers(pokedex) - outputs list of names of all players in the Pokedex.
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "pokedexStructs.h"
#include "pokedexFunctions.h"
//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
newNode = malloc(sizeof(struct PokemonNode)); //Assign memory space for PokemonNode
if (newNode != NULL) { //Ensure it exists
strcpy(newNode->name,name); //Set name
strcpy(newNode->type,type); //Set type
strcpy(newNode->ability,ability); //Set ability
newNode->next = NULL; //Set next pointer to NULL
newNode->prev = NULL; //Set prev pointer to NULL
}
return newNode;
}
//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
PokemonNode **ptrHead = pokedex.ptrToPokemonHead; //Create pointer to pointer variable from pokedex pointer
PokemonNode *temp = *ptrHead; //Create temp pointer variable to store current node in list traversal
//check that the node exists first
if (newNode != NULL){
//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;
}
if (FindPokemon(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)
}
}
//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
PokemonNode *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;
}
//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
PokemonNode *temp = *ptrHead; //Create temp pointer variable to store current node in list traversal
//Traverse list with temp until NULL
while (temp != NULL) {
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
}
}
//NewPlayerNode function - returns variable of type PlayerNode
struct PlayerNode * NewPlayerNode(char name[]){
struct PlayerNode *newNode = NULL; //Create a pointer to a PlayerNode structure, NULL for safety
newNode = malloc(sizeof(struct PlayerNode)); //Assign memory space for PlayerNode
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
}
return newNode;
}
//AddPlayerToList function - returns void
void AddPlayerToList(struct Pokedex pokedex, char name[]) {
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
//Check that the node exists first
if (newNode != NULL) {
//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;
}
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; i < playerAdd->pokemonCount; i++) {
if (playerAdd->pokemonArray[i] == pokemonAdd){
return;
}
}
playerAdd->pokemonArray[playerAdd->pokemonCount] = pokemonAdd;
playerAdd->pokemonCount++;
}
void DisplayPokemonDetails(struct Pokedex pokedex, char name[]) {
PokemonNode *pokemonAdd = FindPokemon(pokedex,name);
printf("Name: %s\n"
"\tType: %s\n"
"\tAbility: %s\n",pokemonAdd->name,pokemonAdd->type,pokemonAdd->ability);
}
void DisplayPlayerDetails(struct Pokedex pokedex, char name[]) {
PlayerNode *playerAdd = FindPlayer(pokedex,name);
printf("Name: %s\n"
"\tPokemon Count: %i\n"
"\tPokemon Owned: ",playerAdd->name,playerAdd->pokemonCount);
for (int i = 0 ; i < playerAdd->pokemonCount ; i++) {
printf("\t\t%s,",playerAdd->pokemonArray[i]->name);
}
}
#include "pokedexStructs.h"
#ifndef _POKEDEXFUNCTIONS_H
#define _POKEDEXFUNCTIONS_H
//Define functions for PokemonNode struct
struct PokemonNode * NewPokemonNode(char name[], char type[], char ability[] );
void AddPokemonToList(struct Pokedex pokedex,char name[], char type[], char ability[]);
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[]);
//Define functions for other functionality
void AddPokemonToPlayer(struct Pokedex pokedex, char playerName[], char pokemonName[]);
void DisplayPokemonDetails(struct Pokedex pokedex, char name[]);
void DisplayPlayerDetails(struct Pokedex pokedex, char name[]);
void ListPokemon(struct Pokedex pokedex);
void ListPlayers(struct Pokedex pokedex);
#endif
\ No newline at end of file
#ifndef _POKEDEXSTRUCTS_H
#define _POKEDEXSTRUCTS_H
//Create struct Pokedex with a pointer to head of Pokemon and head of Player
typedef struct Pokedex {
struct PokemonNode ** ptrToPokemonHead;
struct PlayerNode ** ptrToPlayerHead;
}Pokedex;
//Create struct PokemonNode with name,type,ability variables
//Create variables of type PokemonNode to store next and previous node in DLL
typedef struct PokemonNode {
char name[50];
char type[50];
char ability[50];
struct PokemonNode *next;
struct PokemonNode *prev;
}PokemonNode;
//Create struct PlayerNode wiith name, pokemonCount, and an array of pointers to pokemon owned called pokemonList
//Create variables of type PlayerNode to store next and previous node in DLL
typedef struct PlayerNode {
char name[50];
int pokemonCount;
struct PokemonNode * pokemonArray[20];
struct PlayerNode *next;
struct PlayerNode *prev;
}PlayerNode;
#endif
\ 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