Commit a92e6b4d authored by sully.khalifa's avatar sully.khalifa

INITIAL

parent e394b7bd
#include <stdio.h>
#include "pokedex.h"
#include "pokedex.c"
#include "player.c"
#include "player.h"
#include "pokemon.c"
#include "pokemon.h"
#include "structs.h"
int main()
{
/*creatin object for pokedex lists*/
struct Pokedex pokedex = {NULL,NULL};
/*adding pokemon items*/
AddPokemonToList(&pokedex,(char*)"Bulbasur",(char*)"Grass",(char*)"Overgrow");
AddPokemonToList(&pokedex,(char*)"Charmander",(char*)"Fog",(char*)"Blaze");
AddPokemonToList(&pokedex,(char*)"Squrtle",(char*)"Water",(char*)"Torrent");
AddPokemonToList(&pokedex,(char*)"Butterfree",(char*)"Bug",(char*)"Compound eyes");
AddPokemonToList(&pokedex,(char*)"Pikipek",(char*)"Normal",(char*)"Keen Eye");
AddPokemonToList(&pokedex,(char*)"Venomoth",(char*)"Bug",(char*)"Shield Dust");
AddPokemonToList(&pokedex,(char*)"Piplup",(char*)"Water",(char*)"Torrent");
AddPokemonToList(&pokedex,(char*)"Ducklett",(char*)"Water",(char*)"Keen Eye");
/*adding player ash*/
AddPlayerToList(&pokedex , (char*)"Ash");
/*adding pockemons to player ash*/
AddPokemonToPlayer(&pokedex,(char*)"Ash",(char*)"Charmander");
AddPokemonToPlayer(&pokedex,(char*)"Ash",(char*)"Squrtle");
AddPokemonToPlayer(&pokedex,(char*)"Ash",(char*)"Venomoth");
/*adding player Misty*/
AddPlayerToList(&pokedex , (char*)"Misty");
/*adding pockemons to player Misty*/
AddPokemonToPlayer(&pokedex,(char*)"Misty",(char*)"Butterfree");
AddPokemonToPlayer(&pokedex,(char*)"Misty",(char*)"Piplup");
/*adding player Andy*/
AddPlayerToList(&pokedex , (char*)"Andy Roo");
/*adding pockemons to player Andy*/
AddPokemonToPlayer(&pokedex,(char*)"Andy Roo",(char*)"Piplup");
/*Testing pokedex APIs*/
DisplayPokemonDetails(&pokedex,(char*)"Charmander");
DisplayPlayerDetails(&pokedex,(char*)"Ash");
ListPlayers(&pokedex);
ListPokemon(&pokedex);
return 0;
}
File added
#include "player.h"
#include <string.h>
#include <stdlib.h>
/*creates a new node and returns a pointer to that node. The Pokemon count should be set to zero.*/
struct PlayerNode* NewPlayerNode(char* name) {
struct PlayerNode *node = (struct PlayerNode*) malloc(sizeof(struct PlayerNode)); /*specify memory for the new node with the struct size*/
if(node == NULL){
return NULL; /*failed to allocate memory*/
}
node->PlayerName = name; /*set the struct parameters*/
node->pokemonCount = 0;
node->next = NULL; /*set the next pointer to null to avoid wild pointers*/
return node;
}
/*checks to see if name already exists in the Player list.
If it doesn’t it creates a new node and adds it to the list.
If the player already exists it does nothing.*/
void AddPlayerToList(struct Pokedex *pokedex,char* name){
if(FindPlayer(pokedex,name)==NULL){ /*check if the player already exists or not*/
struct PlayerNode *node = NewPlayerNode(name); /*if it doesn't exist create a new node*/
node->next = pokedex->playerHead; /*set the next node as the current head node*/
pokedex->playerHead = node; /*set the current node as the head of the list*/
}
}
/*
searches the Player list for name.
If it finds name it returns a pointer to the name’s node otherwise it returns NULL.
*/
struct PlayerNode* FindPlayer(struct Pokedex *pokedex,char* name) {
struct PlayerNode* currentNode = pokedex->playerHead; /*node starting from the first node*/
if(pokedex->playerHead == NULL) { /*if the list is empty*/
return NULL;
}
while(strcmp(name,currentNode->PlayerName) !=0) { /*check if the current node name = the search name*/
if(currentNode->next == NULL) { /*if we reached the last node then the player name is NOT found*/
return NULL; /*return NULL if not found*/
}
else {
currentNode = currentNode->next; /*go to the next node*/
}
}
return currentNode; /*return the node if found*/
}
/*adds the Pokemon to the player’s Pokemon list (if it is not already in there)
and increments the Pokemon count for that player.*/
void AddPokemonToPlayer(struct Pokedex *pokedex, char* l_playerName, char* l_pokemonName){
struct PlayerNode* myPlayer = FindPlayer(pokedex,l_playerName); /*search for the player if exists*/
struct PokemonNode* myPokemon = FindPokemon(pokedex,l_pokemonName); /*search for the pokemon if exists*/
if(myPlayer != NULL){ /*if player exists*/
if(myPokemon!= NULL){ /*if pokemon exists*/
myPlayer->PokemonArray[myPlayer->pokemonCount] = myPokemon; /*put the pokemon node to the array at last element*/
myPlayer->pokemonCount++; /*increment the pokemon count*/
}/*if pokemon found*/
}/*if player found*/
}
#ifndef PLAYER_H
#define PLAYER_H
#include "structs.h"
#include "pokemon.h"
/*creates a new node and returns a pointer to that node. The Pokemon count should be set to zero.*/
struct PlayerNode* NewPlayerNode(char* name);
/*checks to see if name already exists in the Player list.
If it doesn’t it creates a new node and adds it to the list.
If the player already exists it does nothing.*/
void AddPlayerToList(struct Pokedex *pokedex,char* name);
/*
searches the Player list for name.
If it finds name it returns a pointer to the name’s node otherwise it returns NULL.
*/
struct PlayerNode* FindPlayer(struct Pokedex *pokedex,char* name);
/*adds the Pokemon to the player’s Pokemon list (if it is not already in there)
and increments the Pokemon count for that player.*/
void AddPokemonToPlayer(struct Pokedex *pokedex, char* l_playerName, char* l_pokemonName);
#endif
#include "pokedex.h"
#include <stdio.h>
/*
• void DisplayPokemonDetails(pokedex, name) – outputs the details of name to the screen
*/
void DisplayPokemonDetails(struct Pokedex *pokedex, char* name){
struct PokemonNode* node = FindPokemon(pokedex,name); /*search for the pokemon name if exists*/
printf("Displaying %s Details",name);
if(node!=NULL){ /*if it exists*/
printf("\n=====================\nName:%s\nType:%s\nAbility:%s\n=====================\n",
node->PokemonName,node->PokemonType,node->PokemonAbility); /*print pokemon details*/
}
}
/*
• void DisplayPlayerDetails(pokedex, name) – outputs the details of name to the screen, including a list of names of all the Pokemon owned
*/
void DisplayPlayerDetails(struct Pokedex *pokedex, char* name){
struct PlayerNode* node = FindPlayer(pokedex,name); /*search for the player node if exists*/
printf("Displaying %s Details",name);
if(node!=NULL){ /*if it exists*/
printf("\n=====================\nName:%s\n",node->PlayerName); /*print player name*/
for (int i = 0;i<node->pokemonCount;i++){ /*print the pokemon array*/
printf("Pokemon %d:%s\n",i+1,node->PokemonArray[i]->PokemonName);
}
printf("=====================\n");
}
}
/*
• void ListPokemon(pokedex) – outputs a list of names of all Pokemon in the Pokedex
*/
void ListPokemon(struct Pokedex *pokedex){
struct PokemonNode *node = pokedex->pokemonHead; /*virtual node to loop through the list*/
printf("====Pokemon Names List====\n");
//start from the beginning
while(node != NULL) { /*loop through the list by the next pointer of each node*/
printf("%s\n",node->PokemonName); /*print the pokemon name*/
node = node->next;
}
printf("=========================\n");
}
/*
• void DisplayPokemonDetails(pokedex, name) – outputs the details of name to the screen
*/
void ListPlayers(struct Pokedex *pokedex){
struct PlayerNode *node = pokedex->playerHead; /*virtual node to loop through the list*/
printf("====Player Names List====\n");
//start from the beginning
while(node != NULL) { /*loop through the list by the next pointer of each node*/
printf("%s\n",node->PlayerName); /*print the player name*/
node = node->next;
}
printf("=========================\n");
}
#ifndef POKEDEX_H
#define POKEDEX_H
#include "pokemon.h"
#include "player.h"
#include "structs.h"
/*
• void DisplayPokemonDetails(pokedex, name) – outputs the details of name to the screen
*/
void DisplayPokemonDetails(struct Pokedex *pokedex, char* name);
/*
• void DisplayPlayerDetails(pokedex, name) – outputs the details of name to the screen, including a list of names of all the Pokemon owned
*/
void DisplayPlayerDetails(struct Pokedex *pokedex, char* name);
/*
• void ListPokemon(pokedex) – outputs a list of names of all Pokemon in the Pokedex
*/
void ListPokemon(struct Pokedex *pokedex);
/*
• void DisplayPokemonDetails(pokedex, name) – outputs the details of name to the screen
*/
void ListPlayers(struct Pokedex *pokedex);
#endif
#include "pokemon.h"
#include <string.h>
#include <stdlib.h>
/*creates a new node and returns a pointer to that node*/
struct PokemonNode* NewPokemonNode(char* name, char* type, char* ability) {
//create a link
struct PokemonNode *node = (struct PokemonNode*) malloc(sizeof(struct PokemonNode));/*specify memory for the new node with the struct size*/
if(node == NULL){
return NULL; /*failed to allocate memory*/
}
node->PokemonName = name; /*set the struct parameters*/
node->PokemonType = type;
node->PokemonAbility = ability;
node->next = NULL; /*set the next pointer to null to avoid wild pointers*/
return node;
}
/*
checks to see if name already exists in the Pokemon list.
If it doesn’t it creates a new node and adds it to the list.
If the Pokemon already exists it does nothing.
*/
void AddPokemonToList(struct Pokedex *pokedex,char* name,char* type,char* ability){
if(FindPokemon(pokedex,name)==NULL){ /*check if the pokemon already exists or not*/
struct PokemonNode *node = NewPokemonNode(name,type,ability); /*if it doesn't exist create a new node*/
node->next = pokedex->pokemonHead; /*set the next node as the current head node*/
pokedex->pokemonHead = node; /*set the current node as the head of the list*/
}
}
/*
searches the Pokemon list for name. If it finds name it returns a pointer to the name’s node
otherwise it returns NULL.
*/
struct PokemonNode* FindPokemon(struct Pokedex *pokedex,char* name) {
struct PokemonNode* currentNode = pokedex->pokemonHead; /*node starting from the first node*/
if(pokedex->pokemonHead == NULL) {/*if the list is empty*/
return NULL;
}
while(strcmp(name,currentNode->PokemonName) !=0) { /*check if the current node name = the search name*/
if(currentNode->next == NULL) {/*if we reached the last node then the player name is NOT found*/
return NULL;/*return NULL if not found*/
} else {
currentNode = currentNode->next;/*go to the next node if not found*/
}
}
return currentNode; /*return the node if found*/
}
#ifndef POKEMON_H
#define POKEMON_H
#include "structs.h"
/*creates a new node and returns a pointer to that node*/
struct PokemonNode* FindPokemon(struct Pokedex *pokedex,char* str);
/*
checks to see if name already exists in the Pokemon list.
If it doesn’t it creates a new node and adds it to the list.
If the Pokemon already exists it does nothing.
*/
struct PokemonNode* NewPokemonNode(char* name, char* type, char* ability);
/*
searches the Pokemon list for name. If it finds name it returns a pointer to the name’s node
otherwise it returns NULL.
*/
void AddPokemonToList(struct Pokedex *pokedex,char* name,char* type,char* ability);
#endif
#ifndef STRUCTS_H
#define STRUCTS_H
struct PlayerNode
{
char* PlayerName; /*string for player name*/
int pokemonCount; /*integer for pokemon number*/
struct PlayerNode *next; /*point to the next node*/
struct PokemonNode* PokemonArray[20]; /*array of pokemon node represents the pokemons owned*/
};
struct Pokedex
{
struct PokemonNode* pokemonHead; /*head of pokemon list*/
struct PlayerNode* playerHead; /*head of players list*/
};
struct PokemonNode
{
char* PokemonName; /*string holds pokemon name*/
char* PokemonType; /*string holds pokemon type*/
char* PokemonAbility; /*string holds pokemon ability*/
struct PokemonNode *next; /*points to the next node*/
};
#endif
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