using System;publicclassKata{publicenum GoodRacesValues
{
Hobbits =1,
Men =2,
Elves =3,
Dwarves =3,
Eagles =4,
Wizards =10}publicenum EvilRacesValues
{
Orcs =1,
Men =2,
Wargs =2,
Goblins =2,
UrukHai =3,
Trolls =5,
Wizards =10}publicstaticstringGoodVsEvil(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);}publicstaticintCalculateForces<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;}publicstaticstringGetBattleResult(int goodArmyForces,int evilArmyForces){if(goodArmyForces > evilArmyForces){return"Battle Result: Good triumphs over Evil";}elseif(goodArmyForces < evilArmyForces){return"Battle Result: Evil eradicates all trace of Good";}else{return"Battle Result: No victor on this battle field";}}}
答案2:
using System;using System.Linq;publicclassKata{publicstaticstringGoodVsEvil(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");}}
答案3:
using System;using System.Linq;publicclassKata{publicstaticstringGoodVsEvil(string good,string evil){var powerOfGood =new[]{1,2,3,3,4,10};var powerOfEvil =new[]{1,2,2,2,3,5,10};var resultOfGood = good.Split(' ').Select(x =>int.Parse(x)).Zip(powerOfGood,(m,p)=> m*p).Sum();var resultOfEvil = evil.Split(' ').Select(x =>int.Parse(x)).Zip(powerOfEvil,(m,p)=> m*p).Sum();var result = resultOfGood > resultOfEvil
?"Battle Result: Good triumphs over Evil": resultOfGood < resultOfEvil
?"Battle Result: Evil eradicates all trace of Good":"Battle Result: No victor on this battle field";return result;}}
答案4:
using System;using System.Linq;publicclassKata{publicstaticstringGoodVsEvil(string good,string evil){int[] goodNumbers ={1,2,3,3,4,10};int[] evilNumbers ={1,2,2,2,3,5,10};
Func<string,int[],int> calculatePower =(army, numbers)=> army.Split(' ').Select(int.Parse).Zip(numbers,(a, b)=> a * b).Sum();int goodPower =calculatePower(good, goodNumbers);int evilPower =calculatePower(evil, evilNumbers);return goodPower == evilPower
?"Battle Result: No victor on this battle field": goodPower > evilPower
?"Battle Result: Good triumphs over Evil":"Battle Result: Evil eradicates all trace of Good";}}
答案5:
using System;using System.Linq;using System.Collections.Generic;publicclassKata{publicstaticint[] GoodPowerz =new[]{1,2,3,3,4,10};publicstaticint[] EvilPowerz =new[]{1,2,2,2,3,5,10};publicstatic Dictionary<int,string> BattleOutcomes =newDictionary<int,string>{{-1,"Battle Result: Evil eradicates all trace of Good"},{0,"Battle Result: No victor on this battle field"},{+1,"Battle Result: Good triumphs over Evil"}};publicstaticstringGoodVsEvil(string good,string evil){var goodHits =Attack(GoodPowerz, good);var evilHits =Attack(EvilPowerz, evil);return BattleOutcomes[goodHits.CompareTo(evilHits)];}privatestaticintAttack(int[] powerz,string groupSizes){return groupSizes.Split(' ').Select(int.Parse).Zip(powerz,(d, p)=> d * p).Sum();}}
答案6:
using System;using System.Linq;publicclassKata{publicstaticstringGoodVsEvil(string good,string evil){var goodPoints =new[]{1,2,3,3,4,10};var evilPoints =new[]{1,2,2,2,3,5,10};var goodResult = good.Split(' ').Select(int.Parse).Select((x, i)=> x * goodPoints[i]).Sum();var evilResult = evil.Split(' ').Select(int.Parse).Select((x, i)=> x * evilPoints[i]).Sum();return goodResult == evilResult
?"Battle Result: No victor on this battle field":(goodResult > evilResult
?"Battle Result: Good triumphs over Evil":"Battle Result: Evil eradicates all trace of Good");}}
答案7:
using System;using System.Linq;publicclassKata{publicstaticstringGoodVsEvil(string good,string evil){var teamGandalf =newint[]{1,2,3,3,4,10};var teamSauron =newint[]{1,2,2,2,3,5,10};var teamGandalfStrength = good.Split(' ').Select(x =>int.Parse(x.ToString())).Zip(teamGandalf,(first, second)=> first*second).Sum();var teamSauronStrength = evil.Split(' ').Select(x =>int.Parse(x.ToString())).Zip(teamSauron,(first, second)=> first*second).Sum();if(teamGandalfStrength > teamSauronStrength)return"Battle Result: Good triumphs over Evil";return teamSauronStrength > teamGandalfStrength ?"Battle Result: Evil eradicates all trace of Good":"Battle Result: No victor on this battle field";}}
答案8:
using System;using System.Linq;publicclassKata{publicstaticstringGoodVsEvil(string good,string evil){var goodList =ConvertToList(good);var goodCount =CalculateGood(goodList);var evilList =ConvertToList(evil);var evilCount =CalculateEvil(evilList);returnGetGoodVsEvilResult(goodCount, evilCount);}privatestaticint[]ConvertToList(string s){return s.Split(' ').Select(int.Parse).ToArray();}privatestaticintCalculateGood(int[] l){return l[0]+2*l[1]+3*l[2]+3*l[3]+4*l[4]+10*l[5];}privatestaticintCalculateEvil(int[] l){return l[0]+2* l[1]+2* l[2]+2* l[3]+3* l[4]+5* l[5]+10* l[6];}privatestaticstringGetGoodVsEvilResult(int goodCount,int badCount){if(goodCount == badCount)return"Battle Result: No victor on this battle field";return goodCount > badCount
?"Battle Result: Good triumphs over Evil":"Battle Result: Evil eradicates all trace of Good";}}
答案9:
using System;using System.Linq;using System.Collections.Generic;publicclassKata{publicstaticstringGoodVsEvil(string good,string evil){int[] powersOfGood ={1,2,3,3,4,10};int[] powersOfEvil ={1,2,2,2,3,5,10};
Func<string,int[],int> calcPower =(fraction, powers)=> fraction.Split(' ').Select(s => Convert.ToInt32(s)).Zip(powers,(count, power)=> count * power).Sum();
Dictionary<int,string> resultOfBattle =newDictionary<int,string>(){{-1,"Battle Result: Evil eradicates all trace of Good"},{0,"Battle Result: No victor on this battle field"},{1,"Battle Result: Good triumphs over Evil"}};int whoIsWin =calcPower(good, powersOfGood).CompareTo(calcPower(evil, powersOfEvil));return resultOfBattle[whoIsWin];}}
答案10:
using System;using System.Linq;publicclassKata{publicstaticstringGoodVsEvil(string good,string evil){var parsedGood =CalculateGood(good);var parsedEvil =CalculateEvil(evil);if(parsedGood == parsedEvil){return"Battle Result: No victor on this battle field";};return parsedGood > parsedEvil ?"Battle Result: Good triumphs over Evil":"Battle Result: Evil eradicates all trace of Good";}privatestaticintCalculateGood(string good){var parsedGood = good.Split().Select(int.Parse).ToList();return parsedGood[0]+(parsedGood[1]*2)+(parsedGood[2]*3)+(parsedGood[3]*3)+(parsedGood[4]*4)+(parsedGood[5]*10);}privatestaticintCalculateEvil(string evil){var parsedEvil = evil.Split().Select(int.Parse).ToList();return parsedEvil[0]+(parsedEvil[1]*2)+(parsedEvil[2]*2)+(parsedEvil[3]*2)+(parsedEvil[4]*3)+(parsedEvil[5]*5)+(parsedEvil[6]*10);}}