Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pokedex.2
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
jair.canelo
pokedex.2
Commits
84b37ed2
Commit
84b37ed2
authored
Apr 04, 2023
by
Jair Canelo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
initial
parent
ea6eee19
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
127 additions
and
0 deletions
+127
-0
pokedex2.c
pokedex2.c
+127
-0
No files found.
pokedex2.c
0 → 100644
View file @
84b37ed2
#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
;
}
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