1

I'm coding a game where after every year depending on results the attributes of characters change. The game is a movie business simulator and here is a example of the ugly code I have in place now:

if (movie.Reviews.Avg > 8.5f)
{
    movie.Producer.Reputation += 6f / movie.Producer.Reputation;
    return;
}
else if (movie.Reviews.Avg > 7.5f)
{
    movie.Producer.Reputation += 5f / movie.Producer.Reputation;
    return;
}
else if (movie.Reviews.Avg > 6.5f)
{
    movie.Producer.Reputation += 4f / movie.Producer.Reputation;
    return;
}
else if (movie.Reviews.Avg > 5)
{
    movie.Producer.Reputation += 1f / movie.Producer.Reputation;
    return;
}
else if (movie.Reviews.Avg > 4.5)
{
    return;
}
else if (movie.Reviews.Avg > 4)
{
    movie.Producer.Reputation -= movie.Producer.Reputation / 6f;
    return;
}
else if (movie.Reviews.Avg > 3)
{
    movie.Producer.Reputation -= movie.Producer.Reputation / 5f;
    return;
}
else if (movie.Reviews.Avg > 2)
{
    movie.Producer.Reputation -= movie.Producer.Reputation / 4f;
    return;
}
else
{
    movie.Producer.Reputation -= movie.Producer.Reputation / 3f;
    return;
}

Few problems I have with this code:

  • Reputations balloon. Good AI producers make good choices so their movies get good reviews and their reputation goes even higher.

  • Because reputations keep growing the absolute changes in attributes get smaller and moving up in ranks gets way too slow and hard.

What I am looking from the function:

  • Range is 0-10.
  • At the top of the range growth is slow even with great reviews, but if your movie gets bad reviews the decline is sharp. And the opposite for the bottom of the range.
Cid
  • 14,968
  • 4
  • 30
  • 45
petju
  • 11
  • 1
  • [Switch-case](https://learn.microsoft.com/en-gb/dotnet/csharp/language-reference/keywords/switch) may be prettier than `if else if else if` :) – Cid Nov 07 '18 at 09:30
  • 2
    This question is more a **game logic** problem than a programming one. I suggest you to ask the same question on the [gamedev](https://gamedev.stackexchange.com) site of the stackexchange network, you'll certainly get accurate answers – Cid Nov 07 '18 at 09:36
  • @Cid that doesn't really improve the code. See this link on how to eliminate long if -else logic code smell. https://softwareengineering.stackexchange.com/q/370400/199658 – Nick Nov 07 '18 at 09:55
  • @Nick Your link shows differents methods called in the conditions. This is differents from OP's if statements. See [this](https://stackoverflow.com/a/44078916/8398549) – Cid Nov 07 '18 at 10:00
  • @Cid yes but the point stands - long if-else logic is an antipattern that the OP can eliminate using the strategy pattern – Nick Nov 07 '18 at 10:02
  • What about storing every review average for each producer and build the reputation based the average of thoses review? – Cid Nov 09 '18 at 08:14

1 Answers1

0

Try following :

            KeyValuePair<float, float>[] average = {
               new KeyValuePair<float,float>(2,-3f),
               new KeyValuePair<float,float>(3,-4f),
               new KeyValuePair<float,float>(4,-5f),
               new KeyValuePair<float,float>(4.5f,-6f),
               new KeyValuePair<float,float>(5,0),
               new KeyValuePair<float,float>(6.5f,1f),
               new KeyValuePair<float,float>(7.5f,4f),
               new KeyValuePair<float,float>(8.5f,5f),

            };


            float reputation = 123;
            float review = 3.3f;
            reputation += average.Where(x => review > x.Key).Select(x => (x.Value == 0) ? 0 : reputation / x.Value).FirstOrDefault();
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • 1
    Although this improves OP's code, I downvoted because 1) This is posting code without any explanation 2) this doesn't answer OP's question. This question is about game logic tweaking rather than coding – Cid Nov 09 '18 at 08:09