Graf_Rav

Untitled

Dec 17th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.64 KB | None | 0 0
  1. /*
  2.  * Решается задача:
  3.  * https://ulearn.me/Course/BasicProgramming/Praktika_Monstry__92931100-7a4c-48ce-bb88-bc7820fc703c
  4.  * Автор решения: Шарафеев Равиль, 11-808
  5.  */
  6.  
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14.  
  15. namespace Digger {
  16.     public class Player : ICreature {
  17.         public string GetImageFileName() {
  18.             return "Digger.png";
  19.         }
  20.  
  21.         public int GetDrawingPriority() {
  22.             return 0;
  23.         }
  24.  
  25.         private bool IsCreatureOfType(int x, int y, Type type) {
  26.             return Game.Map[x, y] != null && Game.Map[x, y].GetType() == type;
  27.         }
  28.  
  29.         public CreatureCommand Act(int x, int y) {
  30.             Keys keys = Game.KeyPressed;
  31.             //Если нажата кнопка перемещения и перемещение возможно, то передает изменение нужной координаты
  32.             //Плюс ограничение перемещения игрока в позицию мешка
  33.             if (keys == Keys.Left || keys == Keys.A) {
  34.                 if (x > 0 && !IsCreatureOfType(x - 1, y, typeof(Sack))) {
  35.                     return new CreatureCommand() { DeltaX = -1 };
  36.                 }
  37.             }
  38.             if (keys == Keys.Up || keys == Keys.W) {
  39.                 if (y > 0 && !IsCreatureOfType(x, y - 1, typeof(Sack))) {
  40.                     return new CreatureCommand() { DeltaY = -1 };
  41.                 }
  42.             }
  43.             if (keys == Keys.Right || keys == Keys.D) {
  44.                 if (x < Game.MapWidth - 1 && !IsCreatureOfType(x + 1, y, typeof(Sack))) {
  45.                     return new CreatureCommand() { DeltaX = 1 };
  46.                 }
  47.             }
  48.             if (keys == Keys.Down || keys == Keys.S) {
  49.                 if (y < Game.MapHeight - 1 && !IsCreatureOfType(x, y + 1, typeof(Sack))) {
  50.                     return new CreatureCommand() { DeltaY = 1 };
  51.                 }
  52.             }
  53.             return new CreatureCommand();
  54.         }
  55.         //игрок умирает, если сталкивается с мешком или монстром
  56.         public bool DeadInConflict(ICreature conflictedObject) {
  57.             return conflictedObject.GetType() == typeof(Sack) || conflictedObject.GetType() == typeof(Monster);
  58.         }
  59.     }
  60.  
  61.     public class Terrain : ICreature {
  62.         public string GetImageFileName() {
  63.             return "Terrain.png";
  64.         }
  65.  
  66.         public int GetDrawingPriority() {
  67.             return 2;
  68.         }
  69.  
  70.         public CreatureCommand Act(int x, int y) {
  71.             return new CreatureCommand();
  72.         }
  73.         //земля исчезает если сталкивается с игроком
  74.         public bool DeadInConflict(ICreature conflictedObject) {
  75.             return
  76.                 conflictedObject.GetType() == typeof(Player);
  77.         }
  78.     }
  79.  
  80.     public class Gold : ICreature {
  81.         public string GetImageFileName() {
  82.             return "Gold.png";
  83.         }
  84.  
  85.         public int GetDrawingPriority() {
  86.             return 1;
  87.         }
  88.  
  89.         public CreatureCommand Act(int x, int y) {
  90.             return new CreatureCommand();
  91.         }
  92.         //Если золото столкнулось с игроком, то начисляются очки и золото пропадает
  93.         public bool DeadInConflict(ICreature conflictedObject) {
  94.             if (conflictedObject.GetType() == typeof(Player)) {
  95.                 Game.Scores += 10;
  96.                 return true;
  97.             }
  98.             return conflictedObject.GetType() == typeof(Monster);
  99.         }
  100.     }
  101.  
  102.     public class Sack : ICreature {
  103.         private int fallsCount;
  104.  
  105.         public Sack() {
  106.             fallsCount = 0;
  107.         }
  108.  
  109.         public Sack(int fallsCount) {
  110.             this.fallsCount = fallsCount;
  111.         }
  112.  
  113.         public string GetImageFileName() {
  114.             return "Sack.png";
  115.         }
  116.  
  117.         public int GetDrawingPriority() {
  118.             return 1;
  119.         }
  120.  
  121.         //Метод проверкии того, должен ли мешок падать
  122.         private bool CheckFalling(int x, int y) {
  123.             //перебираю все координаты вниз от мешка, если встречен мешок, то перехожу к следующим координатам
  124.             //если пустое место или (игрок или монстр при условии того, что мешок уже начал падать), то true
  125.             //иначе false
  126.             for (int i = y + 1; i < Game.MapHeight; i++) {
  127.                 if (
  128.                     Game.Map[x, i] == null
  129.                     || (fallsCount != 0 && (Game.Map[x, i].GetType() == typeof(Player) || Game.Map[x, i].GetType() == typeof(Monster)))
  130.  
  131.                 ) {
  132.                     return true;
  133.                 }
  134.                 if (Game.Map[x, i].GetType() == typeof(Sack)) {
  135.                     continue;
  136.                 }
  137.                 break;
  138.             }
  139.             return false;
  140.         }
  141.  
  142.         public CreatureCommand Act(int x, int y) {
  143.             ICreature newCreature = null;
  144.             int deltaY = 0;
  145.             //Если мешок должен упасть
  146.             if (CheckFalling(x, y)) {
  147.                 fallsCount++;
  148.                 deltaY = 1;
  149.                 newCreature = new Sack(fallsCount);
  150.             }
  151.             //Если мешок не должен упасть, и он не падал, или падал только 1 блок
  152.             else if (fallsCount <= 1) {
  153.                 fallsCount = 0;
  154.                 newCreature = new Sack(fallsCount);
  155.             }
  156.             //Если мешок не должен упасть, но он падал не менее 2 блоков
  157.             else {
  158.                 fallsCount = 0;
  159.                 newCreature = new Gold();
  160.             }
  161.  
  162.             return new CreatureCommand() { DeltaY = deltaY, TransformTo = newCreature };
  163.         }
  164.  
  165.         public bool DeadInConflict(ICreature conflictedObject) {
  166.             return false;
  167.         }
  168.     }
  169.  
  170.     public class Monster : ICreature {
  171.         public string GetImageFileName() {
  172.             return "Monster.png";
  173.         }
  174.  
  175.         public int GetDrawingPriority() {
  176.             return 1;
  177.         }
  178.         //Метод перебирает все поля и ищет игрока
  179.         private Tuple<int, int> FindPlayer() {
  180.             for (int i = 0; i < Game.MapWidth; i++) {
  181.                 for (int j = 0; j < Game.MapHeight; j++) {
  182.                     if (Game.Map[i, j] != null && Game.Map[i, j].GetType() == typeof(Player)) {
  183.                         return new Tuple<int, int>(i, j);
  184.                     }
  185.                 }
  186.             }
  187.             return null;
  188.         }
  189.         //Массив для всех возможных перемещений монстра [... , 0] соответствует x, [... , 0] соответствует y
  190.         private int[,] xy = new int[5, 2] {
  191.             {0, 0},
  192.             {-1, 0},
  193.             {0, -1},
  194.             {0, 1},
  195.             {1, 0}
  196.         };
  197.         //Проверяет, лежат ли точки за краями поля
  198.         private bool CheckBounds(int x, int y) {
  199.             return x >= 0 && x < Game.MapWidth && y >= 0 && y < Game.MapHeight;
  200.         }
  201.         //Проверяет, может ли монстр сходить по координатам
  202.         private bool CheckCreature(int x, int y) {
  203.             return
  204.                 Game.Map[x, y] == null
  205.                 || Game.Map[x, y].GetType() == typeof(Player)
  206.                 || Game.Map[x, y].GetType() == typeof(Gold)
  207.             ;
  208.         }
  209.         //Расстояние между двумя точками x1y1 и x2y2
  210.         private double GetDist(int x1, int y1, int x2, int y2) {
  211.             return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  212.         }
  213.  
  214.         public CreatureCommand Act(int x, int y) {
  215.             Tuple<int, int> playerPos = FindPlayer();
  216.             if (playerPos != null) {
  217.                 //Если игрок найден, проверяем все 5 состояний монстра, чтобы выбрать ему оптимальный по расстоянию до игрока ход
  218.                 double minDist = GetDist(playerPos.Item1, playerPos.Item2, x, y);
  219.                 int index = 0;
  220.                 for (int i = 1; i < 5; i++) {
  221.                     int newX = x + xy[i, 0];
  222.                     int newY = y + xy[i, 1];
  223.                     if (CheckBounds(newX, newY) && CheckCreature(newX, newY)) {
  224.                         double dist = GetDist(playerPos.Item1, playerPos.Item2, newX, newY);
  225.                         if (dist < minDist) {
  226.                             index = i;
  227.                             minDist = dist;
  228.                         }
  229.                     }
  230.                 }
  231.  
  232.                 return new CreatureCommand() { DeltaX = xy[index, 0], DeltaY = xy[index, 1] };
  233.             }
  234.             return new CreatureCommand();
  235.         }
  236.  
  237.         public bool DeadInConflict(ICreature conflictedObject) {
  238.             return conflictedObject.GetType() == typeof(Sack) || conflictedObject.GetType() == typeof(Monster);
  239.         }
  240.     }
  241. }
Add Comment
Please, Sign In to add comment