Ver código fonte

removed some unused methods

added doc-comments to all methods
feature-parse-setup-files
benjamin.m 5 anos atrás
pai
commit
265a8ff0b8
3 arquivos alterados com 38 adições e 47 exclusões
  1. 15
    44
      Buhnenrennen/Field.cs
  2. 16
    3
      Buhnenrennen/Groyne.cs
  3. 7
    0
      Buhnenrennen/GroyneRun.cs

+ 15
- 44
Buhnenrennen/Field.cs Ver arquivo

@@ -11,7 +11,6 @@ namespace Buhnenrennen
class Field
{
public Dictionary<double, List<Groyne>> Groynes { get; private set; }

Regex groynSchema = new Regex( "(?<type>[A-Z]) (?<level>[0-9]+) (?<yCoord>[0-9\\.]+)", RegexOptions.IgnoreCase );

private Field( IEnumerable<string> groynes )
@@ -32,52 +31,19 @@ namespace Buhnenrennen
}
}

/**
* <summary>returns a field object loaded from entries out of File at {filename}</summary>
* <param name="filename">Location of the file to read</param>
*/
public static Field fromFile( string filename )
{
return new Field( File.ReadAllLines( filename ) );
}

public Groyne[] findShortestPath( Dog dog )
{
List<Groyne> poles = new List<Groyne>();
Groyne start = Groynes[ 0 ].Find( groyne => groyne.Type == dog.Start );
if ( start == null ) throw new Exception( "No start position found" );
poles.Add( start );

Groyne previous = start;
for( int level = 70; level <= Groynes.Keys.Max(); level += 70 )
{
List<Groyne> candidates = Groynes[ level ].Where( groyne => groyne.canPass( dog ) ).ToList();
var distances = candidates.Select( groyne => groyne.distanceTo( previous ) ).ToList();
var minDistance = distances.Min();
previous = candidates[ distances.IndexOf( minDistance ) ];
poles.Add( previous );
}

return poles.ToArray();
}

public TimedGroyne findFastestPath( Dog dog )
{
Groyne start = Groynes[ 0 ].Find( groyne => groyne.Type == dog.Start );
if ( start == null ) throw new Exception( "No start position found" );

Groyne lastLevel = start;
Dictionary<int, List<TimedGroyne>> times = new Dictionary<int, List<TimedGroyne>>();
times.Add( 0, new TimedGroyne[] { new TimedGroyne( null, start, 0 ) }.ToList() );
for ( int level = 70; level <= Groynes.Keys.Max(); level += 70 )
{
List<TimedGroyne> subTimes = new List<TimedGroyne>();
List<TimedGroyne> passables = times[ level - 70 ].Where( time => time.Groyne.canPass( dog ) ).ToList();
foreach ( Groyne groyne in Groynes[ level ] )
{
List<TimedGroyne> possibleTimes = passables.Select( passable => new TimedGroyne( passable, groyne, passable.TotalTime + groyne.requiredTimeTo( passable.Groyne, dog ) ) ).ToList();
subTimes.Add( possibleTimes.OrderBy( time => time.TotalTime ).First() );
}
times.Add( level, subTimes );
}
return times[ 3 ].OrderBy( time => time.TotalTime ).First();
}
/**
* <summary>returns a dictionary with groynes and the fastest times the give dog is able to reach them</summary>
* <param name="dog">dog that is running to the groynes</param>
*/

public Dictionary<double, List<TimedGroyne>> listFastestTimes( Dog dog )
{
@@ -90,10 +56,10 @@ namespace Buhnenrennen
for ( int level = 70; level <= Groynes.Keys.Max(); level += 70 )
{
List<TimedGroyne> subTimes = new List<TimedGroyne>();
List<TimedGroyne> passables = times[ level - 70 ].Where( time => time.Groyne.canPass( dog ) ).ToList();
List<TimedGroyne> passables = times[ level - 70 ].Where( time => time.Groyne.CanPass( dog ) ).ToList();
foreach ( Groyne groyne in Groynes[ level ] )
{
List<TimedGroyne> possibleTimes = passables.Select( passable => new TimedGroyne( passable, groyne, passable.TotalTime + groyne.requiredTimeTo( passable.Groyne, dog ) ) ).ToList();
List<TimedGroyne> possibleTimes = passables.Select( passable => new TimedGroyne( passable, groyne, passable.TotalTime + groyne.RequiredTimeTo( passable.Groyne, dog ) ) ).ToList();
subTimes.Add( possibleTimes.OrderBy( time => time.TotalTime ).FirstOrDefault() );
}
times.Add( level, subTimes );
@@ -101,6 +67,11 @@ namespace Buhnenrennen
return times;
}

/**
* <summary>returns the fastest route for {hunted} where {hunted} will always be faster than {hunter}</summary>
* <param name="hunted">The dog, which will be hunted and is not to be catched ( Minnie )</param>
* <param name="hunter">The dog, with the desire to hunt the {hunted} dog ( Max )</param>
*/
public TimedGroyne getFastestSafeRoute( Dog hunter, Dog hunted )
{
var hunterMap = listFastestTimes( hunter );

+ 16
- 3
Buhnenrennen/Groyne.cs Ver arquivo

@@ -17,15 +17,28 @@ namespace Buhnenrennen
this.Type = type;
}

public bool canPass( Dog dog ) => dog.FitsThrough.Contains( Type );
/// <summary>
/// returns if given dog fits through the groyne
/// </summary>
/// <param name="dog">the dog, that wants to fit through</param>
public bool CanPass( Dog dog ) => dog.FitsThrough.Contains( Type );

public double distanceTo( Groyne groyne ) =>
/// <summary>
/// returns the distance between called and given groyne
/// </summary>
/// <param name="groyne">groyne to calculate the distance to</param>
public double DistanceTo( Groyne groyne ) =>
// distance: distance² = A² + B²
// A: 70
// B: difference of Y-Coordinates
Math.Sqrt( Math.Pow( 70, 2 ) + Math.Pow( YCoord - groyne.YCoord, 2 ) );

public double requiredTimeTo( Groyne groyne, Dog dog ) => distanceTo( groyne ) / dog.MeterPerSecond;
/// <summary>
/// returns the time the given {dog} needs to get to given {groyne} starting at current groyne
/// </summary>
/// <param name="groyne">groyne the dog is running to</param>
/// <param name="dog">the dog, thats running to the groyne</param>
public double RequiredTimeTo( Groyne groyne, Dog dog ) => DistanceTo( groyne ) / dog.MeterPerSecond;

public override string ToString() => Type + "( " + YCoord + " )";
}

+ 7
- 0
Buhnenrennen/GroyneRun.cs Ver arquivo

@@ -12,6 +12,10 @@ namespace Buhnenrennen
{
Regex setupFileRegex = new Regex( "^buhnenrennen(?<number>[0-9]+).txt$", RegexOptions.IgnoreCase );
Regex intFormatRegex = new Regex( "^[0-9]+$", RegexOptions.IgnoreCase );

/// <summary>
/// asks the user to enter the number of a setup file and loads the given file
/// </summary>
private Field selectField()
{
var filenumbers_int = Directory.GetFiles( "setups", "buhnenrennen*.txt" )
@@ -41,6 +45,9 @@ namespace Buhnenrennen
return Field.fromFile( "setups/buhnenrennen" + number + ".txt" );
}

/// <summary>
/// loads the field and fastest route to display them in the console
/// </summary>
public void Start()
{
Field field = selectField();

Carregando…
Cancelar
Salvar