Commit 07412a02 authored by jake.beetham's avatar jake.beetham

Add new file

parents
#include <stdio.h>
#include<stdlib.h> // We required stdlib.h for malloc
#include<string.h>
//_________________________________________________________section 1 - structs
//setting up a pokemon node
typedef struct PokemonNode {
//setting the data for the creation of a pokemon
char pokemonName[50];
char pokemonType[50];
char pokemonAbility[50];
//this will allow me to move to the next node in the list
struct PokemonNode *next;
} PokemonNode;
//setting up a player node
typedef struct PlayerNode {
//setting the data for the creation of a player
char playerName[50];
int pokemonCounter;
PokemonNode* PokemonArray[20];
//this will allow me to move to the next node in the list
struct PlayerNode *next;
} PlayerNode;
//setting up pokedex struct
typedef struct Pokedex {
PokemonNode* PokemonHead; //pointer to pokemon list
PlayerNode* PlayerHead; //pointer to pokemon list
} Pokedex;
//______________________________________________section 2 - setting up functions
//pokemon functions
PokemonNode* NewPokemonNode(char pokemonName[50], char pokemonType[50], char pokemonAbility[50]);
void AddPokemonToList(PokemonNode *start, char pokemonName[50], char pokemonType[50], char pokemonAbility[50]);
PokemonNode* FindPokemon(Pokedex* pokedex, char pokemonName[50]);
//player functions
PlayerNode* NewPlayerNode(char playerName[50]) ;
void AddPlayerToList(PlayerNode *start, char playerName[50]);
PlayerNode* FindPlayer(Pokedex* pokedex, char playerName[50]);
void AddPokemonToPlayer(Pokedex* pokedex, char playerName[], char pokemonName[]);
//pokedex functions
void DisplayPokemonDetails(Pokedex* pokedex, char playerName[50]);
void DisplayPlayerDetails(Pokedex* pokedex, char playerName[50]);
void ListPokemon(Pokedex* pokedex);
void ListPlayers(Pokedex* pokedex);
//______________________________________________section 3 - main
int main(void) {
//creating a head for the pokemon list
PokemonNode *PokemonHead = NewPokemonNode("Ludicolo", "Water", "Bubble Beam");
//creating a head for the player list
PokemonNode *PlayerHead = NewPlayerNode("Brock");
//using the AddPokemonToList function to add more pokemon into the list
AddPokemonToList(PokemonHead, "Minun", "Electric", "Thunder Wave");
AddPokemonToList(PokemonHead, "Monferno", "Fire", "Overheat");
AddPokemonToList(PokemonHead, "Froslass", "Ice", "Ice Fang");
AddPokemonToList(PokemonHead, "Reshiram", "Dragon", "Outrage");
//using the AddPlayerToList function to add more players into the list
AddPlayerToList(PlayerHead, "Ash");
AddPlayerToList(PlayerHead, "Dawn");
AddPlayerToList(PlayerHead, "Morty");
AddPlayerToList(PlayerHead, "May");
printf("Welcome to the Pokedex!\n");
printf("Please select one of the options below: \n");
//creating a menu selection variable
int selectOption;
printf("1 • Display all pokemon stored inside of the pokedex: \n");
printf("2 • Display details about a specific pokemon: \n");
printf("3 • Display the players inside our system: \n");
//taking the input of the user selection
scanf("%d", &selectOption);
//if the user selects this option they will be provided all details about every pokemon stored inside of the pokedex
if (selectOption == 1){
printf("You have chosen to display all pokemon stored inside of the pokedex... \n");
PokemonNode *temp = PokemonHead;
//iterating through the list and print each pokemon stored inside
while (temp->next != NULL){
printf("Pokemon Name: %s \n", temp->pokemonName);
printf("Pokemon Type: %s \n", temp->pokemonType);
printf("Pokemon Ability: %s \n\n", temp->pokemonAbility);
temp = temp->next;
}
main();
}
if (selectOption == 2){
printf("You have chosen to display the details of only one pokemon... \n");
printf("Which pokemon would you like to view: \n");
char whichPokemon;
//taking the input of the user selection
scanf("%s", &whichPokemon);
PokemonNode *temp = PokemonHead;
while (temp->next != NULL){
if(strcmp(temp->pokemonName, &whichPokemon) == 0){
printf("Pokemon Name: %s \n", temp->pokemonName);
printf("Pokemon Type: %s \n", temp->pokemonType);
printf("Pokemon Ability: %s \n\n", temp->pokemonAbility);
main();
}
return NULL;
main();
}
}
if (selectOption == 3){
printf("You have chosen to display all of the players in our system... \n");
PlayerNode *temp = PlayerHead;
//iterating through the list and print each pokemon stored inside
while (temp->next != NULL){
printf("Player Name: %s \n\n", temp->playerName);
temp = temp->next;
}
main();
}
return 0;
}
else
//______________________________________________section 4 - creating functions
//creating a new pokemon node
PokemonNode* NewPokemonNode(char pokemonName[50], char pokemonType[50], char pokemonAbility[50]){
// creating a pointer to a node structure, set it to NULL for safety
PokemonNode *new_node = NULL;
// setting aside space in memory for a node structure. If this fails new_node will be set to NULL
new_node = malloc(sizeof(PokemonNode));
// ensuring the node exists before accessing it.
if (new_node != NULL)
{
// setting data values as required
strcpy(new_node->pokemonName, pokemonName);
strcpy(new_node->pokemonType, pokemonType);
strcpy(new_node->pokemonAbility, pokemonAbility);
// Set next to NULL
new_node->next = NULL;
}
return new_node;
}
//adding pokemon to the list
void AddPokemonToList(PokemonNode *start, char pokemonName[50], char pokemonType[50], char pokemonAbility[50]){
PokemonNode *temp = start;
// If next is NULL we are on the last entry in the list
while (temp->next != NULL){
//moving us onto the next node in the list
temp = temp->next;
}
temp->next = NewPokemonNode(pokemonName, pokemonType, pokemonAbility);
}
//finding a pokemon inside the pokedex
PokemonNode* FindPokemon(Pokedex* pokedex, char pokemonName[50]){
PokemonNode *temp = pokedex->PokemonHead;
while (temp->next != NULL){
if(strcmp(temp->pokemonName, pokemonName)== 0){
return temp;
}
return NULL;
}
}
//creating a new player node
PlayerNode* NewPlayerNode(char playerName[50]){
// creating a pointer to a node structure, set it to NULL for safety
PlayerNode *new_node = NULL;
// setting aside space in memory for a node structure. If this fails new_node will be set to NULL
new_node = malloc(sizeof(PokemonNode));
// ensuring the node exists before accessing it.
if (new_node != NULL)
{
// setting data values as required
strcpy(new_node->playerName, playerName);
new_node->pokemonCounter = 0;
// Set next to NULL
new_node->next = NULL;
}
return new_node;
}
//adding players to the list
void AddPlayerToList(PlayerNode *start, char playerName[50]){
PlayerNode *temp = start;
// If next is NULL we are on the last entry in the list
while (temp->next != NULL){
//moving us onto the next node in the list
temp = temp->next;
}
temp->next = NewPlayerNode(playerName);
}
//findingn a player inside of the list
PlayerNode* FindPlayer(Pokedex* pokedex, char playerName[50]){
PlayerNode *temp = pokedex->PlayerHead;
while (temp->next != NULL){
if(strcmp(temp->playerName, playerName)== 0){
return temp;
}
return NULL;
}
}
void AddPokemonToPlayer(Pokedex* pokedex, char playerName[], char pokemonName[]){
PlayerNode * playerNodePtr = FindPlayer(pokedex, playerName);
//checking if the players team is currently full
if (playerNodePtr->pokemonCounter >=6){
printf("This player's team is currently full, please try again...");
}
//if the players team has less than 6 pokemon we can add a pokemon to the players team
if (playerNodePtr->pokemonCounter <6){
//increases players pokemon team number by 1
playerNodePtr->pokemonCounter = playerNodePtr->pokemonCounter + 1;
}
}
\ 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