1
0

fix: add comments to "Recursion.LD_24"

This commit is contained in:
Rokas Puzonas 2022-02-20 23:43:57 +02:00
parent 633c91bb72
commit 8d86e85a6a
13 changed files with 241 additions and 90 deletions

View File

@ -5,10 +5,22 @@ using System.Web;
namespace LD_24.Code namespace LD_24.Code
{ {
/// <summary>
/// Used for keeping the result of path finding
/// </summary>
public class BestPizzeriaResult public class BestPizzeriaResult
{ {
/// <summary>
/// Pizzeria to which every friend will go
/// </summary>
public Point Pizzeria { get; private set; } public Point Pizzeria { get; private set; }
/// <summary>
/// Meeting spot for friends
/// </summary>
public Point MeetingSpot { get; private set; } public Point MeetingSpot { get; private set; }
/// <summary>
/// The turn number of steps that will be taken between all friends
/// </summary>
public int Cost { get; private set; } public int Cost { get; private set; }
public BestPizzeriaResult(Point pizzeria, Point meetingSpot, int cost) public BestPizzeriaResult(Point pizzeria, Point meetingSpot, int cost)
@ -17,5 +29,10 @@ namespace LD_24.Code
MeetingSpot = meetingSpot; MeetingSpot = meetingSpot;
Cost = cost; Cost = cost;
} }
public override string ToString()
{
return String.Format("BestPizzeriaResult{Pizzeria = {0}, MeetingSpot = {1}, Cost = {2}}", Pizzeria, MeetingSpot, Cost);
}
} }
} }

View File

@ -6,8 +6,17 @@ using System.Web;
namespace LD_24.Code namespace LD_24.Code
{ {
public class InOutUtils /// <summary>
/// Functions related to working with files
/// </summary>
public static class InOutUtils
{ {
/// <summary>
/// Read a map from a file
/// </summary>
/// <param name="filename">Target file</param>
/// <returns>A map loaded from the file</returns>
/// <exception cref="Exception">If there was an invalid tile</exception>
public static Map ReadMap(string filename) public static Map ReadMap(string filename)
{ {
string[] lines = File.ReadAllLines(filename); string[] lines = File.ReadAllLines(filename);
@ -42,6 +51,11 @@ namespace LD_24.Code
return map; return map;
} }
/// <summary>
/// Write a map to a file
/// </summary>
/// <param name="writer">Target file writer</param>
/// <param name="map">Target map</param>
public static void WriteMap(StreamWriter writer, Map map) public static void WriteMap(StreamWriter writer, Map map)
{ {
for (int y = 0; y < map.Height; y++) for (int y = 0; y < map.Height; y++)
@ -74,6 +88,11 @@ namespace LD_24.Code
} }
} }
/// <summary>
/// Write out best pizzeria result to file
/// </summary>
/// <param name="writer">Target file</param>
/// <param name="result">Target result</param>
public static void WriteBestPizzeriaResult(StreamWriter writer, BestPizzeriaResult result) public static void WriteBestPizzeriaResult(StreamWriter writer, BestPizzeriaResult result)
{ {
if (result == null) if (result == null)
@ -88,6 +107,11 @@ namespace LD_24.Code
} }
} }
/// <summary>
/// Write out friend positions to file
/// </summary>
/// <param name="writer">Target file</param>
/// <param name="friends">Target friends list</param>
public static void WriteFriendPositions(StreamWriter writer, List<Point> friends) public static void WriteFriendPositions(StreamWriter writer, List<Point> friends)
{ {
foreach (var friend in friends) foreach (var friend in friends)
@ -96,4 +120,4 @@ namespace LD_24.Code
} }
} }
} }
} }

View File

