2010-01-06 82 views
0

我想创建一个字搜索游戏。问题是我无法将单词插入到TableLayoutPanel中。当我写到这,我得到了说“不超载的方法‘placewords’需要‘5’参数编译错误。字搜索益智游戏

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); 
       } 

     } 
    } 
+3

嗯,好像是PlaceWords这里失踪的过载。 – 2010-01-06 19:12:47

+0

作业?如果是这样,请使用作业标签。 – 2010-01-06 19:16:01

+2

“当我写这个......”你真的写了这段代码吗? – 2010-01-06 19:28:16

回答

4

你PlaceWords方法不接受,很多参数,事实上,它不接受参数。

更进一步,它的样子,你PlaceWords是不会退出递归函数,从而导致堆栈溢出。

要解决这个问题,你需要创建一个接受第二PlaceWords功能所有5个参数,并执行PlaceWords所做的任何操作,并返回布尔值。

0

它看起来像你在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 */ 
}