Hi,

aus Langeweile habe ich Fifty! geschrieben (Konsolenanwendung), vielleicht auch etwas unnötig (kompliziert) geschrieben, da Langeweile.

Was kann es?
- Würfel rollen
- benutzte Würfel werden entfernt
- Würfel werden geprüft
- 3x Eingabe (auch wenn's eine falsche Eingabe war), danach werden die Würfel wieder neu gesetzt

Dice.cs:
PHP-Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 
Fifty {
    public 
struct Dice
    
{
        private 
int count, num;

        public 
int Count
        
{ get { return count; } }
        public 
int Num
        
{ get { return num; } }

        public 
Dice(int[] args)
        {
            if (
args != null && args.Length == 2)
            {
                
count = args[0];
                
num = args[1];
            }
            else
            {
                
count = -1;
                
num = -1;
            }
        }
    }
} 
FiftyDice.cs:
PHP-Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 
Fifty
{
    class 
FiftyDice
    
{
        public 
Dice[] userDices { get; set; }
        private 
readonly int[] nums, counts;

        public 
FiftyDice()
        {
            
nums = new int[] { 4, 6, 8, 10, 12, 20 };
            
counts = new int[] { 3, 2, 2, 1, 1, 1 };

            
userDices = new Dice[nums.Length];
            for (
int i = 0; i < nums.Length; i++)
                
userDices[i] = new Dice(new int[] { counts[i], nums[i] });
        }
    }
} 
Program.cs:
PHP-Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 
Fifty
{
    class 
Program
    
{
        static 
void Main(string[] args)
        {
            
Console.Title = "Fifty!";

            
FiftyDice f = new FiftyDice();

            while (
true)
            {
                for (
int r = 0; r < 3; r++)
                {
                    
Console.Write("Eingabe: ");
                    
String eingabe = Console.ReadLine();
                    
Dice[] dices = Utility.Parse(eingabe);

                    if (
dices.Any<Dice>(delegate(Dice n) { return n.Num == -1; }))
                    {
                        
Console.WriteLine("INVALID INPUT!");
                        continue;
                    }

                    
Console.Write("Benutzte Würfel: ");

                    for (
int i = 0; i < dices.Length; i++)
                    {
                        
Dice d = dices[i];

                        if (
d.Count > 1)
                            
Console.Write(d.Count);

                        
Console.Write('W');
                        
Console.Write(d.Num);

                        if (
i < dices.Length - 1)
                            
Console.Write(" + ");
                    }

                    
Console.Write("\n");
                    
KeyValuePair<bool, Dice[]> valid = Utility.IsValid(dices, f.userDices);
                    if (
valid.Value != null)
                        
f.userDices = valid.Value;

                    
Console.Write("{0}VALID DICES!\n", (!valid.Key ? "IN" : String.Empty));
                    
Console.Write("Now: ");

                    for (
int i = 0; i < f.userDices.Length; i++)
                    {
                        if (
f.userDices[i].Count > 1)
                            
Console.Write(f.userDices[i].Count);

                        
Console.Write('W');
                        
Console.Write(f.userDices[i].Num);

                        if (
i < f.userDices.Length - 1)
                            
Console.Write(" + ");
                    }

                    
Console.Write("\n");

                    if (
valid.Key)
                    {
                        
KeyValuePair<int, string> rollDices = Utility.RollDices(dices);
                        
Console.WriteLine("Du würfelst mit {0}", rollDices.Value);
                    }

                    
Console.Write("\n");
                }

                
f = new FiftyDice();
                
Console.WriteLine("3x used, init new dices...");
                
Console.WriteLine();
            }
        }
    }
} 
Utility.cs:
PHP-Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 
Fifty {
    public class 
Utility
    
{
        public static 
KeyValuePair<int, string> RollDices(Dice[] parsedDices)
        {
            
StringBuilder builder = new StringBuilder();
            
StringBuilder diced = new StringBuilder();
            
Random r = new Random();
            
int total = 0;

            for (
int i = 0; i < parsedDices.Length; i++)
            {
                
Dice p = parsedDices[i];
                if (
p.Count > 1)
                    
builder.Append(p.Count);

                
builder.Append('W');
                
builder.Append(p.Num);

                if (
i < parsedDices.Length - 1)
                    
builder.Append(" + ");

                for (
int j = 1; j <= p.Count; j++)
                {
                    
int randomized = r.Next(1, p.Num + 1);
                    
total += randomized;

                    
diced.Append(randomized);
                    while (
randomized == p.Num)
                    {
                        
randomized = r.Next(1, p.Num + 1);
                        
total += randomized;

                        
diced.Append("> ");
                        
diced.Append(randomized);
                    }

                    if (
j < p.Count)
                        
diced.Append(", ");
                }

                if (
i < parsedDices.Length - 1)
                    
diced.Append(" + ");
            }

            
diced.Append(" = ");
            
diced.Append(total);
            
diced.Append(".");

            
builder.Append(": ");
            
builder.Append(diced.ToString());

            return new 
KeyValuePair<int, string>(total, builder.ToString());
        }

        public static 
KeyValuePair<bool, Dice[]> IsValid(Dice[] parsedDices, Dice[] userDices)
        {
            List<
Dice> listedUserDices = userDices.ToList<Dice>();
            foreach (
Dice p in parsedDices)
            {
                
Dice found = listedUserDices.Find(delegate(Dice u) { return u.Num == p.Num; });
                if (
found.Count == -1)
                    return new 
KeyValuePair<bool, Dice[]>(false, null);

                if (
p.Count > found.Count)
                    return new 
KeyValuePair<bool, Dice[]>(false, null);

                
int index = listedUserDices.FindIndex(0, listedUserDices.Count, delegate(Dice u) { return u.Num == p.Num; });
                if (
index != -1) {
                    
listedUserDices[index] = new Dice(new int[] { listedUserDices[index].Count - p.Count, p.Num });

                    if (
listedUserDices[index].Count <= 0)
                        
listedUserDices.RemoveAt(index);
                }
            }

            return new 
KeyValuePair<bool, Dice[]>(true, listedUserDices.ToArray());
        }

        public static 
Dice[] Parse(string s)
        {
            List<
Dice> dices = new List<Dice>();

            if (
s.StartsWith("/d "))
                
s = s.Substring(3);

            
string[] diceParams = s.Split('+');
            for (
int i = 0; i < diceParams.Length; i++)
            {
                
Dice dice = ParseString(diceParams[i]);
                
bool changed = false;

                foreach (
Dice d in dices)
                {
                    if (
d.Num == dice.Num)
                    {
                        
int index = dices.FindIndex(0, dices.Count, delegate(Dice find) { return find.Num == d.Num; });
                        if (
index != -1)
                        {
                            
dices[index] = new Dice(new int[] { d.Count + dice.Count, d.Num });
                            
changed = true;
                        }

                        break;
                    }
                }

                if (!
changed)
                    
dices.Add(dice);
            }

            
Dice[] arrDices = dices.ToArray();
            
arrDices = Sort(arrDices);

            return 
arrDices;
        }

        private static 
Dice[] Sort(Dice[] toSort)
        {
            List<
int> nums = new List<int>();

            foreach (
Dice d in toSort)
                
nums.Add(d.Num);

            
nums.Sort();

            List<
Dice> sortedDices = new List<Dice>();

            foreach (
int num in nums)
                
sortedDices.Add(new Dice(new int[] { GetCount(num, toSort), num }));

            return 
sortedDices.ToArray();
        }

        private static 
int GetCount(int num, Dice[] dices)
        {
            
Dice found = dices.ToList<Dice>().Find(delegate(Dice d) { return d.Num == num; });
            return 
found.Count;
        }


        private static 
Dice ParseString(string s)
        {
            
s = s.Trim().ToLower();
            if (
String.IsNullOrEmpty(s))
                return new 
Dice(null);

            if (
s[0] == 'w')
                
s = s.Substring(1);

            if (!
s.Contains("w"))
            {
                
int num;
                if (
int.TryParse(s, out num))
                    return new 
Dice(new int[] { 1, num });
                else
                    return new 
Dice(null);
            }
            else
            {
                
string[] sParams = s.Split('w');
                
int count, num;
                if (
int.TryParse(sParams[0], out count) && int.TryParse(sParams[1], out num))
                    return new 
Dice(new int[] { count, num });
                else
                    return new 
Dice(null);
            }
        }
    }
} 
Fragen? Dann fragt.

MfG