So I'm trying to write code that simulates people fishing in a pond. Fishers will try to catch fish and then they will keep some that they like in a bucket, while the ones that they don’t like will be put back into the pond. I'm REALLY close to finishing my program, but I'm having trouble with the castLine(Fisher *fisher, Pond *p) function.
This function should simulate a fisher casting his/her line into pond p to try and catch a fish. If there are no fish left in the pond, 0 should be returned, otherwise 1 should be returned at the end of the function. The code should choose a random fish. If the fish is then liked by the fisher, it should be kept in his/her bucket (if not past the limit) and removed from the pond. When removing a fish from the pond, the last fish in the pond should be moved/inserted into the position that the fish was removed from so that there are no gaps.
I'm not quite sure how to do the "if the fish is then liked by the fisher, it should be kept in his/her bucket (if not past the limit) and removed from the pond" as well as the requirement that asks for "the last fish in the pond should be moved/inserted into the position that the fish was removed from so that there are no gaps." What I have for the function will be posted below with the rest of my code so far. I would really really appreciate some help or a push in the right direction!!
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAX_FISHES_IN_BUCKET 10
#define MAX_FISHES_IN_POND 15
typedef struct Fish {
unsigned char size;
char* species;
} Fish;
typedef struct Fisher {
char* name;
unsigned char keepSize;
Fish bucket[MAX_FISHES_IN_BUCKET];
unsigned char numFish;
} Fisher;
typedef struct Pond {
Fish fish[MAX_FISHES_IN_POND];
unsigned char numFish;
} Pond;
int addFish(Pond*p, unsigned char size,char* species) {
if(p -> numFish == MAX_FISHES_IN_POND) return 0;
p -> fish[p -> numFish].species = species;
p -> fish[p -> numFish].size = size;
p -> numFish++;
return 1;
}
void listFish(Fish* arrayOfFish, int n) {
int i;
for(i = 0; i < n; ++i) {
printf("Species: %s, Size: %dcm\n",arrayOfFish[i].species,arrayOfFish[i].size);
}
}
char likes(Fisher* fisher, Fish* f) {
return fisher -> keepSize <= f -> size;
}
int keep(Fisher* fisher, Fish* f) {
if(fisher -> numFish == MAX_FISHES_IN_BUCKET) return 0;
fisher -> bucket[fisher -> numFish] = *f;
fisher -> numFish++;
return 1;
}
int castLine(Fisher* fisher, Pond* p) {
if(p -> numFish == 0) return 0;
int r = rand() % (p -> numFish);
if(likes(fisher,&p->fish[r])) {
p -> numFish--;
}
return 1;
}
Aucun commentaire:
Enregistrer un commentaire