Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Pokedex New
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zak.evans
Pokedex New
Commits
c71c6c36
Commit
c71c6c36
authored
Mar 31, 2023
by
zak.evans
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parent
8681ce52
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
205 additions
and
0 deletions
+205
-0
main.c
main.c
+205
-0
No files found.
main.c
0 → 100644
View file @
c71c6c36
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Pokemon struct
typedef
struct
Pokemon
{
char
name
[
20
];
char
type
[
20
];
char
ability
[
20
];
struct
Pokemon
*
next
;
}
Pokemon
;
// Pointer to next Pokemon in linked list
// Player struct
typedef
struct
Player
{
char
name
[
20
];
int
num_pokemon
;
Pokemon
*
pokemon_list
;
struct
Player
*
next
;
}
Player
;
// Pointer to next Player in linked list
// Pokédex struct
typedef
struct
Pokedex
{
int
num_pokemon
;
Pokemon
*
pokemon_list
;
Player
*
player_list
;
}
Pokedex
;
// Function to create a new Pokemon
Pokemon
*
create_pokemon
(
char
name
[],
char
type
[],
char
ability
[])
{
Pokemon
*
new_pokemon
=
(
Pokemon
*
)
malloc
(
sizeof
(
Pokemon
));
strcpy
(
new_pokemon
->
name
,
name
);
strcpy
(
new_pokemon
->
type
,
type
);
strcpy
(
new_pokemon
->
ability
,
ability
);
new_pokemon
->
next
=
NULL
;
return
new_pokemon
;
}
// Function which adds a Pokemon to the Pokédex
void
add_pokemon_to_pokedex
(
Pokedex
*
pokedex
,
Pokemon
*
new_pokemon
)
{
if
(
pokedex
->
pokemon_list
==
NULL
)
{
pokedex
->
pokemon_list
=
new_pokemon
;
}
else
{
Pokemon
*
current
=
pokedex
->
pokemon_list
;
while
(
current
->
next
!=
NULL
)
{
current
=
current
->
next
;
}
current
->
next
=
new_pokemon
;
}
pokedex
->
num_pokemon
++
;
}
// Function to create a new Player
Player
*
create_player
(
char
name
[])
{
Player
*
new_player
=
(
Player
*
)
malloc
(
sizeof
(
Player
));
strcpy
(
new_player
->
name
,
name
);
new_player
->
num_pokemon
=
0
;
new_player
->
pokemon_list
=
NULL
;
new_player
->
next
=
NULL
;
return
new_player
;
}
// Function which adds a Player to the Pokédex
void
add_player_to_pokedex
(
Pokedex
*
pokedex
,
Player
*
new_player
)
{
if
(
pokedex
->
player_list
==
NULL
)
{
pokedex
->
player_list
=
new_player
;
}
else
{
Player
*
current
=
pokedex
->
player_list
;
while
(
current
->
next
!=
NULL
)
{
current
=
current
->
next
;
}
current
->
next
=
new_player
;
}
}
// Function to capture a Pokemon by a Player
void
capture_pokemon
(
Player
*
player
,
Pokemon
*
pokemon
)
{
pokemon
->
next
=
player
->
pokemon_list
;
player
->
pokemon_list
=
pokemon
;
player
->
num_pokemon
++
;
}
// Function to display all Pokemon in the Pokédex
void
display_all_pokemon
(
Pokedex
*
pokedex
)
{
printf
(
"All Pokemon:
\n
"
);
Pokemon
*
current
=
pokedex
->
pokemon_list
;
while
(
current
!=
NULL
)
{
printf
(
"%s (%s) - %s
\n
"
,
current
->
name
,
current
->
type
,
current
->
ability
);
current
=
current
->
next
;
}
}
// Function to display all Players in the Pokédex
void
display_all_players
(
Pokedex
*
pokedex
)
{
printf
(
"
\n
All Players:
\n
"
);
Player
*
current
=
pokedex
->
player_list
;
while
(
current
!=
NULL
)
{
printf
(
"%s (%d Pokemon)
\n
"
,
current
->
name
,
current
->
num_pokemon
);
current
=
current
->
next
;
}
}
// Function to display a Player's captured Pokemon
void
display_player_pokemon
(
Player
*
player
)
{
printf
(
"
\n
%s's Pokemon:
\n
"
,
player
->
name
);
Pokemon
*
current
=
player
->
pokemon_list
;
while
(
current
!=
NULL
)
{
printf
(
"%s (%s) - %s
\n
"
,
current
->
name
,
current
->
type
,
current
->
ability
);
current
=
current
->
next
;
}
}
int
main
()
{
// Create a new Pokédex
Pokedex
*
pokedex
=
(
Pokedex
*
)
malloc
(
sizeof
(
Pokedex
));
pokedex
->
num_pokemon
=
0
;
pokedex
->
pokemon_list
=
NULL
;
pokedex
->
player_list
=
NULL
;
// Create some Pokemon
Pokemon
*
bulbasaur
=
create_pokemon
(
"Bulbasaur"
,
"Grass"
,
"Overgrow"
);
Pokemon
*
charmander
=
create_pokemon
(
"Charmander"
,
"Fire"
,
"Blaze"
);
Pokemon
*
squirtle
=
create_pokemon
(
"Squirtle"
,
"Water"
,
"Torrent"
);
Pokemon
*
caterpie
=
create_pokemon
(
"Caterpie"
,
"Bug"
,
"Shield Dust"
);
Pokemon
*
metapod
=
create_pokemon
(
"Metapod"
,
"Bug"
,
"Shed Skin"
);
Pokemon
*
butterfree
=
create_pokemon
(
"Butterfree"
,
"Bug"
,
"Compound Eyes"
);
Pokemon
*
beedrill
=
create_pokemon
(
"Beedrill"
,
"Bug"
,
"Swarm"
);
Pokemon
*
pidgey
=
create_pokemon
(
"Pidgey"
,
"Normal"
,
"Keen Eye"
);
// Adds the Pokemon to the Pokédex
add_pokemon_to_pokedex
(
pokedex
,
bulbasaur
);
add_pokemon_to_pokedex
(
pokedex
,
charmander
);
add_pokemon_to_pokedex
(
pokedex
,
squirtle
);
add_pokemon_to_pokedex
(
pokedex
,
caterpie
);
add_pokemon_to_pokedex
(
pokedex
,
metapod
);
add_pokemon_to_pokedex
(
pokedex
,
butterfree
);
add_pokemon_to_pokedex
(
pokedex
,
beedrill
);
add_pokemon_to_pokedex
(
pokedex
,
pidgey
);
// Display all Pokemon in the Pokédex
display_all_pokemon
(
pokedex
);
// Create some of the players
Player
*
zak
=
create_player
(
"Zak"
);
Player
*
leo
=
create_player
(
"Leo"
);
Player
*
will
=
create_player
(
"Will"
);
// Adds the players to the Pokédex
add_player_to_pokedex
(
pokedex
,
zak
);
add_player_to_pokedex
(
pokedex
,
leo
);
add_player_to_pokedex
(
pokedex
,
will
);
// Display all of the players in the Pokédex
display_all_players
(
pokedex
);
// Capture some Pokemon:
//Zak's Pokemon
capture_pokemon
(
zak
,
charmander
);
printf
(
"%s has captured %s!
\n
"
,
zak
->
name
,
charmander
->
name
);
capture_pokemon
(
zak
,
squirtle
);
printf
(
"%s has captured %s!
\n
"
,
zak
->
name
,
squirtle
->
name
);
capture_pokemon
(
zak
,
beedrill
);
printf
(
"%s has captured %s!
\n
"
,
zak
->
name
,
beedrill
->
name
);
//Leo's Pokemon
capture_pokemon
(
leo
,
bulbasaur
);
printf
(
"%s has captured %s!
\n
"
,
leo
->
name
,
bulbasaur
->
name
);
capture_pokemon
(
leo
,
butterfree
);
printf
(
"%s has captured %s!
\n
"
,
leo
->
name
,
butterfree
->
name
);
//Will's Pokemon
capture_pokemon
(
will
,
metapod
);
printf
(
"%s has captured %s!
\n
"
,
will
->
name
,
metapod
->
name
);
capture_pokemon
(
will
,
pidgey
);
printf
(
"%s has captured %s!
\n
"
,
will
->
name
,
pidgey
->
name
);
// Display Zak's captured Pokemon
display_player_pokemon
(
zak
);
display_player_pokemon
(
leo
);
display_player_pokemon
(
will
);
// Free memory
free
(
bulbasaur
);
free
(
charmander
);
free
(
squirtle
);
free
(
caterpie
);
free
(
metapod
);
free
(
butterfree
);
free
(
beedrill
);
free
(
pidgey
);
free
(
zak
);
free
(
leo
);
free
(
will
);
free
(
pokedex
);
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment