C# :: Aufgabe #339 :: Lösung #1

1 Lösung Lösung öffentlich
#339

Kniffel (Yahtzee, Pasch)

Fortgeschrittener - C# von JKooP - 06.11.2020 um 15:24 Uhr
Schreibe eine Klasse/Modul mit der/dem es möglich ist das Spiel Kniffel in abgespeckter Form abzubilden.
Zur Vereinfachung soll statt 3 nur 1 Mal gewürfelt werden.
Als Ergebnis sollen alle möglichen Gewinnstufen eines Wurfs sowohl des oberen als auch des unteren Blocks mit der erreichten Punktzahl ausgegeben werden.

Beispielwurf: 2-2-2-4-4

Oberer Block:
Zweier: 2+2+2 = 6 (nur Summe der 2er zählen)
Vierer: 4+4 = 8 (nur Summe der 4er zählen)

Unterer Block:
Dreierpasch: 2+2+2 und 4+6 = 16 (Summe aller Augen)
Full House: Dreierpasch + Zweierpasch -> Sonderwertung = 25
Chance: 2+2+2+4+6 = 16 (Summe aller Augen)

Als Erweiterung kann auch das dreimalige Würfeln implementiert werden.
Da die Interaktion mit der Konsole nicht allzu bedienerfreundlich ist, sollte
man vielleicht auf eine grafischen Benutzeroberfläche ausweichen.

Viel Spaß
#1
vote_ok
von JKooP (18090 Punkte) - 15.12.2020 um 19:45 Uhr
NET Core 3.x C# 8.x
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;

namespace CS_MDL_CORE_Kniffel
{
    enum Types // specific point values
    {
        aces = 1, twos, threes, fours, fives, sixes,
        chance, two_of_a_kind, three_of_a_kind, four_of_a_kind,
        full_house = 25, small_straight = 30, large_straight = 40, yahtzee = 50,
    }

    class Program
    {
        private static List<int> _lstDice;
        private static readonly List<string> _lstSmallStraight = new List<string> { "1234", "2345", "3456" };
        private static readonly List<string> _lstLargeStraight = new List<string> { "12345", "23456" };

        static void Main(string[] args)
        {
            // dice
            _lstDice = Enumerable.Range(0, 5).Select(x => new Random().Next(1, 7)).OrderBy(x => x).ToList();
            Console.WriteLine("dice:\n");
            Console.WriteLine(string.Join(", ", _lstDice));
            
            // upper section
            Console.WriteLine("\nupper section:\n");
            _lstDice.GroupBy(x => x).Select(x => new { n = x.Key, c = x.Count(), s = x.Key * x.Count() }).ToList().ForEach(x => Console.WriteLine($"{Enum.GetName(typeof(Types), x.n)}:\t{x.c}x => {x.s} Pkt"));
            
            // lower section
            Console.WriteLine("\nlower section:\n");
            var sum = _lstDice.Sum();
            var lstLowerSection = new List<(Types type, bool won, int points)>
            {
                (Types.three_of_a_kind, IsXofKind(3), sum),
                (Types.four_of_a_kind, IsXofKind(4), sum),
                (Types.full_house, IsXofKind(2) && IsXofKind(3), (int)Types.full_house),
                (Types.small_straight, IsStraight(_lstSmallStraight), (int)Types.small_straight),
                (Types.large_straight, IsStraight(_lstLargeStraight), (int)Types.large_straight),
                (Types.yahtzee, IsXofKind(5), (int)Types.yahtzee),
                (Types.chance, true, sum)
            };

            lstLowerSection.Where(x => x.won == true).ToList().ForEach(x => Console.WriteLine($"{x.type}:{new string(' ', 15 - x.type.ToString().Length)} {x.points} Pkt"));

            static bool IsXofKind(int p) =>
                _lstDice.GroupBy(x => x).Select(x => new { n = x.Key, c = x.Count() }).Any(x => x.c == p);

            static bool IsStraight(List<string> comp) =>
                comp.Where(x => string.Join("", _lstDice).Contains(x)).ToList().Count() > 0;
        }
    }
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben