asp.net 独特的90球宾果卡C#

62lalag4  于 2023-10-21  发布在  .NET
关注(0)|答案(1)|浏览(97)

我需要生成宾果卡系统。
每张卡有6个表,每个表有15个不同的数字每个表由3行和9列组成。每行有5个数字和4个空白点。列包含:
第1栏:1-9,第2栏:10-19,第3栏:20-29,第4栏:30-39,第5栏:40-49,第6栏:50-59,第7栏:60-69,第8栏:70-79,第9栏:80-90
举例来说:

我写了这个代码:

public BingoCardGenerator() { 
        public static List<int[][]> GenerateBingoCards()
        {
            List<int[]> columnRanges = new List<int[]>
        {
            new int[] { 1, 9 },
            new int[] { 10, 19 },
            new int[] { 20, 29 },
            new int[] { 30, 39 },
            new int[] { 40, 49 },
            new int[] { 50, 59 },
            new int[] { 60, 69 },
            new int[] { 70, 79 },
            new int[] { 80, 90 }
        };

            Random random = new Random();
            List<int[][]> cards = new List<int[][]>();
            List<int> usedNumbers = new List<int>();
 
          

            for (int cardIndex = 0; cardIndex < 6; cardIndex++)
            {
                int[,] card = new int[3, 9];

                List<int>[] usedNumbersByColumn = new List<int>[9];
                for (int i = 0; i < 9; i++)
                {
                    usedNumbersByColumn[i] = new List<int>();
                }
                for (int col = 0; col <= 8; col++)
                {
                    int[] columnRange = columnRanges[col];
                    int start = columnRange[0];
                    int end = columnRange[1];
                    List<int> columnNumbers = new List<int>();

                    while (columnNumbers.Count < 10)
                    {
                        int num = random.Next(start, end);
                        if (!usedNumbers.Contains(num) || !usedNumbersByColumn[col].Contains(num))
                        {
                            
                                columnNumbers.Add(num);
                                usedNumbersByColumn[col].Add(num);
                                Console.WriteLine("Here = " + num);
                         
                        }else
                        {

                        }
 
                      }
                    for (int row = 0; row < 3; row++)
                    {
                        if (columnNumbers.Count > 0)
                        {
                            card[row, col] = columnNumbers[0];
                            columnNumbers.RemoveAt(0);
                        }
                        else
                        {
                            card[row, col] = 0;
                        }
                    }
                }
                for (int row = 0; row < 3; row++)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        int zeroColumn;
                        do
                        {
                            zeroColumn = random.Next(0, 9);
                        } while (card[row, zeroColumn] == 0);

                        card[row, zeroColumn] = 0;
                    }
                }

                for (int row = 0; row < 3; row++)
                {
                    for (int col = 0; col < 9; col++)
                    {
                        int num = card[row, col];
                        if (num != 0) 
                        {
                            usedNumbers.Add(num);
                        }
                    }
                }

                int[][] cardArray = new int[3][];
                for (int row = 0; row < 3; row++)
                {
                    cardArray[row] = new int[9];
                    for (int col = 0; col < 9; col++)
                    {
                        cardArray[row][col] = card[row, col];
                    }
                }
                

                cards.Add(cardArray);
            }

            return cards;

        }

但在表格中总是有重复

muk1a3rh

muk1a3rh1#

所以基本上,你有一个9 * 9的矩阵,每行有4个随机空格,每列包含1到9,10到19,20到29等之间的随机数字,随机排序,并分为三个3行表。
我会这么做:
1.创建一个方法,根据列索引返回每列中所有可能的数字。
1.创建一个返回9行的方法,每行包含9个布尔值的数组- 5个true和4个false,随机排序。
1.创建一个包含3行的方法,并根据它们和第一个方法中构建的列构建一个表。
1.创建一个方法,通过迭代行来构建一个卡片,每次向上面的方法发送3个,返回一个二维数组的空int数组-所以返回类型是int?[][,]
请确保您也阅读了代码中的注解。

static class BingoGenerator {

    // Note: Hard coded constants can be changed or even taken from configuration (but will have to be replaced with static readonly fields for that),
    // without breaking the functionality.
    // Constants that depends on them (numberOfRows, numberOfColumns) mush be replaced with static readonly fields as well if you replace them. 
    
    // Random can be wrappend and injected using a static property (but then you'll have to check it's not null when calling CreateCard,
    // and if it is, throw an InvalidOperationException with a proper message to indicate it. 
    private const int rowsPerTable = 3; 
    private const int tablesPerCard = 3;
    private const int numberOfRows = tablesPerCard * rowsPerTable;
    
    private const int blanksPerRow = 4;
    private const int numbersPerRow = 5;
    private const int numberOfColumns = blanksPerRow + numbersPerRow;
    private static bool[] _rowTemplate = Enumerable
        .Range(0, blanksPerRow)
        .Select(_ => false)
        .Concat(Enumerable.Range(0, numbersPerRow).Select(_ => true))
        .ToArray();

    private static Random _rnd = new();

    
    public static int?[][,] CreateCard()
    {
        var columns = CreateColumns().ToArray();
        var rows = CreateRows();
        return CreateTables(columns, rows);
    }

    // see item #4
    private static int?[][,] CreateTables(int[][] columns, IEnumerable<bool[]> rows)
    {
        var tables = new int?[tablesPerCard][,];
        
        for(var i=0;i<tablesPerCard;i++)
        {
            tables[i] = CreateTable(i, columns, rows.Skip(i * tablesPerCard).Take(rowsPerTable));
        }
        return tables;
    }

    // see item #3
    private static int?[,] CreateTable(int tableNumber, int[][] columns, IEnumerable<bool[]> rows)
    {
        var table = new int?[rowsPerTable, numberOfColumns];
        var j=0;
        foreach(var row in rows)
        {
            for(var i=0;i<numberOfColumns;i++)
            {
                table[j,i] = row[i] ? columns[i][j + (tableNumber * rowsPerTable)] : null;
            }
            j++;
        }
        return table;
    }

    // see item #2
    private static IEnumerable<bool[]> CreateRows()
        => Enumerable
            .Range(0, numberOfRows)
            .Select(_ => _rowTemplate.OrderBy(r => _rnd.Next()).ToArray());

    // see item #1    
    private static IEnumerable<int[]> CreateColumns()
        => Enumerable
            .Range(0, numberOfColumns)
            .Select(CreateColumn);

    // see item #1
    private static int[] CreateColumn(int columnIndex)
        => Enumerable
            .Range(1 + (10 * columnIndex), numberOfRows)
            .OrderBy(r => _rnd.Next())
            .ToArray();
}

.net fiddle上观看现场演示

相关问题