int move_to_empty (int i, int j) { int npi, npj; int dir; struct animal * t; t = &wator[i][j]; assert ((t->kind == SHARK) || (t->kind == FISH)); dir = choose_empty (i, j); if (dir == REST) return 0; npi = ni (i, dir); npj = nj (j, dir); assert (wator[npi][npj].kind == EMPTY); memcpy(&wator[npi][npj], t, sizeof(animal)); wator[i][j].kind = EMPTY; if ((wator[npi][npj].kind == FISH) && (wator[npi][npj].age >= FBRUT)) { wator[npi][npj].age = 0; new_animal (i, j, FISH); } if ((wator[npi][npj].kind == SHARK) && (wator[npi][npj].age >= HBRUT)) { wator[npi][npj].age = 0; new_animal (i, j, SHARK); } return 1; }
void init_wator (int maxi, int maxj, int hbrut, int fbrut, int fasten) { register int i, j, res; register struct animal *v = &(wator[0][0]); HBRUT = hbrut; FBRUT = fbrut; FASTEN = fasten; assert (hbrut - FASTEN> 0); for (i=0; i<MAXI; ++i) { for (j=0; j<MAXJ; ++j) { res = myrand (30); v->kind = EMPTY; switch (res) { case 0: new_animal ( v ,SHARK);//i, j, SHARK); break; case 1 ... 11: new_animal ( v ,FISH);//i, j, FISH); break; } ++v; } } }
int move_to_empty (struct animal* t ,int i, int j) { register int dir; assert ((t->kind - SHARK == 0) || (t->kind - FISH == 0)); dir = choose_empty(i, j); if (dir - REST == 0) return 0; register struct animal * s = &wator[ ni(i, dir) ][ nj(j, dir) ]; assert (s->kind == 0 ); memcpy(s, t, sizeof(animal)); t->kind = EMPTY; if ((s->kind - FISH == 0) && (s->age - FBRUT >= 0)) { s->age = 0; new_animal (t, FISH); } if ((s->kind - SHARK == 0) && (s->age - HBRUT >= 0)) { s->age = 0; new_animal (t, SHARK); } return 1; }
int move_to_fish (int i, int j) { int npi, npj; int dir; struct animal * t; t = &wator[i][j]; assert (t->kind == SHARK); dir = choose_fish (i, j); if (dir == REST) return 0; npi = ni (i, dir); npj = nj (j, dir); assert (wator[npi][npj].kind == FISH); freecount++; memcpy (&wator[npi][npj], t,sizeof(animal)); wator[i][j].kind = EMPTY; wator[npi][npj].hunger = 0; if (wator[npi][npj].age >= HBRUT) { wator[i][j].age = 0; new_animal (i, j, SHARK); } return 1; }
int move_to_fish (int i, int j) { register int dir; register struct animal * t; t = &wator[i][j]; assert (t->kind - SHARK == 0); dir = choose_fish(i, j); if (dir - REST == 0) return 0; register struct animal * s = &wator[ ni(i, dir) ][ nj(j, dir) ]; assert (s->kind - FISH == 0); ++freecount; memcpy (s, t, sizeof(animal)); t->kind = EMPTY; s->hunger = 0; if (s->age - HBRUT >= 0) { t->age = 0; new_animal (i, j, SHARK); } return 1; }
void init_wator (int maxi, int maxj, int hbrut, int fbrut, int fasten) { int i, j; HBRUT = hbrut; FBRUT = fbrut; FASTEN = fasten; assert (hbrut > FASTEN); for (i=0; i<MAXI; ++i) { for (j=0; j<MAXJ; ++j) { int res = myrand (30); wator[i][j].kind = EMPTY; switch (res) { case 0: new_animal (i, j, SHARK); break; case 1 ... 11: new_animal (i, j, FISH); break; } } } }
// Main int main(int argc, char* argv[]) { animal* animal_p = new_animal("whale", 0); printf("animal: %s, %d\n", animal_p->name(animal_p), animal_p->legs(animal_p)); animal_p->greet(animal_p, "lei"); animal_p->free(animal_p); animal* bp = new_bird("sparrow", 2, "bii"); bird* bird_p = (bird*) bp->dp_; printf("bird: %s, %d\n", bp->name(bp), bp->legs(bp)); bp->greet(bp, "lei"); bird_p->singit(bird_p); bp->free(bp); return 0; }