Skip to main content
 首页 » 编程设计

c#之单词搜索益智游戏

2024年02月24日27mate10pro

我正在尝试创建一个单词搜索游戏。问题是我无法将单词插入 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); 
               } 
 
        } 
    } 

请您参考如下方法:

您的 PlaceWords 方法不接受那么多参数,事实上,它不接受任何参数。

此外,从外观上看,您的 PlaceWords 是一个不会退出的递归函数,会导致堆栈溢出。

要解决此问题,您需要创建第二个 PlaceWords 函数,它接受所有 5 个参数,执行 PlaceWords 执行的任何操作,并返回一个 bool 值。