i got given this problem for homework here it is : create a simple hang man game with a set of related words in an array. these words should be selected randomly from the array and the correct number of characters in the selected word should be indicated on the screen. the player guesses letters from the word and, if these are in the word they are placed in the correct postion, otherwise one life is lost. the player as six lives.
the system should:
- record the letters that have been used by the player and not accept these again during the play.
- record the number of lives left after the word has been guessed and use these as a score.
- allow an end user to enter new sets of words.
the things that i have underlined i cannot do please help below is my code in a windows form application :-
public Form1(){
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
string word = "";
List
int number = 7;
int amount = 0;
string[] array = new string[1];
void makelabels()
{
word = getrandomword();
char[] chars = word.ToCharArray();
int between = 330 / chars.Length - 1;
for (int i = 0; i < chars.Length - 1; i++)
{
labels.Add(new Label());
labels[i].Location = new Point((i * between) + 10, 80);
labels[i].Text = "_";
labels[i].Parent = groupBox1;
labels[i].BringToFront();
labels[i].CreateControl();
}
label1.Text = "word length:" + (chars.Length - 1).ToString();
}
string getrandomword()
{
}
private void Form1_Load(object sender, EventArgs e)
{
makelabels();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
char letter = textBox2.Text.ToCharArray()[0];
if (!char.IsLetter(letter))
{
MessageBox.Show("you can only enter letters!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (word.Contains(letter))
{
char[] letters = word.ToCharArray();
for (int i = 0; i < letters.Length; i++)
{
if (letters[i] == letter)
labels[i].Text = letter.ToString();
}
foreach (Label l in labels)
if (l.Text == "_") return;
MessageBox.Show("you have won");
resetgame();
}
else
{
MessageBox.Show("the letter you guessed is wrong!", "sorry");
label2.Text += " " + letter.ToString() + ",";
amount++;
if (amount == 6)
{
MessageBox.Show("sorry you lost the word was: " + word);
resetgame();
}
}
}
void resetgame()
{
label2.Text = "Missed: ";
textBox1.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox2.Text == word)
{
MessageBox.Show("you won");
resetgame();
}
else
{
MessageBox.Show("you guessed wrong");
}
}
private void label3_Click(object sender, EventArgs e)
{
}
}
}
FroglegPosted Jun 26, 2012, 3:33 PM
joe hinderPosted Jun 26, 2012, 2:38 PM
FroglegPosted Jun 26, 2012, 2:11 PM
FroglegPosted Jun 26, 2012, 1:47 PM
The code probably needs changing because if you use all the lives, the program exits
On your previous question - look at this application
joe hinderPosted Jun 26, 2012, 12:39 PM
joe hinderPosted Jun 26, 2012, 2:04 AM
also thank you very much for all your help
FroglegPosted Jun 25, 2012, 4:41 PM
joe hinderPosted Jun 25, 2012, 3:44 PM
these are the things i cannot get to work
FroglegPosted Jun 24, 2012, 3:58 PM
C# operators
Equal to a == b
Not equal to a != b
Greater than a > b
Less than a < b
Greater than or equal to a >= b
Less than or equal to a <= b
Add x = 1 + 2;
joe hinderPosted Jun 24, 2012, 5:11 AM
FroglegPosted Jun 23, 2012, 3:14 PM
if (letterguess == guesss)//two ==
{
Console.WriteLine("you cannot type in the same letter twice");
}
else
{
}
joe hinderPosted Jun 23, 2012, 9:28 AM
char[] guesss = letterguess.ToCharArray();
FroglegPosted Jun 22, 2012, 3:38 PM
Do as much as you can and I'll help you through it
joe hinderPosted Jun 21, 2012, 1:45 PM
i got given this problem for homework here it is : create a simple hang man game with a set of related words in an array. these words should be selected randomly from the array and the correct number of characters in the selected word should be indicated on the screen. the player guesses letters from the word and, if these are in the word they are placed in the correct postion, otherwise one life is lost. the player as six lives.
the system should:
it wouldnt let me put it into a file sorry.
my code:
FroglegPosted Jun 18, 2012, 4:40 PM
Do as much as you can, then upload your project so we can help - but not do it all for you
joe hinderPosted Jun 18, 2012, 4:17 PM
FroglegPosted Jun 18, 2012, 2:45 PM
First off, as the user inputs a char, you will add that to an array - 26 letters long ie - inputChar[26]
Then create an integer called "missed" that increments each time an wrong letter choice is made
If the char matches the first letter of the word, then label1 will display that letter. You can have an integer say index =0
As correct letters are added to the label, index would increment - so the program knows where it is.
The index would also align with your chars array.
Second
At the next char input, you check if inputChar contains the new char,then the index to check the input char to see if chars[1]
is a match.
Increment index or missed as required
See how you go from there
Also a tip, upload your project rather than pasting code as it makes it
far easier to spot problems
joe hinderPosted Jun 18, 2012, 1:41 PM
How can i get that also to work even if they get the letter wrong
FroglegPosted Jun 17, 2012, 4:33 PM
To get the char pressed
1: Set form1 keyPreview to true
2:use
char input;
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsLetter(e.KeyChar))
{
input = e.KeyChar;
}
}
You can put the chars in an array, then with each key press
check if array contains the pressed char