Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

im coding a word-guessing game in c# and I just finished with the code to displa

ID: 3698103 • Letter: I

Question

im coding a word-guessing game in c# and I just finished with the code to display the jumbled up word one each label in the form. However, when I run the program the form will not even pop up but there are no errors being recorded. Not exactly sure what I did wrong but here's my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Project10
{
public partial class Form1 : Form
{
private Label [] words;
private string[] pickedWords = new string[5];
private string[] jumbledWords = new string[5];
private TextBox[] guesses;
private Button check;
private Random r = new Random();
private int numWords;

public Form1()
{
InitializeComponent();

words = new Label[5];
words[0] = word1;
words[1] = word2;
words[2] = word3;
words[3] = word4;
words[4] = word5;

guesses = new TextBox[5];
guesses[0] = guess1;
guesses[1] = guess2;
guesses[2] = guess3;
guesses[3] = guess4;
guesses[4] = guess5;
numWords = words.Length;

for (int i = 0; i < words.Length; i++)
{
pickedWords[i] = pickWords(numWords);
}
for (int j = 0; j < pickedWords.Length; j++)
{
jumbledWords[j] = jumbleWord(pickedWords[j]);
}

for(int k = 0; k < words.Length; k++)
{
words[k].Text = "" + jumbledWords[k];
}
  

}

private string pickWords(int num)
{
int numWords;
string input;
List <string> words = new List<string>();
string filename = "words.txt";
StreamReader sr = new StreamReader(filename);
while ((input = sr.ReadLine()) != null)
{
words.Add(input);
}
sr.Close();

string word; ;
int sizeList = words.Count;
numWords = r.Next(sizeList);
word = words[numWords];
  
return word;
  
}

private string jumbleWord (string words)
{
int i = 0;
bool[] used = new bool[words.Length];
for(int j = 0; j < words.Length; j++)
{
used[j] = false;
}
string jumble = "";
StringBuilder sb = new StringBuilder(jumble);
int random;
while (jumble.Length < words.Length)
{
random = r.Next(words.Length);
if (used[random] == false)
{
used[random] = true;
sb.Insert(i, words[random]);
i++;
}

}
return jumble;
}

}
}

Explanation / Answer

1) There is error in your code. word1,word2,word3,word4,word5,word6,guess1,guess2,guess3,guess4,guess5,guess6 are not declared anywhere. you need to declare those variables and initialize them.

The issue in the following lines.

words = new Label[5];
words[0] = word1;
words[1] = word2;
words[2] = word3;
words[3] = word4;
words[4] = word5; guesses = new TextBox[5];
guesses[0] = guess1;
guesses[1] = guess2;
guesses[2] = guess3;
guesses[3] = guess4;
guesses[4] = guess5;