SimsPersonalityGenerator/Sim.cs
Tony Bark f6ef83fd91 Adjusted the personality algorithm
- Use traits enum in example instead of strings
- Moved traits enum to separate file
- Asked AI for some documentation comments
2025-02-02 03:09:17 -05:00

112 lines
3.5 KiB
C#

namespace SimsPersonalityGenerator;
using System.Text.Json;
public class Sim
{
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; }
/// <summary>
/// Initializes a new instance of the Sim class with a random personality.
/// </summary>
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>
// TODO: Fix trait mix up
public void SetTraitHigher(Traits trait, bool isHigh)
{
switch (trait)
{
case Traits.Nice:
if (isHigh)
Nice = Math.Min(8, Neat + 5);
else
Neat = Math.Min(8, Nice + 5);
break;
case Traits.Neat:
if (isHigh)
Neat = Math.Min(8, Active + 5);
else
Active = Math.Min(8, Neat + 5);
break;
case Traits.Outgoing:
if (isHigh)
Outgoing = Math.Min(8, Playful + 5);
else
Playful = Math.Min(8, Outgoing + 5);
break;
case Traits.Active:
if (isHigh)
Active = Math.Min(8, Nice + 5);
else
Nice = Math.Min(8, Active + 5);
break;
case Traits.Playful:
if (isHigh)
Playful = Math.Min(8, Outgoing + 5);
else
Outgoing = Math.Min(8, Playful + 5);
break;
default:
throw new ArgumentException("Invalid trait name.");
}
}
/// <summary>
/// Generates a random personality with balanced traits.
/// </summary>
private void GenerateRandomPersonality()
{
Random rand = new Random();
Nice = rand.Next(11);
Neat = rand.Next(11);
Outgoing = rand.Next(11);
Active = rand.Next(11);
Playful = rand.Next(11);
// Ensure that the sum of traits is balanced
int totalSum = Nice + Neat + Outgoing + Active + Playful;
if (totalSum > 50)
{
int average = totalSum - 50;
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;
}
}
}
/// <summary>
/// Returns a JSON representation of the character's personality traits.
/// </summary>
/// <returns>A JSON containing the values of all personality traits.</returns>
public override string ToString()
{
var sim = new Sim();
{
Nice = Nice;
Neat = Neat;
Outgoing = Outgoing;
Playful = Playful;
Active = Active;
};
return JsonSerializer.Serialize<Sim>(sim);
}
}