@ -1,16 +1,30 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LD_24.Code namespace LD_24.Code
{ {
/// <summary>
/// Class used to store a single map
/// </summary>
public class Map public class Map
{ {
/// <summary>
/// Tile map
/// </summary>
MapTile[,] data; MapTile[,] data;
/// <summary>
/// Map width
/// </summary>
public int Width { get; set; } public int Width { get; set; }
/// <summary>
/// Map height
/// </summary>
public int Height { get; set; } public int Height { get; set; }
/// <summary>
/// Create an empty map
/// </summary>
/// <param name="width">Target width</param>
/// <param name="height">Target height</param>
public Map(int width, int height) public Map(int width, int height)
{ {
data = new MapTile[width, height]; data = new MapTile[width, height];
@ -18,21 +32,44 @@ namespace LD_24.Code
Height = height; Height = height;
} }
/// <summary>
/// Change a single tile in map
/// </summary>
/// <param name="x">Target x</param>
/// <param name="y">Target y</param>
/// <param name="tile">Target tile</param>
public void Set(int x, int y, MapTile tile) public void Set(int x, int y, MapTile tile)
{ {
data[x, y] = tile; data[x, y] = tile;
} }
/// <summary>
/// Retrieve a single tile from map
/// </summary>
/// <param name="x">Target x</param>
/// <param name="y">Target y</param>
/// <returns>Tile at target position</returns>
public MapTile Get(int x, int y) public MapTile Get(int x, int y)
{ {
return data[x, y]; return data[x, y];
} }
/// <summary>
/// Check if a position is whithin the bounds of the map
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public bool IsInBounds(int x, int y) public bool IsInBounds(int x, int y)
{ {
return x >= 0 && x < Width && y >= 0 && y < Height; return x >= 0 && x < Width && y >= 0 && y < Height;
} }
/// <summary>
/// Find all positions of a certain tile type
/// </summary>
/// <param name="tile"></param>
/// <returns></returns>
public List<Point> FindAll(MapTile tile) public List<Point> FindAll(MapTile tile)
{ {
List<Point> points = new List<Point>(); List<Point> points = new List<Point>();
@ -48,5 +85,10 @@ namespace LD_24.Code
} }
return points; return points;
} }
public override string ToString()
{
return string.Format("Map{Width = {0}, Height = {1}}", Width, Height);
}
} }
} }

View File

@ -5,6 +5,9 @@ using System.Web;
namespace LD_24.Code namespace LD_24.Code
{ {
/// <summary>
/// Enum for storing the type of each tile in a map
/// </summary>
public enum MapTile public enum MapTile
{ {
Empty, Empty,
@ -13,4 +16,4 @@ namespace LD_24.Code
Pizzeria, Pizzeria,
MeetingSpot MeetingSpot
} }
} }

View File

@ -5,9 +5,18 @@ using System.Web;
namespace LD_24.Code namespace LD_24.Code
{ {
/// <summary>
/// A simple point class for storing x and y together.
/// </summary>
public class Point public class Point
{ {
/// <summary>
/// The x component
/// </summary>
public int X { get; set; } public int X { get; set; }
/// <summary>
/// The y component
/// </summary>
public int Y { get; set; } public int Y { get; set; }
public Point(int x, int y) public Point(int x, int y)
@ -16,6 +25,11 @@ namespace LD_24.Code
Y = y; Y = y;
} }
/// <summary>
/// Check if 2 points are the same
/// </summary>
/// <param name="obj">Other point</param>
/// <returns>Are the x and y the same</returns>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
return obj is Point point && return obj is Point point &&
@ -31,5 +45,9 @@ namespace LD_24.Code
return hashCode; return hashCode;
} }
public override string ToString()
{
return String.Format("Point({0}, {1})", X, Y);
}
} }
} }

View File

@ -6,7 +6,7 @@ using System.Web;
namespace LD_24.Code namespace LD_24.Code
{ {
public class TaskUtils public static class TaskUtils
{ {
public static BestPizzeriaResult FindBestPizzeria(Map map) public static BestPizzeriaResult FindBestPizzeria(Map map)
{ {
@ -112,4 +112,4 @@ namespace LD_24.Code
} }
} }
} }
} }

View File

