C# 字谜游戏
我正在尝试创建一个单词搜索游戏。问题是我无法在TableLayoutPanel中插入单词。当我写这篇文章时,我得到了一个编译错误,它说“没有重载方法'placewords'接受'5'个参数C# 字谜游戏,c#,C#,我正在尝试创建一个单词搜索游戏。问题是我无法在TableLayoutPanel中插入单词。当我写这篇文章时,我得到了一个编译错误,它说“没有重载方法'placewords'接受'5'个参数 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Random r = new Random();
for (int a = 0; a < tableLayoutPanel1.ColumnCount; a++)
{
for (int b = 0; b < tableLayoutPanel1.RowCount; b++)
{
Label nl = new Label();
int x = r.Next(65, 90);
char c = (char)x;
nl.Text = c.ToString();
tableLayoutPanel1.Controls.Add(nl, a, b);
}
}
}
private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void PlaceWords()
{
string[] words = { "byte", "char" };
Random rn = new Random();
foreach (string p in words)
{
String s = p.Trim();
bool placed = false;// continue trying to place the word in // the matrix until it fits
while (placed == false)// generate a new random row and column
{
int nRow = rn.Next(30);// generate a new random x & y direction vector
int nCol = rn.Next(30);// x direction: -1, 0, or 1
int nDirX = 0; // y direction -1, 0, or 1
int nDirY = 0; // (although direction can never be 0, 0, this is null)
while (nDirX == 0 && nDirY == 0)
{
nDirX = rn.Next(3) - 1;
nDirY = rn.Next(3) - 1;
}
placed =PlaceWords(s.ToUpper(),nRow,nCol,nDirX,nDirY);
}
}
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
随机r=新随机();
对于(int a=0;a
您的PlaceWords方法不接受那么多参数,事实上,它不接受任何参数
此外,从外观上看,PlaceWords是一个递归函数,不会退出,从而导致堆栈溢出
要解决这个问题,您需要创建第二个PlaceWords函数,该函数接受所有5个参数,执行PlaceWords所做的任何操作,并返回一个布尔值。您的PlaceWords方法不接受那么多参数,事实上,它不接受任何参数 此外,从外观上看,PlaceWords是一个递归函数,不会退出,从而导致堆栈溢出
要解决此问题,您需要创建第二个PlaceWords函数,该函数接受所有5个参数,执行PlaceWords所做的任何操作,并返回一个布尔值。看起来Form1中的嵌套for循环应该将随机字符放入tableLayoutPanel1。然后需要调用PlaceWords()它将确定在单词列表中放置每个单词的位置和方向。在单词的末尾,您正在调用PlaceWords(s.ToUpper(),nRow,nCol,nDirX,nDirY),它应该实际将单词放入tableLayoutPanel1。这第二个带有5个参数的PlaceWords应该有不同的名称(我建议使用PlaceString);它不应该尝试调用它所在的Placewords方法。 然后,您需要编写方法PlaceString,该方法如下所示:
public bool PlaceString(string s, int nRow, int nCol, int nDirX, int nDirY)
{
/* whatever code you need to put the string into tableLayoutPanel1 */
}
看起来Form1_Load中的嵌套for循环应该将随机字符放入tableLayoutPanel1。然后需要调用PlaceWords(),它将确定在单词列表中放置每个单词的位置和方向。在PlaceWords的末尾,您将调用PlaceWords(s.ToUpper()、nRow、nCol、nDirX、nDirY)它应该实际将单词放入tableLayoutPanel1中。这个带有5个参数的第二个PlaceWords应该有不同的名称(我建议使用PlaceString);它不应该尝试调用它所在的同一个PlaceWords方法。 然后,您需要编写方法PlaceString,该方法如下所示:
public bool PlaceString(string s, int nRow, int nCol, int nDirX, int nDirY)
{
/* whatever code you need to put the string into tableLayoutPanel1 */
}
嗯,这里似乎缺少了太多的单词。家庭作业?如果是,请使用家庭作业标签。“当我写这个…”你真的写了这个代码吗?他的意思是你应该问代码的作者如何处理:)你明白(可以解释)你想用TableLayoutPanel和标签做什么?嗯,这里似乎缺少了太多的单词。家庭作业?如果是,请使用家庭作业标签。“当我写这个…”你真的写了这个代码吗?他的意思是你应该问代码的作者如何处理它:)你明白(可以解释)您想用TableLayoutPanel和标签做什么?