Advertisement
Guest User

SimpleStrategy

a guest
Apr 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 KB | None | 0 0
  1. #include "SimpleStrategy.h"
  2. #include "BattleField.h"
  3.  
  4. void SimpleStrategy::Move(Unit::UnitInfo &unitInfo, const SimpleStrategy::Direction &direction)
  5. {
  6.     switch (direction)
  7.     {
  8.         case Right:
  9.             unitInfo.second.Right();
  10.             break;
  11.         case Left:
  12.             unitInfo.second.Left();
  13.             break;
  14.         case Up:
  15.             unitInfo.second.Up();
  16.             break;
  17.         case Down:
  18.             unitInfo.second.Down();
  19.             break;
  20.     }
  21. }
  22.  
  23. void SimpleStrategy::MoveTo(Unit::UnitInfo &unitInfo, const SimpleStrategy::Direction &direction)
  24. {
  25.     BattleField::getInstance().FreePosition(unitInfo.second);
  26.     Move(unitInfo, direction);
  27.     BattleField::getInstance().TakePosition(unitInfo.second);
  28. }
  29.  
  30. void SimpleStrategy::MakeStep(const Hero &heroFrom, const Hero &heroTo)
  31. {
  32.     Unit::Collection unitsFrom;
  33.     heroFrom.GetUnits(unitsFrom);
  34.  
  35.     Unit::Collection unitsTo;
  36.     heroTo.GetUnits(unitsTo);
  37.  
  38.     std::vector<Direction> Directions =  {Right, Left,  Up, Down};
  39.  
  40.     //суммарное расстоние от текущего юнита до всех противников
  41.     //ходить будет тот юнит, у которого оно менбше всего
  42.     int MinDistance = -1;
  43.  
  44.     //собственно юнит, котого будем двигать
  45.     Unit::UnitInfo UnitToMove; //добавить: надо создать юнита по умолчанию
  46.  
  47.     //каким способом мы попали в нужное место
  48.     Direction WayToMove;
  49.  
  50.     //перебираем всех юнитов из команды, чей сейчас ход
  51.     for (auto unitFrom : unitsFrom)
  52.     {
  53.         Unit::UnitInfo CurrentUnit = unitFrom;
  54.  
  55.         //перебираем все направления, куда можем сходить им
  56.         for(auto direction : Directions)
  57.         {
  58.             Move(CurrentUnit, direction);
  59.             //если эта позиция свободна
  60.             if (BattleField::getInstance().IsFreePosition(CurrentUnit.second))
  61.             {
  62.                 int CurrentDistance = 0;
  63.                 //ищем это суммарное расстояние
  64.                 for (auto unitTo : unitsTo)
  65.                 {
  66.                     //если нашелся юнит из команды противника, то наносим удар
  67.                     if (AreNeighbours(CurrentUnit.second, unitTo.second))
  68.                     {
  69.                         Attack(unitFrom, unitTo);
  70.                         return;
  71.                     }
  72.                     CurrentDistance += GetDistance(CurrentUnit.second, unitTo.second);
  73.                 }
  74.                 if (MinDistance == -1 || CurrentDistance < MinDistance)
  75.                 {
  76.                     MinDistance = CurrentDistance;
  77.                     UnitToMove = UnitToMove;
  78.                     WayToMove = direction;
  79.                 }
  80.             }
  81.         }
  82.     }
  83.  
  84.     //если нашелся юнит, который может передвинуться
  85.     if (UnitToMove) //надо сделать нормальную проверку
  86.     {
  87.         MoveTo(UnitToMove, WayToMove);
  88.     }
  89.     //иначе надо обработать искоючение
  90. }
  91.  
  92. void SimpleStrategy::Attack(Unit::UnitInfo &unitInfo, Unit::UnitInfo &unitInfoTo)
  93. {
  94.     unitInfoTo.first->DecreaseUnitsOfLfe(unitInfo.first->GetPower());
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement