Benny
Benny

Reputation: 35

C# Help drawing letter (Word Game)

I'm creating a word game(lingo).This is how it works: A random word is selected from an array and the user has to guess the correct word. Then the word is printed out. Red: wrong letter Yellow: correct letter but wrong position Green :position and letter correct

Right now the problem is that it only draws a few letter of the guessed word. I also need to change the y value so that everything is not on the same line.

This is what i got so far.

    public string CorrectPlace; // Correct letter and postion
    public string WrongPlace; // Correct letter but incorrect postion
    public string NoneExist;  // Wrong
    public string inputwordstring; // user input
    public string CorrectWord; // Correct word
    public char[] CorrectWordchar;
    public char[] InputWord;
    int x;// position
    int y; //position
    public int points = 0;
    char replacemt = '#'; 
    public string[] Wordarray = null; // Declare string array
    public string getRandomWord() // generate random word
    {
        Random ran = new Random();
        return Wordarray[(ran.Next(0, Wordarray.Length - 1))];

    }

    public void Gamers() // 
    {
        Wordarray = new string[] { "radar", "andar", "axars", "rapar", "raser", "matar", "rikas", "ratas", "bakar", "bruka" }; // Dictionary
        CorrectWord = getRandomWord(); // store the random word in a string
    }
    public void CheckWords(string name, Graphics g)
    {

        if (CorrectWord == inputwordstring)
        {

            points += 100;
            MessageBox.Show("AMAZING");




        }
        for (int i = 0; i < CorrectWord.Length; i++)  
        {

            for (int b = 0; b < CorrectWord.Length; b++)
            {

                CorrectWordchar = CorrectWord.ToCharArray();
                InputWord = inputwordstring.ToCharArray();

                if (InputWord[i] == CorrectWordchar[b]) // check if the have the same index
                {
                    if (i == b)
                    {
                        CorrectPlace = CorrectWord;
                        SolidBrush s = new SolidBrush(Color.Green);
                        FontFamily ff = new FontFamily("Arial");
                        System.Drawing.Font font = new System.Drawing.Font(ff, 20);
                        g.DrawString(InputWord[i].ToString(), font, s, new PointF(x, y));
                        x += 20;
                        // draw out green letters
                        break;
                    }
                    else
                    {
                        if (InputWord[b] != CorrectWordchar[b])
                        {
                            SolidBrush s = new SolidBrush(Color.DarkOrange);
                            FontFamily ff = new FontFamily("Arial");
                            System.Drawing.Font font = new System.Drawing.Font(ff, 20);

                            g.DrawString(InputWord[b].ToString(), font, s, new PointF(x, y));
                            x += 20;
                            // Yellow letters

                            CorrectWordchar[b] = replacemt; // ersätter rätt bokstav med #
                            break;
                        }
                        else
                        {
                            b = 4;
                            SolidBrush s = new SolidBrush(Color.Red);
                            FontFamily ff = new FontFamily("Arial");
                            System.Drawing.Font font = new System.Drawing.Font(ff, 20);
                            g.DrawString(InputWord[b].ToString(), font, s, new PointF(x, y));
                            x += 20;
                            // Red letters
                            break;
                        }

                    }

                }
            }
        }
    }
}

}

Upvotes: 0

Views: 1023

Answers (1)

Matthias
Matthias

Reputation: 5764

Some comments and sample code.

  1. You have lots of duplicated code just because you are drawing red and green. This can be summarized.

  2. Most of the classes used in WinForms drawing have to be disposed. Otherwise you run into memory leaks and GDI+ leaks. Brushes, Fonts and others should be disposed.

  3. Use Graphics.MeasureString to get the size of each character. The resulting size can be used to forward in X and Y.

  4. The chars of a string can be accessed by an index directly. You don't need to convert them into char arrays.

    void YourDrawMethod(Graphics g) { var wrongBrush = new SolidBrush(Color.Red); var correctBrush = new SolidBrush(Color.Green);

    var ff = new FontFamily("Arial"); using(var font = new System.Drawing.Font(ff, 20)) { int x = 0; int y = 0;

    foreach(car letter in InputWord)
    {
      SolidBrush brush = InputWord[i] == CorrectWord[b] ? correctBrush : wrongBrush;
      g.DrawString(letter.ToString(), font, brush, new PointF(x, y));         
      Size sizeOfLetter = g.MeasureString(letter.ToString(), font);
      x += sizeOfLetter.Width;
    }
    

    }

    wrongBrush.Dispose(); correctBrush.Dispose(); }

Upvotes: 2

Related Questions