2010-01-06 4 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의 과부하있을 것 같습니다 . –

+0

숙제? 그렇다면 숙제 태그를 사용하십시오. –

+2

"내가 이것을 썼을 때 ..."이 코드를 실제로 작성 했습니까? –

답변

4

많은 매개 변수를 허용하지 않습니다 PlaceWords 방법은, 사실, 그것은 전혀 받아 매개 변수를 설정합니다. 또한 더

, 그것은 보이는 방법, 당신의 PlaceWords 스택 오버플로 이어지는 종료되지 않습니다 재귀 함수입니다.

는이 문제를 해결하려면, 당신이 받아들이는 두 번째 PlaceWords 기능을 작성해야 모든 5 개의 매개 변수와 PlaceWords의 기능을 수행하고 부울을 반환합니다.

0

Form1_Load에서 중첩 된 for 루프가 tableLayoutPanel1에 임의의 문자를 배치해야합니다. 그런 다음 단어 목록에 각 단어를 배치 할 위치와 방향을 결정하는 PlaceWords()를 호출해야합니다. PlaceWords가 끝날 무렵 placeWords (s.ToUpper(), nRow, nCol, nDirX, nDirY)가 실제로 tableLayoutPanel1에 단어를 삽입하게됩니다. 이 다섯 번째 매개 변수가있는 두 번째 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 */ 
}