1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| #include <bits/stdc++.h> #include <windows.h> using namespace std;
#define width 80 #define height 15
void gotoxy(int x, int y){ COORD pos; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); pos.X = x, pos.Y = y; SetConsoleCursorPosition(hOut, pos); }
void HideCursor(){ CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); }
class Robot{ private: int x; int y; int chargingX; int chargingY; int power; int maxPower; int targetX; int targetY; public: Robot(): x(0), y(0), chargingX(0), chargingY(0), power(100), maxPower(100), targetX(0), targetY(0){} Robot(int a, int b): x(a), y(b), chargingX(0), chargingY(0), power(100), maxPower(100), targetX(0), targetY(0){} Robot(int a, int b, int ca, int cb): x(a), y(b), chargingX(ca), chargingY(cb), power(100), maxPower(100), targetX(0), targetY(0){} void SetLocation(int a, int b){ x = a; y = b; } void SetChargingLocation(int cx, int cy){ chargingX = cx; chargingY = cy; } void SetPower(int p){ power = p; } void SetMaxPower(int p){ maxPower = p; } double PowerPro(){ return (double)power/(double)maxPower*100.0; } void SetTargetLocation(int tx, int ty){ targetX = tx; targetY = ty; } bool clearSuc(){ if(x == targetX && y == targetY) return true; return false; } void Status(){ gotoxy(0, 16); cout << "Location = (" << x << "," << y << ") "; gotoxy(0, 17); cout << "Charging Location = (" << chargingX << "," << chargingY << ") "; gotoxy(0, 18); cout << "Power = "; cout << fixed << setprecision(2) << PowerPro() << "% "; gotoxy(0, 19); cout << "Target Location = (" << targetX << "," << targetY << ") "; gotoxy(0, 20); } bool MoveTo(double CapacityPro){ if(x == targetX && y == targetY) return false; if(PowerPro() >= 10 && CapacityPro >= 5){ power -= 5; gotoxy(x, y); cout << " "; if(x < targetX) ++x; else if(x > targetX) --x; if(y < targetY) ++y; else if(y > targetY) --y; gotoxy(x, y); cout << "R"; } else { gotoxy(x, y); cout << " "; if(x < chargingX) ++x; else if(x > chargingX) --x; if(y < chargingY) ++y; else if(y > chargingY) --y; gotoxy(x, y); cout << "R"; } if(x == chargingX && y == chargingY){ if(PowerPro() < 10) power = maxPower; else return true; } return false; } };
class SweeperRobot: public Robot{ private: int capacity; int maxCapacity; public: SweeperRobot():Robot(){} SweeperRobot(int a, int b, int ca, int cb, int n):Robot(a, b, ca, cb){ capacity = 0; maxCapacity = n; } void SetCapacity(int c){ capacity = c; } void SetMaxCapacity(int c){ maxCapacity = c; } void SetTargetLocation(int tx, int ty){ Robot::SetTargetLocation(tx, ty); } double CapacityPro(){ return (1.0-(double)capacity/(double)maxCapacity)*100.0; } void MoveTo(int mode = 0){ bool clearCapacity = Robot::MoveTo(CapacityPro()); if(clearCapacity) capacity = 0; Status(); } void Status(){ Robot::Status(); cout << "Capacity = "; cout << fixed << setprecision(2) << CapacityPro() << "% "; } bool clearSuc(){ if(Robot::clearSuc()){ capacity += 5; return true; } return false; } };
class Garbage{ private: int x; int y; public: int GetX(){ return x; } int GetY(){ return y; } Garbage(){ x = rand() % width; y = rand() % height; show(); } void show(){ gotoxy(x, y); cout << "G"; } };
int main(){ srand(time(NULL)); HideCursor();
SweeperRobot *srobot = new SweeperRobot(0, 14, 0, 14, 100); srobot->SetPower(100); srobot->SetMaxPower(1000); srobot->SetTargetLocation(50, 10);
int time = 0; queue<Garbage*> q; Garbage * gar = new Garbage(); q.push(gar); Garbage *temp = q.front();
while(true){ Sleep(20); ++time; if(!q.empty()){ temp = q.front(); srobot->SetTargetLocation(temp->GetX(), temp->GetY()); if(srobot->clearSuc()){ q.pop(); delete temp; } } if(time % 50 == 0){ Garbage * gar = new Garbage(); q.push(gar); } srobot->MoveTo(); } return 0; }
|