direct robot

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2009
Posts: 77
Reputation: EvilLinux is an unknown quantity at this point 
Solved Threads: 0
EvilLinux EvilLinux is offline Offline
Junior Poster in Training

direct robot

 
0
  #1
Sep 25th, 2009
Hi, been working on this project and just confusing the heck out of myself with classes and stuff and was hoping maybe someone could lend a hand on what I might be doing wrong?

The general idea of this is to get the arrow (int 231-234) to point in the direct that the use clicked (n/s/w/e) and then when they press go 1 or go 10 it goes the direction selected and go either 1 or 10. But I have to limit it from going over 100 in any direct and show a message that they can't do that. And on load it set to 0,0 and set to north which I believe I have figured already.

General I'm confused on the class\method stuff, which is below, I think I got it right, but for some reason on my form I can't get it to find the getDirection function just errors up on all that.
I'm not sure if I coded something wrong or what, suggested hints or tricks would be helpful.
Thanks in advance.

Main Form:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10.  
  11. namespace simple_robot
  12. {
  13. public partial class Robot : Form
  14. {
  15. Robot myRobot = new Robot();
  16. public Robot()
  17. {
  18. InitializeComponent();
  19. pointer.Text = myRobot.getDirection().ToString();
  20. }
  21.  
  22. private void button3_Click(object sender, EventArgs e)
  23. {
  24. this.Close();
  25. }
  26.  
  27. private void label1_Click(object sender, EventArgs e)
  28. {
  29.  
  30. }
  31.  
  32. private void robotbox_SelectedIndexChanged(object sender, EventArgs e)
  33. {
  34.  
  35. }
  36.  
  37. private void label1_Click_1(object sender, EventArgs e)
  38. {
  39.  
  40. }
  41.  
  42. private void northBtn_Click(object sender, EventArgs e)
  43. {
  44. myRobot.setDirection(231);
  45. pointer.Text = myRobot.getDirection().ToString();
  46.  
  47. }
  48.  
  49. private void westBtn_Click(object sender, EventArgs e)
  50. {
  51. myRobot.SetDirection(232);
  52. pointer.Text = myRobot.getDirection().ToString();
  53. }
  54.  
  55. private void southBtn_Click(object sender, EventArgs e)
  56. {
  57. myRobot.SetDirection(233);
  58. pointer.Text = myRobot.getDirection().ToString();
  59. }
  60.  
  61. private void eastBtn_Click(object sender, EventArgs e)
  62. {
  63. myRobot.SetDirection(234);
  64. pointer.Text = myRobot.getDirection().ToString();
  65. }
  66.  
  67. private void label1_Click_2(object sender, EventArgs e)
  68. {
  69.  
  70. }
  71.  
  72. private void button1_Click(object sender, EventArgs e)
  73. {
  74. this.OnClick = myRobot.getX(myRobot.getX() + 1);
  75. }
  76.  
  77. private void button2_Click(object sender, EventArgs e)
  78. {
  79. this.OnClick = myRobot.getX(myRobot.getX() + 10);
  80. }
  81. }
  82. }

robot.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace simple_robot
  7. {
  8. class robot
  9. {
  10. private int direction;
  11. private int x;
  12. private int y;
  13.  
  14. public void setDirection(int newDirection)
  15. {
  16. direction = newDirection;
  17. }
  18.  
  19. public int getDirection(int direction)
  20. {
  21. return direction;
  22. }
  23.  
  24. public int getX()
  25. {
  26. return x;
  27. }
  28.  
  29. public int getY()
  30. {
  31. return y;
  32. }
  33.  
  34. public void move(int unitToMove)
  35. {
  36. }
  37.  
  38. public robot()
  39. {
  40. x = 0;
  41. y = 0;
  42. direction = 231;
  43. }
  44.  
  45. }
  46. }
Last edited by EvilLinux; Sep 25th, 2009 at 2:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,927
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: direct robot

 
0
  #2
Sep 25th, 2009
Whew!

Just going to concentrate on your robot class here.
Find out what automatic properties are.
I rewrote it so:
  1. class robot
  2. {
  3. public robot() // Default constructor
  4. {
  5. location = new Point(); // Sets location.X=0 and location.Y=0
  6. direction = 231;
  7. }
  8.  
  9. public int direction { get; set; } //auto property
  10. public Point location { get; set; } //auto property
  11.  
  12. public void move(int unitToMove)
  13. {
  14. }
  15. }
Use this class like this:
  1. robot Robby = new robot();
  2. Point P = new Point();
  3. P = Robby.location; // Get location of Robby
  4.  
  5. Point P2 = new Point();
  6. P2.X = 23; P2.Y = 100;
  7. Robby.location = P2; // Set location of Robby
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 77
Reputation: EvilLinux is an unknown quantity at this point 
Solved Threads: 0
EvilLinux EvilLinux is offline Offline
Junior Poster in Training

Re: direct robot

 
0
  #3
Sep 25th, 2009
Originally Posted by ddanbe View Post
Whew!

Just going to concentrate on your robot class here.
Find out what automatic properties are.
I rewrote it so:
  1. class robot
  2. {
  3. public robot() // Default constructor
  4. {
  5. location = new Point(); // Sets location.X=0 and location.Y=0
  6. direction = 231;
  7. }
  8.  
  9. public int direction { get; set; } //auto property
  10. public Point location { get; set; } //auto property
  11.  
  12. public void move(int unitToMove)
  13. {
  14. }
  15. }
Use this class like this:
  1. robot Robby = new robot();
  2. Point P = new Point();
  3. P = Robby.location; // Get location of Robby
  4.  
  5. Point P2 = new Point();
  6. P2.X = 23; P2.Y = 100;
  7. Robby.location = P2; // Set location of Robby
I'll play around with this, why the heck doesn't my book cover this, idk but wish it did because this makes more sense then C# 2.0 way ~_~.
Thanks, although confused on the second part of the code where do you work with that, I'm guessing the form itself?
Last edited by EvilLinux; Sep 25th, 2009 at 4:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,927
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: direct robot

 
0
  #4
Sep 25th, 2009
This is indeed example code in a form.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 77
Reputation: EvilLinux is an unknown quantity at this point 
Solved Threads: 0
EvilLinux EvilLinux is offline Offline
Junior Poster in Training

Re: direct robot

 
0
  #5
Sep 25th, 2009
Originally Posted by ddanbe View Post
This is indeed example code in a form.
ok cool, I'm going to keep trying to solve the project with the C# 2.0 way because idk what is required for the assignment, I'm figuring follow the way the book is teaching . SO going to have to use the old method of coding C# 2.0 crap. But if I give up on that I'll go with this way which seems a lot simpler.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 77
Reputation: EvilLinux is an unknown quantity at this point 
Solved Threads: 0
EvilLinux EvilLinux is offline Offline
Junior Poster in Training

Re: direct robot

 
0
  #6
Sep 25th, 2009
I'm going to feel slightly bad for wasting a bit of your time, I figured a big issue I had, one of my methods was called setDirection if you look in my form I put it as SetDirection and was causing HUGE issues. I think I'm more on track now with my form. I'll post if I need some help again, but I think I'm figure out the C#2.0 way lol.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,927
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: direct robot

 
0
  #7
Sep 25th, 2009
This is how I think your complete robot class might look like:
  1. class robot
  2. {
  3. public enum Direction
  4. {
  5. North, West, South, East
  6. }
  7.  
  8. public robot() // Default constructor
  9. {
  10. location = new Point(); // Sets location.X=0 and location.Y=0
  11. direction = Direction.North;
  12. }
  13.  
  14. public Direction direction { get; set; }
  15. public Point location { get; set; }
  16.  
  17. public void Move(int unitToMove)
  18. {
  19. Point P = new Point();
  20.  
  21. switch (direction)
  22. {
  23. case Direction.North:
  24. P.X = location.X;
  25. P.Y = location.Y - unitToMove;
  26. break;
  27. case Direction.West:
  28. P.X = location.X - unitToMove;
  29. P.Y = location.Y;
  30. break;
  31. case Direction.South:
  32. P.X = location.X;
  33. P.Y = location.Y + unitToMove;
  34. break;
  35. case Direction.East:
  36. P.X = location.X + unitToMove;
  37. P.Y = location.Y;
  38. break;
  39. default:
  40. break;
  41. }
  42. location = P;
  43. }
  44.  
  45. public void Draw(Graphics G)
  46. {
  47. // Our robot will look like a red ball
  48. // but you could draw anything here
  49. SolidBrush redBrush = new SolidBrush(Color.Red);
  50. Size S = new Size(40,40);
  51. Rectangle R = new Rectangle(location, S);
  52. // Make a red ball size 40,40 and put it at location
  53. G.FillEllipse(redBrush, R);
  54. }
  55. }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 77
Reputation: EvilLinux is an unknown quantity at this point 
Solved Threads: 0
EvilLinux EvilLinux is offline Offline
Junior Poster in Training

Re: direct robot

 
0
  #8
Sep 25th, 2009
Originally Posted by ddanbe View Post
This is how I think your complete robot class might look like:
  1. class robot
  2. {
  3. public enum Direction
  4. {
  5. North, West, South, East
  6. }
  7.  
  8. public robot() // Default constructor
  9. {
  10. location = new Point(); // Sets location.X=0 and location.Y=0
  11. direction = Direction.North;
  12. }
  13.  
  14. public Direction direction { get; set; }
  15. public Point location { get; set; }
  16.  
  17. public void Move(int unitToMove)
  18. {
  19. Point P = new Point();
  20.  
  21. switch (direction)
  22. {
  23. case Direction.North:
  24. P.X = location.X;
  25. P.Y = location.Y - unitToMove;
  26. break;
  27. case Direction.West:
  28. P.X = location.X - unitToMove;
  29. P.Y = location.Y;
  30. break;
  31. case Direction.South:
  32. P.X = location.X;
  33. P.Y = location.Y + unitToMove;
  34. break;
  35. case Direction.East:
  36. P.X = location.X + unitToMove;
  37. P.Y = location.Y;
  38. break;
  39. default:
  40. break;
  41. }
  42. location = P;
  43. }
  44.  
  45. public void Draw(Graphics G)
  46. {
  47. // Our robot will look like a red ball
  48. // but you could draw anything here
  49. SolidBrush redBrush = new SolidBrush(Color.Red);
  50. Size S = new Size(40,40);
  51. Rectangle R = new Rectangle(location, S);
  52. // Make a red ball size 40,40 and put it at location
  53. G.FillEllipse(redBrush, R);
  54. }
  55. }
Damn was pretty close to this. the switch direction part is what I was getting caught up on just now. I'm using a label that using Wingdings font with int 231-234 to cause the arrow, that is the only thing I can't figure out. Thank you again.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,927
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: direct robot

 
0
  #9
Sep 25th, 2009
I should also put a Panel on your Form, and use the Paint event of that Panel, so you can have a Graphics object to draw the robot in.
Seems to me much better than moving around a Label with a strange Font. Albeit, I must admit it was a nice try
Last edited by ddanbe; Sep 25th, 2009 at 6:14 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 77
Reputation: EvilLinux is an unknown quantity at this point 
Solved Threads: 0
EvilLinux EvilLinux is offline Offline
Junior Poster in Training

Re: direct robot

 
0
  #10
Sep 25th, 2009
Originally Posted by ddanbe View Post
I should also put a Panel on your Form, and use the Paint event of that Panel, so you can have a Graphics object to draw the robot in.
Seems to me much better than moving around a Label with a strange Font.
I know I'd love that also, but the assignment calls to use this retarded looking wingdingy thing.. I'm sure if I did a wonderful paint and all that it would look cool, but probably get an F for failing directions. I'm just trying to get through this stuff because I have a handle on it, but the assignments are 1 week long and are 50% not covered in the book which is why I come here for help .
Reply With Quote Quick reply to this message  
Reply

Tags
c#, directrobot, form, robot

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC