#include "library.h" void read_parameters(double & fieldw, double & fieldl, double & boyx, double & boyy, double & bullx, double & bully, double & treex, double & treey, double & boys, double & bulls, double & speedscale) { ifstream fin("boyandbull.txt"); while (true) { string s; fin >> s; if (fin.fail()) break; if (s=="field") fin >> fieldw >> fieldl; else if (s=="boy") fin >> boyx >> boyy >> boys; else if (s=="bull") fin >> bullx >> bully >> bulls; else if (s=="tree") fin >> treex >> treey; else if (s=="speed") fin >> speedscale; else cout << "File is bad!\n"; } cout << "field dims w,l " << fieldw << ", " << fieldl << "\n"; } void run_simulation(double & fieldw, double & fieldl, double & boyx, double & boyy, double & bullx, double & bully, double & treex, double & treey, double & boys, double & bulls, double & speedscale) { const double dirboy = direction_from_to_in_radians(boyx, boyy, treex, treey); const double boynewx = boyx + speedscale*boys * sin(dirboy); const double boynewy = boyy - speedscale*boys * cos(dirboy); set_pen_color(color::blue); move_to(boyx, boyy); draw_to(boynewx, boynewy); boyx = boynewx; boyy = boynewy; const double dirbull = direction_from_to_in_radians(bullx, bully, boyx, boyy); const double bullnewx = bullx + speedscale*bulls * sin(dirbull); const double bullnewy = bully - speedscale*bulls * cos(dirbull); set_pen_color(color::red); move_to(bullx, bully); draw_to(bullnewx, bullnewy); bullx = bullnewx; bully = bullnewy; } double distance(const double x1, const double y1, const double x2, const double y2) { return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); } void main() { double fieldw, fieldl, boyx, boyy, bullx, bully, treex, treey, boys, bulls, speedscale; read_parameters(fieldw, fieldl, boyx, boyy, bullx, bully, treex, treey, boys, bulls, speedscale); make_window(fieldw, fieldl); set_pen_width(5); set_pen_color(color::green); draw_point(treex, treey); set_pen_color(color::blue); set_font_size(36); while (true) { if (distance(boyx, boyy, treex, treey)<1) { move_to(treex, treey); write_string("Boy Safe"); break; } if (distance(bullx, bully, boyx, boyy)<0.5) { move_to(boyx, boyy); write_string("Boy Eaten"); break; } run_simulation(fieldw, fieldl, boyx, boyy, bullx, bully, treex, treey, boys, bulls, speedscale); wait(0.01); } }