oop - How to change orientation of robot in c# -
i have class called baserobot, following code
{ //=== defines possible orientation of robot. //=== note order important allow cycling performed in meaningful manner. public enum compass { north, east, south, west }; //=== basic robot. public class baserobot { //--- behaviour properties identified, associated state. //--- robot identification number. private int mid; public int id { { return mid; } } //--- direction in robot facing. private compass morientation; public compass orientation { { return morientation; } set { morientation = value; } } //--- robot's current position. private point mposition; public point position { { return mposition; } } //--- robot's home position, created. public point mhome; public point home { { return mhome; } } //--- turn orientation left (anti-clockwise) or right (clockwise). //--- implementation relies on n, e, s, w ordering of enumeration values allow arithmetic work. public void turnleft() { --morientation; if (morientation < 0) morientation = compass.west; } // end turnleft method. public void turnright() { morientation = (compass)(((int)morientation + 1) % 4); } // end turnright method. //--- move 1 unit forward in current orientation. public void move() { switch (morientation) { case compass.north: mposition.y++; break; case compass.east: mposition.x++; break; case compass.south: mposition.y--; break; case compass.west: mposition.x--; break; } } // end move method. //--- constructor methods. public baserobot(int aid) { mid = aid; mhome.x = 0; mhome = new point(0, 0); mposition = mhome; } public baserobot(int aid, int ax, int ay) { mid = aid; mhome = new point(ax, ay); mposition = mhome; } // end baserobot constructor methods. } // end baserobot class. } // end namespace. in main program class have this
//calling baserobot constructors var robot1 = new baserobot(0); var robot2 = new baserobot(0,0,0); console.writeline("===defualt robot==="); stringbuilder db = new stringbuilder(); db.appendformat("robot#1 has home @ <{0},{0}>. ", robot1.home.x, robot1.home.y); db.appendformat("it facing {0} ", robot1.orientation); db.appendformat("and @ <{0},{0}>.", robot1.position.x, robot1.position.y); console.writeline(db.tostring()); this works fine wondering how change way robot facing south, east , west? when change home coordinates of robot (35,12), unhandled error message saying index (zero based) must greater or equal 0 , less size of argument list.
the turnleft , turnright methods rotate robot modifying morientation field , since have provided setter property can change direction directly by:
robot.orientation = compass.west;
Comments
Post a Comment