static void attack(int rel_dir, struct item *itemp) { if (!(itemp->flags & ON_SIDE)) { face_and_move_direction(rel_dir, 0); command[comlen++] = 'o'; command[comlen++] = 'o'; duck(FRONT); command[comlen++] = ' '; } else if (itemp->distance > 1) { face_and_move_direction(rel_dir, 2); duck(FRONT); } else { face_and_move_direction(rel_dir, 1); if (itemp->flags & ON_LEFT) rel_dir = LEFT; else rel_dir = RIGHT; (void) face_and_move_direction(rel_dir, 0); command[comlen++] = 'f'; command[comlen++] = 'f'; duck(FRONT); command[comlen++] = ' '; } }
int otto(int y, int x, char face, char *buf, size_t buflen) { int i; if (usleep(Otto_pause) < 0) panic("usleep"); /* save away parameters so other functions may use/update info */ switch (face) { case '^': facing = NORTH; break; case '<': facing = WEST; break; case 'v': facing = SOUTH; break; case '>': facing = EAST; break; default: panic("unknown face"); } row = y; col = x; been_there[row][col] |= 1 << facing; /* initially no commands to be sent */ comlen = 0; /* find something to do */ look_around(); for (i = 0; i < NUMDIRECTIONS; i++) { if (strchr(OPPONENT, flbr[i].what) != NULL) { attack(i, &flbr[i]); memset(been_there, 0, sizeof been_there); goto done; } } if (strchr(SHOTS, bitem.what) != NULL && !(bitem.what & ON_SIDE)) { duck(BACK); memset(been_there, 0, sizeof been_there); } else if (go_for_ammo(BOOT_PAIR)) { memset(been_there, 0, sizeof been_there); } else if (go_for_ammo(BOOT)) { memset(been_there, 0, sizeof been_there); } else if (go_for_ammo(GMINE)) memset(been_there, 0, sizeof been_there); else if (go_for_ammo(MINE)) memset(been_there, 0, sizeof been_there); else wander(); done: if (comlen) { if (comlen > buflen) panic("not enough buffer space"); memcpy(buf, command, comlen); } return comlen; }
//! Client int main() { std::auto_ptr<MallardDuck> duck (new MallardDuck()); std::auto_ptr<WildTurkey> turkey (new WildTurkey()); std::auto_ptr<Duck> turkeyAdapter (new TurkeyAdapter(turkey.get())); std::cout << "The Turkey says..." << std::endl; turkey->gobble(); turkey->fly(); std::cout << "The Duck says..." << std::endl; testDuck(duck.get()); std::cout << "The Turkey Adapter says..." << std::endl; testDuck(turkeyAdapter.get()); return 0; }
int main(int argc, char* argv[]) { shared_ptr<MallardDuck> duck(new MallardDuck()); shared_ptr<WildTurkey> turkey(new WildTurkey()); shared_ptr<Duck> turkeyAdapter(new TurkeyAdapter(turkey)); cout << "The Turkey says..." << endl; turkey->gobble(); turkey->fly(); cout << endl << "The Duck says..." << endl; testDuck(duck); cout << endl << "The TurkeyAdapter says..." << endl; testDuck(turkeyAdapter); return 0; }
static void wander(void) { int i, j, rel_dir, dir_mask, dir_count; for (i = 0; i < NUMDIRECTIONS; i++) if (!(flbr[i].flags & BEEN) || flbr[i].distance <= 1) break; if (i == NUMDIRECTIONS) memset(been_there, 0, sizeof been_there); dir_mask = dir_count = 0; for (i = 0; i < NUMDIRECTIONS; i++) { j = (RIGHT + i) % NUMDIRECTIONS; if (flbr[j].distance <= 1 || flbr[j].flags & DEADEND) continue; if (!(flbr[j].flags & BEEN_SAME)) { dir_mask = 1 << j; dir_count = 1; break; } if (j == FRONT && num_turns > 4 + (arc4random_uniform( ((flbr[FRONT].flags & BEEN) ? 7 : HEIGHT)))) continue; dir_mask |= 1 << j; dir_count = 1; break; } if (dir_count == 0) { duck(arc4random_uniform(NUMDIRECTIONS)); num_turns = 0; return; } else { rel_dir = ffs(dir_mask) - 1; } if (rel_dir == FRONT) num_turns++; else num_turns = 0; face_and_move_direction(rel_dir, 1); }
/** * Allows the player to wander in the waze. * [PSR] */ STATIC void wander() { int i, j, rel_dir, dir_mask, dir_count; for (i = 0; i < NUMDIRECTIONS; i++) { if (!(flbr[i].flags & BEEN) || flbr[i].distance <= 1) { break; } } if (i == NUMDIRECTIONS) { memset(been_there, 0, sizeof been_there); } dir_mask = dir_count = 0; for (i = 0; i < NUMDIRECTIONS; i++) { j = (RIGHT + i) % NUMDIRECTIONS; if (flbr[j].distance <= 1 || flbr[j].flags & DEADEND) { continue; } if (!(flbr[j].flags & BEEN_SAME)) { dir_mask = 1 << j; dir_count = 1; break; } if (j == FRONT && num_turns > 4 + (random() % ((flbr[FRONT].flags & BEEN) ? 7 : HEIGHT))) { continue; } dir_mask |= 1 << j; # ifdef notdef dir_count++; # else dir_count = 1; break; # endif } if (dir_count == 0) { duck(random() % NUMDIRECTIONS); num_turns = 0; return; } else if (dir_count == 1) rel_dir = ffs(dir_mask) - 1; else { rel_dir = ffs(dir_mask) - 1; dir_mask &= ~(1 << rel_dir); while (dir_mask != 0) { i = ffs(dir_mask) - 1; if (random() % 5 == 0) { rel_dir = i; } dir_mask &= ~(1 << i); } } if (rel_dir == FRONT) { num_turns++; } else { num_turns = 0; } # ifdef DEBUG fprintf(debug, " w(%c)", RELCHARS[rel_dir]); # endif face_and_move_direction(rel_dir, 1); }
/** * Implements the game logic of a otto-matic player located at a * given coordinate with a given orientation. * @param[in] y A coordinate. * @param[in] x A coordinate. * @param[in] face The orientation of the player. * [PSR] */ void otto(int y,int x,char face) { int i; struct sigaction handler, old_handler; /* Added to substitute deprecated signals functions. [PSR] */ sigset_t old_mask, sig_mask; /* Added to substitute deprecated signals functions. [PSR] */ bool done; # ifdef DEBUG if (debug == NULL) { debug = fopen("bug", "w"); setbuf(debug, NULL); } fprintf(debug, "\n%c(%d,%d)", face, y, x); # endif handler.sa_handler=&empty_handler; /* Added to substitute deprecated signals functions. [PSR] */ sigaction(SIGALRM, &handler, &old_handler);/* Added to substitute deprecated signals functions. [PSR] */ sigemptyset(&sig_mask);/* Added to substitute deprecated signals functions. [PSR] */ sigaddset(&sig_mask, SIGALRM);/* Added to substitute deprecated signals functions. [PSR] */ sigprocmask(SIG_BLOCK, &sig_mask, &old_mask);/* Added to substitute deprecated signals functions. [PSR] */ setitimer(ITIMER_REAL, &pause_time, NULL);/* Added to substitute deprecated signals functions. [PSR] */ sigsuspend(&old_mask);/* Added to substitute deprecated signals functions. [PSR] */ sigprocmask(SIG_SETMASK, &old_mask, NULL);/* Added to substitute deprecated signals functions. [PSR] */ /* save away parameters so other functions may use/update info */ switch (face) { case '^': facing = NORTH; break; case '<': facing = WEST; break; case 'v': facing = SOUTH; break; case '>': facing = EAST; break; default: abort(); } row = y; col = x; been_there[row][col] |= 1 << facing; /* initially no commands to be sent */ comlen = 0; /* find something to do */ look_around(); done=false; for (i = 0; i < NUMDIRECTIONS; i++) { if (strchr(OPPONENT, flbr[i].what) != NULL) { attack(i, &flbr[i]); memset(been_there, 0, sizeof been_there); done=true; break; } } if(!done) { if (strchr(SHOTS, bitem.what) != NULL && !(bitem.what & ON_SIDE)) { duck(BACK); memset(been_there, 0, sizeof been_there); # ifdef BOOTS } else if (go_for_ammo(BOOT_PAIR)) { memset(been_there, 0, sizeof been_there); } else if (go_for_ammo(BOOT)) { memset(been_there, 0, sizeof been_there); # endif } else if (go_for_ammo(GMINE)) { memset(been_there, 0, sizeof been_there); } else if (go_for_ammo(MINE)) { memset(been_there, 0, sizeof been_there); } else { wander(); } } write_and_push(main_socket, command, comlen); otto_count += comlen; # ifdef DEBUG (void) fwrite(command, 1, comlen, debug); # endif }