c# - 单词搜索益智游戏

标签 c#

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

关于c# - 单词搜索益智游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2015456/

相关文章:

c# - 正则表达式匹配不一致

c# - 将字符串中的二进制数据作为 COM 事件参数传递

c# - 如何在 WebBrowser 控件中注入(inject) Javascript?

c# - ASP.NET core 中第三方数据上下文的依赖注入(inject)

c# - 有 parent 和 child 的树结构

c# - Xamarin。加载mp3以通过MediaPlayer播放时遇到问题:FileNotFoundException

c# - 使用 String.Contains 的 EF Core 查询

c# - WPF 动画宽度和高度,后面有代码(放大)

c# - 在 .NET 4.5 中为 websocket 握手设置自定义 header

c# - DLL 'cvCreateContourTree'中的 'opencv_imgproc231'-已弃用?