From 7ef8b914585daa04a6d2eaa8d1d4f07fcd8624a7 Mon Sep 17 00:00:00 2001 From: victinass Date: Mon, 4 Mar 2024 10:53:16 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=963?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Battleship/Entities/EntityBattleship.cs | 3 -- .../Battleship/Entities/EntityWarship.cs | 8 +--- .../MovementStrategy/MoveToBorder.cs | 38 ++++++++++++------- .../MovementStrategy/MoveToCenter.cs | 23 ++++------- 4 files changed, 34 insertions(+), 38 deletions(-) diff --git a/Battleship/Battleship/Entities/EntityBattleship.cs b/Battleship/Battleship/Entities/EntityBattleship.cs index c961dc1..2a84efe 100644 --- a/Battleship/Battleship/Entities/EntityBattleship.cs +++ b/Battleship/Battleship/Entities/EntityBattleship.cs @@ -17,9 +17,6 @@ public class EntityBattleship : EntityWarship /// Основной цвет /// public Color BodyColor { get; private set; } - /// - /// Перемещение военного корабля - /// /// /// /// Признак (опция) отсека для ракет /// diff --git a/Battleship/Battleship/Entities/EntityWarship.cs b/Battleship/Battleship/Entities/EntityWarship.cs index 8d20e4d..9b88f8e 100644 --- a/Battleship/Battleship/Entities/EntityWarship.cs +++ b/Battleship/Battleship/Entities/EntityWarship.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Battleship.Entities; +namespace Battleship.Entities; /// /// Класс-сущность Военный корабль /// diff --git a/Battleship/Battleship/MovementStrategy/MoveToBorder.cs b/Battleship/Battleship/MovementStrategy/MoveToBorder.cs index e941770..257dbbc 100644 --- a/Battleship/Battleship/MovementStrategy/MoveToBorder.cs +++ b/Battleship/Battleship/MovementStrategy/MoveToBorder.cs @@ -1,40 +1,52 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - namespace Battleship.MovementStrategy; public class MoveToBorder : AbstractStrategy { protected override bool IsTargetDestination() { - var objParams = GetObjectParameters; + ObjectParameters? objParams = GetObjectParameters; if (objParams == null) { return false; } - return objParams.RightBorder + GetStep() >= FieldWidth && - objParams.DownBorder + GetStep() >= FieldHeight; + + return objParams.RightBorder - GetStep() <= FieldWidth && objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder - GetStep() <= FieldHeight && objParams.DownBorder + GetStep() >= FieldHeight; } protected override void MoveToTarget() { - var objParams = GetObjectParameters; + ObjectParameters? objParams = GetObjectParameters; if (objParams == null) { return; } - var diffX = objParams.RightBorder - FieldWidth; + + int diffX = objParams.RightBorder - FieldWidth; if (Math.Abs(diffX) > GetStep()) { - MoveRight(); + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } } - var diffY = objParams.DownBorder - FieldHeight; + + int diffY = objParams.DownBorder - FieldHeight; if (Math.Abs(diffY) > GetStep()) { - MoveDown(); + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } } } } diff --git a/Battleship/Battleship/MovementStrategy/MoveToCenter.cs b/Battleship/Battleship/MovementStrategy/MoveToCenter.cs index 7fd9ad9..84274a2 100644 --- a/Battleship/Battleship/MovementStrategy/MoveToCenter.cs +++ b/Battleship/Battleship/MovementStrategy/MoveToCenter.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Battleship.MovementStrategy; +namespace Battleship.MovementStrategy; /// /// Стратегия перемещения объекта в центр экрана @@ -14,20 +8,19 @@ public class MoveToCenter : AbstractStrategy protected override bool IsTargetDestination() { ObjectParameters? objParams = GetObjectParameters; - if (objParams != null ) + if (objParams == null) { return false; } return objParams.ObjectMiddleHorizontall - GetStep() <= FieldWidth / 2 && objParams.ObjectMiddleHorizontall + GetStep() >= FieldWidth / 2 && - objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 - && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; } protected override void MoveToTarget() { ObjectParameters? objParams = GetObjectParameters; - if (objParams == null ) + if (objParams == null) { return; } @@ -45,10 +38,10 @@ public class MoveToCenter : AbstractStrategy } } - int diffy = objParams.ObjectMiddleVertical - FieldHeight / 2; - if (Math.Abs(diffy) > GetStep()) + int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) { - if (diffy > 0) + if (diffY > 0) { MoveUp(); } @@ -58,4 +51,4 @@ public class MoveToCenter : AbstractStrategy } } } -} +} \ No newline at end of file