mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Minor code clean-up
This commit is contained in:
@@ -0,0 +1,37 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Immutable;
|
||||||
|
|
||||||
|
namespace Game.Extensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Provides additional methods for the <see cref="ImmutableArray{T}"/> class.
|
||||||
|
/// </summary>
|
||||||
|
public static class ImmutableArrayExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Maps each element in an immutable array to a new value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TSource">
|
||||||
|
/// The type of elements in the source array.
|
||||||
|
/// </typeparam>
|
||||||
|
/// <typeparam name="TResult">
|
||||||
|
/// The type of elements in the resulting array.
|
||||||
|
/// </typeparam>
|
||||||
|
/// <param name="source">
|
||||||
|
/// The source array.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="selector">
|
||||||
|
/// Function which receives an element from the source array and its
|
||||||
|
/// index and returns the resulting element.
|
||||||
|
/// </param>
|
||||||
|
public static ImmutableArray<TResult> Map<TSource, TResult>(this ImmutableArray<TSource> source, Func<TSource, int, TResult> selector)
|
||||||
|
{
|
||||||
|
var builder = ImmutableArray.CreateBuilder<TResult>(source.Length);
|
||||||
|
|
||||||
|
for (var i = 0; i < source.Length; ++i)
|
||||||
|
builder.Add(selector(source[i], i));
|
||||||
|
|
||||||
|
return builder.MoveToImmutable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ namespace Game
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines the set of companies that will be simulated in the game.
|
/// Defines the set of companies that will be simulated in the game.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static ImmutableArray<Company> Companies = ImmutableArray.CreateRange(new[]
|
private readonly static ImmutableArray<Company> Companies = ImmutableArray.CreateRange(new[]
|
||||||
{
|
{
|
||||||
new Company("INT. BALLISTIC MISSILES", "IBM", sharePrice:100),
|
new Company("INT. BALLISTIC MISSILES", "IBM", sharePrice:100),
|
||||||
new Company("RED CROSS OF AMERICA", "RCA", sharePrice:85 ),
|
new Company("RED CROSS OF AMERICA", "RCA", sharePrice:85 ),
|
||||||
|
|||||||
@@ -38,13 +38,13 @@ namespace Game
|
|||||||
},
|
},
|
||||||
(parameters, previousDay) => previousDay with
|
(parameters, previousDay) => previousDay with
|
||||||
{
|
{
|
||||||
Companies = ImmutableArray.CreateRange(
|
Companies = previousDay.Companies.Map(
|
||||||
previousDay.Companies.Select ((company, index) => AdjustSharePrice(
|
(company, index) => AdjustSharePrice(
|
||||||
random,
|
random,
|
||||||
company,
|
company,
|
||||||
parameters.trend,
|
parameters.trend,
|
||||||
parameters.positiveSpike == index,
|
parameters.positiveSpike == index,
|
||||||
parameters.negativeSpike == index)))
|
parameters.negativeSpike == index))
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ namespace Game
|
|||||||
/// The maximum number of days each trend should last.
|
/// The maximum number of days each trend should last.
|
||||||
/// </param>
|
/// </param>
|
||||||
public static IEnumerable<double> Trends(Random random, int minDays, int maxDays) =>
|
public static IEnumerable<double> Trends(Random random, int minDays, int maxDays) =>
|
||||||
random.Integers(minDays, maxDays + 1).SelectMany(days => Enumerable.Repeat(GenerateTrend(random), days));
|
random.Integers(minDays, maxDays + 1).SelectMany(daysInCycle => Enumerable.Repeat(GenerateTrend(random), daysInCycle));
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates a random value for the market trend.
|
/// Generates a random value for the market trend.
|
||||||
@@ -143,7 +143,7 @@ namespace Game
|
|||||||
private static IEnumerable<int?> PriceSpikes(Random random, int companyCount, int minDays, int maxDays) =>
|
private static IEnumerable<int?> PriceSpikes(Random random, int companyCount, int minDays, int maxDays) =>
|
||||||
random.Integers(minDays, maxDays + 1)
|
random.Integers(minDays, maxDays + 1)
|
||||||
.SelectMany(
|
.SelectMany(
|
||||||
days => Enumerable.Range(0, days),
|
daysInCycle => Enumerable.Range(0, daysInCycle),
|
||||||
(days, dayNumber) => dayNumber == 0 ? random.Next(companyCount) : default(int?));
|
(daysInCycle, dayNumber) => dayNumber == 0 ? random.Next(companyCount) : default(int?));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user