Commit 86805d49 authored by jamilah.hamade's avatar jamilah.hamade

Add new file

parents
#include <stdio.h> // for printf
#include <string.h>
#include <stdlib.h> // for malloc
#include <stdbool.h>
typedef struct Pokedex {
struct Pokedex *headP ;
struct Pokedex *headPlr ;
} Pokedex;
typedef struct PokemonNode {
char Name [20];
char Type [10];
char Ability [20];
struct PokemonNode * next;
} PokemonNode;
typedef struct PlayerNode {
char Username[20];
int PokemonOwned;
PokemonNode* PokemonArray[20];
struct PlayerNode * next;
} PlayerNode;
/* function declaration */
void ListPokemon();
void AddPokemonToList();
void DisplayPokemonDetails();
void ListPlayer();
void AddPlayerToList();
void DisplayPlayerDetails();
void AddPokemonToPlayer();
PokemonNode *NewPokemon(PokemonNode, char Name [20], char Type[10], char Ability[20]) {
PokemonNode * NewPokemon = NULL;
NewPokemon = (PokemonNode*)malloc(sizeof(PokemonNode));
if (NewPokemon==NULL){
strcpy(NewPokemon->Name, Name);
strcpy(NewPokemon->Type, Type);
strcpy(NewPokemon->Ability, Ability);
printf("Pokemon added");
}
else {
printf("already exists");
}
return NewPokemon;
}
PlayerNode *NewPlayer(PokemonNode, char Username [20]) {
PlayerNode * NewPlayer = NULL;
NewPlayer = (PlayerNode*)malloc(sizeof(PlayerNode));
if (NewPlayer==NULL){
strcpy(NewPlayer->Username, Username);
NewPlayer->PokemonOwned=0;
printf("Player added");
}
else {
printf("already exists");
}
return NewPlayer;
}
int main (void) {
PokemonNode * headP ; //new empty list is created (PokemonNode), with a head node of 0 value.
headP= (struct PokemonNode*)malloc(sizeof(PokemonNode));
// setting space for node structure.
if (headP == NULL) // if failed to allocate in memory -> exit.
{
printf("Memory allocation failed. Exiting...");
exit(0);
}
/* setting value of 6 pokemons each in a different node */
PokemonNode PokemonNode1;
PokemonNode* PokemonNode1ptr;
PokemonNode1ptr= &PokemonNode1;
strcpy(PokemonNode1.Name, "\tPikachu");
strcpy(PokemonNode1.Type, "\tElectric");
strcpy(PokemonNode1.Ability, "Static\n");
headP -> next = PokemonNode1ptr;
PokemonNode PokemonNode2;
PokemonNode* PokemonNode2ptr;
PokemonNode2ptr= &PokemonNode2;
strcpy(PokemonNode2.Name, "Clefairy");
strcpy(PokemonNode2.Type, "Fairy");
strcpy(PokemonNode2.Ability, "\tCuteCharm\n");
PokemonNode1ptr -> next = PokemonNode2ptr;
PokemonNode PokemonNode3;
PokemonNode* PokemonNode3ptr;
PokemonNode3ptr= &PokemonNode3;
strcpy(PokemonNode3.Name, "\tVulpix");
strcpy(PokemonNode3.Type, "\tFire");
strcpy(PokemonNode3.Ability, "\tFlash Fire\n");
PokemonNode2ptr -> next = PokemonNode3ptr;
PokemonNode PokemonNode4;
PokemonNode* PokemonNode4ptr;
PokemonNode4ptr= &PokemonNode4;
strcpy(PokemonNode4.Name, "\tWigglyTuff");
strcpy(PokemonNode4.Type, "Fairy");
strcpy(PokemonNode4.Ability, "\tCompetitive\n");
PokemonNode3ptr -> next = PokemonNode4ptr;
PokemonNode PokemonNode5;
PokemonNode* PokemonNode5ptr;
PokemonNode5ptr= &PokemonNode5;
strcpy(PokemonNode5.Name, "\tMeowth");
strcpy(PokemonNode5.Type, "\tNormal");
strcpy(PokemonNode5.Ability, "\tTechnician\n");
PokemonNode4ptr -> next = PokemonNode5ptr;
PokemonNode PokemonNode6;
PokemonNode* PokemonNode6ptr;
PokemonNode6ptr= &PokemonNode6;
strcpy(PokemonNode6.Name, "\tPonyta");
strcpy(PokemonNode6.Type, "\tFire");
strcpy(PokemonNode6.Ability, "\tRun Away\n");
PokemonNode5ptr -> next = PokemonNode6ptr;
PokemonNode6ptr -> next = NULL;
PlayerNode *headPlr = NULL; //new empty list is created (PlayerNode), with a head node of 0 value.
headPlr = malloc(sizeof(PlayerNode)); // setting space for PlayerNode structure.
if (headPlr == NULL) // if failed to allocate in memory -> exit.
{
printf("Memory allocation failed. Exiting...");
exit(0);
}
/* setting value of 3 players in different nodes*/
PlayerNode PlayerNode1;
PlayerNode* PlayerNode1ptr;
PlayerNode1ptr= &PlayerNode1;
strcpy(PlayerNode1.Username, "\tElise");
PlayerNode1.PokemonOwned=2;
headPlr -> next = PlayerNode1ptr;
PlayerNode PlayerNode2;
PlayerNode* PlayerNode2ptr;
PlayerNode2ptr= &PlayerNode2;
strcpy(PlayerNode2.Username, "\tIvan");
PlayerNode2.PokemonOwned=2;
PlayerNode1ptr -> next = PlayerNode2ptr;
PlayerNode PlayerNode3;
PlayerNode* PlayerNode3ptr;
PlayerNode3ptr= &PlayerNode3;
strcpy(PlayerNode3.Username, "\tZoya");
PlayerNode3.PokemonOwned=1;
PlayerNode2ptr -> next = PlayerNode3ptr;
PlayerNode3ptr -> next = NULL;
printf("\t\t\t\t\t\tWelcome to Pokedex\n\n\n What would you like to display? (Enter corresponding number)\n\n \t1)Pokemon list\n\n\t2)Player List\n\n");
int userinp1;
scanf("%d", &userinp1);
if (userinp1 == 1) {
printf(" \n\nPokemons registered:\t" );
ListPokemon (headP);
printf("\n\nEnter one of the following numbers");
printf("\n\n1.To display a Pokemon's details.");
printf("\n\n2.To add a new pokemon.");
printf("\n\n3.Exit.\n\n");
int userinp2;
scanf("%d", &userinp2);
if (userinp2 == 1){
char search[20];
DisplayPokemonDetails(headP);
printf("\n\n");
}
else if (userinp2 == 2){
printf("\n\n");
char Name[20], Type[10], Ability[20];
printf("enter name : ");
scanf("%s", Name);
printf("enter type : ");
scanf("%s", Type);
printf("enter ability : ");
scanf("%s", Ability);
AddPokemonToList();
}
else {
printf("Exiting...");
}
}
else if (userinp1 == 2){
printf("\n\nPlayers registered:\t");
ListPlayer(headPlr);
printf("\n\nEnter one of the following numbers.");
printf("\n\n1.To display a player's details.");
printf("\n\n2.To add a new player.");
printf("\n\n3.Exit.\n\n");
int search2;
scanf("%d", &search2);
if (search2 == 1){
DisplayPlayerDetails();
}
else if (search2 == 2){
char Username[20];
printf("enter name : ");
scanf("%s", Username);
AddPlayerToList();
}
else if (search2 == 3){
printf("Exiting...");
}
}
}
void ListPokemon(PokemonNode* headP){
PokemonNode*temp ;
temp = headP;
while (temp != NULL){
temp = temp -> next;
printf("%s \t ", temp -> Name);
}}
void ListPlayer(PlayerNode* headPlr){
PlayerNode*temp ;
temp = headPlr;
while (temp != NULL){
temp = temp -> next;
printf("%s \t ", temp-> Username);
}
}
void DisplayPokemonDetails(PokemonNode * headP, char search[20])
{
printf("enter name\n");
scanf("%s", search);
PokemonNode*FindPokemon;
FindPokemon= headP;
while (FindPokemon != NULL){
if (FindPokemon->Name == search)
{
printf("%s \n %s \n %s \n ", FindPokemon->Name, FindPokemon->Type, FindPokemon->Ability);
break;
} FindPokemon = FindPokemon->next;
}
}
void DisplayPlayerDetails(PlayerNode*headPlr,char Username[20], char searchh[20]){
printf("enter name: \t");
scanf("%s", searchh);
PlayerNode*FindPlayer;
FindPlayer= headPlr;
while (FindPlayer != NULL){
if (FindPlayer->Username == searchh)
{
printf("%s \n %d \n ", FindPlayer->Username, FindPlayer->PokemonOwned);
break;
}FindPlayer = FindPlayer->next;
}
}
void AddPokemonToList(PokemonNode, char Name[20])
{
printf("\nPokemon added.");
}
void AddPlayerToList(Pokedex, char Username[20]){
printf("\nPlayer 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