Good vs Evil

Good vs Evil

Description

Middle Earth is about to go to war. The forces of good will have many battles with the forces of evil. Different races will certainly be involved. Each race has a certain 'worth' when battling against others. On the side of good we have the following races, with their associated worth:

  • Hobbits - 1
  • Men - 2
  • Elves - 3
  • Dwarves - 3
  • Eagles - 4
  • Wizards - 10

On the side of evil we have:

  • Orcs - 1
  • Men - 2
  • Wargs - 2
  • Goblins - 2
  • Uruk Hai - 3
  • Trolls - 5
  • Wizards - 10

Although weather, location, supplies and valor play a part in any battle, if you add up the worth of the side of good and compare it with the worth of the side of evil, the side with the larger worth will tend to win.

Thus, given the count of each of the races on the side of good, followed by the count of each of the races on the side of evil, determine which side wins.

Input:

The function will be given two parameters. Each parameter will be a string separated by a single space. Each string will contain the count of each race on the side of good and evil.

The first parameter will contain the count of each race on the side of good in the following order:

  • Hobbits, Men, Elves, Dwarves, Eagles, Wizards.

The second parameter will contain the count of each race on the side of evil in the following order:

  • Orcs, Men, Wargs, Goblins, Uruk Hai, Trolls, Wizards.

All values are non-negative integers. The resulting sum of the worth for each side will not exceed the limit of a 32-bit integer.

Output:

Return ""Battle Result: Good triumphs over Evil" if good wins, "Battle Result: Evil eradicates all trace of Good" if evil wins, or "Battle Result: No victor on this battle field" if it ends in a tie.

 

复制代码
using System;
using System.Linq;

public class Kata
{
  public static string GoodVsEvil(string good, string evil)
  {
     string result = "Battle Result: Good triumphs over Evil";
            int[] goodValue = { 1, 2, 3, 3, 4, 10 };
            int[] evilValue = { 1, 2, 2, 2, 3, 5, 10 };
            int[] goodCount = good.Split(' ').Select(x => Convert.ToInt32(x)).ToArray();
            int[] evilCount = evil.Split(' ').Select(x => Convert.ToInt32(x)).ToArray();
            int goodSum = Enumerable.Range(0, goodValue.Length).Aggregate(0, (sum, x) => sum + goodValue[x] * goodCount[x]);
            int evilSum = Enumerable.Range(0, evilValue.Length).Aggregate(0, (sum, x) => sum + evilValue[x] * evilCount[x]);
            if (goodSum == evilSum)
            {
                result = "Battle Result: No victor on this battle field";
            }
            else if (goodSum < evilSum)
            {
                result = "Battle Result: Evil eradicates all trace of Good";
            }
            return result;
  }
}
复制代码

 

 

其他人的解法:

复制代码
using System;

public class Kata
{
  public enum GoodRacesValues
  {
    Hobbits = 1,
    Men = 2,
    Elves = 3,
    Dwarves = 3,
    Eagles = 4,
    Wizards = 10
  }
  
  public enum EvilRacesValues
  {
    Orcs = 1,
    Men = 2,
    Wargs = 2,
    Goblins = 2,
    UrukHai = 3,
    Trolls = 5,
    Wizards = 10
  }

  public static string GoodVsEvil(string good, string evil)
  {
     string[] goodArmyValues = good.Split(' ');
     string[] evilArmyValues = evil.Split(' ');
     int goodArmyForces = Kata.CalculateForces<GoodRacesValues>(goodArmyValues);
     int evilArmyForces = Kata.CalculateForces<EvilRacesValues>(evilArmyValues);
  
     return Kata.GetBattleResult(goodArmyForces, evilArmyForces);
  }
  
  public static int CalculateForces<T>(string[] armyValues)
  {
      int i = 0;
      int totalForces = 0;
      foreach(T raceValue in Enum.GetValues(typeof(T)))
      {
        totalForces += Convert.ToInt32(raceValue) * int.Parse(armyValues[i]);
        ++i;
      }
      return totalForces;
  }
  
  public static string GetBattleResult(int goodArmyForces, int evilArmyForces)
  {
      if (goodArmyForces > evilArmyForces)
      {
        return "Battle Result: Good triumphs over Evil";
      } 
      else if (goodArmyForces < evilArmyForces)
      {
        return "Battle Result: Evil eradicates all trace of Good"; 
      }
      else
      {
        return "Battle Result: No victor on this battle field";       
      }
  }
}
复制代码

 

 

复制代码
using System;
using System.Linq;

public class Kata
{
  public static string GoodVsEvil(string good, string evil)
  {
    var gWorth = new[] { 1, 2, 3, 3, 4, 10 };
            var eWorth = new[] { 1, 2, 2, 2, 3, 5, 10 };
            var g = good.Split(' ').Select(int.Parse).Zip(gWorth, (f, s) => f * s).Sum();
            var b = evil.Split(' ').Select(int.Parse).Zip(eWorth, (f, s) => f * s).Sum();
            return (g > b) ? "Battle Result: Good triumphs over Evil" : ((g == b) ? "Battle Result: No victor on this battle field" : "Battle Result: Evil eradicates all trace of Good");
  }
}
复制代码

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(451)  评论(0编辑  收藏  举报
编辑推荐:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
历史上的今天:
2014-07-10 Win7系统中如何查看当前文件被哪一个程序占用了
点击右上角即可分享
微信分享提示