In: Computer Science
Based on this player.h file, could you make a player.c file which defines the 4 functions? Thanks.
player.h:
#ifndef PLAYERH
#define PLAYERH
#define MAX_ITEMS 10
typedef struct Player
{
int location;
int num_items;
int mitems[MAX_ITEMS]
}t_player;
void player_init_player(t_player *player, int location);
//initialize player
void player_add_item(t_player *player, int item);
void player_remove_item(t_player *player, int item);
int player_has_item(t_player *player, int item); //returns -1 if no
item exists
#endif
Hey There,
- I have understood your question and I have given the answer to the best of my knowledge.
- Here I have used C as my programming language and I have attached both the files and the screenshot showing that the code is working perfectly fine.
- I have also put comments into my code so that you can also understand what I did in the code.
- If you need any further explanations then do let me know in the comments box I will be more than happy to help you.
Answer:
- "player.h" file
#ifndef PLAYERH
#define PLAYERH
#define MAX_ITEMS 10
typedef struct Player
{
int location;
int num_items;
int mitems[MAX_ITEMS];
}t_player;
void player_init_player(t_player *player, int location); //initialize player
void player_add_item(t_player *player, int item);
void player_remove_item(t_player *player, int item);
int player_has_item(t_player *player, int item); //returns -1 if no item exists
#endif
- "player.c" file
// Including header files
#include<stdio.h>
#include "player.h"
// This function will initialize the location of the player
void player_init_player(t_player *player, int location){
player->location = location;
}
// This function will add the given item
void player_add_item(t_player *player, int item){
player->mitems[player->num_items] = item;
player->num_items++;
}
// This function will remove the given item
void player_remove_item(t_player *player, int item){
int i, j, count = 0;
for(i=0; i<player->num_items; i++){
if(player->mitems[i] == item){
printf("The given item removed!\n");
count++;
for(j=i; j<player->num_items-1; j++){
player->mitems[j] = player->mitems[j+1];
}
}
player->num_items--;
}
if(count == 0){
printf("The given item is not present!");
}
}
// This function will check if the given item is peresent or not
int player_has_item(t_player *player, int item){
int count = 0, i;
for(i=0; i<player->num_items; i++){
if(player->mitems[i] == item){
count++;
return 1;
}
}
if(count == 0){
return -1;
}
}
int main(){
// creating struct type object t1
t_player t1;
// Initializing num_items equals to 0
t1.num_items = 0;
// Calling the first function - player_init_player
player_init_player(&t1, 2);
// Printing location to see if the above function worked correctly
printf("The added Location is : %d\n\n", t1.location);
// Calling the second function - player_add_item
player_add_item(&t1, 3);
// Printing total no. of items to see if the above function worked correctly
printf("After adding %d the total Items = %d\n\n", 3, t1.num_items);
// Again calling the second function - player_add_item
player_add_item(&t1, 25);
printf("After adding %d the total Items = %d\n\n", 25, t1.num_items);
// Calling the third function - player_remove_item
player_remove_item(&t1, 3);
// Printing total no. of items to see if the above function worked correctly
printf("After removing %d the total items = %d\n\n", 3, t1.num_items);
// Calling the third function - player_has_item
int result = player_has_item(&t1, 25);
if(result == 1){
printf("The given item %d is present!", 25);
}
if(result == -1){
printf("The given item %d is not present!", 25);
}
return 0;
}
Screenshot of code working correctly:
Hope it helps:)