我只有 print BINGO ,现在我必须将随机数分配给每个 col 行
I only have the print BINGO , Now I have to put a randomizer number will go to each col row
这是主要代码。我确实需要一个 Randomizer 编号并且无需重复即可在我的 BINGO Board
中创建一个 table 编号
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace BingoCardGen
{
class Program
{
static void Main(string[] args)
{
string[,] table = new string[5, 5];
printboard(table);
Console.ReadKey();
}
此代码只会打印宾果游戏板。我缺少的代码是生成一个随机数并将其放在正确的列和单元格中
没有重复的数字
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();
}
}
}
}
我已经修改了你的代码来生成宾果卡:(后面有解释)
using System;
using System.Linq;
namespace BingoCardGen
{
class Program
{
private const int BingoSpacing = 5;
private const int BingoBallBuckets = 15;
private const int BingoCardLength = 5;
private const int BingoCardAmount = 3;
private static Random random = new Random();
static void Main()
{
for (int i = 0; i < BingoCardAmount; i++)
{
var table = new string[BingoCardLength, BingoCardLength];
PopulateBingoBoard(table);
PrintBoard(table);
Console.WriteLine();
}
Console.ReadKey();
}
private static void PopulateBingoBoard(string[,] table)
{
for (var col = 0; col < table.GetLength(0); col++)
{
// generate all possible bingo ball numbers for this column
var allNumbers = Enumerable.Range(1 + (col * BingoBallBuckets), BingoBallBuckets).ToList();
// randomly place those numbers into the rows of this column
for (var row = 0; row < table.GetLength(1); row++)
{
var chosenIndex = random.Next(allNumbers.Count);
table[row, col] = allNumbers[chosenIndex].ToString();
allNumbers.RemoveAt(chosenIndex);
}
}
// middle cell is always free
table[table.GetLength(0) / 2, table.GetLength(1) / 2] = "X";
}
private static void PrintBoard(string[,] table)
{
// print the title
string[] headings = { "B", "I", "N", "G", "O" };
for (var i = 0; i < headings.Length; i++)
{
Console.Write(headings[i].PadRight(BingoSpacing));
}
Console.WriteLine();
// print the numbers
for (var i = 0; i < table.GetLength(0); i++)
{
for (var j = 0; j < table.GetLength(1); j++)
{
Console.Write(table[i, j].PadRight(BingoSpacing));
}
Console.WriteLine();
}
}
}
}
我做了以下事情:
private const int BingoSpacing = 5;
private const int BingoBallBuckets = 15;
private const int BingoCardLength = 5;
private const int BingoCardAmount = 3;
我在代码的顶部定义了一些数字,因为这使它们易于更改并且使我们的代码更加readable/understandable。
private static void PopulateBingoBoard(string[,] table)
{
for (var col = 0; col < table.GetLength(0); col++)
{
// generate all possible bingo ball numbers for this column
var allNumbers = Enumerable.Range(1 + (col * BingoBallBuckets), BingoBallBuckets).ToList();
// randomly place those numbers into the rows of this column
for (var row = 0; row < table.GetLength(1); row++)
{
var chosenIndex = random.Next(allNumbers.Count);
table[row, col] = allNumbers[chosenIndex].ToString();
allNumbers.RemoveAt(chosenIndex);
}
}
// middle cell is always free
table[table.GetLength(0) / 2, table.GetLength(1) / 2] = "X";
}
这是新功能。我们发送数组,函数修改该数组。
对于每一列:
我们使用 Linq 生成该列的可能数字列表。例如,它会为第一列生成一个 { 1, 2, 3 ... 13, 14, 15 }
的列表。
代码然后循环遍历该列中的每个单元格,从列表中随机选择一个宾果球,将该宾果球编号放入 table,然后从列表中删除该宾果球。这确保没有重复项,因为球永远不会被选中两次。
Console.Write(table[i, j].PadRight(BingoSpacing));
我已将 PadRight
添加到所有输出。这确保一切都很好地排列。 PadRight
将在字符串的末尾添加空格,直到该字符串达到我们要求的长度。所以如果我们想要长度为 6 并且我们的字符串是 frog
,那么输出将是 frog
.
for (var i = 0; i < table.GetLength(0); i++)
我没有将 5
硬编码到输出循环中,而是改为使用数组长度。这意味着如果数组长度发生变化,代码将继续工作。
示例输出:
B I N G O
1 30 37 49 61
15 16 35 53 75
12 29 X 58 68
4 28 39 51 70
10 24 34 47 64
这是主要代码。我确实需要一个 Randomizer 编号并且无需重复即可在我的 BINGO Board
中创建一个 table 编号using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace BingoCardGen
{
class Program
{
static void Main(string[] args)
{
string[,] table = new string[5, 5];
printboard(table);
Console.ReadKey();
}
此代码只会打印宾果游戏板。我缺少的代码是生成一个随机数并将其放在正确的列和单元格中 没有重复的数字
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();
}
}
}
}
我已经修改了你的代码来生成宾果卡:(后面有解释)
using System;
using System.Linq;
namespace BingoCardGen
{
class Program
{
private const int BingoSpacing = 5;
private const int BingoBallBuckets = 15;
private const int BingoCardLength = 5;
private const int BingoCardAmount = 3;
private static Random random = new Random();
static void Main()
{
for (int i = 0; i < BingoCardAmount; i++)
{
var table = new string[BingoCardLength, BingoCardLength];
PopulateBingoBoard(table);
PrintBoard(table);
Console.WriteLine();
}
Console.ReadKey();
}
private static void PopulateBingoBoard(string[,] table)
{
for (var col = 0; col < table.GetLength(0); col++)
{
// generate all possible bingo ball numbers for this column
var allNumbers = Enumerable.Range(1 + (col * BingoBallBuckets), BingoBallBuckets).ToList();
// randomly place those numbers into the rows of this column
for (var row = 0; row < table.GetLength(1); row++)
{
var chosenIndex = random.Next(allNumbers.Count);
table[row, col] = allNumbers[chosenIndex].ToString();
allNumbers.RemoveAt(chosenIndex);
}
}
// middle cell is always free
table[table.GetLength(0) / 2, table.GetLength(1) / 2] = "X";
}
private static void PrintBoard(string[,] table)
{
// print the title
string[] headings = { "B", "I", "N", "G", "O" };
for (var i = 0; i < headings.Length; i++)
{
Console.Write(headings[i].PadRight(BingoSpacing));
}
Console.WriteLine();
// print the numbers
for (var i = 0; i < table.GetLength(0); i++)
{
for (var j = 0; j < table.GetLength(1); j++)
{
Console.Write(table[i, j].PadRight(BingoSpacing));
}
Console.WriteLine();
}
}
}
}
我做了以下事情:
private const int BingoSpacing = 5;
private const int BingoBallBuckets = 15;
private const int BingoCardLength = 5;
private const int BingoCardAmount = 3;
我在代码的顶部定义了一些数字,因为这使它们易于更改并且使我们的代码更加readable/understandable。
private static void PopulateBingoBoard(string[,] table)
{
for (var col = 0; col < table.GetLength(0); col++)
{
// generate all possible bingo ball numbers for this column
var allNumbers = Enumerable.Range(1 + (col * BingoBallBuckets), BingoBallBuckets).ToList();
// randomly place those numbers into the rows of this column
for (var row = 0; row < table.GetLength(1); row++)
{
var chosenIndex = random.Next(allNumbers.Count);
table[row, col] = allNumbers[chosenIndex].ToString();
allNumbers.RemoveAt(chosenIndex);
}
}
// middle cell is always free
table[table.GetLength(0) / 2, table.GetLength(1) / 2] = "X";
}
这是新功能。我们发送数组,函数修改该数组。
对于每一列:
我们使用 Linq 生成该列的可能数字列表。例如,它会为第一列生成一个 { 1, 2, 3 ... 13, 14, 15 }
的列表。
代码然后循环遍历该列中的每个单元格,从列表中随机选择一个宾果球,将该宾果球编号放入 table,然后从列表中删除该宾果球。这确保没有重复项,因为球永远不会被选中两次。
Console.Write(table[i, j].PadRight(BingoSpacing));
我已将 PadRight
添加到所有输出。这确保一切都很好地排列。 PadRight
将在字符串的末尾添加空格,直到该字符串达到我们要求的长度。所以如果我们想要长度为 6 并且我们的字符串是 frog
,那么输出将是 frog
.
for (var i = 0; i < table.GetLength(0); i++)
我没有将 5
硬编码到输出循环中,而是改为使用数组长度。这意味着如果数组长度发生变化,代码将继续工作。
示例输出:
B I N G O
1 30 37 49 61
15 16 35 53 75
12 29 X 58 68
4 28 39 51 70
10 24 34 47 64