Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

I have the following code that i coded in c# but for some reason the code is not doing what it is supposed to .

I have the following code that i coded in c# but for some reason the code is not doing what it is supposed to . The code needs a gui that i created using codes but it is still giving errors

Please fix the code for me

Thank you!

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.Net; //allowing access to the internet

namespace WindowsFormsApp2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string word = ""; List

enum BodyParts { Head, LeftEye, RightEye, Mouth, LeftArm, RightArm, Body, LeftLeg, RightLeg } void DrawHangPole() { Graphics g = flowLayoutPanel1.CreateGraphics(); Pen p = new Pen(Color.Blue, 12); g.DrawLine(p, new Point(130, 218), new Point(130, 5)); g.DrawLine(p, new Point(135, 5), new Point(65, 5)); g.DrawLine(p, new Point(60, 0), new Point(60, 50)); DrawBodyPart(BodyParts.Head); DrawBodyPart(BodyParts.LeftEye); DrawBodyPart(BodyParts.RightEye); DrawBodyPart(BodyParts.Mouth); DrawBodyPart(BodyParts.Body); DrawBodyPart(BodyParts.LeftArm); DrawBodyPart(BodyParts.RightArm); DrawBodyPart(BodyParts.LeftLeg); DrawBodyPart(BodyParts.RightLeg); MessageBox.Show(GenerateRandomWord());

}

void DrawBodyPart(BodyParts bp) { Graphics g = flowLayoutPanel1.CreateGraphics(); Pen p = new Pen(Color.Red, 3); if (bp == BodyParts.Head) g.DrawEllipse(p, 40, 50, 40, 40); else if (bp == BodyParts.LeftEye) { SolidBrush s = new SolidBrush(Color.Black); g.FillEllipse(s, 50, 60, 7, 7); //adding eyes } else if (bp == BodyParts.RightEye) { SolidBrush s = new SolidBrush(Color.Black); g.FillEllipse(s, 62, 60, 7, 7); } else if (bp == BodyParts.Mouth) { SolidBrush s = new SolidBrush(Color.Black); g.DrawArc(p, 50, 60, 20, 20, 45, 90); } else if (bp == BodyParts.Body) { g.DrawLine(p, new Point(60, 90), new Point(60, 170)); //adding body } else if (bp == BodyParts.LeftArm) { g.DrawLine(p, new Point(60, 100), new Point(10, 130)); // adding arms

} else if (bp == BodyParts.RightArm) { g.DrawLine(p, new Point(60, 100), new Point(100, 130));

} else if (bp == BodyParts.LeftLeg) { g.DrawLine(p, new Point(60, 170), new Point(30, 200));

} else if (bp == BodyParts.RightLeg) { g.DrawLine(p, new Point(60, 170), new Point(90, 200));

}

} void MakeLabels() { word = GenerateRandomWord(); char[] chars = word.ToCharArray(); //Inserting characters into character array int between = 330 / chars.Length - 1; //minus 1 character due to (adding new line) for (int i = 0; i < chars.Length - 1; i++) //incrementing i { labels.Add(new Label()); labels[i].Location = new Point((i * between + 10, 80)); //space between each label labels[i].Text = "_"; labels[i].Parent = groupBox2; labels[i].BringToFront(); //bringing label on top of every control labels[i].CreateControl(); } label1.Text = "Word Length: " + (chars.Length - 1.ToString()); }

string GenerateRandomWord() { WebClient wc = new WebClient(); string wordList = wc.DownloadString("https://www.mit.edu/~ecprice/wordlist.10000"); //using random wordlist from the internet string[] words = wordList.Split(' '); Random r = new Random(); return words[r.Next(0, words.Length - 1)]; } private void groupBox2_Enter(object sender, EventArgs e) {

}

private void button1_Click(object sender, EventArgs e) { char letter = textBox1.Text.ToLower().ToCharArray()[0]; if (!char.IsLetter(letter)) { MessageBox.Show("You can only submit letters!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (word.Contains(letter)) { char[] letters = word.ToCharArray(); //creating array for all words generated for (int i = 0; i < letters.Length; i++)//going thru every letter in the word { if (letters[i] == letter) labels[i].Text = letter.ToString(); } foreach (Label l in labels) if (l.Text == "_") return; MessageBox.Show("You Won!", "Well Done!"); ResetGame(); } else { MessageBox.Show("Incorrect Guess!", "Sorry"); label2.Text += " " + letter.ToString() + ","; DrawBodyPart((BodyParts)amount); amount++; if (amount == 8) { MessageBox.Show("You Lost! The word was" + word); ResetGame(); } } } void ResetGame() { Graphics g = flowLayoutPanel1.CreateGraphics(); g.Clear(flowLayoutPanel1.BackColor); GenerateRandomWord(); MakeLabels(); DrawHangPole(); label2.Text = "Missed: "; textBox1.Text = ""; }

private void Form1_Load(object sender, EventArgs e) {

}

private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e) {

}

private void Form1_Shown(object sender, EventArgs e) { DrawHangPole(); MakeLabels(); }

private void label2_Click(object sender, EventArgs e) {

}

private void button2_Click(object sender, EventArgs e) { if (textBox2.Text == word) ; word = textBox2.Text.ToLower().ToCharArray()[0]; { MessageBox.Show("You Won!", "Well Done!"); ResetGame(); } else { MessageBox.Show("Incorrect Guess!", "Sorry"); DrawBodyPart((BodyParts)amount); amount++; if (amount == 9) { MessageBox.Show("You Lost! The word was" + word); ResetGame(); } } } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions