Quick actions

cmd+k|ctrl+k

Navigation

Languages

Gladiators

Snippet info

Language

Csharp

Visibility

public

Author

levi

Created

2016-05-03T12:54:51Z

Updated

2016-05-03T12:54:51Z

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass {
    private static readonly Random rand = new Random();
    const int ROUND_COUNT = 1000;
    
    static void Main() {
        Console.WriteLine("Gannicus");
        OutputDictionary(SimRound(4,4,true, false));
        Console.WriteLine();
        
        Console.WriteLine("Spartacus");
        OutputDictionary(SimRound(4,4,false, true));
    }
    
    static void OutputDictionary(IDictionary<int,int> results)
    {
        int totalWounds = 0;
        foreach(KeyValuePair<int,int> pair in results)
        {
            Console.WriteLine("Wounds: {0}, {1}%",pair.Key, pair.Value / (double)ROUND_COUNT * 100);
            totalWounds += pair.Key * pair.Value;
        }
        
        Console.WriteLine("Average wounds per round: {0}", totalWounds / (double)ROUND_COUNT);
    }
    
    static IDictionary<int,int> SimRound(int attackerDice, int defenderDice, bool winsOnTies, bool extraWoundsWithDoubles)
    {
        IDictionary<int,int> dict = new SortedDictionary<int,int>();
        
        for(int i = 0; i < ROUND_COUNT; ++i)
        {
            int wounds = NumberOfWounds(
                    Enumerable.Range(0,attackerDice).Select(x => rand.Next(1,7)).ToArray(),
                    Enumerable.Range(0,defenderDice).Select(x => rand.Next(1,7)).ToArray(),
                    winsOnTies, extraWoundsWithDoubles);
                    
            if(!dict.ContainsKey(wounds))
                dict.Add(wounds,0);
                
            dict[wounds]++;
        }
        
        return dict;
    }
    
    static int NumberOfWounds(int[] attacker, int[] defender, bool winsOnTies, bool extraWoundsWithDoubles)
    {
        int wounds = 0;
        {
            int i = 0;
            for(;i < defender.Length && i < attacker.Length; ++i)
            {
                if(attacker[i] > (winsOnTies ? defender[i] - 1 : defender[i]))
                    wounds++;
            }
            
            for(;i < attacker.Length; ++i)
            {
                if(attacker[i] > 3)
                    wounds++;
            }
        }
        
        if(extraWoundsWithDoubles)
        {
            for(int i = 0; i < attacker.Length - 1; ++i)
            {
                if(attacker[i] == attacker[i+1])
                {
                    wounds++;
                    ++i;
                }
            }
        }
        
        return wounds;
    }
}
INFO