Example code

This commit is contained in:
Philip Johansson 2023-09-12 22:17:12 +02:00
commit 3a54b6ca41
24 changed files with 407 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

BIN
.vs/RVC/v17/.futdcache.v2 Normal file

Binary file not shown.

BIN
.vs/RVC/v17/.suo Normal file

Binary file not shown.

25
RVC.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33110.190
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RVC", "RVC\RVC.csproj", "{5E87F4ED-4CB0-48C5-8377-73757507CBFE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5E87F4ED-4CB0-48C5-8377-73757507CBFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E87F4ED-4CB0-48C5-8377-73757507CBFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E87F4ED-4CB0-48C5-8377-73757507CBFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E87F4ED-4CB0-48C5-8377-73757507CBFE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5637E056-C171-4FEC-986C-1A874ABD5DC6}
EndGlobalSection
EndGlobal

2
RVC/Program.cs Normal file
View File

@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

157
RVC/RVC.cs Normal file
View File

@ -0,0 +1,157 @@
using System.Numerics;
namespace RVC
{
// The following logic applies.
// - The room has knowledge of its own size and what objects are in it
// - A Vacuum askes the room if it can be placed at a desired position
// - A vacuum can rotate unrestricted but has to ask the room to see if it can move
// - A Vacuum does not know its own position but has to ask the room for it
// - A Vacuum does know of its own rotation
internal class Floor
{
public Floor(float x, float y)
{
m_size = new(x, y);
}
private bool PositionIsOnFloor(Vector2 position)
{
// Bare in mind the size e.g. 3,3 of the floor means
// the positin range is within 0,0 and 2,2
return ((position.X < 0 || position.X >= m_size.X)
|| (position.Y < 0 || position.Y >= m_size.Y));
}
public bool PlaceRobot(Vacuum vacuum, Vector2 position)
{
if (!PositionIsOnFloor(position))
{
return false;
}
// Check if vacuum is already on the floor
if (m_vacuums.ContainsKey(vacuum))
{
return false;
}
// Check if the position is occupied
// (Could be extended with other objects other than Vacuums)
else if (m_vacuums.ContainsValue(position))
{
return false;
}
m_vacuums[vacuum] = position;
return true;
}
public bool MoveRobot(Vacuum vacuum, Vector2 relativeMove)
{
if (!m_vacuums.TryGetValue(vacuum, out var position))
{
return false;
}
var newPosition = position + relativeMove;
if (m_vacuums.ContainsValue(newPosition))
{
return false;
}
m_vacuums[vacuum] = newPosition;
return true;
}
public Vector2 GetRobotPosition(Vacuum vacuum) => m_vacuums[vacuum];
private Vector2 m_size;
private readonly Dictionary<Vacuum, Vector2> m_vacuums = new();
}
internal class Vacuum
{
public enum Movement
{
A,
W,
S,
D,
}
public enum Orientation
{
W,
N,
E,
S,
}
public Vacuum(Floor floor)
{
m_floor = floor;
}
public bool Placed { private set; get; } = false;
public bool Move(Movement move)
{
// Rotation is always allowed as it keeps the position
switch (move)
{
// Rotate CCW
case Movement.A:
m_orientation = new(-m_orientation.Y, m_orientation.X);
return true;
// Rotate CW
case Movement.D:
m_orientation = new(m_orientation.Y, -m_orientation.X);
return true;
// Forward
case Movement.W:
return m_floor.MoveRobot(this, m_orientation);
// Backward
case Movement.S:
return m_floor.MoveRobot(this, -m_orientation);
}
return false;
}
public bool Place(Vector2 position, Orientation orientation)
{
if (Placed)
{
// already placed
return false;
}
bool success = m_floor.PlaceRobot(this, position);
if (success)
{
Placed = true;
m_orientation = m_orientationToVector[orientation];
}
return success;
}
public Orientation GetOrientation() => m_orientationToVector.FirstOrDefault(x => x.Value == m_orientation).Key;
public (float, float) GetPosition()
{
var pos = m_floor.GetRobotPosition(this);
return (pos.X, pos.Y);
}
private Floor m_floor;
private Vector2 m_orientation;
private Dictionary<Orientation, Vector2> m_orientationToVector = new()
{
{ Orientation.W, new(-1, 0) },
{ Orientation.N, new(0, 1) },
{ Orientation.E, new(1, 0) },
{ Orientation.S, new(0, -1) },
};
}
}

