Commit a66953a4 authored by luke.bullas's avatar luke.bullas

Add new file

parent c8bfaff2
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct pokemon {
char pokeName[20];
char pokeType[10];
char pokeAbility[15];
struct pokemon *prevPokemon;
struct pokemon *nextPokemon;
} pokemon;
typedef struct player {
char playerName[20];
int numPokemonOwned;
struct pokemon *playerPokemon[20];
struct player *prevPlayer;
struct player *nextPlayer;
} player;
typedef struct pokedex {
struct pokemon *headPokemon;
struct pokemon *tailPokemon;
struct player *headPlayer;
struct player *tailPlayer;
} pokedex;
pokedex *mainPokedex;
pokemon *newPokemonNode(char pokeName[20], char pokeType[10],
char pokeAbility[15]);
player *newPlayerNode(char playerName[20]);
void listPlayerPokemon(pokedex *pokedex, player *player);
void addPokemonToList(pokedex *pokedex, char name[20], char type[10],
char ability[15]);
void addPlayerToList(pokedex *pokedex, char name[20]);
void addPokemonToPlayer(pokedex *pokedex, char *pokeName, char *playerName);
pokemon *findPokemon(pokedex *pokedex, char *name);
player *findPlayer(pokedex *pokedex, char *name);
void displayPokemonDetails(pokedex *pokedex, char *name);
void displayPlayerDetails(pokedex *pokedex, char *name);
bool pokemonExistsInPlayersPokemon(player *player, pokemon *pokemon);
// function definitions
char *getUserInput() {
char *input = (char *)malloc(100 * sizeof(char));
scanf("%s", input);
return input;
}
char *convertArrayToCharPointer(char array[20]) {
int size = strlen(array);
char *pointer = (char *)malloc((size + 1) * sizeof(char));
strcpy(pointer, array);
return pointer;
}
bool PokemonExistsInPlayersPokemon(player *player, pokemon *pokemon) {
int numOfPokemon = player->numPokemonOwned;
for (int pokeNum = 0; pokeNum <= numOfPokemon; pokeNum++){
if (strcmp(player->playerPokemon[pokeNum]->pokeName, pokemon->pokeName)){
return true;
}
return false;
}
return false;
}
pokemon *newPokemonNode(char pokeName[20], char pokeType[10],
char pokeAbility[15]) {
pokemon *newPokemonNode = NULL;
newPokemonNode = malloc(sizeof(pokemon));
newPokemonNode->prevPokemon = NULL;
newPokemonNode->nextPokemon = NULL;
strcpy(newPokemonNode->pokeName, pokeName);
strcpy(newPokemonNode->pokeType, pokeType);
strcpy(newPokemonNode->pokeAbility, pokeAbility);
return newPokemonNode;
};
player *newPlayerNode(char playerName[12]) {
player *newPlayerNode = NULL;
newPlayerNode = malloc(sizeof(player));
struct pokemon *playersPokemon;
playersPokemon = (struct pokemon *)malloc(20* sizeof(struct pokemon));
newPlayerNode->prevPlayer = NULL;
newPlayerNode->nextPlayer = NULL;
memset(newPlayerNode->playerPokemon, 0, sizeof(newPlayerNode->playerPokemon));
newPlayerNode->numPokemonOwned = 0;
strcpy(newPlayerNode->playerName, playerName);
return newPlayerNode;
}
void AddPokemonToList(pokedex *pokedex, char name[20], char type[10],
char ability[15]) {
pokemon *newPokemon = newPokemonNode(name, type, ability);
newPokemon->nextPokemon = NULL;
if (pokedex->headPokemon == NULL) {
pokedex->headPokemon = newPokemon;
pokedex->tailPokemon = newPokemon;
}
else {
newPokemon->nextPokemon = NULL;
newPokemon->prevPokemon = pokedex->tailPokemon;
pokedex->tailPokemon->nextPokemon = newPokemon;
pokedex->tailPokemon = newPokemon;
}
}
void addPlayerToList(pokedex *pokedex, char name[20]) {
player *newPlayer = newPlayerNode(name);
newPlayer->nextPlayer = NULL;
if (pokedex->headPlayer == NULL) {
pokedex->headPlayer = newPlayer;
pokedex->tailPlayer = newPlayer;
}
else {
newPlayer->nextPlayer = NULL;
newPlayer->prevPlayer = pokedex->tailPlayer;
pokedex->tailPlayer->nextPlayer = newPlayer;
pokedex->tailPlayer = newPlayer;
}
}
void addPokemonToPlayer(pokedex *pokedex, char *pokeName, char *playerName) {
player *Player = NULL;
pokemon *Pokemon = NULL;
if (findPlayer(pokedex, playerName) == NULL) {
printf("COULDN'T FIND PLAYER %s", playerName);
return;
}
if (findPokemon(pokedex, pokeName) == NULL) {
printf("Cannot find Pokemon: %s", pokeName);
return;
}
Player = findPlayer(pokedex, playerName);
Pokemon = findPokemon(pokedex, pokeName);
if (Player->numPokemonOwned == 0) {
Player->playerPokemon[0] = Pokemon;
Player->numPokemonOwned++;
return;
}
else if (PokemonExistsInPlayersPokemon(Player, Pokemon) == true) {
printf("Pokemon already exists in %s's, Pokedex.", Player->playerName);
}
else {
Player->playerPokemon[Player->numPokemonOwned] = Pokemon;
Player->numPokemonOwned++;
}
}
pokemon *findPokemon(pokedex *pokedex, char *name) {
struct pokemon *pointInList = NULL;
if (pokedex->headPokemon == NULL) {
return NULL;
}
pointInList = pokedex->headPokemon;
while (1) {
if (pointInList == NULL) {
printf("Not Found \n");
return NULL;
}
else if (strcmp(convertArrayToCharPointer(pointInList->pokeName), name) ==
0) {
printf("Pokemon found \n");
return pointInList;
}
else {
pointInList = pointInList->nextPokemon;
}
}
}
player *findPlayer(pokedex *pokedex, char *name) {
struct player *pointInList = NULL;
if (pokedex->headPlayer == NULL) {
printf("There are no players in Pokedex \n");
return NULL;
}
pointInList = pokedex->headPlayer;
while (1) {
if (pointInList == NULL) {
printf("Not Found \n");
return NULL;
break;
}
else if (strcmp(convertArrayToCharPointer(pointInList->playerName), name) ==
0) {
return pointInList;
break;
}
else {
pointInList = pointInList->nextPlayer;
}
}
}
void displayPokemonDetails(pokedex *pokedex, char *name) {
struct pokemon *pointInList = NULL;
if (pokedex->headPokemon == NULL) {
printf("There are no Pokemon in Pokedex \n");
return;
}
pointInList = pokedex->headPokemon;
while (1) {
if (pointInList == NULL) {
printf("Not Found \n");
break;
}
else if (strcmp(convertArrayToCharPointer(pointInList->pokeName), name) ==
0) {
printf("Name: %s \n", pointInList->pokeName);
printf("Type: %s \n", pointInList->pokeType);
printf("Ability: %s \n", pointInList->pokeAbility);
break;
}
else {
pointInList = pointInList->nextPokemon;
}
}
}
void DisplayPlayerDetails(pokedex *pokedex, char *name) {
player *Player = NULL;
struct player *pointInList = NULL;
if (pokedex->headPlayer == NULL) {
printf("No Players in Pokedex \n");
return;
}
pointInList = pokedex->headPlayer;
while (1) {
if (pointInList == NULL) {
printf("Not Found \n");
break;
}
else if (strcmp(convertArrayToCharPointer(pointInList->playerName), name) ==
0) {
printf("Name: %s \n", pointInList->playerName);
listPlayerPokemon(pokedex, pointInList);
break;
}
else {
pointInList = pointInList->nextPlayer;
}
}
}
void listPlayerPokemon(pokedex *pokedex, player *player) {
if (player->numPokemonOwned == 0) {
printf("Has no pokemon \n");
}
else {
for (int pokeNum = 0; pokeNum <= player->numPokemonOwned;
pokeNum++) {
if (player->playerPokemon[pokeNum] != NULL){
printf("%s", player->playerPokemon[pokeNum]->pokeName);
printf(" \n");
}
}
}
}
void mainMenu() {
printf(" \n");
printf("Welcome to your Pokedex! Input a number to choose an option \n");
printf("1 Add a Pokemon \n");
printf("2 Add a Player \n");
printf("3 Give a Pokemon to a Player \n");
printf("4 Display a Pokemons Details \n");
printf("5 Display a Players Details \n");
printf("Enter option: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
printf(" \n");
printf("What is the name of the Pokemon? \n");
char *pokemonName = getUserInput();
printf("What is the type of the Pokemon? \n");
char *pokemonType = getUserInput();
printf("What is the ability of the Pokemon? \n");
char *pokemonAbility = getUserInput();
if (findPokemon(mainPokedex, pokemonName) == NULL) {
addPokemonToList(mainPokedex, pokemonName, pokemonType, pokemonAbility);
printf("Successfully added Pokemon: %s, %s, %s \n", pokemonName,
pokemonType, pokemonAbility);
}
break;
case 2:
printf(" \n");
printf("What is the players name? \n");
char *playerName = getUserInput();
pokemon *playerPokeArray = NULL;
if (findPlayer(mainPokedex, playerName) == NULL) {
addPlayerToList(mainPokedex, playerName);
printf("Added %s\n", playerName);
}
break;
case 3:
printf(" \n");
printf("What is the name of the Pokemon? \n");
pokemonName = getUserInput();
printf("What is the name of the Player? \n");
playerName = getUserInput();
if (findPlayer(mainPokedex, playerName) != NULL) {
if (findPokemon(mainPokedex, pokemonName) != NULL) {
addPokemonToPlayer(mainPokedex, pokemonName, playerName);
} else {
printf("Pokemon does not exist \n");
}
} else {
printf("Player does not exist \n");
}
break;
case 4:
printf(" \n");
printf("What is the name of the Pokemon? \n");
char *pokemonToSearch = getUserInput();
displayPokemonDetails(mainPokedex, pokemonToSearch);
break;
case 5:
printf(" \n");
printf("What is the name of the Player? \n");
char *playerToSearch = getUserInput();
DisplayPlayerDetails(mainPokedex, playerToSearch);
break;
}
mainMenu();
int main(void){
setbuf(stdout, 0);
mainPokedex = (pokedex *)malloc(sizeof(pokedex));
mainPokedex->headPokemon = NULL;
mainPokedex->headPlayer = NULL;
mainPokedex->tailPokemon = NULL;
mainPokedex->tailPlayer = NULL;
while (1) {
mainMenu();
}
return 0;
}
\ 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