5 Replies - 4057 Views - Last Post: 02 June 2012 - 02:11 PM Rate Topic: -----

#1 asymptotes14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 27-February 11

using classes

Posted 02 June 2012 - 05:48 AM

so i have this simple game code and i wanted to optimize it
i wanted to implement classes on my program but im having troubles at some point
i was able to use a class to generate a random word
can anyone give me some points on how i can use more classes?
here is the code

namespace HangmanGame
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string word = "";
        int amount = 0;
        
        List <Label> label = new List<Label>();
        GetRandomWord gw = new GetRandomWord();
        Draw draw = new Draw();       
        
        enum Bodyparts
        {
            Head,
            Left_Eye,
            Right_Eye,
            Mouth,
            Right_Arm,
            Left_Arm,
            Body,
            Right_Leg,
            Left_Leg
        } 


        void DrawBodyParts(Bodyparts bp)
         {
             Graphics g = panel1.CreateGraphics();
             Pen p = new Pen(Color.Blue,2);
             if (bp == Bodyparts.Head)
                 g.DrawEllipse(p, 40, 50, 40, 40);
             else if (bp == Bodyparts.Left_Eye)
             {
                 SolidBrush s = new SolidBrush(Color.Black);
                 g.FillEllipse(s, 50, 60, 5, 5);
             }
             else if (bp == Bodyparts.Right_Eye)
             {
                 SolidBrush s = new SolidBrush(Color.Black);
                 g.FillEllipse(s,63,60,5,5);
             }
             else if (bp == Bodyparts.Mouth)
             {
                 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));
             }
             else if (bp == Bodyparts.Left_Arm)
             {
                 g.DrawLine(p, new Point(60, 100), new Point(30, 85));
             }
             else if (bp == Bodyparts.Right_Arm)
             {
                 g.DrawLine(p, new Point(60, 100), new Point(90, 85));
             }
             else if (bp == Bodyparts.Right_Leg)
             {
                 g.DrawLine(p, new Point(60, 170), new Point(30, 190));
             }
             else if (bp == Bodyparts.Left_Leg)
             {
                 g.DrawLine(p, new Point(60, 170), new Point(90, 190));
             }
            
         }
        
        void MakeLabels()
        {
            word = gw.GetWord();
            char[] chars = word.ToCharArray();
            int bet = 330 / chars.Length ;
            for (int i = 0; i < chars.Length; i++)
            {
                label.Add(new Label());
                label[i].Location = new Point((i *bet)+ 10,80);
                label[i].Text = "_";
                label[i].Parent = groupBox2;
                label[i].BringToFront();
                label[i].CreateControl();
            }
            label1.Text = "Word Length:" + (chars.Length).ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            Graphics g = panel1.CreateGraphics();
            draw.DrawHangPost(g);
            MakeLabels();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = panel1.CreateGraphics();
            char letter = textBox1.Text.ToLower().ToCharArray()[0];
            if (!char.IsLetter(letter))
            {
                MessageBox.Show("You can only submit letters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (word.Contains(letter))
            {
                char[] letters = word.ToCharArray();
                for (int i = 0; i < letters.Length; i++)
                {
                    if (letters[i] == letter)
                        label[i].Text = letter.ToString();
                }
                foreach (Label l in label)
                    if (l.Text == "_") return;
                MessageBox.Show("You have won!", "Congrats");
                ResetGame();
            }
            else
            {
                
                MessageBox.Show("Letter not in the word", "Sorry");
                label2.Text += " " + letter.ToString() + ", ";             
                DrawBodyParts((Bodyparts)amount);
                amount++;
                if (amount == 9)
                {
                    MessageBox.Show("Sorry but you lose!The word was:  "+ word);
                    ResetGame();
                }
            }
        }

        void ResetGame()
        {
            Graphics g = panel1.CreateGraphics();
            g.Clear(panel1.BackColor);
            gw.GetWord();
            MakeLabels();
            draw.DrawHangPost(g);
            label2.Text = "Missed: ";
            textBox1.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == word)
            {
                MessageBox.Show("You have won!", "Congrats");
                ResetGame();
            }
            else
            {
                MessageBox.Show("The word you guessed is wrong!", "Sorry");
                DrawBodyParts((Bodyparts)amount);
                amount++;
                if (amount == 9)
                {
                    MessageBox.Show("Sorry but you lose!The word was:  " + word);
                    ResetGame();
                }
                
            }


        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: using classes

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,731
  • Joined: 02-June 10

Re: using classes

Posted 02 June 2012 - 11:35 AM

Your thread has been moved to C# help, not C# challenges.


So your question is "how can I use classes to write my homework?" ??

Well that pretty much equates to either "Will someone do my program design for me?" or "Will someone do my thinking for me?"

Do you understand classes? Maybe this will help you understand them. Once you understand them, you may see how you can use them.

Spoiler


Beyond that... Your question is just far too vague. We can't teach you all about classes in a thread just because you didn't pay attention in class or won't re-read that chapter in your textbook.

You might as well be asking "how can I use metal to build car?"
Can you see how the vagueness of the question makes it impossible to answer in this context?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,731
  • Joined: 02-June 10

Re: using classes

Posted 02 June 2012 - 11:42 AM

Let me say that I think you did a good job of breaking up tasks into decently named methods. Lots of rookies take a while to see the advantage of this.

Some suggestions:
Get rid of that horrible if...if...if... chain and use a switch construct.

You might want to make the comparrison of the player's guessed word not be case sensitive.
152 if (textBox2.Text == word)
If they guess "TREE" and the word was "tree" this will fail when they really did get the word right.


Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,627
  • Joined: 16-October 07

Re: using classes

Posted 02 June 2012 - 11:44 AM

My first thought, looking that is, is that Graphics g = panel1.CreateGraphics(); is a super messy thing to do. You should be working with the paint of your control or form.

Make a user control to draw you man. That will move a lot of you code for drawing where it should be. That enum isn't being use as an enum, so don't bother. You have a maximum number of misses the user can have, just use that.

This was fun, so I did the control:
public partial class UcMan : UserControl {
	private Pen defaultPen;
	private SolidBrush defaultBrush;
	public UcMan() {
		InitializeComponent();
		defaultPen = new Pen(Color.Blue, 2);
		defaultBrush = new SolidBrush(Color.Black);
		Reset();
	}

	public void NextStep() {
		if (this.CurrentStep < this.LastStep) { this.CurrentStep++; }
		this.Refresh();
	}

	public void Reset() { CurrentStep = 0; }

	public int LastStep { get { return 9; } }

	public int CurrentStep { get; private set; }

	public bool IsDone { get { return this.CurrentStep >= this.LastStep; } }

	private void DrawMan(Graphics g) {
		if (CurrentStep > 0) { g.DrawEllipse(defaultPen, 40, 50, 40, 40); }
		if (CurrentStep > 1) { g.FillEllipse(defaultBrush, 50, 60, 5, 5); }
		if (CurrentStep > 2) { g.FillEllipse(defaultBrush, 63, 60, 5, 5); }
		if (CurrentStep > 3) { g.DrawArc(defaultPen, 50, 60, 20, 20, 45, 90); }
		if (CurrentStep > 4) { g.DrawLine(defaultPen, new Point(60, 90), new Point(60, 170)); }
		if (CurrentStep > 5) { g.DrawLine(defaultPen, new Point(60, 100), new Point(30, 85)); }
		if (CurrentStep > 6) { g.DrawLine(defaultPen, new Point(60, 100), new Point(90, 85)); }
		if (CurrentStep > 7) { g.DrawLine(defaultPen, new Point(60, 170), new Point(30, 190)); }
		if (CurrentStep > 8) { g.DrawLine(defaultPen, new Point(60, 170), new Point(90, 190)); }
	}

	private void UcMan_Paint(object sender, PaintEventArgs e) { DrawMan(e.Graphics); }

}



It should be clear how to throw this in. Use it in place of panel1. Don't store the CurrentStep in your Form, just use it here.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,731
  • Joined: 02-June 10

Re: using classes

Posted 02 June 2012 - 12:11 PM

asymptotes14 please be sure to let baavgai know what grade he got on your homework.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,627
  • Joined: 16-October 07

Re: using classes

Posted 02 June 2012 - 02:11 PM

Bah! The code I offered is the OP's, with but minor rearrangement. To obfuscate it or hint at the idea I wished to illustrate would only cause more, not less, confusion.

I would add that the MakeLabels and those related objects would also make sense in a user control.

Another organization would be to have an object that strictly has game state; current word and guesses. That object could be shared among other elements.

There are any number of ways to organize the code. Or any code. No one way is correct.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1