Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Adapters/MovableAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using SpaceButtleHomeTask2.Interfaces;
using System.Drawing;

namespace SpaceButtleHomeTask2
{
public class IAdapter : IAdapter
{
private readonly IMovable _movement;

public MovableAdapter(IMovable movement)
{
_movement = movement;
}

public Point Execute(Point oldPosition, Direction<Point> newPosition)
=> _movement.MoveLinearMotion(oldPosition, newPosition.Value);
}

}
18 changes: 18 additions & 0 deletions Adapters/RotatableAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using SpaceButtleHomeTask2.Interfaces;
using System.Drawing;

namespace SpaceButtleHomeTask2
{
public class RotatableAdapter : IAdapter
{
IRotatble _rotation;
public RotatableAdapter(IRotatble rotation)
{
_rotation = rotation;
}


public Point Execute(Point oldPosition, Direction<int> direction)
=> _rotation.Rotate(oldPosition, direction.Value);
}
}
7 changes: 7 additions & 0 deletions Direction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SpaceButtleHomeTask2
{
public class Direction<T>
{
public T Value { get; set; } = default!;
}
}
9 changes: 9 additions & 0 deletions Interfaces/IAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Drawing;

namespace SpaceButtleHomeTask2.Interfaces
{
public interface IAdapter
{
Point Execute<TDirection>(Point oldValue, Direction<TDirection> direction);
}
}
9 changes: 9 additions & 0 deletions Interfaces/IMovable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Drawing;

namespace SpaceButtleHomeTask2.Interfaces
{
public interface IMovable
{
Point MoveLinearMotion(Point oldPosition, Point position);
}
}
11 changes: 11 additions & 0 deletions Interfaces/IRotatble.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Drawing;

namespace SpaceButtleHomeTask2.Interfaces
{
public interface IRotatble
{
Point Rotate(Point oldPosition, int direction);
}
}


11 changes: 11 additions & 0 deletions Interfaces/ISpaceship.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Drawing;

namespace SpaceButtleHomeTask2.Interfaces
{
public interface ISpaceship
{
void Move(Point val);
void Rotate(int direction);
Point GetCurrentPosition();
}
}
16 changes: 16 additions & 0 deletions Motion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using SpaceButtleHomeTask2.Interfaces;
using System.Drawing;

namespace SpaceButtleHomeTask2
{
public class Motion : IMovable
{
public Point MoveLinearMotion(Point oldPosition, Point position)
{
var x = position.X + oldPosition.X;
var y = position.Y + oldPosition.X;

return new Point(x, y);
}
}
}
15 changes: 15 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using SpaceButtleHomeTask2;
using System.Drawing;

var directionNumber = 8;
var velocity = 0;


var movableAdapter = new MovableAdapter(new Motion());
var rotatbleAdapter = new RotatableAdapter(new Rotation(velocity, directionNumber));


var spaceShip = new Spaceship(movableAdapter, rotatbleAdapter);
spaceShip.Move(new Point(-7, 3));
spaceShip.Rotate(5);

25 changes: 25 additions & 0 deletions Rotation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using SpaceButtleHomeTask2.Interfaces;
using System.Drawing;

namespace SpaceButtleHomeTask2
{
public class Rotation : IRotatble
{
private readonly int _velocity;
private readonly int _directionNumber;

public Rotation(int velocity, int directionNumber )
{
_velocity = velocity;
_directionNumber = directionNumber;
}

public Point Rotate(Point oldPosition, int direction)
{
var x = oldPosition.X + _velocity * Math.Cos(direction / 360 * _directionNumber);
var y = oldPosition.Y + _velocity * Math.Cos(direction / 360 * _directionNumber);

return new Point((int)x, (int)y);
}
}
}
20 changes: 20 additions & 0 deletions SpaceButtleHomeTask2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0-preview-20220301-01" />
<PackageReference Include="Moq" Version="4.17.2" />
<PackageReference Include="xunit" Version="2.4.2-pre.12" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions SpaceButtleHomeTask2.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32203.90
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpaceButtleHomeTask2", "SpaceButtleHomeTask2.csproj", "{7D0F9C9E-139A-4CAA-B2DA-93ACF5C8B780}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7D0F9C9E-139A-4CAA-B2DA-93ACF5C8B780}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7D0F9C9E-139A-4CAA-B2DA-93ACF5C8B780}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D0F9C9E-139A-4CAA-B2DA-93ACF5C8B780}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D0F9C9E-139A-4CAA-B2DA-93ACF5C8B780}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8594A1EC-E8E1-4C5C-B617-8962E69064DC}
EndGlobalSection
EndGlobal
48 changes: 48 additions & 0 deletions Spaceship.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using SpaceButtleHomeTask2.Interfaces;
using System.Drawing;

namespace SpaceButtleHomeTask2
{
public class Spaceship: ISpaceship
{
private Point _currentPosition;

private readonly IAdapter _movableAdapter;
private readonly IAdapter _rotatableAdapter;
public Spaceship(IAdapter movableAdapter, IAdapter rotatableAdapter, Point? currentPosition = null)
{
_movableAdapter = movableAdapter;
_rotatableAdapter = rotatableAdapter;

if (currentPosition != null)
_currentPosition = currentPosition.Value;

_currentPosition = new Point(0, 0);
}

public void Move(Point p)
{
var currentPosition = _movableAdapter.Execute(GetCurrentPosition(), new Direction<Point>()
{
Value = p
});
_currentPosition = currentPosition;
}


public void Rotate(int direction)
{
var currentPosition = _rotatableAdapter.Execute(GetCurrentPosition(), , new Direction<int>()
{
Value = direction
});

_currentPosition = currentPosition;

}

public Point GetCurrentPosition()
=> _currentPosition;

}
}
34 changes: 34 additions & 0 deletions Tests/SpaceshipTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Moq;
using SpaceButtleHomeTask2.Interfaces;
using System.Drawing;
using Xunit;

namespace SpaceButtleHomeTask2.Tests
{
public class SpaceshipTests
{

[Fact]
[Trait("Category", "Unit")]
public void Test_Spaceship_Move_Should_Return_Ok()
{
var currentPosition = new Point(12, 5);
var newPosition = new Point(-7, 3);

var expectedPoint = new Point(5, 8);

var movableAdapterMock = new Mock<IAdapter>();
var rotableAdapterMock = new Mock<IAdapter>();

var spaceship = new Spaceship(movableAdapterMock.Object, rotableAdapterMock.Object);

spaceship.Move(currentPosition);
spaceship.Move(newPosition);
var position = spaceship.GetCurrentPosition();

Assert.Equal(expectedPoint, position);


}
}
}