@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LD_24
{
public partial class Forma1 : System.Web.UI.Page
{
/// <summary>
/// Put target friends into a bulleted list element
/// </summary>
/// <param name="list">Target list</param>
/// <param name="friends">Target friends</param>
private void ShowFriends(BulletedList list, List<Code.Point> friends)
{
list.Items.Clear();
foreach (var friend in friends)
{
list.Items.Add(String.Format("X = {0}, Y = {1}", friend.X + 1, friend.Y + 1));
}
}
/// <summary>
/// Put best pizzeria result into a label
/// </summary>
/// <param name="label">Target label</param>
/// <param name="result">Target result</param>
private void ShowBestPizzeriaResult(Label label, Code.BestPizzeriaResult result)
{
if (result == null)
{
label.Text = "Neįmanoma";
return;
}
label.Text = String.Format("Susitikimo vieta (X = {0}, Y = {1})", result.MeetingSpot.X + 1, result.MeetingSpot.Y + 1);
label.Text += "<br />";
label.Text += String.Format("Picerija (X = {0}, Y = {1})", result.Pizzeria.X + 1, result.Pizzeria.Y + 1);
label.Text += "<br />";
label.Text += String.Format("Nueita {0}", result.Cost);
}
/// <summary>
/// Put a maps tiles into a table
/// </summary>
/// <param name="table">Target table</param>
/// <param name="map">Target map</param>
private void ShowMap(Table table, Code.Map map)
{
table.Rows.Clear();
for (int y = 0; y < map.Height; y++)
{
TableRow row = new TableRow();
for (int x = 0; x < map.Width; x++)
{
TableCell cell = new TableCell();
cell.Width = 20;
cell.Height = 20;
switch (map.Get(x, y))
{
case Code.MapTile.Empty:
cell.Text = ".";
break;
case Code.MapTile.Pizzeria:
cell.Text = "P";
break;
case Code.MapTile.Friend:
cell.Text = "D";
break;
case Code.MapTile.MeetingSpot:
cell.Text = "S";
break;
case Code.MapTile.Wall:
cell.Text = "X";
break;
default:
cell.Text = "?";
break;
}
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
}
}
}

View File

@ -8,12 +8,18 @@ using System.Web.UI.WebControls;
namespace LD_24 namespace LD_24
{ {
public partial class Forma1 : System.Web.UI.Page public partial class Forma1 : System.Web.UI.Page
{ {
private string inputFilename; private string inputFilename;
private string outputFilename; private string outputFilename;
private Code.Map map; private Code.Map map;
/// <summary>
/// Load initial map and show it in the interface
/// </summary>
/// <param name="sender">Sender element</param>
/// <param name="e">Event</param>
protected void Page_Load(object sender, EventArgs e) protected void Page_Load(object sender, EventArgs e)
{ {
inputFilename = Server.MapPath(@"App_Data/U3.txt"); inputFilename = Server.MapPath(@"App_Data/U3.txt");
@ -22,7 +28,13 @@ namespace LD_24
map = Code.InOutUtils.ReadMap(inputFilename); map = Code.InOutUtils.ReadMap(inputFilename);
ShowMap(Table1, map); ShowMap(Table1, map);
} }
/// <summary>
/// Find which is the best pizzeria, show it in the interface and output the result
/// to a file
/// </summary>
/// <param name="sender">Sender element</param>
/// <param name="e">Event</param>
protected void Button1_Click(object sender, EventArgs e) protected void Button1_Click(object sender, EventArgs e)
{ {
List<Code.Point> friends = map.FindAll(Code.MapTile.Friend); List<Code.Point> friends = map.FindAll(Code.MapTile.Friend);
@ -43,67 +55,5 @@ namespace LD_24
Code.InOutUtils.WriteBestPizzeriaResult(writer, result); Code.InOutUtils.WriteBestPizzeriaResult(writer, result);
} }
} }
private void ShowFriends(BulletedList list, List<Code.Point> friends)
{
list.Items.Clear();
foreach (var friend in friends)
{
list.Items.Add(String.Format("X = {0}, Y = {1}", friend.X + 1, friend.Y + 1));
}
}
private void ShowBestPizzeriaResult(Label label, Code.BestPizzeriaResult result)
{
if (result == null)
{
label.Text = "Neįmanoma";
return;
}
label.Text = String.Format("Susitikimo vieta (X = {0}, Y = {1})", result.MeetingSpot.X + 1, result.MeetingSpot.Y + 1);
label.Text += "<br />";
label.Text += String.Format("Picerija (X = {0}, Y = {1})", result.Pizzeria.X + 1, result.Pizzeria.Y + 1);
label.Text += "<br />";
label.Text += String.Format("Nueita {0}", result.Cost);
}
private void ShowMap(Table table, Code.Map map)
{
table.Rows.Clear();
for (int y = 0; y < map.Height; y++)
{
TableRow row = new TableRow();
for (int x = 0; x < map.Width; x++)
{
TableCell cell = new TableCell();
cell.Width = 20;
cell.Height = 20;
switch (map.Get(x, y))
{
case Code.MapTile.Empty:
cell.Text = ".";
break;
case Code.MapTile.Pizzeria:
cell.Text = "P";
break;
case Code.MapTile.Friend:
cell.Text = "D";
break;
case Code.MapTile.MeetingSpot:
cell.Text = "S";
break;
case Code.MapTile.Wall:
cell.Text = "X";
break;
default:
cell.Text = "?";
break;
}
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
}
} }
} }

View File

@ -7,13 +7,13 @@
<ProductVersion> <ProductVersion>
</ProductVersion> </ProductVersion>
<SchemaVersion>2.0</SchemaVersion> <SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{6AAB1A29-81E1-4A08-A8C3-9C203C684B8F}</ProjectGuid> <ProjectGuid>{4E9466EF-CDF4-4620-B51E-125F5A014A04}</ProjectGuid>
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids> <ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>LD_24</RootNamespace> <RootNamespace>LD_24</RootNamespace>
<AssemblyName>LD_24</AssemblyName> <AssemblyName>LD_24</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion> <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<UseIISExpress>true</UseIISExpress> <UseIISExpress>true</UseIISExpress>
<Use64BitIISExpress /> <Use64BitIISExpress />
<IISExpressSSLPort> <IISExpressSSLPort>
@ -71,6 +71,11 @@
<Content Include="App_Data\Rezultatai.txt" /> <Content Include="App_Data\Rezultatai.txt" />
<Content Include="App_Data\U3.txt" /> <Content Include="App_Data\U3.txt" />
<Content Include="Forma1.aspx" /> <Content Include="Forma1.aspx" />
<Content Include="interface-scheme.png" />
<Content Include="tests\1\inputs\U3.txt" />
<Content Include="tests\1\outputs\Rezultatai.txt" />
<Content Include="tests\2\inputs\U3.txt" />
<Content Include="tests\2\outputs\Rezultatai.txt" />
<Content Include="Web.config" /> <Content Include="Web.config" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -80,6 +85,9 @@
<Compile Include="Code\MapTile.cs" /> <Compile Include="Code\MapTile.cs" />
<Compile Include="Code\Point.cs" /> <Compile Include="Code\Point.cs" />
<Compile Include="Code\TaskUtils.cs" /> <Compile Include="Code\TaskUtils.cs" />
<Compile Include="Forma1-helper.aspx.cs">
<SubType>ASPXCodeBehind</SubType>
</Compile>
<Compile Include="Forma1.aspx.cs"> <Compile Include="Forma1.aspx.cs">
<DependentUpon>Forma1.aspx</DependentUpon> <DependentUpon>Forma1.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType> <SubType>ASPXCodeBehind</SubType>
@ -98,7 +106,6 @@
<DependentUpon>Web.config</DependentUpon> <DependentUpon>Web.config</DependentUpon>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup />
<PropertyGroup> <PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
@ -112,9 +119,9 @@
<WebProjectProperties> <WebProjectProperties>
<UseIIS>True</UseIIS> <UseIIS>True</UseIIS>
<AutoAssignPort>True</AutoAssignPort> <AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>57614</DevelopmentServerPort> <DevelopmentServerPort>51983</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath> <DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>http://localhost:44363/</IISUrl> <IISUrl>http://localhost:44364/</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication> <NTLMAuthentication>False</NTLMAuthentication>
<UseCustomServer>False</UseCustomServer> <UseCustomServer>False</UseCustomServer>
<CustomServerUrl> <CustomServerUrl>

View File

@ -20,7 +20,7 @@ using System.Runtime.InteropServices;
[assembly: ComVisible(false)] [assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM // The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("6aab1a29-81e1-4a08-a8c3-9c203c684b8f")] [assembly: Guid("4e9466ef-cdf4-4620-b51e-125f5a014a04")]
// Version information for an assembly consists of the following four values: // Version information for an assembly consists of the following four values:
// //

View File

@ -5,8 +5,8 @@
--> -->
<configuration> <configuration>
<system.web> <system.web>
<compilation debug="true" targetFramework="4.7.2" /> <compilation debug="true" targetFramework="4.8" />
<httpRuntime targetFramework="4.7.2" /> <httpRuntime targetFramework="4.8" />
<globalization fileEncoding="utf-8" /> <globalization fileEncoding="utf-8" />
</system.web> </system.web>
<system.codedom> <system.codedom>
@ -15,4 +15,4 @@
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
</compilers> </compilers>
</system.codedom> </system.codedom>
</configuration> </configuration>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.1" targetFramework="net472" /> <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.1" targetFramework="net48" />
</packages> </packages>

View File

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.1.32210.238 VisualStudioVersion = 17.1.32210.238
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LD_24", "LD_24\LD_24.csproj", "{6AAB1A29-81E1-4A08-A8C3-9C203C684B8F}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LD_24", "LD_24\LD_24.csproj", "{4E9466EF-CDF4-4620-B51E-125F5A014A04}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -11,10 +11,10 @@ Global
Release|Any CPU = Release|Any CPU Release|Any CPU = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6AAB1A29-81E1-4A08-A8C3-9C203C684B8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {4E9466EF-CDF4-4620-B51E-125F5A014A04}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6AAB1A29-81E1-4A08-A8C3-9C203C684B8F}.Debug|Any CPU.Build.0 = Debug|Any CPU {4E9466EF-CDF4-4620-B51E-125F5A014A04}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6AAB1A29-81E1-4A08-A8C3-9C203C684B8F}.Release|Any CPU.ActiveCfg = Release|Any CPU {4E9466EF-CDF4-4620-B51E-125F5A014A04}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6AAB1A29-81E1-4A08-A8C3-9C203C684B8F}.Release|Any CPU.Build.0 = Release|Any CPU {4E9466EF-CDF4-4620-B51E-125F5A014A04}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE