2010-01-06 153 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 */ 
}