Commit 84b37ed2 authored by Jair Canelo's avatar Jair Canelo

initial

parent ea6eee19
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// this function is used to store pokemon's data such as id, type, name,level.
struct Pokédex {
char name[100];
int id;
char type[50];
int level;
};
// This function is used to store each trainers's data such as ID, name, age, hometown.
struct trainers {
char name[100];
int age;
int experience;
int pokeballs;
struct Pokédex team[6]; // an array of 6 Pokédex structs
};
typedef struct pokemon {
int number;
char name[50];
char type[20];
int level;
struct pokemon* next;
} Pokemon;
typedef struct player {
char name[50];
int score;
Pokemon* pokemon_list;
struct player* next;
} Player;
void print_pokemon(Pokemon* pokemon) {
printf("%d %s (%s) - level %d\n", pokemon->number, pokemon->name, pokemon->type, pokemon->level);
}
void print_pokemon_list(Pokemon* head) {
for (Pokemon* current = head; current != NULL; current = current->next) {
print_pokemon(current);
}
}
void print_player(Player* player) {
printf("%s - Score: %d\n", player->name, player->score);
printf("Captured Pokémon:\n");
print_pokemon_list(player->pokemon_list);
}
void print_player_list(Player* head) {
printf("Player List:\n");
for (Player* current = head; current != NULL; current = current->next) {
print_player(current);
}
}
void insert_pokemon(Pokemon** head, int number, char* name, char* type, int level) {
// allocate memory for the new Pokemon
Pokemon* new_pokemon = (Pokemon*) malloc(sizeof(Pokemon));
if (new_pokemon == NULL) {
// error handling for when malloc fails to allocate memory
printf("Error: failed to allocate memory for new Pokemon\n");
return;
}
// copy values into the new Pokemon node
new_pokemon->number = number;
strcpy_s(new_pokemon->name, sizeof(new_pokemon->name), name);
strcpy_s(new_pokemon->type, sizeof(new_pokemon->type), type);
new_pokemon->level = level;
// insert the new Pokemon at the head of the list
new_pokemon->next = *head;
*head = new_pokemon;
}
void insert_player(Player** head, char* name, int score, Pokemon* pokemon_list) {
// allocate memory for the new Player
Player* new_player = (Player*) malloc(sizeof(Player));
if (new_player == NULL) {
// error handling for when malloc fails to allocate memory
printf("Error: failed to allocate memory for new Player\n");
return;
}
// copy values into the new Player node
strcpy_s(new_player->name, sizeof(new_player->name), name);
new_player->score = score;
new_player->pokemon_list = pokemon_list;
// insert the new Player at the head of the list
new_player->next = *head;
*head = new_player;
}
int main() {
// create Ash's list of Pokemon
Pokemon* ash_pokemon_list = NULL;
insert_pokemon(&ash_pokemon_list, 25, "Pikachu", "Electric", 12);
insert_pokemon(&ash_pokemon_list, 1, "Bulbasaur", "Grass/Poison", 8);
insert_pokemon(&ash_pokemon_list, 4, "Charmander", "Fire", 10);
// create Brock's list of Pokemon
Pokemon* brock_pokemon_list = NULL;
insert_pokemon(&brock_pokemon_list, 74, "Geodude", "Rock/Ground", 9);
insert_pokemon(&brock_pokemon_list, 95, "Onix", "Rock/Ground", 11);
insert_pokemon(&brock_pokemon_list, 76, "Golem", "Rock/Ground", 15);
// create Ash's player node and insert it into the player list
Player* ash = NULL;
insert_player(&ash, "Ash", 100, ash_pokemon_list);
// create Brock's player node and insert it into the player list
Player* brock = NULL;
insert_player(&brock, "Brock", 50, brock_pokemon_list);
// print the list of players and their captured Pokemon
print_player_list(ash);
print_player_list(brock);
return 0;
}
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