Commit 5ac9084e authored by elliot.copeland's avatar elliot.copeland

Uploading assignment to repo.

parents
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
///////////////////
// Basic Structs //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////
// //
// //
// //
// //
///
struct PokemonNode {
char Name[15];
char Type[15];
char Move[15];
struct PokemonNode *Next;
};
typedef struct PokemonNode PokemonNode;
struct PlayerNode {
char Name[15];
int PokemonOwned;
PokemonNode *PokemonArray[20];
struct PlayerNode *Next;
};
typedef struct PlayerNode PlayerNode;
struct Pokedex {
PokemonNode *HeadOfPokemonList;
PlayerNode *HeadOfPlayerList;
};
typedef struct Pokedex Pokedex;
///////////////////////
// Pokemon Functions //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////
//// ////
//// ////
//// ////
//// ////
///////
void ListPokemon(Pokedex *pokedex) {
/*
Prints the whole pokemon list.
*/
PokemonNode *Temporary = pokedex->HeadOfPokemonList; //Creates a reference to the first item in the pokemon list.
printf("\n\nPrinting Pokemon List!:\n");
while(Temporary != NULL) { //While the current item in the list exists...
printf("\t- %s\n", Temporary->Name); //.. Print the Node...
Temporary = Temporary->Next; //... then move onto the next node.
}
};
PokemonNode *NewPokemonNode(char name[15], char type[15], char move[15]) {
/*
Creates a new pokemon node.
*/
PokemonNode *newNode = malloc(sizeof(PokemonNode)); //Allocates the required memory for a new pokemon node.
strcpy(newNode->Name, name);
strcpy(newNode->Type, type); // Sets the passed in values
strcpy(newNode->Move, move);
newNode->Next = NULL;
return newNode;
}
PokemonNode *FindPokemon(PokemonNode *head, char name[15]) {
/*
Searches the pokemon list for the chosen pokemon.
*/
PokemonNode *Temporary = head; //creates a refernce to the first item in the list...
while(Temporary != NULL) { //... while the current item exists....
if(strcmp(name, Temporary->Name) == 0) { //... if the name passed in matches the name in the node...
return Temporary; // ...return a pointer to the matched node,
} else {
Temporary = Temporary->Next; //Otherwise move onto the next item.
}
}
return 0; // If the item doesnt exist, return 0.
}
void AddPokemonToList(Pokedex *pokedex, char name[15], char type[15], char move[15]) {
/*
Adds a new pokemon to the list.
*/
if(FindPokemon(pokedex->HeadOfPokemonList, name) == 0) { //If the pokemon isnt already in the list..
PokemonNode *Temporary = NewPokemonNode(name, type, move); //..Create the new pokemon Node..
Temporary->Next = pokedex->HeadOfPokemonList; //..Change the new pokemon node's next pointer to the current head...
pokedex->HeadOfPokemonList = Temporary; //..then make the new node the new head.
printf("Added %s As a new pokemon!\n", name);
} else {
printf("Poke already exists L\n");
}
}
void DisplayPokemonDetails(Pokedex* pokedex, char name[15]) {
PokemonNode *Temporary = FindPokemon(pokedex->HeadOfPokemonList, name);
if(Temporary != 0) {
printf("\n%s: \n", Temporary->Name);
printf(" - %s type,\n", Temporary->Type);
printf(" - '%s' is their favourite move.\n", Temporary->Move);
} else {
printf("\nUnable to find that pokemon, sorry!\n");
}
}
///////////////////////
// Player Functions //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////
//// ////
//// ////
//// ////
//// ////
///////
void ListPlayers(Pokedex *pokedex) {
/*
Prints the whole player list.
*/
PlayerNode *Temporary = pokedex->HeadOfPlayerList; //Creates a reference to the first item in the player list.
printf("\n\nPrinting Player List!:\n");
while(Temporary != NULL) { //While the current item in the list exists...
printf("\t- %s\n", Temporary->Name); //.. Print the Node...
Temporary = Temporary->Next; //... then move onto the next node.
}
};
PlayerNode *NewPlayerNode(char name[15]) {
/*
Creates a new player node.
*/
PlayerNode *newNode = malloc(sizeof(PlayerNode)); //Allocates the required memory for a new pokemon node.
strcpy(newNode->Name, name);// Sets the passed in values
newNode->PokemonOwned = 0;
memset(newNode->PokemonArray, NULL, sizeof(newNode->PokemonArray)); // Initialises all values to NULL.
newNode->Next = NULL;
return newNode;
}
PlayerNode *FindPlayer(PlayerNode *head, char name[15]) {
/*
Searches the player list for the chosen player.
*/
PlayerNode *Temporary = head; //creates a refernce to the first item in the list...
while(Temporary != NULL) { //... while the current item exists....
if(strcmp(name, Temporary->Name) == 0) { //... if the name passed in matches the name in the node...
return Temporary; // ...return a pointer to the matched node,
} else {
Temporary = Temporary->Next; //Otherwise move onto the next item.
}
}
return 0;
}
void AddPlayerToList(Pokedex *pokedex, char name[15]) {
/*
Adds a new player to the list.
*/
if(FindPlayer(pokedex->HeadOfPlayerList, name) == 0) { //If the player isnt already in the list..
PlayerNode *Temporary = NewPlayerNode(name); //..Create the new player Node..
Temporary->Next = pokedex->HeadOfPlayerList; //..Change the new player node's next pointer to the current head...
pokedex->HeadOfPlayerList = Temporary; //..then make the new node the new head.
printf("Added %s As a new player!\n", name);
} else {
printf("Player already exists L\n");
}
}
void AddPokemonToPlayer(Pokedex *pokedex, char pokeName[15], char playerName[15]) {
PlayerNode *TempPlayerPoint = FindPlayer(pokedex->HeadOfPlayerList, playerName);
PokemonNode *TempPokePoint = FindPokemon(pokedex->HeadOfPokemonList, pokeName);
if ((TempPlayerPoint != 0) && (TempPokePoint != 0)) {
for(int i=0; i <= TempPlayerPoint->PokemonOwned; i++) {
if(TempPlayerPoint->PokemonArray[i] == TempPokePoint) {
printf("\n%s is already on %s's team, sorry!", TempPokePoint->Name, TempPlayerPoint->Name);
break;
} else if (TempPlayerPoint->PokemonArray[i] != NULL) {
continue;
} else if(TempPlayerPoint->PokemonArray[i] == NULL) {
TempPlayerPoint->PokemonArray[i] = TempPokePoint;
TempPlayerPoint->PokemonOwned++;
printf("\n%s has been added to %s's team!", TempPokePoint->Name, TempPlayerPoint->Name);
break;
} else {
printf("\n%s's team is full, sorry!\n", TempPlayerPoint->Name);
break;
}
}
}
}
void DisplayPlayerDetails(Pokedex *pokedex, char name[15]) {
PlayerNode *Temporary = FindPlayer(pokedex->HeadOfPlayerList, name);
if (Temporary != 0) {
printf("\n- %s\n", Temporary->Name); //.. Print the Node...
if(Temporary->PokemonOwned > 0) {
for(int i = 0; i < Temporary->PokemonOwned; i++) {
printf("\t- %s\n", Temporary->PokemonArray[i]->Name);
}
printf("\n");
}
}
}
//////////
// MAIN ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
// //
// //
// //
//
int main() {
//TESTING FUNCTIONS
// Initialising the pokedex \/ ////////////////////////////////////////////////
Pokedex maindex;
PokemonNode *firstPoke = NewPokemonNode("Lucario", "Fighting", "Sword Dance");
PlayerNode *firstPlayer = NewPlayerNode("Elliot");
maindex.HeadOfPokemonList = firstPoke;
maindex.HeadOfPlayerList = firstPlayer;
Pokedex* maindexPTR = &maindex;
///////////////////////////////////////////////////////////////////////////////
AddPokemonToList(maindexPTR, "Decidueye", "Grass", "Spirit Shackle");
AddPokemonToList(maindexPTR, "Quaquaval", "Water/Fight", "Aqua Step"); //Adding new pokemon to the pokedex
AddPokemonToList(maindexPTR, "Vaporeon", "Water", "Surf");
AddPokemonToList(maindexPTR, "Jolteon", "Electric", "Thunder");
AddPokemonToList(maindexPTR, "Sylveon", "Fairy", "Destiny Bond");
AddPlayerToList(maindexPTR, "Andy"); // Adding new players to the pokedex
AddPlayerToList(maindexPTR, "Hamid");
AddPlayerToList(maindexPTR, "Andy"); //Testing repeat players
ListPokemon(maindexPTR); //Printing Both Lists
ListPlayers(maindexPTR);
AddPokemonToPlayer(maindexPTR, "Decidueye", "Elliot");
AddPokemonToPlayer(maindexPTR, "Quaquaval", "Elliot"); // Adding pokemon to specific players
AddPokemonToPlayer(maindexPTR, "Vaporeon", "Elliot");
AddPokemonToPlayer(maindexPTR, "Jolteon", "Andy");
AddPokemonToPlayer(maindexPTR, "Sylveon", "Hamid");
AddPokemonToPlayer(maindexPTR, "Quaquaval", "Elliot"); //testing repeat pokemon
DisplayPokemonDetails(maindexPTR, "Vaporeon"); // Testing displaying pokemon details
DisplayPokemonDetails(maindexPTR, "Decidueye");
DisplayPlayerDetails(maindexPTR, "Elliot"); // Testing displaying player details
DisplayPlayerDetails(maindexPTR, "Andy");
DisplayPlayerDetails(maindexPTR, "Hamid");
};
\ No newline at end of file
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