Advertisement
Onigiri10

ships (28.11.21)

Nov 28th, 2021
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.01 KB | None | 0 0
  1. Upd: создан класс Корабль и Маршрутный лист, перенесено всё в классы
  2.  
  3. #pragma once
  4. #include "windows.h"
  5. #include <list>
  6.  
  7. namespace ships {
  8.  
  9.     using namespace System;
  10.     using namespace System::ComponentModel;
  11.     using namespace System::Collections;
  12.     using namespace System::Windows::Forms;
  13.     using namespace System::Data;
  14.     using namespace System::Drawing;
  15.     using namespace System::Collections::Generic;
  16.  
  17.     /// <summary>
  18.     /// Сводка для MyForm
  19.     /// </summary>
  20.     enum class Direction
  21.     {
  22.         EAST,
  23.         SOUTH,
  24.         WEST,
  25.         NORTH
  26.     };
  27.  
  28.     enum class ShipMode
  29.     {
  30.         ROADS_MODE,             //0-на рейде
  31.         FULL_SPEED_MODE,        //1-полный ход
  32.         SLOW_SPEED_MODE,        //2-малый ход
  33.         UNLOAD_MODE,            //3-разгрузка
  34.         LOAD_MODE               //4-загрузка
  35.     };
  36.     private ref class RoutePoint
  37.     {
  38.     public:
  39.         const int x;
  40.         const int y;
  41.         const Direction dir;
  42.         const ShipMode mode;
  43.         RoutePoint(int _x, int _y, Direction _dir, ShipMode _mode): x(_x), y(_y),dir(_dir),mode(_mode) {}
  44.     };
  45.  
  46.     private ref class Ship
  47.     {
  48.     private:
  49.         String^ name;
  50.         int passengers;                     //кол-во пассажирова на судне
  51.         int capacity;                       //максимальная вместимсть пассажиров
  52.         int routeTargetIndex;               //номер целевой точки в маршрутном листе
  53.         double x;
  54.         double y;
  55.         double v;                           //м/c
  56.         Direction direction;
  57.         ShipMode mode;                      //режим судна
  58.         List<RoutePoint^>^ route = nullptr;
  59.  
  60.     public:
  61.         Ship(String^ n, int p, int c)
  62.         {
  63.             name = n;
  64.             passengers = p;
  65.             capacity = c;                  
  66.             routeTargetIndex = -1;         
  67.             x = 0;
  68.             y = 0;
  69.             v = 0;                         
  70.             direction = Direction::EAST;
  71.             mode = ShipMode::ROADS_MODE;
  72.         }
  73.         double get_x()
  74.         {
  75.             return x;
  76.         }
  77.         double get_y()
  78.         {
  79.             return y;
  80.         }
  81.         int get_p()
  82.         {
  83.             return passengers;
  84.         }
  85.         int get_c()
  86.         {
  87.             return capacity;
  88.         }
  89.         Direction get_dir()
  90.         {
  91.             return direction;
  92.         }
  93.         ShipMode get_mode()
  94.         {
  95.             return mode;
  96.         }
  97.         String^ get_name()
  98.         {
  99.             return name;
  100.         }
  101.         void load(int dp)                               //dp - кол-во входящих/выходящих пассажиров в секунду
  102.         {
  103.             passengers += dp;
  104.             if (passengers > capacity) passengers = capacity;
  105.         }
  106.         void unload(int dp)
  107.         {
  108.             passengers -= dp;
  109.             if (passengers < 0) passengers = 0;
  110.         }
  111.         void setRoute(List<RoutePoint^>^ r)             //функция установки маршрутного листа
  112.         {
  113.             route = r;
  114.             routeTargetIndex = 0;
  115.             x = route[0]->x;
  116.             y = route[0]->y;
  117.             direction = route[0]->dir;
  118.             mode = route[0]->mode;
  119.             v = vFromMode(mode);
  120.         }
  121.         double vFromMode(ShipMode m)                    //функция расчета скорости в зависимости от режима
  122.         {
  123.             switch (m)
  124.             {
  125.             case ships::ShipMode::ROADS_MODE:
  126.                 return 0;
  127.             case ships::ShipMode::FULL_SPEED_MODE:
  128.                 return 2.0;
  129.             case ships::ShipMode::SLOW_SPEED_MODE:
  130.                 return 0.5;
  131.             case ships::ShipMode::UNLOAD_MODE:
  132.                 return 0;
  133.             case ships::ShipMode::LOAD_MODE:
  134.                 return 0;
  135.             default:
  136.                 return 0;
  137.             }
  138.         }
  139.         static String^ modeStr(ShipMode m)              //функция для вывода информационных сообщений
  140.         {
  141.             switch (m)
  142.             {
  143.             case ships::ShipMode::ROADS_MODE:
  144.                 return "Ожидаю на рейде";
  145.             case ships::ShipMode::FULL_SPEED_MODE:
  146.                 return "Режим полный ход";
  147.             case ships::ShipMode::SLOW_SPEED_MODE:
  148.                 return "Режим малый ход";
  149.             case ships::ShipMode::UNLOAD_MODE:
  150.                 return "Режим разгрузки";
  151.             case ships::ShipMode::LOAD_MODE:
  152.                 return "Режим загрузки";
  153.             default:
  154.                 return "Неизвестный режим";
  155.             }
  156.         }
  157.         void nextSecond()                               //функция моделирования движения судна
  158.         {
  159.             if (route == nullptr) return;
  160.             if (routeTargetIndex < 0 || routeTargetIndex >= route->Count) return;
  161.  
  162.             if (v > 0)
  163.             {
  164.                 int tx = route[routeTargetIndex]->x;                            //tx,ty - координаты целевой точки на маршруте
  165.                 int ty = route[routeTargetIndex]->y;
  166.                 double d = Math::Sqrt((x - tx)*(x - tx) + (y - ty)*(y - ty));   //расстояние от текущей точ
  167.                 if (d < v)
  168.                 {
  169.                     if (routeTargetIndex == route->Count - 1)
  170.                     {
  171.                         return;
  172.                     }
  173.                     direction = route[routeTargetIndex]->dir;
  174.                     mode = route[routeTargetIndex]->mode;
  175.                     v = vFromMode(mode);
  176.                     if (v > 0)
  177.                     {
  178.                         routeTargetIndex++;
  179.                     }
  180.                     x = tx;
  181.                     y = ty;
  182.                     return;
  183.                 }
  184.                 else
  185.                 {
  186.                     switch (direction)
  187.                     {
  188.                     case ships::Direction::EAST:
  189.                         x += v;
  190.                         break;
  191.                     case ships::Direction::SOUTH:
  192.                         y += v;
  193.                         break;
  194.                     case ships::Direction::WEST:
  195.                         x -= v;
  196.                         break;
  197.                     case ships::Direction::NORTH:
  198.                         y -= v;
  199.                         break;
  200.                     default:
  201.                         break;
  202.                     }
  203.                 }
  204.                 return;
  205.             }
  206.             else
  207.             {
  208.                 if (mode == ShipMode::UNLOAD_MODE)
  209.                 {
  210.                     unload(5);
  211.                     if (passengers == 0)
  212.                     {
  213.                         routeTargetIndex++;
  214.                         if (routeTargetIndex < route->Count)
  215.                         {
  216.                             mode = route[routeTargetIndex]->mode;
  217.                             v = vFromMode(mode);
  218.                         }
  219.                     }
  220.                     return;
  221.                 }
  222.                 if (mode == ShipMode::LOAD_MODE)
  223.                 {
  224.                     load(5);
  225.                     if (passengers == capacity)
  226.                     {
  227.                         routeTargetIndex++;
  228.                         if (routeTargetIndex < route->Count)
  229.                         {
  230.                             mode = route[routeTargetIndex]->mode;
  231.                             v = vFromMode(mode);
  232.                         }
  233.                     }
  234.                     return;
  235.                 }
  236.             }
  237.         }
  238.     };
  239.  
  240.     public ref class MyForm : public System::Windows::Forms::Form
  241.     {
  242.         Image^ ship1 = Image::FromFile("C:/Users/golubova/Desktop/ship1.png");
  243.         Image^ ship1_2 = Image::FromFile("C:/Users/golubova/Desktop/ship_135.png");
  244.         Image^ ship2 = Image::FromFile("C:/Users/golubova/Desktop/ship2.png");
  245.         Image^ ship2_3 = Image::FromFile("C:/Users/golubova/Desktop/ship_225.png");
  246.         Image^ ship3 = Image::FromFile("C:/Users/golubova/Desktop/ship3.png");
  247.         Image^ ship3_4 = Image::FromFile("C:/Users/golubova/Desktop/ship_315.png");
  248.         Image^ ship4 = Image::FromFile("C:/Users/golubova/Desktop/ship4.png");
  249.         Image^ ship4_1 = Image::FromFile("C:/Users/golubova/Desktop/ship_45.png");
  250.  
  251.         int height, width;
  252.         const double metrPerPixels = 2;
  253.         int modelSecondPerRealSecond = 1; //1 секунда на 1 шаг моделирования (тик)
  254.         List<RoutePoint^>^ route = gcnew List<RoutePoint^>();
  255.  
  256.         Ship^ pShip = gcnew Ship("Беринг", 100, 100);
  257.  
  258.         void modelStep()
  259.         {
  260.             for (int k = 0; k < modelSecondPerRealSecond; k++)
  261.             {
  262.                 ShipMode oldMode = pShip->get_mode();
  263.                 pShip->nextSecond();
  264.                 ShipMode newMode = pShip->get_mode();
  265.                 if (oldMode != newMode)
  266.                 {
  267.                     String^ msg = "'" + pShip->get_name() + "' : " + Ship::modeStr(newMode);
  268.                     listBox1->Items->Add(msg);
  269.                 }
  270.             }
  271.         }
  272.  
  273.     public:
  274.         MyForm(void)
  275.         {
  276.             InitializeComponent();
  277.             //
  278.             //TODO: добавьте код конструктора
  279.             //
  280.         }
  281.  
  282.     protected:
  283.         /// <summary>
  284.         /// Освободить все используемые ресурсы.
  285.         /// </summary>
  286.         ~MyForm()
  287.         {
  288.             if (components)
  289.             {
  290.                 delete components;
  291.             }
  292.         }
  293.  
  294.     private: System::Windows::Forms::Timer^  timer1;
  295.     private: System::Windows::Forms::TrackBar^  trackBar1;
  296.     private: System::Windows::Forms::ListBox^  listBox1;
  297.     private: System::Windows::Forms::PictureBox^  picPort;
  298.     //private: System::Windows::Forms::PictureBox^  picPortKors;
  299.     protected:
  300.  
  301.     private: System::Windows::Forms::PictureBox^  picShip;
  302.  
  303.     private: System::ComponentModel::IContainer^  components;
  304.     protected:
  305.  
  306.  
  307.     protected:
  308.  
  309.  
  310.     protected:
  311.  
  312.     private:
  313.         /// <summary>
  314.         /// Обязательная переменная конструктора.
  315.         /// </summary>
  316.  
  317.  
  318. #pragma region Windows Form Designer generated code
  319.         /// <summary>
  320.         /// Требуемый метод для поддержки конструктора — не изменяйте
  321.         /// содержимое этого метода с помощью редактора кода.
  322.         /// </summary>
  323.         void InitializeComponent(void)
  324.         {
  325.             this->components = (gcnew System::ComponentModel::Container());
  326.             System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(MyForm::typeid));
  327.             this->picPort = (gcnew System::Windows::Forms::PictureBox());
  328.             this->picShip = (gcnew System::Windows::Forms::PictureBox());
  329.             this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
  330.             this->trackBar1 = (gcnew System::Windows::Forms::TrackBar());
  331.             this->listBox1 = (gcnew System::Windows::Forms::ListBox());
  332.             //this->picPortKors = (gcnew System::Windows::Forms::PictureBox());
  333.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picPort))->BeginInit();
  334.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picShip))->BeginInit();
  335.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->trackBar1))->BeginInit();
  336.             //(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picPortKors))->BeginInit();
  337.             this->SuspendLayout();
  338.             //
  339.             // picPort
  340.             //
  341.             this->picPort->BackColor = System::Drawing::SystemColors::ActiveCaption;
  342.             this->picPort->Dock = System::Windows::Forms::DockStyle::Fill;
  343.             this->picPort->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"picPort.Image")));
  344.             this->picPort->Location = System::Drawing::Point(0, 0);
  345.             this->picPort->Name = L"picPort";
  346.             this->picPort->Size = System::Drawing::Size(871, 613);
  347.             this->picPort->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage;
  348.             this->picPort->TabIndex = 0;
  349.             this->picPort->TabStop = false;
  350.             //
  351.             // picShip
  352.             //
  353.             this->picShip->BackColor = System::Drawing::Color::LightSkyBlue;
  354.             this->picShip->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"picShip.Image")));
  355.             this->picShip->Location = System::Drawing::Point(0, 12);
  356.             this->picShip->Name = L"picShip";
  357.             this->picShip->Size = System::Drawing::Size(51, 20);
  358.             this->picShip->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
  359.             this->picShip->TabIndex = 1;
  360.             this->picShip->TabStop = false;
  361.             this->picShip->Visible = false;
  362.             this->picShip->Click += gcnew System::EventHandler(this, &MyForm::pictureBox2_Click);
  363.             //
  364.             // timer1
  365.             //
  366.             this->timer1->Enabled = true;
  367.             this->timer1->Interval = 20;
  368.             this->timer1->Tick += gcnew System::EventHandler(this, &MyForm::timer1_Tick);
  369.             //
  370.             // trackBar1
  371.             //
  372.             this->trackBar1->Location = System::Drawing::Point(660, 520);
  373.             this->trackBar1->Maximum = 100;
  374.             this->trackBar1->Minimum = 1;
  375.             this->trackBar1->Name = L"trackBar1";
  376.             this->trackBar1->Size = System::Drawing::Size(189, 69);
  377.             this->trackBar1->TabIndex = 2;
  378.             this->trackBar1->TickFrequency = 75;
  379.             this->trackBar1->Value = 1;
  380.             this->trackBar1->Scroll += gcnew System::EventHandler(this, &MyForm::trackBar1_Scroll);
  381.             //
  382.             // listBox1
  383.             //
  384.             this->listBox1->FormattingEnabled = true;
  385.             this->listBox1->ItemHeight = 20;
  386.             this->listBox1->Location = System::Drawing::Point(604, 50);
  387.             this->listBox1->Name = L"listBox1";
  388.             this->listBox1->Size = System::Drawing::Size(245, 84);
  389.             this->listBox1->TabIndex = 3;
  390.             /*
  391.             // picPortKors
  392.             //
  393.             this->picPortKors->BackColor = System::Drawing::Color::Transparent;
  394.             this->picPortKors->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"picPortKors.Image")));
  395.             this->picPortKors->Location = System::Drawing::Point(190, 499);
  396.             this->picPortKors->Name = L"picPortKors";
  397.             this->picPortKors->Size = System::Drawing::Size(81, 56);
  398.             this->picPortKors->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
  399.             this->picPortKors->TabIndex = 5;
  400.             this->picPortKors->TabStop = false;
  401.             */
  402.             //
  403.             // MyForm
  404.             //
  405.             this->AutoScaleDimensions = System::Drawing::SizeF(9, 20);
  406.             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  407.             this->ClientSize = System::Drawing::Size(871, 613);
  408.             //this->Controls->Add(this->picPortKors);
  409.             this->Controls->Add(this->listBox1);
  410.             this->Controls->Add(this->trackBar1);
  411.             this->Controls->Add(this->picShip);
  412.             this->Controls->Add(this->picPort);
  413.             this->Name = L"MyForm";
  414.             this->Text = L"Модель захода морских судов в порт";
  415.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picPort))->EndInit();
  416.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picShip))->EndInit();
  417.             (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->trackBar1))->EndInit();
  418.             //(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->picPortKors))->EndInit();
  419.             this->ResumeLayout(false);
  420.             this->PerformLayout();
  421.  
  422.             /*Маршрутная карта*/
  423.             route->Add(gcnew RoutePoint(0, 250, Direction::EAST, ShipMode::FULL_SPEED_MODE)); //старт
  424.             route->Add(gcnew RoutePoint(375, 250, Direction::SOUTH, ShipMode::SLOW_SPEED_MODE));
  425.             route->Add(gcnew RoutePoint(375, 400, Direction::SOUTH, ShipMode::UNLOAD_MODE));
  426.             route->Add(gcnew RoutePoint(375, 400, Direction::SOUTH, ShipMode::LOAD_MODE));
  427.             route->Add(gcnew RoutePoint(375, 400, Direction::SOUTH, ShipMode::SLOW_SPEED_MODE));
  428.             route->Add(gcnew RoutePoint(375, 520, Direction::EAST, ShipMode::SLOW_SPEED_MODE));
  429.             route->Add(gcnew RoutePoint(700, 520, Direction::NORTH, ShipMode::SLOW_SPEED_MODE));
  430.             route->Add(gcnew RoutePoint(700, 200, Direction::EAST, ShipMode::SLOW_SPEED_MODE));
  431.             route->Add(gcnew RoutePoint(800, 200, Direction::EAST, ShipMode::FULL_SPEED_MODE));
  432.             route->Add(gcnew RoutePoint(1210, 200, Direction::EAST, ShipMode::ROADS_MODE));
  433.             pShip->setRoute(route);
  434.         }
  435. #pragma endregion
  436.     private: System::Void pictureBox1_Click(System::Object^  sender, System::EventArgs^  e) {
  437.     }
  438.     private: System::Void pictureBox2_Click(System::Object^  sender, System::EventArgs^  e) {
  439.     }
  440.     private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
  441.  
  442.         modelStep();
  443.         if (pShip->get_dir() == Direction::EAST)
  444.         {
  445.             picShip->Image = ship1;
  446.             picShip->Width = ship1->Width;
  447.             picShip->Height = ship1->Height;
  448.         }
  449.         else if (pShip->get_dir() == Direction::SOUTH)
  450.         {
  451.             picShip->Image = ship2;
  452.             picShip->Width = ship2->Width;
  453.             picShip->Height = ship2->Height;
  454.         }
  455.         else if (pShip->get_dir() == Direction::WEST)
  456.         {
  457.             picShip->Image = ship3;
  458.             picShip->Width = ship3->Width;
  459.             picShip->Height = ship3->Height;
  460.         }
  461.         else
  462.         {
  463.             picShip->Image = ship4;
  464.             picShip->Width = ship4->Width;
  465.             picShip->Height = ship4->Height;
  466.         }
  467.  
  468.         int leftUpCornerY =  pShip->get_y()/ metrPerPixels - picShip->Height / 2;
  469.         int leftUpCornerX = pShip->get_x() / metrPerPixels - picShip->Width / 2;
  470.         picShip->Top = leftUpCornerY;
  471.         picShip->Left = leftUpCornerX;
  472.  
  473.         picShip->Visible = true;
  474.     }  
  475.  
  476.     private: System::Void trackBar1_Scroll(System::Object^  sender, System::EventArgs^  e)
  477.     {
  478.         modelSecondPerRealSecond = trackBar1->Value;
  479.     }
  480. };
  481. }
  482.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement