Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Pokedex Prog3
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
antony.adewunmi-jones
Pokedex Prog3
Commits
1252f0dc
Commit
1252f0dc
authored
Jan 16, 2023
by
antony.adewunmi-jones
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parents
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
600 additions
and
0 deletions
+600
-0
pokedex.c
pokedex.c
+600
-0
No files found.
pokedex.c
0 → 100644
View file @
1252f0dc
// import libraries
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef
struct
pokemon
{
// Var for pokeomon name
char
pokeName
[
12
];
// Var for pokemon type
char
pokeType
[
15
];
// Var for pokemon ability
char
pokeAbility
[
25
];
// Pointer to the previous and next pokemon
struct
pokemon
*
prevPokemon
;
struct
pokemon
*
nextPokemon
;
}
pokemon
;
typedef
struct
player
{
// Var for player name
char
playerName
[
12
];
// Number of Pokemon Owned by Player
int
numPokemonOwned
;
// Variable array of pokemon owned by player
struct
pokemon
*
playerPokemon
[
20
];
// Pointers to previous and next entries
struct
player
*
prevPlayer
;
struct
player
*
nextPlayer
;
}
player
;
// for the head of the list of pokemon and players
typedef
struct
pokedex
{
// Head (root/first) Pokemon
struct
pokemon
*
headPokemon
;
// Tail (last) Pokemon
struct
pokemon
*
tailPokemon
;
// Head (first) player
struct
player
*
headPlayer
;
// Tail (last) player
struct
player
*
tailPlayer
;
}
pokedex
;
pokedex
*
mainPokedex
;
////////////////////////////////////////////////////////////////////////////////////////////
// Declare Functions
pokemon
*
NewPokemonNode
(
char
pokeName
[
12
],
char
pokeType
[
15
],
char
pokeAbility
[
25
]);
player
*
NewPlayerNode
(
char
playerName
[
12
]);
void
listPlayerPokemon
(
pokedex
*
pokedex
,
player
*
player
);
void
AddPokemonToList
(
pokedex
*
pokedex
,
char
name
[
12
],
char
type
[
15
],
char
ability
[
25
]);
void
AddPlayerToList
(
pokedex
*
pokedex
,
char
name
[
12
]);
void
AddPokemonToPlayer
(
pokedex
*
pokedex
,
char
*
pokeName
,
char
*
playerName
);
void
ListPokemon
(
pokedex
*
pokedex
);
void
ListPlayers
(
pokedex
*
pokedex
);
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
// grabs users input upon request and returns it as a character
char
*
getUserInput
()
{
char
*
input
=
(
char
*
)
malloc
(
100
*
sizeof
(
char
));
scanf
(
"%s"
,
input
);
return
input
;
}
// function to convert data types to stop crashdumping
char
*
convertArrayToCharPointer
(
char
array
[
12
])
{
int
size
=
strlen
(
array
);
char
*
pointer
=
(
char
*
)
malloc
((
size
+
1
)
*
sizeof
(
char
));
strcpy
(
pointer
,
array
);
return
pointer
;
}
// function to check if a pokemon already exists in a player's pokemon array
bool
PokemonExistsInPlayersPokemon
(
player
*
player
,
pokemon
*
pokemon
)
{
//Iterate through the players pokemon array and see if the name matches the pokemon we are looking for
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
;
}
// creates a new node and returns a pointer to that node
pokemon
*
NewPokemonNode
(
char
pokeName
[
12
],
char
pokeType
[
15
],
char
pokeAbility
[
25
])
{
// allocate new memory
pokemon
*
newPokemonNode
=
NULL
;
newPokemonNode
=
malloc
(
sizeof
(
pokemon
));
// new variable allocation
newPokemonNode
->
prevPokemon
=
NULL
;
newPokemonNode
->
nextPokemon
=
NULL
;
strcpy
(
newPokemonNode
->
pokeName
,
pokeName
);
strcpy
(
newPokemonNode
->
pokeType
,
pokeType
);
strcpy
(
newPokemonNode
->
pokeAbility
,
pokeAbility
);
return
newPokemonNode
;
};
// creates a new node and returns a pointer to that node. The Pokemon count
// should be set to zero.
player
*
NewPlayerNode
(
char
playerName
[
12
])
{
// allocate new memory
player
*
newPlayerNode
=
NULL
;
newPlayerNode
=
malloc
(
sizeof
(
player
));
// Create a new List of Pokemon for the player
struct
pokemon
*
playersPokemon
;
playersPokemon
=
(
struct
pokemon
*
)
malloc
(
20
*
sizeof
(
struct
pokemon
));
// new variable allocation
newPlayerNode
->
prevPlayer
=
NULL
;
newPlayerNode
->
nextPlayer
=
NULL
;
memset
(
newPlayerNode
->
playerPokemon
,
0
,
sizeof
(
newPlayerNode
->
playerPokemon
));
newPlayerNode
->
numPokemonOwned
=
0
;
strcpy
(
newPlayerNode
->
playerName
,
playerName
);
//newPlayerNode->playerPokemon = playersPokemon;
return
newPlayerNode
;
}
// checks to see if name already exists in the Pokemon list. If it doesn’t it
// creates a new node and adds it to the list. It the Pokemon already exists it
// does nothing.
void
AddPokemonToList
(
pokedex
*
pokedex
,
char
name
[
12
],
char
type
[
15
],
char
ability
[
25
])
{
// Pokemon does not exist as already checked by FindPokemon and returned NULL
// Create a new Pokemon Node :)
// We set the node to NULL just to make sure
pokemon
*
newPokemon
=
NewPokemonNode
(
name
,
type
,
ability
);
// Since we just made a new Node, we need to set the next PokemonNode to NULL
newPokemon
->
nextPokemon
=
NULL
;
// Quick check to see if there is any Pokemon in the Pokedex
if
(
pokedex
->
headPokemon
==
NULL
)
{
// If 0 Pokemon, first Pokemon needs to be head of the Pokemon List
pokedex
->
headPokemon
=
newPokemon
;
pokedex
->
tailPokemon
=
newPokemon
;
// Because there is only one Pokemon, it is both the first and last Pokemon
}
// If there is already Pokemon in the Pokemon List, lets add it to the end of
// the most recent Pokemon
else
{
newPokemon
->
nextPokemon
=
NULL
;
// The PREVIOUS last pokemon will now the NEW pokemons previous Pokemon
newPokemon
->
prevPokemon
=
pokedex
->
tailPokemon
;
// The current LAST Pokemons next pokemon now needs to be our new one
pokedex
->
tailPokemon
->
nextPokemon
=
newPokemon
;
// The new LAST pokemon is our new pokemon
pokedex
->
tailPokemon
=
newPokemon
;
}
}
// checks to see if name already existsin the Player list. If it doesn’t it
// creates a new node and adds it to the list. It the player already exists it
// does nothing.
void
AddPlayerToList
(
pokedex
*
pokedex
,
char
name
[
12
])
{
// already checked if player exists with FindPlayer and it has come back NULL
// newplayer node
// for this set the new players pokemon list to null: newPlayer->pokemon =
// NULL
player
*
newPlayer
=
NewPlayerNode
(
name
);
// Since we just made a new Node, we need to set the next PokemonNode to NULL
newPlayer
->
nextPlayer
=
NULL
;
// Quick check to see if there is any Players in the Pokedex
if
(
pokedex
->
headPlayer
==
NULL
)
{
// If 0 Players, first Player needs to be head of the Player List
pokedex
->
headPlayer
=
newPlayer
;
pokedex
->
tailPlayer
=
newPlayer
;
// Because there is only one Player, it is both the first and last Player
}
// If there is already a player in the Player List, lets add it to the end of
// the most recent players
else
{
newPlayer
->
nextPlayer
=
NULL
;
// The PREVIOUS last player will now the NEW players previous player
newPlayer
->
prevPlayer
=
pokedex
->
tailPlayer
;
// The current LAST Players next player now needs to be our new one
pokedex
->
tailPlayer
->
nextPlayer
=
newPlayer
;
// The new LAST player is our new pokemon
pokedex
->
tailPlayer
=
newPlayer
;
}
}
// adds the Pokemon to the player’s Pokemon list (if it is not already in there)
// and increments the Pokemon count for that player.
void
AddPokemonToPlayer
(
pokedex
*
pokedex
,
char
*
pokeName
,
char
*
playerName
)
{
// names of player and pokemon to be added already taken in menu
// Both returned as existing in menu
player
*
Player
=
NULL
;
pokemon
*
Pokemon
=
NULL
;
// Check if the Player Exists
if
(
FindPlayer
(
pokedex
,
playerName
)
==
NULL
)
{
printf
(
"COULDN'T FIND PLAYER %s"
,
playerName
);
return
;
}
// Check if Pokemon Exists
if
(
FindPokemon
(
pokedex
,
pokeName
)
==
NULL
)
{
printf
(
"COULDN'T FIND POKEMON: %s"
,
pokeName
);
return
;
}
// If we find both of them, set the variables = to the player & pokemon
Player
=
FindPlayer
(
pokedex
,
playerName
);
Pokemon
=
FindPokemon
(
pokedex
,
pokeName
);
// Check if the player has any Pokemon in their Pokemon List
if
(
Player
->
numPokemonOwned
==
0
)
{
// If not, set the Pokemon
Player
->
playerPokemon
[
0
]
=
Pokemon
;
// Update the Players Pokemon Count to 1
Player
->
numPokemonOwned
++
;
return
;
}
// If the player already has the pokemon in the list, we don't need to add it
// again?
else
if
(
PokemonExistsInPlayersPokemon
(
Player
,
Pokemon
)
==
true
)
{
printf
(
"Pokemon already exists in %s's, Pokemon."
,
Player
->
playerName
);
}
// If the player already has Pokemon, we need to update the TAIL Pokemon
else
{
Player
->
playerPokemon
[
Player
->
numPokemonOwned
]
=
Pokemon
;
Player
->
numPokemonOwned
++
;
}
}
// outputs a list of names of all Pokemon in the Pokedex
void
ListPokemon
(
pokedex
*
pokedex
)
{
struct
pokemon
*
pointInList
=
NULL
;
printf
(
"
\n
"
);
// check if list of pokemon is empty
if
(
pokedex
->
headPokemon
==
NULL
)
{
printf
(
"No Pokemon in Pokedex
\n
"
);
return
;
}
// if there are entries, list them
pointInList
=
pokedex
->
headPokemon
;
while
(
pointInList
!=
NULL
)
{
printf
(
"%s"
,
pointInList
->
pokeName
);
printf
(
"
\n
"
);
// Move to next pokemon
pointInList
=
pointInList
->
nextPokemon
;
}
}
// outputs a list of names of all players in the Pokedex
void
ListPlayers
(
pokedex
*
pokedex
)
{
struct
player
*
pointInList
=
NULL
;
if
(
pokedex
->
headPlayer
==
NULL
)
{
printf
(
"No Players in Pokedex
\n
"
);
return
;
}
pointInList
=
pokedex
->
headPlayer
;
while
(
pointInList
!=
NULL
)
{
printf
(
"%s"
,
pointInList
->
playerName
);
// Move to next player
pointInList
=
pointInList
->
nextPlayer
;
}
}
// searches the Pokemon list for name. If it finds name it returns a pointer to
// the name’s node otherwise it returns NULL.
pokemon
*
FindPokemon
(
pokedex
*
pokedex
,
char
*
name
)
{
// pointer to hold where in stack
struct
pokemon
*
pointInList
=
NULL
;
if
(
pokedex
->
headPokemon
==
NULL
)
{
return
NULL
;
}
pointInList
=
pokedex
->
headPokemon
;
while
(
1
)
{
// if current place in list is empty, it has passed all entries in the list
// without finding it
if
(
pointInList
==
NULL
)
{
printf
(
"Not Found
\n
"
);
return
NULL
;
}
// if the current point in the list is the same as the name given by user
// if 0, it means they match
else
if
(
strcmp
(
convertArrayToCharPointer
(
pointInList
->
pokeName
),
name
)
==
0
)
{
printf
(
"pokemon found
\n
"
);
return
pointInList
;
}
// if not the same, increment pointInList
else
{
pointInList
=
pointInList
->
nextPokemon
;
}
}
}
// searches the Player list for name. If it finds name it returns a pointer to
// the name’s node otherwise it returns NULL
player
*
FindPlayer
(
pokedex
*
pokedex
,
char
*
name
)
{
// pointer to hold where in stack
struct
player
*
pointInList
=
NULL
;
if
(
pokedex
->
headPlayer
==
NULL
)
{
printf
(
"No Players in Pokedex
\n
"
);
return
NULL
;
}
pointInList
=
pokedex
->
headPlayer
;
while
(
1
)
{
// if current place in list is empty, it has passed all entries in the list
// without finding it
if
(
pointInList
==
NULL
)
{
printf
(
"Not Found
\n
"
);
return
NULL
;
break
;
}
// if the current point in the list is the same as the name given by user
// if 0, it means they match
else
if
(
strcmp
(
convertArrayToCharPointer
(
pointInList
->
playerName
),
name
)
==
0
)
{
return
pointInList
;
break
;
}
// if not the same, increment pointInList
else
{
pointInList
=
pointInList
->
nextPlayer
;
}
}
}
// outputs the details of name to the screen
void
displayPokemonDetails
(
pokedex
*
pokedex
,
char
*
name
)
{
// pointer to hold where in stack
struct
pokemon
*
pointInList
=
NULL
;
if
(
pokedex
->
headPokemon
==
NULL
)
{
printf
(
"No Pokemon in Pokedex
\n
"
);
return
;
}
pointInList
=
pokedex
->
headPokemon
;
while
(
1
)
{
// if current place in list is empty, it has passed all entries in the list
// without finding it
if
(
pointInList
==
NULL
)
{
printf
(
"Not Found
\n
"
);
break
;
}
// if the current point in the list is the same as the name given by user
// USE strcmp(pointInList->pokemonName, name == 0 ) if 0, it means they
// match :)
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
;
}
// if not the same, increment pointInList
else
{
pointInList
=
pointInList
->
nextPokemon
;
}
}
}
// outputs the details of name to the screen, including a list of names of all
// the Pokemon owned
void
DisplayPlayerDetails
(
pokedex
*
pokedex
,
char
*
name
)
{
player
*
Player
=
NULL
;
// pointer to hold where in stack
struct
player
*
pointInList
=
NULL
;
if
(
pokedex
->
headPlayer
==
NULL
)
{
printf
(
"No Players in Pokedex
\n
"
);
return
;
}
pointInList
=
pokedex
->
headPlayer
;
while
(
1
)
{
// if current place in list is empty, it has passed all entries in the list
// without finding it
if
(
pointInList
==
NULL
)
{
printf
(
"Not Found
\n
"
);
break
;
}
// if the current point in the list is the same as the name given by user
// if 0, it means they match
else
if
(
strcmp
(
convertArrayToCharPointer
(
pointInList
->
playerName
),
name
)
==
0
)
{
// print player name
printf
(
"Name: %s
\n
"
,
pointInList
->
playerName
);
// print player pokemon
listPlayerPokemon
(
pokedex
,
pointInList
);
break
;
}
// if not the same, increment pointInList
else
{
pointInList
=
pointInList
->
nextPlayer
;
}
}
}
// prints out the array of players pokemon if there are any
void
listPlayerPokemon
(
pokedex
*
pokedex
,
player
*
player
)
{
// Check if the player has any Pokemon in their Pokemon List
if
(
player
->
numPokemonOwned
==
0
)
{
// If not, output
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 the Pokedex, please input a number to choose option
\n
"
);
printf
(
"1 AddPokemonToList
\n
"
);
printf
(
"2 AddPlayerToList
\n
"
);
printf
(
"3 AddPokemonToPlayer
\n
"
);
printf
(
"4 DisplayPokemonDetails
\n
"
);
printf
(
"5 DisplayPlayerDetails
\n
"
);
printf
(
"6 ListPokemon
\n
"
);
printf
(
"7 ListPlayers
\n
"
);
printf
(
"Enter choice: "
);
int
choice
;
scanf
(
"%d"
,
&
choice
);
// switch statement to run the correct function as requested by user
switch
(
choice
)
{
case
1
:
// ADDPOKEMONTOLIST
// Get Name of Pokemon
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
();
// first checks that pokemon does not exist yet.
if
(
FindPokemon
(
mainPokedex
,
pokemonName
)
==
NULL
)
{
AddPokemonToList
(
mainPokedex
,
pokemonName
,
pokemonType
,
pokemonAbility
);
printf
(
"Successfully added Pokemon: %s, %s, %s
\n
"
,
pokemonName
,
pokemonType
,
pokemonAbility
);
}
break
;
case
2
:
// Get Name of Player
printf
(
"
\n
"
);
printf
(
"What is the name of the Player?
\n
"
);
char
*
playerName
=
getUserInput
();
pokemon
*
playerPokeArray
=
NULL
;
// first checks that player does not exist yet.
if
(
FindPlayer
(
mainPokedex
,
playerName
)
==
NULL
)
{
AddPlayerToList
(
mainPokedex
,
playerName
);
printf
(
"Added %s
\n
"
,
playerName
);
}
break
;
case
3
:
// AddPokemonToPlayer
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
:
// DisplayPokemonDetails
printf
(
"
\n
"
);
printf
(
"What is the name of the Pokemon?
\n
"
);
char
*
pokemonToSearch
=
getUserInput
();
// char search[12] = "poke2";
displayPokemonDetails
(
mainPokedex
,
pokemonToSearch
);
break
;
case
5
:
// DisplayPlayerDetails
printf
(
"
\n
"
);
printf
(
"What is the name of the Player?
\n
"
);
char
*
playerToSearch
=
getUserInput
();
DisplayPlayerDetails
(
mainPokedex
,
playerToSearch
);
break
;
case
6
:
// ListPokemon
printf
(
"
\n
"
);
ListPokemon
(
mainPokedex
);
break
;
case
7
:
// ListPlayers
printf
(
"
\n
"
);
ListPlayers
(
mainPokedex
);
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
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