Laba2 KozyrevSS GasolineTanker JAVA HARD

This commit is contained in:
Sergey Kozyrev 2023-10-18 12:33:39 +04:00
parent c6fc3c0b37
commit 5815f489b0
17 changed files with 523 additions and 141 deletions

69
AbstractStrategy.java Normal file
View File

@ -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;
}
}

View File

@ -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;
}
}

39
BaseTanker.java Normal file
View File

@ -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;
}
}

View File

@ -7,6 +7,7 @@ import java.util.Random;
class DrawGasoline extends JComponent { class DrawGasoline extends JComponent {
private DrawingTanker _drawingTanker; private DrawingTanker _drawingTanker;
private AbstractStrategy _abstractStrategy;
public void paintComponent(Graphics g) public void paintComponent(Graphics g)
{ {
super.paintComponent(g); super.paintComponent(g);
@ -15,15 +16,24 @@ class DrawGasoline extends JComponent {
_drawingTanker.DrawTransport(g); _drawingTanker.DrawTransport(g);
super.repaint(); super.repaint();
} }
protected void CreateCarButton_Click() protected void CreateGasolineTankerButton_Click()
{ {
Random rnd = new Random(); Random rnd = new Random();
_drawingTanker = new DrawingTanker();
Color addColor = new Color(rnd.nextInt(0, 256), rnd.nextInt(0, 256), rnd.nextInt(0, 256)); 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)); 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)), 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)); _drawingTanker.SetPosition(rnd.nextInt(10, 100), rnd.nextInt(10, 100));
super.repaint(); super.repaint();
} }
@ -44,6 +54,35 @@ class DrawGasoline extends JComponent {
{ {
return n > 0; return n > 0;
} }
protected void ButtonStep_Click(JComboBox<String> 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 { class Frame extends JFrame {
@ -97,20 +136,51 @@ class Frame extends JFrame {
add(buttonDown); add(buttonDown);
buttonDown.addActionListener(moving); buttonDown.addActionListener(moving);
String[] items = {
"0",
"1"
};
JComboBox<String> strategy = new JComboBox<String>(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")); buttonUp.setIcon(new ImageIcon("Arrows/Up.png"));
buttonDown.setIcon(new ImageIcon("Arrows/Down.png")); buttonDown.setIcon(new ImageIcon("Arrows/Down.png"));
buttonLeft.setIcon(new ImageIcon("Arrows/Left.png")); buttonLeft.setIcon(new ImageIcon("Arrows/Left.png"));
buttonRight.setIcon(new ImageIcon("Arrows/Right.png")); buttonRight.setIcon(new ImageIcon("Arrows/Right.png"));
JButton buttonCreate = new JButton("Create"); JButton buttonCreateTanker = new JButton("Create Base Tanker");
buttonCreate.setBounds(60, Height - 120, 120, 50); buttonCreateTanker.setBounds(60, Height - 120, 200, 50);
buttonCreate.setLayout(null); buttonCreateTanker.setLayout(null);
add(buttonCreate); add(buttonCreateTanker);
buttonCreate.addActionListener(new ActionListener() { buttonCreateTanker.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { 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();
} }
}); });

29
DrawGasolineTanker.java Normal file
View File

@ -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);
}
}
}

41
DrawWheelCircle.java Normal file
View File

@ -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);}
}
}
}

View File

@ -1,6 +1,6 @@
import java.awt.*; import java.awt.*;
public class WheelsDrawing { public class DrawWheelClassic implements IWheelDraw{
private WheelCounter wheelCounter; private WheelCounter wheelCounter;
public WheelCounter getWheelCounter() public WheelCounter getWheelCounter()
{ {
@ -26,5 +26,6 @@ public class WheelsDrawing {
case THREE -> {g2d.fillOval(45 + _startPosX, 60 + _startPosY ,20, 20);} 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);} case FOUR -> {g2d.fillOval(30 + _startPosX, 60 + _startPosY ,20, 20); g2d.fillOval(60 + _startPosX, 60 + _startPosY ,20, 20);}
} }
} }
} }

42
DrawWheelSquare.java Normal file
View File

@ -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);}
}
}
}

27
DrawingObjectTanker.java Normal file
View File

@ -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); }
}

View File

