用于存放学校的作业便于复习。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
5.4 KiB

  1. // @Time : 2023-10-28 16:11:17
  2. // @FileName: MVC.cpp
  3. // @Author : 423A35C7
  4. // @Software: VSCode
  5. #include "controller.hpp"
  6. #include "MVC.h"
  7. // #ifdef __WIN32__
  8. // 添加"-D_HAS_STD_BYTE=0",的方法不知道为什么没用
  9. // #include <windows.h>
  10. // #else
  11. // #include <unistd.h>
  12. // #define Sleep(a) usleep(a * 1000) // 需要小于一秒
  13. // #endif
  14. // 用_sleep了,虽然会提示不建议使用了,但是用window.h编译出来的会被认为是病毒
  15. class Simple {
  16. private:
  17. SingleQueueModel<Customer> model;
  18. SimpleQueueView<Customer, SingleQueueModel<Customer>> view{model, 5, 5};
  19. // SimpleQueueView<SingleQueueModel<Customer>> view = SimpleQueueView<SingleQueueModel<Customer>>(model, 5, 5, 20);
  20. BackgroundView background_view{gate_x, gate_y};
  21. SingleQueueController<Customer> controller{model, view, speed};
  22. // int probability_num = DEFAULT_PROBABILITY_NUM;
  23. public:
  24. // main可以认为是外部对Controller的操作
  25. // num是序号
  26. Status _main() {
  27. static int_ num = 0;
  28. if (rand() % probability_num == 0) { // 0.01的概率
  29. Customer customer{num++, rand() % (max_money) + 1};
  30. this->model.push(customer);
  31. }
  32. return OK;
  33. }
  34. // 可以认为此操作不应出错
  35. void refresh() {
  36. this->controller.refresh();
  37. this->background_view.refresh();
  38. this->view.refresh();
  39. _sleep(sleep_time);
  40. }
  41. };
  42. class Multi {
  43. using one_model = SingleQueueModel<Customer>;
  44. using one_view = SimpleQueueView<Customer, SingleQueueModel<Customer>>;
  45. using one_controller = SingleQueueController<Customer>;
  46. std::vector<std::shared_ptr<one_model>> models;
  47. std::vector<std::shared_ptr<one_view>> views;
  48. BackgroundView background_view{gate_x, gate_y};
  49. std::vector<std::shared_ptr<one_controller>> controllers;
  50. DriftingView<Customer> drifting_view;
  51. DriftingController<Customer> drift_controller;
  52. int len = 0;
  53. int probability_num = DEFAULT_PROBABILITY_NUM;
  54. int speed = DEFAULT_SPEED;
  55. int max_money = MAX_MONEY;
  56. double walk_speed = DEFAULT_WALK_SPEED;
  57. public:
  58. Multi(int len, int probability_num = DEFAULT_PROBABILITY_NUM, int speed = DEFAULT_SPEED, int max_money = MAX_MONEY, double walk_speed = DEFAULT_WALK_SPEED) : len(len), probability_num(probability_num), speed(speed), max_money(max_money), walk_speed(walk_speed), drift_controller(controllers, drifting_view) {
  59. if (len < 1)
  60. throw std::bad_array_new_length(); // 异常这样用好像不太对
  61. for (int i = 0; i < len; i++) {
  62. // one_model _model {};
  63. // one_view _view(_model, 5 + i * 20, 5);
  64. // one_controller _controller(_model, _view); // 这里的引用很奇怪,可能是自己创建的类在_model离开作用域后继续引用它无法保证它不被修改,也就是说它引用的可能还是原来的变量名,但原来的变量名被更改了,但vector里可以继续正确引用
  65. // one_view _view(this->models.back()); // 这样也无法正确引用,看来还是得用指针
  66. // one_controller _controller(this->models.back(), this->views.back());
  67. // vector内对一个对象可能有多个引用,所以可能使用了unique_ptr就无法放置在vector里
  68. std::shared_ptr<one_model> _model(new one_model());
  69. std::shared_ptr<one_view> _view(new one_view(*_model, base_x, base_y + i * sep));
  70. std::shared_ptr<one_controller> _controller(new one_controller(*_model, *_view, this->speed));
  71. this->models.push_back(_model);
  72. this->views.push_back(_view);
  73. this->controllers.push_back(_controller);
  74. }
  75. }
  76. // 如果有多个最短的,最左边的优先
  77. int get_shortest_queue() {
  78. int min_value = int_MAX;
  79. int argmin = 0;
  80. for (int i = 0; i < this->len; i++) {
  81. if (this->models[i]->get_length() < min_value) {
  82. min_value = this->models[i]->get_length();
  83. argmin = i;
  84. }
  85. }
  86. return argmin;
  87. }
  88. Status _main() {
  89. static int_ num = 0;
  90. this->drifting_view.main();
  91. if (rand() % this->probability_num == 0) { // 0.01的概率
  92. Customer customer{num++, rand() % (this->max_money) + 1};
  93. // this->models[this->get_shortest_queue()]->push(customer);
  94. this->drift_controller.push(
  95. customer,
  96. this->get_shortest_queue(),
  97. std::make_pair(gate_x, gate_y),
  98. this->walk_speed);
  99. }
  100. return OK;
  101. }
  102. void refresh() {
  103. for (auto _controller : this->controllers) {
  104. _controller->refresh();
  105. }
  106. this->background_view.refresh();
  107. for (auto _view : this->views) {
  108. _view->refresh();
  109. }
  110. this->drifting_view.refresh();
  111. _sleep(sleep_time);
  112. }
  113. };
  114. Status main_simple() {
  115. Simple simple;
  116. std::function<Status()> _main = std::bind(&Simple::_main, &simple);
  117. std::function<void()> refresh = std::bind(&Simple::refresh, &simple);
  118. return mainloop(_main, refresh, total_time);
  119. }
  120. Status main_multi() {
  121. Multi multi(window_num, probability_num, speed, max_money);
  122. std::function<Status()> _main = std::bind(&Multi::_main, &multi);
  123. std::function<void()> refresh = std::bind(&Multi::refresh, &multi);
  124. return mainloop(_main, refresh, total_time);
  125. }