10
RVC/RVC.csproj Normal file
View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,4 @@
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]

View File

@ -0,0 +1,23 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("RVC")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("RVC")]
[assembly: System.Reflection.AssemblyTitleAttribute("RVC")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generated by the MSBuild WriteCodeFragment class.

View File

@ -0,0 +1 @@
c22bea74c0dc332b4a3f050f9be278acf710d9f7

View File

@ -0,0 +1,11 @@
is_global = true
build_property.TargetFramework = net6.0
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
build_property.ProjectTypeGuids =
build_property.InvariantGlobalization =
build_property.PlatformNeutralAssembly =
build_property.EnforceExtendedAnalyzerRules =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = RVC
build_property.ProjectDir = D:\Git\RVC-To-Jonas\RVC\

View File

@ -0,0 +1,8 @@
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;

Binary file not shown.

View File

@ -0,0 +1,67 @@
{
"format": 1,
"restore": {
"D:\\Git\\RVC-To-Jonas\\RVC\\RVC.csproj": {}
},
"projects": {
"D:\\Git\\RVC-To-Jonas\\RVC\\RVC.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "D:\\Git\\RVC-To-Jonas\\RVC\\RVC.csproj",
"projectName": "RVC",
"projectPath": "D:\\Git\\RVC-To-Jonas\\RVC\\RVC.csproj",
"packagesPath": "C:\\Users\\phils\\.nuget\\packages\\",
"outputPath": "D:\\Git\\RVC-To-Jonas\\RVC\\obj\\",
"projectStyle": "PackageReference",
"fallbackFolders": [
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"
],
"configFilePaths": [
"C:\\Users\\phils\\AppData\\Roaming\\NuGet\\NuGet.Config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
],
"originalTargetFrameworks": [
"net6.0"
],
"sources": {
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\7.0.100\\RuntimeIdentifierGraph.json"
}
}
}
}
}

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\phils\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.4.0</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="C:\Users\phils\.nuget\packages\" />
<SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />

View File

@ -0,0 +1,73 @@
{
"version": 3,
"targets": {
"net6.0": {}
},
"libraries": {},
"projectFileDependencyGroups": {
"net6.0": []
},
"packageFolders": {
"C:\\Users\\phils\\.nuget\\packages\\": {},
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages": {}
},
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "D:\\Git\\RVC-To-Jonas\\RVC\\RVC.csproj",
"projectName": "RVC",
"projectPath": "D:\\Git\\RVC-To-Jonas\\RVC\\RVC.csproj",
"packagesPath": "C:\\Users\\phils\\.nuget\\packages\\",
"outputPath": "D:\\Git\\RVC-To-Jonas\\RVC\\obj\\",
"projectStyle": "PackageReference",
"fallbackFolders": [
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"
],
"configFilePaths": [
"C:\\Users\\phils\\AppData\\Roaming\\NuGet\\NuGet.Config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
],
"originalTargetFrameworks": [
"net6.0"
],
"sources": {
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\7.0.100\\RuntimeIdentifierGraph.json"
}
}
}
}

View File

@ -0,0 +1,8 @@
{
"version": 2,
"dgSpecHash": "HoCMqrNdHWTpH0gkmHKbhGesSJe7u8yg4xt7cA65NcYzrlXDPBRXl/C6eUFwzKFOYPcm7QJO2Lt4sePrwsBLew==",
"success": true,
"projectFilePath": "D:\\Git\\RVC-To-Jonas\\RVC\\RVC.csproj",
"expectedPackageFiles": [],
"logs": []
}