SimsPersonalityGenerator/Sim.cs

121 lines
4 KiB
C#
Raw Normal View History

2025-02-02 01:34:43 -05:00
namespace SimsPersonalityGenerator;
using System.Text.Json;
2025-02-02 02:00:36 -05:00
2025-02-02 01:34:43 -05:00
public class Sim
{
2025-02-02 02:00:36 -05:00
public int Nice { get; private set; }
public int Neat { get; private set; }
public int Outgoing { get; private set; }
public int Active { get; private set; }
public int Playful { get; private set; }
2025-02-02 01:34:43 -05:00
const int MIN_HIGHEST_POINT = 8;
const int MAX_LOWEST_POINT = 5;
/// <summary>
/// Initializes a new instance of the Sim class with a random personality.
/// </summary>
2025-02-02 01:34:43 -05:00
public Sim()
{
GenerateRandomPersonality();
}
/// <summary>
/// Sets one personality trait higher or lower and compensates by adjusting another trait.
/// </summary>
/// <param name="trait">The name of the trait to adjust.</param>
/// <param name="isHigh">True if the trait should be set higher, false if it should be set lower.</param>
public void RebalanceTraits(Traits trait, bool isHigh = false)
2025-02-02 01:34:43 -05:00
{
2025-02-02 02:00:36 -05:00
switch (trait)
2025-02-02 01:34:43 -05:00
{
2025-02-02 02:00:36 -05:00
case Traits.Nice:
if (isHigh && Nice >= MAX_LOWEST_POINT)
Nice = Math.Min(MIN_HIGHEST_POINT, Neat + 5);
2025-02-02 01:34:43 -05:00
else
Neat = Math.Min(MIN_HIGHEST_POINT, Nice + 5);
2025-02-02 01:34:43 -05:00
break;
2025-02-02 02:00:36 -05:00
case Traits.Neat:
if (isHigh && Neat >= MAX_LOWEST_POINT)
Neat = Math.Min(MIN_HIGHEST_POINT, Active + 5);
2025-02-02 01:34:43 -05:00
else
Active = Math.Min(MIN_HIGHEST_POINT, Neat + 5);
2025-02-02 01:34:43 -05:00
break;
2025-02-02 02:00:36 -05:00
case Traits.Outgoing:
if (isHigh && Outgoing >= MAX_LOWEST_POINT)
Outgoing = Math.Min(MIN_HIGHEST_POINT, Playful + 5);
2025-02-02 01:34:43 -05:00
else
Playful = Math.Min(MIN_HIGHEST_POINT, Outgoing + 5);
2025-02-02 01:34:43 -05:00
break;
2025-02-02 02:00:36 -05:00
case Traits.Active:
if (isHigh && Active >= MAX_LOWEST_POINT)
Active = Math.Min(MIN_HIGHEST_POINT, Nice + 5);
2025-02-02 01:34:43 -05:00
else
Nice = Math.Min(MIN_HIGHEST_POINT, Active + 5);
2025-02-02 01:34:43 -05:00
break;
2025-02-02 02:00:36 -05:00
case Traits.Playful:
if (isHigh && Playful >= MAX_LOWEST_POINT)
Playful = Math.Min(MIN_HIGHEST_POINT, Outgoing + 5);
2025-02-02 01:34:43 -05:00
else
Outgoing = Math.Min(MIN_HIGHEST_POINT, Playful + 5);
2025-02-02 01:34:43 -05:00
break;
default:
throw new ArgumentException("Invalid trait name.");
}
}
/// <summary>
/// Generates a random personality with balanced traits.
/// </summary>
public void GenerateRandomPersonality()
2025-02-02 01:34:43 -05:00
{
Random rand = new Random();
Nice = rand.Next(11);
Neat = rand.Next(11);
Outgoing = rand.Next(11);
Active = rand.Next(11);
Playful = rand.Next(11);
2025-02-02 01:34:43 -05:00
// Ensure that the sum of traits is balanced
int totalSum = Nice + Neat + Outgoing + Active + Playful;
2025-02-02 01:34:43 -05:00
if (totalSum > 50)
{
int average = totalSum - 50;
2025-02-02 01:34:43 -05:00
int traitToReduce = rand.Next(5);
switch (traitToReduce)
{
case 0: Nice -= Math.Min(Nice, average); break;
case 1: Neat -= Math.Min(Neat, average); break;
case 2: Outgoing -= Math.Min(Outgoing, average); break;
case 3: Active -= Math.Min(Active, average); break;
case 4: Playful -= Math.Min(Playful, average); break;
2025-02-02 01:34:43 -05:00
}
}
}
/// <summary>
/// Returns a JSON representation of the character's personality traits.
/// </summary>
/// <returns>A JSON containing the values of all personality traits.</returns>
2025-02-02 01:34:43 -05:00
public override string ToString()
{
var sim = new Sim();
{
Nice = Nice;
Neat = Neat;
Outgoing = Outgoing;
Playful = Playful;
Active = Active;
};
var options = new JsonSerializerOptions()
{
WriteIndented = true,
};
return JsonSerializer.Serialize<Sim>(sim, options);
2025-02-02 01:34:43 -05:00
}
}