C# :: Aufgabe #243 :: Lösung #2

3 Lösungen Lösungen öffentlich
#243

Klassisches Bingo mit Verzögert gezogenen Nummern

Fortgeschrittener - C# von maxi72501 - 11.03.2019 um 14:57 Uhr
In dieser Aufgabe soll man ein Klassisches Bingo erstellen wo man eine zufällige Karte erhält wo auf einem 5 * 5 Feld Zahlen von 1 bis 75 verteilt liegen. Zu beachten ist das in der ersten Spalte zahlen von 1 bis 15, in der zweiten Spalte 16 bis 30, in der Dritten 31 bis 45, in der vierten 46 bis 50 und in der letzten 51 bis 75.
#2
1x
vote_ok
von hollst (13980 Punkte) - 31.03.2019 um 14:32 Uhr
Quellcode ausblenden C#-Code
using System;
using static System.Console;
using System.Text;

namespace Bingo_Console
{
    class Program
    {
        static void Main()
        {
            byte anzahl = 5;
            bool bo_loop = true;
            while (bo_loop)
            {
                anzahl.Coupon().CouponToString().MessageLine();
                "next (ESC = STOP)".MessageLine();
                ConsoleKeyInfo ki = ReadKey(true);
                bo_loop = !(ki.Key == ConsoleKey.Escape);
            }
        }
    }

    public static class Bingo_Extentions
    {
        public static byte[] Shuffle(this byte count, Random r) //count = 15
        {
            byte[] result = new byte[count];
            for (byte i = 0; i < result.Length; i++)
                result[i] = i;
            byte temp, next;
            for(byte i = 0; i < result.Length; i++)
            {
                temp = result[i];
                next = (byte)r.Next(i, result.Length);
                result[i] = result[next];
                result[next] = temp;                
            }
            return result;
        }

        public static void MessageLine(this string s) => WriteLine(s);

        public static byte[][] Coupon(this byte count) //count = 5
        {
            Random r = new Random();
            byte[][] result = new byte[count][];
            for (byte i = 0; i < count; i++)
                result[i] = new byte[count];
            byte count_random = 15;
            for(byte i = 0; i < count; i++)
            {
                byte[] temp = count_random.Shuffle(r);
                for (byte j = 0; j < count; j++)
                    result[j][i] = (byte)(i * count_random + temp[j] + 1);
            }
            return result;
        }

        public static string CouponToString(this byte[][] a)
        {
            StringBuilder sb = new StringBuilder();
            for(var i = 0; i < a.Length; i++)
            {
                for (var j = 0; j < a[i].Length; j++)
                {
                    if(i == a.Length / 2 && j == i)
                        sb.Append($"   ");
                    else
                        sb.Append($"{a[i][j],2} ");
                }
                sb.AppendLine();
            }
            return sb.ToString();
        }
    }
}

Kommentare:

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

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