Commit 8681ce52 authored by zak.evans's avatar zak.evans

Delete main.c

parent ea865ec8
#include <iostream>
#include <string>
using namespace std;
// Pokemon struct
struct Pokemon {
string name;
int id;
string type;
string ability;
Pokemon* next;
// Pointer to next Pokemon in linked list
};
// Player struct
struct Player {
string name;
Player* next;
// Pointer to next Player in linked list
Pokemon* pokemon;
// Pointer to the first Pokemon in the linked list of the player's captured Pokemon
};
// Defining the global variables
Pokemon* pokemon = nullptr;
Player* player = nullptr;
// Function to create a new Pokemon node
Pokemon* createPokemon(string name, int id, string type, string ability) {
Pokemon* newPokemon = new Pokemon;
newPokemon->name = name;
newPokemon->id = id;
newPokemon->type = type;
newPokemon->ability = ability;
newPokemon->next = nullptr;
return newPokemon;
}
// Function to create a new Player node
Player* createPlayer(string name) {
Player* newPlayer = new Player;
newPlayer->name = name;
newPlayer->pokemon = nullptr;
newPlayer->next = nullptr;
return newPlayer;
}
// Function to add a Pokemon to the Pokemons list
void addPokemon(Pokemon** newPokemon, Pokemon** head) {
(*newPokemon)->next = *head;
*head = *newPokemon;
}
// Function to add a Player to the Players list
void addPlayer(Player** newPlayer, Player** head) {
(*newPlayer)->next = *head;
*head = *newPlayer;
}
// Function to add a Pokemon to a Player's Pokemons list
void addPokemonToPlayer(Player* player, Pokemon* newPokemon) {
newPokemon->next = player->pokemon;
player->pokemon = newPokemon;
}
// Function to display all the Pokemons
void displayPokemons(Pokemon* head) {
Pokemon* pokemonHead = head;
while (pokemonHead != nullptr) {
cout << pokemonHead->name << " (#" << pokemonHead->id << ")" << " (#" << pokemonHead->type << ")" " (#" << pokemonHead->ability << ")" << endl;
pokemonHead = pokemonHead->next;
}
}
// Function to display all the Players
void displayPlayers(Player* head) {
Player* playerHead = head;
while (playerHead != nullptr) {
cout << playerHead->name << endl;
playerHead = playerHead->next;
}
}
// Function to display a Player's Pokemons
void displayPlayerPokemons(Player* player) {
displayPokemons(player->pokemon);
}
// Function to find a speicifc Pokemon
Pokemon* findPokemon(string name, Pokemon* head) {
Pokemon* current = head;
while (current != nullptr) {
if (current->name == name) {
return current;
}
current = current->next;
}
return nullptr;
}
//Function to find a specific player
Player* findPlayer(string name, Player* head) {
Player* current = head;
while (current != nullptr) {
if (current->name == name) {
return current;
}
current = current->next;
}
return nullptr;
}
// Function to delete a Pokemon list
void deletePokemonList(Pokemon** head) {
Pokemon* current = *head;
while (current != nullptr) {
Pokemon* next = current->next;
delete current;
current = next;
}
*head = nullptr;
}
// Function to delete a Player list
void deletePlayerList(Player** head) {
Player* current = *head;
while (current != nullptr) {
Player* next = current->next;
deletePokemonList(&current->pokemon);
delete current;
current = next;
}
*head = nullptr;
}
// Main function
int main() {
// Create the first few Pokemons
Pokemon* bulbasaur = createPokemon("Bulbasaur", 1, "Grass", "Overgrow");
Pokemon* charmander = createPokemon("Charmander", 2, "Fire", "Blaze");
Pokemon* squirtle = createPokemon("Squirtle", 3, "Water", "Torrent");
Pokemon* caterpie = createPokemon("Caterpie", 4, "Bug", "Shield Dusk");
// Create the first few Players
Player* zak = createPlayer("Zak");
Player* will = createPlayer("Will");
Player* tom = createPlayer("Tom");
// Adding the Pokemons to the Pokemons list
addPokemon(&bulbasaur, &pokemon);
addPokemon(&charmander, &pokemon);
addPokemon(&squirtle, &pokemon);
addPokemon(&caterpie, &pokemon);
// Adding the Players to the Players list
addPlayer(&zak, &player);
addPlayer(&will, &player);
addPlayer(&tom, &player);
// Add some of the Pokemons to the Players' Pokemons list
addPokemonToPlayer(zak, bulbasaur);
addPokemonToPlayer(zak, charmander);
addPokemonToPlayer(will, squirtle);
addPokemonToPlayer(tom, caterpie);
// Display the Pokemons and Players with their Pokemons
cout << "All Pokemons:\n";
displayPokemons(pokemon);
cout << endl;
cout << "All Players:\n";
displayPlayers(player);
cout << endl;
cout << "Zak's Pokemons:\n";
displayPlayerPokemons(zak);
cout << endl;
cout << "Will's Pokemons:\n";
displayPlayerPokemons(will);
cout << endl;
cout << "Tom's Pokemons:\n";
displayPlayerPokemons(tom);
cout << endl;
// Cleans up the memory at the end
deletePokemonList(&pokemon);
deletePlayerList(&player);
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