@ -1,89 +1,101 @@
import java.awt.*; import java.awt.*;
public class DrawingTanker { public class DrawingTanker {
private BaseCar GasolineTanker; protected BaseTanker GasolineTanker;
private WheelsDrawing wheelsDrawing; public BaseTanker GetGasolineTanker() {return GasolineTanker;}
private int _pictureWidth; private IWheelDraw wheelsDrawing;
private int _pictureHeight; protected int _pictureWidth;
private int _startPosX; protected int _pictureHeight;
private int _startPosY; protected int _startPosX;
private final int _carWidth = 120; protected int _startPosY;
private final int _carHeight = 120; protected final int _carWidth = 110;
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, boolean bodyKit, boolean wing, boolean sportLine, int width, int height, int wheelCount) 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; _pictureHeight = height;
_pictureWidth = width; _pictureWidth = width;
GasolineTanker = new BaseCar(); GasolineTanker = new BaseTanker(speed, weight, bodyColor);
GasolineTanker.Init(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine); int mode = wheelMode % 3;
wheelsDrawing = new WheelsDrawing(); switch (mode)
{
case 0 -> {wheelsDrawing = new DrawWheelCircle();}
case 1 -> {wheelsDrawing = new DrawWheelSquare();}
case 2 -> {wheelsDrawing = new DrawWheelClassic();}
}
wheelsDrawing.setWheelCount(wheelCount); wheelsDrawing.setWheelCount(wheelCount);
} }
public void SetPosition(int x, int y) public void SetPosition(int x, int y)
{ {
_startPosX = x; _startPosX = x;
_startPosY = y; _startPosY = y;
} }
public void MoveTransport(Direction direction) public void MoveTransport(Direction direction)
{ {
if (GasolineTanker == null) if (!CanMove(direction) || GasolineTanker == null)
return; return;
switch (direction) switch (direction)
{ {
case Left -> { case Left:
if (_startPosX - GasolineTanker.Step > 0)
{ {
_startPosX -= (int)GasolineTanker.Step; _startPosX -= (int)GasolineTanker.Step;
} }
} break;
case Up -> { case Up:
if (_startPosY - GasolineTanker.Step > 0)
{ {
_startPosY -= (int)GasolineTanker.Step; _startPosY -= (int)GasolineTanker.Step;
} }
} break;
case Right -> { case Right:
if (_startPosX + _carWidth + GasolineTanker.Step < _pictureWidth)
{ {
_startPosX += (int)GasolineTanker.Step; _startPosX += (int)GasolineTanker.Step;
} }
} break;
case Down -> { case Down:
if (_startPosY + GasolineTanker.Step + _carHeight < _pictureHeight)
{ {
_startPosY += (int)GasolineTanker.Step; _startPosY += (int)GasolineTanker.Step;
}
break;
} }
} }
}
}
public void DrawTransport(Graphics g) public void DrawTransport(Graphics g)
{ {
if (GasolineTanker == null) if (GasolineTanker == null)
return; return;
var g2d = (Graphics2D) g; var g2d = (Graphics2D) g;
if (GasolineTanker.getBodyKit())
{
g2d.setColor(GasolineTanker.getAdditionalColor());
g2d.fillOval(10 + _startPosX, 10 + _startPosY, 70, 30);
}
g2d.setColor(GasolineTanker.getBodyColor()); g2d.setColor(GasolineTanker.getBodyColor());
// Отрисовка корпуса // Отрисовка корпуса
g2d.fillRect(10 + _startPosX, 40 + _startPosY, 90, 20); g2d.fillRect(10 + _startPosX, 40 + _startPosY, 90, 20);
g2d.fillRect(80 + _startPosX, 10 + _startPosY, 20, 40); g2d.fillRect(80 + _startPosX, 10 + _startPosY, 20, 40);
// Отрисовка колесиков // Отрисовка колесиков
wheelsDrawing.DrawWheels(_startPosX, _startPosY, GasolineTanker.getBodyColor(), g2d); 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);
}
} }
} }

20
GasolineTanker.java Normal file
View File

@ -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;
}
}

6
IMoveableObject.java Normal file
View File

@ -0,0 +1,6 @@
public interface IMoveableObject {
ObjectParameters GetObjectParameters();
int GetStep();
boolean CheckCanMove(Direction direction);
void MoveObject(Direction direction);
}

7
IWheelDraw.java Normal file
View File

@ -0,0 +1,7 @@
import java.awt.*;
public interface IWheelDraw {
void DrawWheels(int _startPosX, int _startPosY, Color bodyColor, Graphics2D g2d);
void setWheelCount(int wheelCount);
}

30
MoveToBorder.java Normal file
View File

@ -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();
}
}
}

35
MoveToCenter.java Normal file
View File

@ -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();
}
}
}
}

19
ObjectParameters.java Normal file
View File

@ -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;
}
}

14
Status.java Normal file
View File

@ -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;
}
}