diff --git a/AbstractStrategy.java b/AbstractStrategy.java new file mode 100644 index 0000000..8be154e --- /dev/null +++ b/AbstractStrategy.java @@ -0,0 +1,69 @@ +public abstract class AbstractStrategy { + private IMoveableObject _moveableObject; + private Status _state = Status.NotInit; + + private int FieldWidth; + private void SetFieldWidth(int width) {FieldWidth = width;} + protected int GetFieldWidth() {return FieldWidth;} + private int FieldHeight; + private void SetFieldHeight(int height) {FieldHeight = height;} + protected int GetFieldHeight() {return FieldHeight;} + + public Status GetStatus() {return _state;} + + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = Status.NotInit; + return; + } + _state = Status.InProgress; + _moveableObject = moveableObject; + SetFieldWidth(width); + SetFieldHeight(height); + } + + public void MakeStep() + { + if(_state != Status.InProgress) + return; + if (IsTargetDestination()) + { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + + protected boolean MoveLeft() {return MoveTo(Direction.Left); } + protected boolean MoveRight() {return MoveTo(Direction.Right); } + protected boolean MoveUp() {return MoveTo(Direction.Up); } + protected boolean MoveDown() {return MoveTo(Direction.Down); } + protected ObjectParameters GetObjectParameters() {return _moveableObject.GetObjectParameters(); } + + protected Integer GetStep() + { + if (_state != Status.InProgress) + { + return null; + } + return _moveableObject.GetStep(); + } + + protected abstract void MoveToTarget(); + + protected abstract boolean IsTargetDestination(); + private boolean MoveTo(Direction direction) + { + if (_state != Status.InProgress) + return false; + if (!_moveableObject.CheckCanMove(direction)) + { + _moveableObject.MoveObject(direction); + return true; + } + return false; + } + +} diff --git a/BaseCar.java b/BaseCar.java deleted file mode 100644 index 3370cea..0000000 --- a/BaseCar.java +++ /dev/null @@ -1,79 +0,0 @@ -import java.awt.*; - -public class BaseCar { - private int Speed; - public int getSpeed() { - return Speed; - } - private void setSpeed(int speed) - { - Speed = speed; - } - private double Weight; - public double getWeight() - { - return Weight; - } - private void setWeight(double weight) - { - Weight = weight; - } - private Color BodyColor; - public Color getBodyColor() - { - return BodyColor; - } - private void setBodyColor(Color bodyColor) - { - BodyColor = bodyColor; - } - private Color AdditionalColor; - public Color getAdditionalColor() - { - return AdditionalColor; - } - private void setAdditionalColor(Color additionalColor) - { - AdditionalColor = additionalColor; - } - private boolean BodyKit; - public boolean getBodyKit() - { - return BodyKit; - } - private void setBodyKit(boolean bodyKit) - { - BodyKit = bodyKit; - } - private boolean Wing; - public boolean getWing() - { - return Wing; - } - private void setWing(boolean wing) - { - Wing = wing; - } - private boolean SportLine; - public boolean getSportLine() - { - return SportLine; - } - private void setSportLine(boolean sportLine) - { - SportLine = sportLine; - } - public double Step; - - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - BodyKit = bodyKit; - Wing = wing; - SportLine = sportLine; - Step = (double)Speed * 100 / Weight; - } -} diff --git a/BaseTanker.java b/BaseTanker.java new file mode 100644 index 0000000..2cc44da --- /dev/null +++ b/BaseTanker.java @@ -0,0 +1,39 @@ +import java.awt.*; + +public class BaseTanker { + private int Speed; + public int getSpeed() { + return Speed; + } + private void setSpeed(int speed) + { + Speed = speed; + } + private double Weight; + public double getWeight() + { + return Weight; + } + private void setWeight(double weight) + { + Weight = weight; + } + private Color BodyColor; + public Color getBodyColor() + { + return BodyColor; + } + private void setBodyColor(Color bodyColor) + { + BodyColor = bodyColor; + } + public double Step; + + public BaseTanker(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + Step = (double)Speed * 100 / Weight; + } +} diff --git a/DrawFrame.java b/DrawFrame.java index 0c64d8f..5f8b159 100644 --- a/DrawFrame.java +++ b/DrawFrame.java @@ -7,6 +7,7 @@ import java.util.Random; class DrawGasoline extends JComponent { private DrawingTanker _drawingTanker; + private AbstractStrategy _abstractStrategy; public void paintComponent(Graphics g) { super.paintComponent(g); @@ -15,15 +16,24 @@ class DrawGasoline extends JComponent { _drawingTanker.DrawTransport(g); super.repaint(); } - protected void CreateCarButton_Click() + protected void CreateGasolineTankerButton_Click() { Random rnd = new Random(); - _drawingTanker = new DrawingTanker(); Color addColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); Color bodyColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); - _drawingTanker.Init(rnd.nextInt(100, 200), rnd.nextInt(2000, 4000), + _drawingTanker = new DrawGasolineTanker(rnd.nextInt(100, 200), rnd.nextInt(2000, 4000), bodyColor, addColor, IntToBool(rnd.nextInt(0, 2)), IntToBool(rnd.nextInt(0, 2)), - IntToBool(rnd.nextInt(0, 2)), Frame.Width, Frame.Height, rnd.nextInt(0, 30)); + IntToBool(rnd.nextInt(0, 2)), Frame.Width, Frame.Height, rnd.nextInt(0, 30), rnd.nextInt(1, 200)); + _drawingTanker.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); + super.repaint(); + } + + protected void CreateBaseTankerButton_Click() + { + Random rnd = new Random(); + Color bodyColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); + _drawingTanker = new DrawingTanker(rnd.nextInt(100, 200), rnd.nextInt(2000, 4000), bodyColor, Frame.Width, Frame.Height, rnd.nextInt(0, 30), rnd.nextInt(1, 200)); + Color addColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); _drawingTanker.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100)); super.repaint(); } @@ -44,6 +54,35 @@ class DrawGasoline extends JComponent { { return n > 0; } + + protected void ButtonStep_Click(JComboBox JCB, int width, int height) + { + if (_drawingTanker == null) + return; + if (JCB.isEnabled()) + { + String strat = String.valueOf(JCB.getSelectedItem()); + switch (strat) + { + case "0" -> {_abstractStrategy = new MoveToCenter();} + case "1" -> {_abstractStrategy = new MoveToBorder();} + default -> {_abstractStrategy = null;} + }; + if (_abstractStrategy == null) + return; + _abstractStrategy.SetData(new DrawingObjectTanker(_drawingTanker), width, height); + JCB.setEnabled(false); + } + if (_abstractStrategy == null) + return; + _abstractStrategy.MakeStep(); + super.repaint(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + JCB.setEnabled(true); + _abstractStrategy = null; + } + } } class Frame extends JFrame { @@ -97,20 +136,51 @@ class Frame extends JFrame { add(buttonDown); buttonDown.addActionListener(moving); + String[] items = { + "0", + "1" + }; + JComboBox strategy = new JComboBox(items); + + strategy.setBounds(Width - 100, 100, 80, 20); + strategy.setLayout(null); + strategy.setEnabled(true); + add(strategy); + + JButton buttonStrategy = new JButton(); + buttonStrategy.setBounds(Width - 60, 120, 40, 20); + buttonStrategy.setLayout(null); + add(buttonStrategy); + buttonStrategy.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Gasoline.ButtonStep_Click(strategy, Width, Height); + } + }); buttonUp.setIcon(new ImageIcon("Arrows/Up.png")); buttonDown.setIcon(new ImageIcon("Arrows/Down.png")); buttonLeft.setIcon(new ImageIcon("Arrows/Left.png")); buttonRight.setIcon(new ImageIcon("Arrows/Right.png")); - JButton buttonCreate = new JButton("Create"); - buttonCreate.setBounds(60, Height - 120, 120, 50); - buttonCreate.setLayout(null); - add(buttonCreate); - buttonCreate.addActionListener(new ActionListener() { + JButton buttonCreateTanker = new JButton("Create Base Tanker"); + buttonCreateTanker.setBounds(60, Height - 120, 200, 50); + buttonCreateTanker.setLayout(null); + add(buttonCreateTanker); + buttonCreateTanker.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - Gasoline.CreateCarButton_Click(); + Gasoline.CreateBaseTankerButton_Click(); + } + }); + JButton buttonCreateGasolineTanker = new JButton("Create Update Tanker"); + buttonCreateGasolineTanker.setBounds(260, Height - 120, 200, 50); + buttonCreateGasolineTanker.setLayout(null); + add(buttonCreateGasolineTanker); + buttonCreateGasolineTanker.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Gasoline.CreateGasolineTankerButton_Click(); } }); diff --git a/DrawGasolineTanker.java b/DrawGasolineTanker.java new file mode 100644 index 0000000..2afeea0 --- /dev/null +++ b/DrawGasolineTanker.java @@ -0,0 +1,29 @@ +import java.awt.*; + +public class DrawGasolineTanker extends DrawingTanker { + public DrawGasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine, int width, int height, int wheelCount, int wheelMode) { + super(speed, weight, bodyColor, width, height, wheelCount, wheelMode); + if (GasolineTanker != null) { + GasolineTanker = new GasolineTanker(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine); + } + } + @Override + public void DrawTransport(Graphics g) { + if (!(GasolineTanker instanceof GasolineTanker)) { + return; + } + super.DrawTransport(g); + g.setColor(((GasolineTanker) GasolineTanker).GetAdditionalColor()); + if (((GasolineTanker) GasolineTanker).GetBodyKit()) { + g.fillOval(10 + _startPosX, 10 + _startPosY, 70, 30); + } + if (((GasolineTanker) GasolineTanker).GetSportLine()) { + g.fillRect(15 + _startPosX, 45 + _startPosY, 20, 5); + g.fillRect(40 + _startPosX, 45 + _startPosY, 20, 5); + g.fillRect(65 + _startPosX, 45 + _startPosY, 20, 5); + } + if (((GasolineTanker) GasolineTanker).GetWing()) { + g.fillRect(87 + _startPosX, 5 + _startPosY, 5, 5); + } + } +} diff --git a/DrawWheelCircle.java b/DrawWheelCircle.java new file mode 100644 index 0000000..a2f7bb5 --- /dev/null +++ b/DrawWheelCircle.java @@ -0,0 +1,41 @@ +import java.awt.*; +import java.util.Random; + +public class DrawWheelCircle implements IWheelDraw { + private WheelCounter wheelCounter; + public WheelCounter getWheelCounter() + { + return wheelCounter; + } + public void setWheelCount(int count) + { + if (count % 3 == 0) + wheelCounter = WheelCounter.THREE; + else if (count % 3 == 1) + wheelCounter = WheelCounter.FOUR; + else if (count % 3 == 2) + wheelCounter = WheelCounter.TWO; + } + + public void DrawWheels(int _startPosX, int _startPosY, Color bodyColor, Graphics2D g2d) + { + g2d.setColor(bodyColor); + g2d.fillOval(10 + _startPosX, 60 + _startPosY, 20, 20); + g2d.fillOval(80 + _startPosX, 60 + _startPosY, 20, 20); + switch (wheelCounter) + { + case THREE -> {g2d.fillOval(45 + _startPosX, 60 + _startPosY ,20, 20);} + case FOUR -> {g2d.fillOval(30 + _startPosX, 60 + _startPosY ,20, 20); g2d.fillOval(60 + _startPosX, 60 + _startPosY ,20, 20);} + } + Random rnd = new Random(); + Color stringColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); + g2d.setColor(stringColor); + g2d.drawString("S",15 + _startPosX, 75 + _startPosY); + g2d.drawString("S",85 + _startPosX, 75 + _startPosY); + switch (wheelCounter) + { + case THREE -> {g2d.drawString("S",50 + _startPosX, 75 + _startPosY);} + case FOUR -> {g2d.drawString("S",35 + _startPosX, 75 + _startPosY); g2d.drawString("S",65 + _startPosX, 75 + _startPosY);} + } + } +} diff --git a/WheelsDrawing.java b/DrawWheelClassic.java similarity index 94% rename from WheelsDrawing.java rename to DrawWheelClassic.java index 43ef1a4..273c716 100644 --- a/WheelsDrawing.java +++ b/DrawWheelClassic.java @@ -1,6 +1,6 @@ import java.awt.*; -public class WheelsDrawing { +public class DrawWheelClassic implements IWheelDraw{ private WheelCounter wheelCounter; public WheelCounter getWheelCounter() { @@ -26,5 +26,6 @@ public class WheelsDrawing { case THREE -> {g2d.fillOval(45 + _startPosX, 60 + _startPosY ,20, 20);} case FOUR -> {g2d.fillOval(30 + _startPosX, 60 + _startPosY ,20, 20); g2d.fillOval(60 + _startPosX, 60 + _startPosY ,20, 20);} } + } } diff --git a/DrawWheelSquare.java b/DrawWheelSquare.java new file mode 100644 index 0000000..3dd7c18 --- /dev/null +++ b/DrawWheelSquare.java @@ -0,0 +1,42 @@ +import java.awt.*; +import java.util.Random; + +public class DrawWheelSquare implements IWheelDraw{ + private WheelCounter wheelCounter; + public WheelCounter getWheelCounter() + { + return wheelCounter; + } + public void setWheelCount(int count) + { + if (count % 3 == 0) + wheelCounter = WheelCounter.THREE; + else if (count % 3 == 1) + wheelCounter = WheelCounter.FOUR; + else if (count % 3 == 2) + wheelCounter = WheelCounter.TWO; + } + + public void DrawWheels(int _startPosX, int _startPosY, Color bodyColor, Graphics2D g2d) + { + g2d.setColor(bodyColor); + g2d.fillOval(10 + _startPosX, 60 + _startPosY, 20, 20); + g2d.drawString("S",15 + _startPosX, 60 + _startPosY); + g2d.fillOval(80 + _startPosX, 60 + _startPosY, 20, 20); + switch (wheelCounter) + { + case THREE -> {g2d.fillOval(45 + _startPosX, 60 + _startPosY ,20, 20);} + case FOUR -> {g2d.fillOval(30 + _startPosX, 60 + _startPosY ,20, 20); g2d.fillOval(60 + _startPosX, 60 + _startPosY ,20, 20);} + } + Random rnd = new Random(); + Color stringColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); + g2d.setColor(stringColor); + g2d.fillRect(15 + _startPosX, 65 + _startPosY, 10, 10); + g2d.fillRect(85 + _startPosX, 65 + _startPosY, 10, 10); + switch (wheelCounter) + { + case THREE -> {g2d.fillRect(50 + _startPosX, 65 + _startPosY, 10, 10);} + case FOUR -> {g2d.fillRect(35 + _startPosX, 65 + _startPosY, 10, 10); g2d.fillRect(65 + _startPosX, 65 + _startPosY, 10, 10);} + } + } +} diff --git a/DrawingObjectTanker.java b/DrawingObjectTanker.java new file mode 100644 index 0000000..cc1b0cc --- /dev/null +++ b/DrawingObjectTanker.java @@ -0,0 +1,27 @@ +import java.util.Optional; + +public class DrawingObjectTanker implements IMoveableObject { + private final DrawingTanker _drawTanker; + + public DrawingObjectTanker(DrawingTanker drawTanker) { + _drawTanker = drawTanker; + } + + public ObjectParameters GetObjectParameters() { + if (_drawTanker == null || _drawTanker.GetGasolineTanker() == null) { + return null; + } + return new ObjectParameters(_drawTanker.GetPosX(), _drawTanker.GetPosY(), _drawTanker.GetWidth(), _drawTanker.GetHeight()); + } + + public int GetStep() { + int s = (int) _drawTanker.GasolineTanker.Step; + if (s != 0) + return s; + return 0; + } + + public boolean CheckCanMove(Direction direction) {return !_drawTanker.CanMove(direction); } + public void MoveObject(Direction direction) { _drawTanker.MoveTransport(direction); } + +} \ No newline at end of file diff --git a/DrawingTanker.java b/DrawingTanker.java index 61cd005..592a7bf 100644 --- a/DrawingTanker.java +++ b/DrawingTanker.java @@ -1,89 +1,101 @@ import java.awt.*; public class DrawingTanker { - private BaseCar GasolineTanker; - private WheelsDrawing wheelsDrawing; - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private final int _carWidth = 120; - private final int _carHeight = 120; - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine, int width, int height, int wheelCount) + protected BaseTanker GasolineTanker; + public BaseTanker GetGasolineTanker() {return GasolineTanker;} + private IWheelDraw wheelsDrawing; + protected int _pictureWidth; + protected int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + protected final int _carWidth = 110; + protected final int _carHeight = 120; + public int GetPosX() {return _startPosX;} + public int GetPosY() {return _startPosY;} + public int GetWidth() {return _carWidth;} + public int GetHeight() {return _carHeight;} + + public boolean CanMove(Direction direction) + { + if (GasolineTanker == null) + return false; + switch (direction) + { + case Left -> { + return _startPosX - GasolineTanker.Step > 0; } + case Up -> { + return _startPosY - GasolineTanker.Step > 0; } + case Right -> { + return _startPosX + _carWidth + GasolineTanker.Step < _pictureWidth; } + case Down -> { + return _startPosY + _carHeight + GasolineTanker.Step < _pictureHeight; } + default -> { + return false; } + + } + } + public DrawingTanker(int speed, double weight, Color bodyColor, int width, int height, int wheelCount, int wheelMode) { _pictureHeight = height; _pictureWidth = width; - GasolineTanker = new BaseCar(); - GasolineTanker.Init(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine); - wheelsDrawing = new WheelsDrawing(); + GasolineTanker = new BaseTanker(speed, weight, bodyColor); + int mode = wheelMode % 3; + switch (mode) + { + case 0 -> {wheelsDrawing = new DrawWheelCircle();} + case 1 -> {wheelsDrawing = new DrawWheelSquare();} + case 2 -> {wheelsDrawing = new DrawWheelClassic();} + } wheelsDrawing.setWheelCount(wheelCount); } + public void SetPosition(int x, int y) { _startPosX = x; _startPosY = y; } + public void MoveTransport(Direction direction) { - if (GasolineTanker == null) + if (!CanMove(direction) || GasolineTanker == null) return; switch (direction) { - case Left -> { - if (_startPosX - GasolineTanker.Step > 0) - { - _startPosX -= (int)GasolineTanker.Step; - } + case Left: + { + _startPosX -= (int)GasolineTanker.Step; } - case Up -> { - if (_startPosY - GasolineTanker.Step > 0) - { - _startPosY -= (int)GasolineTanker.Step; - } + break; + case Up: + { + _startPosY -= (int)GasolineTanker.Step; } - case Right -> { - if (_startPosX + _carWidth + GasolineTanker.Step < _pictureWidth) - { - _startPosX += (int)GasolineTanker.Step; - } - } - case Down -> { - if (_startPosY + GasolineTanker.Step + _carHeight < _pictureHeight) - { - _startPosY += (int)GasolineTanker.Step; - } + break; + case Right: + { + _startPosX += (int)GasolineTanker.Step; } + break; + case Down: + { + _startPosY += (int)GasolineTanker.Step; + } + break; } } + public void DrawTransport(Graphics g) { if (GasolineTanker == null) return; var g2d = (Graphics2D) g; - if (GasolineTanker.getBodyKit()) - { - g2d.setColor(GasolineTanker.getAdditionalColor()); - g2d.fillOval(10 + _startPosX, 10 + _startPosY, 70, 30); - } g2d.setColor(GasolineTanker.getBodyColor()); // Отрисовка корпуса g2d.fillRect(10 + _startPosX, 40 + _startPosY, 90, 20); g2d.fillRect(80 + _startPosX, 10 + _startPosY, 20, 40); // Отрисовка колесиков wheelsDrawing.DrawWheels(_startPosX, _startPosY, GasolineTanker.getBodyColor(), g2d); - if (GasolineTanker.getSportLine()) - { - g2d.setColor(GasolineTanker.getAdditionalColor()); - g2d.fillRect( 15 + _startPosX, 45 + _startPosY, 20, 5); - g2d.fillRect(40 + _startPosX, 45 + _startPosY, 20, 5); - g2d.fillRect(65 + _startPosX, 45 + _startPosY, 20, 5); - } - if (GasolineTanker.getWing()) - { - g2d.setColor(GasolineTanker.getAdditionalColor()); - g2d.fillRect(87 + _startPosX, 5 + _startPosY, 5, 5); - } } } diff --git a/GasolineTanker.java b/GasolineTanker.java new file mode 100644 index 0000000..14e10fc --- /dev/null +++ b/GasolineTanker.java @@ -0,0 +1,20 @@ +import java.awt.*; + +public class GasolineTanker extends BaseTanker{ + private Color AdditionalColor; + public Color GetAdditionalColor() {return AdditionalColor;} + private boolean BodyKit; + public boolean GetBodyKit() {return BodyKit;} + private boolean Wing; + public boolean GetWing() {return Wing;} + private boolean SportLine; + public boolean GetSportLine() {return SportLine;} + public GasolineTanker(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine) + { + super(speed, weight, bodyColor); + AdditionalColor = additionalColor; + BodyKit = bodyKit; + Wing = wing; + SportLine = sportLine; + } +} diff --git a/IMoveableObject.java b/IMoveableObject.java new file mode 100644 index 0000000..61b86b4 --- /dev/null +++ b/IMoveableObject.java @@ -0,0 +1,6 @@ +public interface IMoveableObject { + ObjectParameters GetObjectParameters(); + int GetStep(); + boolean CheckCanMove(Direction direction); + void MoveObject(Direction direction); +} diff --git a/IWheelDraw.java b/IWheelDraw.java new file mode 100644 index 0000000..a4b7634 --- /dev/null +++ b/IWheelDraw.java @@ -0,0 +1,7 @@ +import java.awt.*; + +public interface IWheelDraw { + void DrawWheels(int _startPosX, int _startPosY, Color bodyColor, Graphics2D g2d); + void setWheelCount(int wheelCount); + +} diff --git a/MoveToBorder.java b/MoveToBorder.java new file mode 100644 index 0000000..f81f847 --- /dev/null +++ b/MoveToBorder.java @@ -0,0 +1,30 @@ +public class MoveToBorder extends AbstractStrategy +{ + @Override + protected boolean IsTargetDestination() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) { + return false; + } + return objParams.RightBorder() + GetStep() >= GetFieldWidth() && objParams.DownBorder() + GetStep() >= GetFieldHeight(); + } + + @Override + protected void MoveToTarget() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) { + return; + } + int diffX = objParams.RightBorder() - GetFieldWidth(); + int diffY = objParams.DownBorder() - GetFieldHeight(); + if (diffX >= 0) { + MoveDown(); + } else if (diffY >= 0) { + MoveRight(); + } else if (Math.abs(diffX) > Math.abs(diffY)) { + MoveRight(); + } else { + MoveDown(); + } + } +} \ No newline at end of file diff --git a/MoveToCenter.java b/MoveToCenter.java new file mode 100644 index 0000000..ad28f35 --- /dev/null +++ b/MoveToCenter.java @@ -0,0 +1,35 @@ +public class MoveToCenter extends AbstractStrategy { + @Override + protected boolean IsTargetDestination() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) { + return false; + } + return objParams.ObjectMiddleHorizontal() <= GetFieldWidth() / 2 && objParams.ObjectMiddleHorizontal() + GetStep() >= GetFieldWidth() / 2 && + objParams.ObjectMiddleVertical() <= GetFieldHeight() / 2 && objParams.ObjectMiddleVertical() + GetStep() >= GetFieldHeight() / 2; + } + + @Override + protected void MoveToTarget() { + ObjectParameters objParams = GetObjectParameters(); + if (objParams == null) { + return; + } + int diffX = objParams.ObjectMiddleHorizontal() - GetFieldWidth() / 2; + if (Math.abs(diffX) > GetStep()) { + if (diffX > 0) { + MoveLeft(); + } else { + MoveRight(); + } + } + int diffY = objParams.ObjectMiddleVertical() - GetFieldHeight() / 2; + if (Math.abs(diffY) > GetStep()) { + if (diffY > 0) { + MoveUp(); + } else { + MoveDown(); + } + } + } +} diff --git a/ObjectParameters.java b/ObjectParameters.java new file mode 100644 index 0000000..0b0487d --- /dev/null +++ b/ObjectParameters.java @@ -0,0 +1,19 @@ +public class ObjectParameters { + private final int _x; + private final int _y; + private final int _width; + private final int _height; + + public int LeftBorder() { return _x; } + public int RightBorder() { return _x + _width; } + public int TopBorder() { return _y; } + public int DownBorder() { return _y + _height; } + public int ObjectMiddleHorizontal() { return _x + _width / 2; } + public int ObjectMiddleVertical() { return _y + _height / 2; } + public ObjectParameters(int x, int y, int width, int height) { + _x = x; + _y = y; + _width = width; + _height = height; + } +} diff --git a/Status.java b/Status.java new file mode 100644 index 0000000..4d17a03 --- /dev/null +++ b/Status.java @@ -0,0 +1,14 @@ +public enum Status { + NotInit ("NI"), + InProgress ("IP"), + Finish ("F"); + private String status; + Status(String d) + { + status = d; + } + public String getDirect() + { + return status; + } +}