using ElectricLocomotive; namespace ElectricLocomotive { public partial class MainForm : Form { private DrawingLocomotiv? _drawingLocomotiv; private AbstractStrategy _abstractStrategy; public MainForm() { InitializeComponent(); } private void Draw() { if (_drawingLocomotiv == null) return; Bitmap bmp = new(locoBox.Width, locoBox.Height); Graphics g = Graphics.FromImage(bmp); _drawingLocomotiv.DrawLoco(g); locoBox.Image = bmp; } private void PaintLocoButton_click(object sender, EventArgs e) { Random random = new(); _drawingLocomotiv = new DrawingLocomotiv(random.Next(100, 300), random.Next(1000, 3000), locoBox.Width, locoBox.Height); _drawingLocomotiv.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } private void PaintElectricLocoButton_click(object sender, EventArgs e) { Random random = new(); _drawingLocomotiv = new DrawingElectricLocomotiv(random.Next(100, 300), random.Next(1000, 3000), locoBox.Width, locoBox.Height); _drawingLocomotiv.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } private void MoveSideButton_Click(object sender, EventArgs e) { if (_drawingLocomotiv == null) return; Control button = sender as Control; string name = button.Name; switch (name) { case "moveUpButton": _drawingLocomotiv.MoveTransport(DirectionType.Up); break; case "moveDownButton": _drawingLocomotiv.MoveTransport(DirectionType.Down); break; case "moveLeftButton": _drawingLocomotiv.MoveTransport(DirectionType.Left); break; case "moveRightButton": _drawingLocomotiv.MoveTransport(DirectionType.Right); break; } Draw(); } private void MoveButton_Click(object sender, EventArgs e) { if (_drawingLocomotiv == null) return; if (movesBox.Enabled) { _abstractStrategy = movesBox.SelectedIndex switch { 0 => new MoveToEdge(), 1 => new MoveToCenter(), _ => null, } ; if (_abstractStrategy == null) { return; } _abstractStrategy.SetData(new DrawingObjectLocomotiv(_drawingLocomotiv), locoBox.Width, locoBox.Height); movesBox.Enabled = false; } if (_abstractStrategy == null) { return; } _abstractStrategy.MakeStep(); Draw(); if (_abstractStrategy.GetStatus() == Status.Finish) { movesBox.Enabled = true; _abstractStrategy = null; } } } }