Commit 21b0d7d8 authored by louis.colclough's avatar louis.colclough

Add new file

parents
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Pokedex hold head of both Pokemon and Player
struct Pokedex {
struct PokemonNode *PokemonNodePointer;
struct PlayerNode *PlayerNodePointer;
} Pokedex;
// defines the variables in the list and has pointer to next node
typedef struct PokemonNode {
char Name[50];
char Type[50];
char Ability[50];
struct PokemonNode *Next;
} PokemonNode;
// defines the variables in the list and has pointer to next node
typedef struct PlayerNode {
char Name[50];
int PokemonOwned;
struct PokemonNode *Pokemon[6];
struct PlayerNode *Next;
} PlayerNode;
PokemonNode *NewPokemonNode(char Name[50], char Type[50], char Ability[50]);
void InsertPokemonNodeAtEnd(char Name[50], char Type[50], char Ability[50]);
PlayerNode *NewPlayerNode(char PlayerName[50], char PokemonCount[50],PokemonNode *PokemonParty[6]);
void InsertPlayerNodeAtEnd(char PlayerName[50], char PokemonCount[50],char PokemonOwned[6][50]);
PokemonNode *TraversePokemon(char Name[50]);
void DisplayPlayerDetails();
void DisplayPokemonDetails();
void ListPokemon();
void ListPlayers();
int main(void) {
// Sets pointers to NULL when starts
Pokedex.PokemonNodePointer = NULL;
Pokedex.PlayerNodePointer = NULL;
// Reading file for pokemon
FILE *fp;
char Name[50];
char Type[50];
char Ability[50];
int Menu;
fp = fopen("Pokemon.txt", "r");
// Reads file in line by line
while (fscanf(fp, "%s", Name) != EOF) {
fscanf(fp, "%s", Type);
fscanf(fp, "%s", Ability);
InsertPokemonNodeAtEnd(Name, Type, Ability);
}
fclose(fp);
char PlayerName[50];
char PokemonCount[50];
char PokemonOwned[6][50];
fp = fopen("Players.txt", "r");
while (fscanf(fp, "%s", PlayerName) != EOF) {
fscanf(fp, "%s", PokemonCount);
for (int i = 0; i < atoi(PokemonCount); i++) {
fscanf(fp, "%s", PokemonOwned[i]);
}
InsertPlayerNodeAtEnd(PlayerName, PokemonCount, PokemonOwned);
}
fclose(fp);
printf(" Hello and welcome to the Pokedex\n");
while (true) {
printf("What would you like to do?\n1.See All Players\n2.See All Pokemon\n3.See pokemon and their abilities\n4.See Players and the Pokemon they have caught\n5.Exit Program\n");
printf("Please type what number corresponds with the action you want to take\n");
scanf("%d", &Menu);
switch (Menu) {
case 1:
ListPlayers();
break;
case 2:
ListPokemon();
break;
case 3:
DisplayPokemonDetails();
break;
case 4:
DisplayPlayerDetails();
break;
case 5:
exit(0);
break;
default:
printf("Error Invalid Input\n");
break;
}
}
return 0;
}
// creating new node for pokemon
PokemonNode *NewPokemonNode(char Name[50], char Type[50], char Ability[50]) {
PokemonNode *NewNode = NULL;
NewNode = malloc(sizeof(PokemonNode));
if (&NewNode != NULL) {
strcpy(NewNode->Name, Name);
strcpy(NewNode->Type, Type);
strcpy(NewNode->Ability, Ability);
NewNode->Next = NULL;
}
return NewNode;
}
// Takes the data from the text file and inserts that into the list
void InsertPokemonNodeAtEnd(char Name[50], char Type[50], char Ability[50]) {
if (Pokedex.PokemonNodePointer == NULL) {
Pokedex.PokemonNodePointer = NewPokemonNode(Name, Type, Ability);
} else {
PokemonNode *temp = Pokedex.PokemonNodePointer;
while (temp->Next != NULL) {
temp = temp->Next;
}
temp->Next = NewPokemonNode(Name, Type, Ability);
}
}
// Creating new node for players
PlayerNode *NewPlayerNode(char PlayerName[50], char PokemonCount[50],PokemonNode *PokemonParty[6]) {
PlayerNode *NewNode = NULL;
NewNode = malloc(sizeof(PlayerNode));
if (&NewNode != NULL) {
strcpy(NewNode->Name, PlayerName);
NewNode->PokemonOwned = atoi(PokemonCount);
for (int i = 0; i < 6; i++) {
if (PokemonParty[i] != NULL) {
NewNode->Pokemon[i] = PokemonParty[i];
}
}
NewNode->Next = NULL;
}
return NewNode;
}
// Takes the data from the text file and inserts that into the list
void InsertPlayerNodeAtEnd(char PlayerName[50], char PokemonCount[50],char PokemonOwned[6][50]) {
PokemonNode *PokemonParty[6];
for (int i = 0; i < atoi(PokemonCount); i++) {
PokemonParty[i] = TraversePokemon(PokemonOwned[i]);
}
if (Pokedex.PlayerNodePointer == NULL) {
Pokedex.PlayerNodePointer =
NewPlayerNode(PlayerName, PokemonCount, PokemonParty);
} else {
PlayerNode *temp = Pokedex.PlayerNodePointer;
while (temp->Next != NULL) {
temp = temp->Next;
}
temp->Next = NewPlayerNode(PlayerName, PokemonCount, PokemonParty);
}
}
// find the corresponding pokemon from the player and add it to the node
PokemonNode *TraversePokemon(char Name[50]) {
PokemonNode *temp = Pokedex.PokemonNodePointer;
while (temp->Next != NULL) {
if (strcmp(temp->Name, Name) == 0) {
return temp;
}
temp = temp->Next;
}
if (strcmp(temp->Name, Name) == 0) {
return temp;
} else {
return NULL;
}
}
// Outputs the players and their owned pokemon
void DisplayPlayerDetails() {
PlayerNode *temp = Pokedex.PlayerNodePointer;
while (temp->Next != NULL) {
printf("%s\n", temp->Name);
for (int i = 0; i < temp->PokemonOwned; i++) {
printf("%s ", temp->Pokemon[i]->Name);
}
printf("\n");
temp = temp->Next;
}
printf("%s\n", temp->Name);
for (int i = 0; i < temp->PokemonOwned; i++) {
printf("%s ", temp->Pokemon[i]->Name);
}
printf("\n");
}
// This goes through the pokemon pointers to get the Pokemon with their type and abilities and prints them each out
void DisplayPokemonDetails() {
PokemonNode *temp = Pokedex.PokemonNodePointer;
while (temp->Next != NULL) {
printf("%s ", temp->Name);
printf("%s ", temp->Type);
printf("%s ", temp->Ability);
printf("\n");
temp = temp->Next;
}
}
// This displayes the Pokemons names
void ListPokemon() {
PokemonNode *temp = Pokedex.PokemonNodePointer;
while (temp->Next != NULL) {
printf("%s ", temp->Name);
printf("\n");
temp = temp->Next;
}
}
// This displayes the Players names
void ListPlayers() {
PlayerNode *temp = Pokedex.PlayerNodePointer;
while (temp->Next != NULL) {
printf("%s ", temp->Name);
printf("\n");
temp = temp->Next;
}
}
\ 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