Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Решается задача:
- * https://ulearn.me/Course/BasicProgramming/Praktika_Monstry__92931100-7a4c-48ce-bb88-bc7820fc703c
- * Автор решения: Шарафеев Равиль, 11-808
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Digger {
- public class Player : ICreature {
- public string GetImageFileName() {
- return "Digger.png";
- }
- public int GetDrawingPriority() {
- return 0;
- }
- private bool IsCreatureOfType(int x, int y, Type type) {
- return Game.Map[x, y] != null && Game.Map[x, y].GetType() == type;
- }
- public CreatureCommand Act(int x, int y) {
- Keys keys = Game.KeyPressed;
- //Если нажата кнопка перемещения и перемещение возможно, то передает изменение нужной координаты
- //Плюс ограничение перемещения игрока в позицию мешка
- if (keys == Keys.Left || keys == Keys.A) {
- if (x > 0 && !IsCreatureOfType(x - 1, y, typeof(Sack))) {
- return new CreatureCommand() { DeltaX = -1 };
- }
- }
- if (keys == Keys.Up || keys == Keys.W) {
- if (y > 0 && !IsCreatureOfType(x, y - 1, typeof(Sack))) {
- return new CreatureCommand() { DeltaY = -1 };
- }
- }
- if (keys == Keys.Right || keys == Keys.D) {
- if (x < Game.MapWidth - 1 && !IsCreatureOfType(x + 1, y, typeof(Sack))) {
- return new CreatureCommand() { DeltaX = 1 };
- }
- }
- if (keys == Keys.Down || keys == Keys.S) {
- if (y < Game.MapHeight - 1 && !IsCreatureOfType(x, y + 1, typeof(Sack))) {
- return new CreatureCommand() { DeltaY = 1 };
- }
- }
- return new CreatureCommand();
- }
- //игрок умирает, если сталкивается с мешком или монстром
- public bool DeadInConflict(ICreature conflictedObject) {
- return conflictedObject.GetType() == typeof(Sack) || conflictedObject.GetType() == typeof(Monster);
- }
- }
- public class Terrain : ICreature {
- public string GetImageFileName() {
- return "Terrain.png";
- }
- public int GetDrawingPriority() {
- return 2;
- }
- public CreatureCommand Act(int x, int y) {
- return new CreatureCommand();
- }
- //земля исчезает если сталкивается с игроком
- public bool DeadInConflict(ICreature conflictedObject) {
- return
- conflictedObject.GetType() == typeof(Player);
- }
- }
- public class Gold : ICreature {
- public string GetImageFileName() {
- return "Gold.png";
- }
- public int GetDrawingPriority() {
- return 1;
- }
- public CreatureCommand Act(int x, int y) {
- return new CreatureCommand();
- }
- //Если золото столкнулось с игроком, то начисляются очки и золото пропадает
- public bool DeadInConflict(ICreature conflictedObject) {
- if (conflictedObject.GetType() == typeof(Player)) {
- Game.Scores += 10;
- return true;
- }
- return conflictedObject.GetType() == typeof(Monster);
- }
- }
- public class Sack : ICreature {
- private int fallsCount;
- public Sack() {
- fallsCount = 0;
- }
- public Sack(int fallsCount) {
- this.fallsCount = fallsCount;
- }
- public string GetImageFileName() {
- return "Sack.png";
- }
- public int GetDrawingPriority() {
- return 1;
- }
- //Метод проверкии того, должен ли мешок падать
- private bool CheckFalling(int x, int y) {
- //перебираю все координаты вниз от мешка, если встречен мешок, то перехожу к следующим координатам
- //если пустое место или (игрок или монстр при условии того, что мешок уже начал падать), то true
- //иначе false
- for (int i = y + 1; i < Game.MapHeight; i++) {
- if (
- Game.Map[x, i] == null
- || (fallsCount != 0 && (Game.Map[x, i].GetType() == typeof(Player) || Game.Map[x, i].GetType() == typeof(Monster)))
- ) {
- return true;
- }
- if (Game.Map[x, i].GetType() == typeof(Sack)) {
- continue;
- }
- break;
- }
- return false;
- }
- public CreatureCommand Act(int x, int y) {
- ICreature newCreature = null;
- int deltaY = 0;
- //Если мешок должен упасть
- if (CheckFalling(x, y)) {
- fallsCount++;
- deltaY = 1;
- newCreature = new Sack(fallsCount);
- }
- //Если мешок не должен упасть, и он не падал, или падал только 1 блок
- else if (fallsCount <= 1) {
- fallsCount = 0;
- newCreature = new Sack(fallsCount);
- }
- //Если мешок не должен упасть, но он падал не менее 2 блоков
- else {
- fallsCount = 0;
- newCreature = new Gold();
- }
- return new CreatureCommand() { DeltaY = deltaY, TransformTo = newCreature };
- }
- public bool DeadInConflict(ICreature conflictedObject) {
- return false;
- }
- }
- public class Monster : ICreature {
- public string GetImageFileName() {
- return "Monster.png";
- }
- public int GetDrawingPriority() {
- return 1;
- }
- //Метод перебирает все поля и ищет игрока
- private Tuple<int, int> FindPlayer() {
- for (int i = 0; i < Game.MapWidth; i++) {
- for (int j = 0; j < Game.MapHeight; j++) {
- if (Game.Map[i, j] != null && Game.Map[i, j].GetType() == typeof(Player)) {
- return new Tuple<int, int>(i, j);
- }
- }
- }
- return null;
- }
- //Массив для всех возможных перемещений монстра [... , 0] соответствует x, [... , 0] соответствует y
- private int[,] xy = new int[5, 2] {
- {0, 0},
- {-1, 0},
- {0, -1},
- {0, 1},
- {1, 0}
- };
- //Проверяет, лежат ли точки за краями поля
- private bool CheckBounds(int x, int y) {
- return x >= 0 && x < Game.MapWidth && y >= 0 && y < Game.MapHeight;
- }
- //Проверяет, может ли монстр сходить по координатам
- private bool CheckCreature(int x, int y) {
- return
- Game.Map[x, y] == null
- || Game.Map[x, y].GetType() == typeof(Player)
- || Game.Map[x, y].GetType() == typeof(Gold)
- ;
- }
- //Расстояние между двумя точками x1y1 и x2y2
- private double GetDist(int x1, int y1, int x2, int y2) {
- return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
- }
- public CreatureCommand Act(int x, int y) {
- Tuple<int, int> playerPos = FindPlayer();
- if (playerPos != null) {
- //Если игрок найден, проверяем все 5 состояний монстра, чтобы выбрать ему оптимальный по расстоянию до игрока ход
- double minDist = GetDist(playerPos.Item1, playerPos.Item2, x, y);
- int index = 0;
- for (int i = 1; i < 5; i++) {
- int newX = x + xy[i, 0];
- int newY = y + xy[i, 1];
- if (CheckBounds(newX, newY) && CheckCreature(newX, newY)) {
- double dist = GetDist(playerPos.Item1, playerPos.Item2, newX, newY);
- if (dist < minDist) {
- index = i;
- minDist = dist;
- }
- }
- }
- return new CreatureCommand() { DeltaX = xy[index, 0], DeltaY = xy[index, 1] };
- }
- return new CreatureCommand();
- }
- public bool DeadInConflict(ICreature conflictedObject) {
- return conflictedObject.GetType() == typeof(Sack) || conflictedObject.GetType() == typeof(Monster);
- }
- }
- }
Add Comment
Please, Sign In to add comment