C# :: Aufgabe #243

3 Lösungen Lösungen öffentlich

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.

Lösungen:

vote_ok
von suppengruen (200 Punkte) - 15.03.2019 um 13:47 Uhr
Für die 4. Spalte hast du in der Aufgabestellung einen Fehler, und zwar schreibst du von 46 bis 50 aber ich denke es soll 60 sein und dann in der letzen Spalte ab 61.

Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
/*
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 60 und in der letzten 61 bis 75.
*/
namespace Klassisches_Bingo
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rnd = new Random();
            int[,] zahlen = new int[5,5];
            int z, x, y;
            int cursorleft;
            int cursortop;
            List<int> help = new List<int>();
            for (x = 0; x <= 4; x++)
            {
                help.Clear();
                for(y = 0; y <= 4; y++)
                {
                    switch (x)
                    {
                        case 0:                            
                            z = rnd.Next(1, 15);
                           
                            if (y != 0)
                            {
                                foreach (int zahl in help)
                                {
                                    if (help.Contains(z))
                                        goto case 0;
                                }
                            }
                            help.Add(z);
                            zahlen[x, y] = z;
                  
                            break;
                        case 1:                            
                            z = rnd.Next(16, 30);
                            
                            if (y != 0)
                            {
                                foreach (int zahl in help)
                                {
                                    if (help.Contains(z))
                                        goto case 1;
                                } 
                            }
                            help.Add(z);
                            zahlen[x, y] = z; 
                            break;
                        case 2:                            
                            z = rnd.Next(31, 45);
                            if (y != 0)
                            {
                                foreach (int zahl in help)
                                {
                                    if (help.Contains(z))
                                        goto case 2;
                                }                               
                            }
                            help.Add(z);
                            zahlen[x, y] = z;                           
                            break;                        
                        case 3:                           
                            z = rnd.Next(46, 60);                            
                            if (y != 0)
                            {
                                foreach (int zahl in help)
                                {
                                    if (help.Contains(z))
                                        goto case 3;
                                } 
                            }
                            help.Add(z);
                            zahlen[x, y] = z;                            
                            break;
                        case 4:                            
                            z = rnd.Next(61, 75);                            
                            if (y != 0)
                            {
                                foreach (int zahl in help)
                                {
                                    if (help.Contains(z))
                                        goto case 4;
                                } 
                            }
                            help.Add(z);
                            zahlen[x, y] = z;                            
                            break;                   
                    }
                }   
            }
            Console.WriteLine("__________________________");
            for(int a = 0; a <= 4; a++)
            {
                Console.WriteLine("|    |    |    |    |    |");
                Console.WriteLine("|    |    |    |    |    |");
                Console.WriteLine("|____|____|____|____|____|");
            }
            x = 0;
            y = 0;
            cursorleft = 1;
            cursortop = 2;
            foreach (int zahl in zahlen)
            {
                Console.SetCursorPosition(cursorleft, cursortop);
                Console.WriteLine("{0}", zahlen[x, y]);                
                cursortop = cursortop + 3;
                y++;
                if(y == 5)
                {
                    y = 0;
                    cursorleft = cursorleft + 5;
                    cursortop = 2;
                    x++;
                }
            }
            Console.ReadLine();
        }
    }
}
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();
        }
    }
}
1 Kommentar
vote_ok
von JKooP (18090 Punkte) - 02.04.2020 um 17:06 Uhr
NET Core 3.x

Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;

namespace CS_Aufgabe_243_Bingo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(" B  | I  | N  | G  | O  |");
            Console.WriteLine(new string('-', 25));

            foreach (var b in BingoLottery())
            {
                Console.WriteLine($" {b} |");
                Console.WriteLine(new string('-', 25));
            }
        }

        static IEnumerable<string> BingoLottery()
        {
            for (int i = 0; i < 5; i++)
            {
                yield return string.Join(" | ", Enumerable.Range(i * 15 + 1, 15)
                    .OrderBy(x => Guid.NewGuid())
                    .Take(5).OrderBy(x => x)
                    .Select(x => x.ToString("00")));
            }
        }
    }
}
1801141

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.