创建宾果游戏时,表格创建并不是将数字放在正确的列中

时间:2018-03-19 18:12:10

标签: c#

B 1-15 | 我16-30 | N 31-45 | G 46-60 | O 61-75

问题出在' fillcard()' ,我添加了一些检查和评论,我认为问题正在发生。问题在于典型的宾果卡,字母遵循如上所述的特定数字范围,当打印表格时,这些值不对应但是它是随机的。例如,我将在B列中获得24或在G列中获得65,该卡将填入' fillcard()'而且我不确定为什么会这样。     使用系统;     使用System.Collections.Generic;     使用System.Linq;     使用System.Text;     使用System.Threading.Tasks;     使用System.Threading;

namespace BINGO
{
    class Program
    {
        static string user;
        static bool bingo = false;
        static string[,] table = new string[5, 5];
        static void Main(string[] args)
        {
            fillcard(table);
            Console.WriteLine("Welcome to BINGO, What is your name?");
            user = Console.ReadLine();
            Console.WriteLine("Hello " + user + ", here is your bingo card.");
            printboard(table);
            Master(table, bingo);        
        }
        static string[,] Master(string[,] table, bool bingo)
        {
            Random rnd = new Random();
            while (bingo == false)
            {   
                string letter = "";
                int number = 0;
                int num = rnd.Next(1, 5), B = rnd.Next(1, 15), I = rnd.Next(16, 30), N = rnd.Next(31, 45), G = rnd.Next(46, 60), O = rnd.Next(61, 75);
                switch (num)
                {
                    case 1:
                        letter = "B";
                        number = B;
                        break;
                    case 2:
                        letter = "I";
                        number = I;
                        break;
                    case 3:
                        letter = "N";
                        number = N;
                        break;
                    case 4:
                        letter = "G";
                        number = G;
                        break;
                    case 5:
                        letter = "O";
                        number = O;
                        break;
                }
                Console.WriteLine(letter + " " + number);
                var waitTime = new TimeSpan(0, 0, 1);
                var waitUntil = DateTime.Now + waitTime;

                waitUntil = DateTime.Now + waitTime;

                while (DateTime.Now <= waitUntil)
                {
                    Thread.Sleep(1000);
                    if (Console.KeyAvailable)
                    {
                        ConsoleKeyInfo k = Console.ReadKey(true);
                        Console.WriteLine("Do you have BINGO or a mark?");
                        string response = Console.ReadLine().ToLower();
                        if(response == "mark")
                        {
                            checkmark(table, number);
                            Console.Clear();
                            printboard(table);
                        }
                        else if(response == "bingo")
                        {

                        }
                    }

                }
            } 
            return table;
        }
        static string[,] checkmark(string[,] table, int number)
        {
            for (int i = 0; i < 5; i++)
            { 
                for (int j = 0; j < 5; j++)
                {
                    if(table[i,j] == "X ")
                    {

                    }
                    else if (number == Convert.ToInt32(table[i, j]))
                    {
                        table[i, j] = "X ";
                    }
                }
            }
            return table;
        }
        static string[,] fillcard(string[,] table)
        {
            int i, j, step = 0;
            Random rnd = new Random();
            int[] prev = new int[100];
                string letter = "";
                for (i = 0; i < 5; i++) //Y
                {
                for (j = 0; j < 5; j++) //X
                {
                    int B = rnd.Next(1, 15), I = rnd.Next(16, 30), N = rnd.Next(31, 45), G = rnd.Next(46, 60), O = rnd.Next(61, 75);
                    switch (j)
                    {
                        case 0:
                            letter = Convert.ToString(B);
                            break;
                        case 1:
                            letter = Convert.ToString(I);
                            break;
                        case 2:
                            letter = Convert.ToString(N);
                            break;
                        case 3:
                            letter = Convert.ToString(G);
                            break;
                        case 4:
                            letter = Convert.ToString(O);
                            break;
                    }
                    bool next = false;
                    if(Convert.ToInt32(letter) < 10) //adds extra space for formatting
                    {
                        letter = (letter + " ");
                    }
                    if (!(step == 0))//Step is making sure it doesnt check for a previous value if it is the first value on the table
                    {
                        for (int k = 0; k < prev.Length; k++) //Checks for previous values in an array and if true it will subtract j and regenerate that letter.
                        {
                            if (Convert.ToInt32(letter) == prev[k])
                            {
                                if(j == 0)
                                {

                                }
                                else {
                                    j--;
                                }
                                break;
                            }
                            else
                            {
                                next = true;
                            }
                        }
                    }
                    if (next == true)
                    {
                        if (i == 2 && j == 2)
                        {
                        }
                        else
                        {
                            Console.WriteLine(j);
                            Console.WriteLine(letter);
                            Console.WriteLine();
                            table[i, j] = Convert.ToString(letter);
                        }

                    }
                    else
                    {
                        Console.WriteLine(j);
                        Console.WriteLine(letter);
                        Console.WriteLine();
                        table[i, j] = Convert.ToString(letter);
                    }
                    prev[step] = Convert.ToInt32(letter);
                    step++;
                }
            }

            return table;
        }
        static void printboard(string[,] table)
        {
            int i, j;
            string[] headings = { "B", "I", "N", "G", "O" };
            for (i = 0; i < 5; i++)
            {
                Console.Write("{0}      ", headings[i]);
            }
            Console.WriteLine();

            for (i = 0; i < 5; i++)
            {
                for (j = 0; j < 5; j++)
                {
                    if (i == 2 && j == 2)
                        Console.Write("X      ");
                    else
                        Console.Write("{0}     ", table[i, j]);
                }
                Console.WriteLine();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我会做什么来存储List<int>中每个字母的可用值,然后选择一个随机的值并每次从列表中删除它。通过这种方式,您将获得一个有效的值,它将是随机的,并且不会有重复。

由于处理了五个不同的字母,我们将有5个不同的可用列表。而且,由于从列表中选择随机项并删除它的代码是相同的,我们可以将该部分提取到函数中。另请注意,我在类范围内声明Rnd变量,因此此方法可以访问它:

public class Program
{
    private static readonly Random Rnd = new Random();

    private static string GetRandomItemAndRemoveIt(IList<int> items)
    {
        if (items == null || items.Count == 0) return string.Empty;

        var randomItem = items[Rnd.Next(items.Count)];
        items.Remove(randomItem);

        // The PadRight method will ensure each string is at least two characters wide
        return randomItem.ToString().PadRight(2, ' ');
    }

现在,在我们的FillCard方法中,我们只需要创建五个列表,并为每个字母调用我们的方法:

    static void FillCard(string[,] table)
    {
        var BValues = Enumerable.Range(1, 15).ToList();
        var IValues = Enumerable.Range(16, 15).ToList();
        var NValues = Enumerable.Range(31, 15).ToList();
        var GValues = Enumerable.Range(46, 15).ToList();
        var OValues = Enumerable.Range(61, 15).ToList();

        for (int row = 0; row < 5; row++)
        {
            for (int col = 0; col < 5; col++)
            {
                switch (col)
                {
                    case 0: // B
                        table[row, col] = GetRandomItemAndRemoveIt(BValues);
                        break;
                    case 1: // I
                        table[row, col] = GetRandomItemAndRemoveIt(IValues);
                        break;
                    case 2: // N
                        table[row, col] = GetRandomItemAndRemoveIt(NValues);
                        break;
                    case 3: // G
                        table[row, col] = GetRandomItemAndRemoveIt(GValues);
                        break;
                    case 4: // O
                        table[row, col] = GetRandomItemAndRemoveIt(OValues);
                        break;
                }
            }
        }
    }

我写了一个不同的方法来将数组显示为宾果卡,所以尽管它对你的问题并不重要,但它是:

    public static void DisplayBingoCard(string[,] values)
    {
        Console.WriteLine();
        Console.WriteLine("╔════╦════╦════╦════╦════╗");
        Console.WriteLine("║ B  ║ I  ║ N  ║ G  ║ O  ║");
        Console.WriteLine("╠════╬════╬════╬════╬════╣");

        for (int row = 0; row < 5; row++)
        {
            for (int col = 0; col < 5; col++)
            {
                Console.Write("║ " + values[row, col] + " ");
            }

            Console.WriteLine("║");

            if (row < 4)
            {
                Console.WriteLine("╠════╬════╬════╬════╬════╣");
            }
            else
            {
                Console.WriteLine("╚════╩════╩════╩════╩════╝");
            }
        }
    }

现在我们可以创建数组,使用此方法填充它,然后将其输出到屏幕:

    private static void Main()
    {
        string[,] table = new string[5, 5];

        FillCard(table);
        DisplayBingoCard(table);

        Console.Write("\nPress any key to exit...");
        Console.ReadKey();
    }
}

<强>输出

enter image description here