newgirl
newgirl

Reputation: 11

word search puzzle game

I am trying to create a word search game. the problem is I am unable to insert words into a TableLayoutPanel. When I wrote this, I got a compile error that says "no overload for method 'placewords' takes '5' arguments.

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

        }
    }

Upvotes: 1

Views: 3740

Answers (2)

Bill W
Bill W

Reputation: 1488

It looks like your nested for loops in Form1_Load should be placing random characters into tableLayoutPanel1. You then need to call PlaceWords() which will determine the location and direction to place each word in the words list. Near the end of PlaceWords you are calling PlaceWords(s.ToUpper(),nRow,nCol,nDirX,nDirY) which is supposed to actually put the word into tableLayoutPanel1. This second PlaceWords with 5 parameters should have a different name (I suggest PlaceString); it should not be trying to call the same Placewords method that it is in. You need to then write the method PlaceString that will look like:

public bool PlaceString(string s, int nRow, int nCol, int nDirX, int nDirY)
{
/* whatever code you need to put the string into tableLayoutPanel1 */
}

Upvotes: 0

Malfist
Malfist

Reputation: 31795

Your PlaceWords method doesn't accept that many parameters, in fact, it accepts no parameters.

Further more, the way it looks, your PlaceWords is a recursive function that won't exit, leading to a stack overflow.

To fix this, you need to create a second PlaceWords function that accepts all 5 parameters, and does whatever PlaceWords does, and returns a boolean.

Upvotes: 4

Related Questions