4 Replies - 699 Views - Last Post: 07 August 2012 - 01:02 AM Rate Topic: -----

#1 rookie0Ken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-August 12

How to change the direction of an entity?

Posted 06 August 2012 - 03:54 AM

Hi am a rookie trying to get learn the ropes and become the best,
i have being trying to build a turtle graphics console app that simply modifies the values an of array element from 0 to 1, when the turtle moves depending on whether the pen is up or down. I am really new at this and so far the code i have seems too long, performs poorly and doesn't work. the thing is am kinda stuck on how to change the direction of the turtles motion. Any observations or criticism at all would help. here is the rookie code.
    class Turtle4
    {
        const int maxFloorSize = 20;

        enum turtDirection
        {
            TURN_RIGHT=1,
            TURN_DOWN=2,
            TURN_LEFT=3,
            TURN_UP=4
        }
        enum turtCommand
        {
            PEN_UP=1,
            PEN_DOWN=2,
            MOVE_RIGHT=3,
            MOVE_LEFT=4,
            SET_LENGHT=5,
            PRINT_FLOOR=6,
            CLEAR_FLOOR=7
        }
        static void Main(string[] args)
        {
            int x=0,
                y=0;
            int count = 0;
            int[,] floor = new int[maxFloorSize, maxFloorSize];
            bool penDown = false;
            int lenghtToMove = 0;
            int direction = (int)turtDirection.TURN_RIGHT;
            int command;
            do
            {
                Console.Write("Enter the commands to move the turtle as below"
                    + "\n" + "Type 1: for Pen Up" + "\n" + "Type 2: for Pen Down" + "\n"
                    + "type 3: for Move Right" + "\n" + "Type 4: to Move Left" + "\n" +
                    "type 5: to enter the distance" + "\n" + "Type 6: to Print" + "\n" +
                     "type 7: to clear floor" + "\n" + "Type 9: end programm" + "\n\n" +
                     "Please enter the command as per instructions above: ");
                command = Int32.Parse(Console.ReadLine());

                switch (command)
                {
                    case (int)turtCommand.PEN_UP:
                        penDown = false;
                        break;
                    case (int)turtCommand.PEN_DOWN:
                        penDown = true;
                        break;
                    case (int)turtCommand.MOVE_RIGHT:
                        direction = (int) turtDirection.TURN_RIGHT;
                        break;
                    case (int)turtCommand.MOVE_LEFT:
                        direction = (int)turtDirection.TURN_DOWN;
                        break;
                    case (int)turtCommand.SET_LENGHT:
                        Console.Write("Enter lenght to move (not exceeding 19): ");
                        lenghtToMove = Int32.Parse(Console.ReadLine());
                        moveTurtle(floor, ref y, ref x, lenghtToMove, penDown, direction);
                        break;
                    case (int)turtCommand.PRINT_FLOOR:
                        printFloor(floor);
                        break;
                    case (int)turtCommand.CLEAR_FLOOR:
                        clearFloor(floor);
                        break;
                    
                }
            } while (command != 9);

           
        }// end method Main

        static void printFloor(int[,] a)
        {
            for (int y = 0; y < a.GetLength(0); y++)
            {
                for (int x = 0; x < a.GetLength(1); x++)
                    Console.Write(a[y, x] + " ");
                Console.WriteLine();
            }
        }// end method printFloor
        static void clearFloor(int[,] a)
        {
            for (int y = 0; y < a.GetLength(0); y++)
            {
                for (int x = 0; x < a.GetLength(1); x++)
                    a[y, x] = 0;
            }
        }// end method clearFloor

        static void moveTurtle(int[,]tFloor,ref int yPos, ref int xPos,int tMoveLenght,bool tPenDown,int tDirection)
        {
            int y = yPos;
            int x = xPos;

            if (tPenDown == true)
            {
                switch (tDirection)
                {
                    case (int)turtDirection.TURN_RIGHT:
                        
                        for (; y <=y+ 1; y++)
                        {
                            for (; x < tMoveLenght; x++)
                            {
                                tFloor[y, x] = 1;
                                xPos = x;
                            }
                            yPos = y;
                        }
                        break;
                    case (int)turtDirection.TURN_DOWN:
                        for (; y < tMoveLenght; y++)
                        {
                            for (; x <= x+1; x++)
                            {
                                tFloor[y, x] = 1;
                                xPos = x;
                            }
                            yPos = y;
                        }
                        break;
                    case (int)turtDirection.TURN_LEFT:
                        for (; y >= y-1; y--)
                        {
                            for (; x >= x - tMoveLenght; x--)
                            {
                                tFloor[y, x] = 1;
                                xPos = x;
                            }
                            yPos = y;
                        }
                        break;
                    case (int)turtDirection.TURN_UP:
                        for (; y >=y- tMoveLenght; y--)
                        {
                            for (; x>=x- 1; x--)
                            {
                                tFloor[y, x] = 1;
                                xPos = x;
                            }
                            yPos = y;
                        }
                        break;
                    default:
                        break;
                }
            }
            if (tPenDown == false)
            {
                switch (tDirection)
                {
                    case (int)turtDirection.TURN_RIGHT:

                        for (; y <= y + 1; y++)
                        {
                            for (; x < tMoveLenght; x++)
                            {
                                xPos = x;
                            }
                            yPos = y;
                        }
                        break;
                    case (int)turtDirection.TURN_DOWN:
                        for (; y < tMoveLenght; y++)
                        {
                            for (; x <= x + 1; x++)
                            {
                                xPos = x;
                            }
                            yPos = y;
                        }
                        break;
                    case (int)turtDirection.TURN_LEFT:
                        for (; y >= y - 1; y--)
                        {
                            for (; x >= x - tMoveLenght; x--)
                            {
                                xPos = x;
                            }
                            yPos = y;
                        }
                        break;
                    case (int)turtDirection.TURN_UP:
                        for (; y >= y - tMoveLenght; y--)
                        {
                            for (; x >= x - 1; x--)
                            {
                               xPos = x;
                            }
                            yPos = y;
                        }
                        break;
                    default:
                        break;
                }
            }
        }// end method moveTurtle

        static int getDirection(ref int tCount,int tCommand, int tDirection)
        {
            tCount += tCommand;
            switch (tCount)
            {
                case 3:
                    return tDirection = (int)turtDirection.TURN_RIGHT;
                case 6:
                    return tDirection = (int)turtDirection.TURN_DOWN;
                case 9:
                    return tDirection = (int)turtDirection.TURN_LEFT;
                case 12:
                    tCount = 0;
                    return tDirection = (int)turtDirection.TURN_UP;
                case 4:
                    return tDirection = (int)turtDirection.TURN_LEFT;
                case 8:
                    return tDirection = (int)turtDirection.TURN_DOWN;
                case 16:
                    tCount = 0;
                    return tDirection = (int)turtDirection.TURN_UP;
                case 10:
                    return tDirection = (int)turtDirection.TURN_LEFT;
                case 11:
                    return tDirection = (int)turtDirection.TURN_RIGHT;
                default:
                    return tDirection = (int)turtDirection.TURN_RIGHT;
            }

        }//end method getDirection





- Mod edit -
Title updated. Please use descriptive titles when posting your questions. We already know you're posting a question, so calling the tread "Question" is of no use.

Also, I fix the code tags. They should be used like this:

:code:

This post has been edited by Atli: 06 August 2012 - 04:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to change the direction of an entity?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9574
  • View blog
  • Posts: 36,268
  • Joined: 12-June 08

Re: How to change the direction of an entity?

Posted 06 August 2012 - 08:09 AM

I am not seeing where the turtle is made part of the 'drawing the floor'... or the past lines.. or any of that movement data collected.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: How to change the direction of an entity?

Posted 06 August 2012 - 11:18 AM

You really something like this:
enum TurtleDirection
{
    North = 0,
    East = 1,
    South = 2,
    West = 3,
};



And the turn logic is basically this pseudo code:
int direction = (int) turtleDirection + 4;
if command is turn right
    direction++;
if command is turn left
    direction--;
turtleDirection = (TurtleDirection)(direction % 4);



And the move logic is basically this pseudo code:
Look up in table the turtleDirection:
{
North:    xDelta =  0;  yDelta = -1;
South:    xDelta =  0;  yDelta =  1;
East:     xDelta = -1;  yDelta =  0;
West:     xDelta =  1;  yDelta =  0;
}
for(int i = 0; i < length; i++)
{
   if pen is down
       mark surface
   xPos += xDelta;
   yPos += yDelta;
}


Was This Post Helpful? 2
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • Joined: 16-October 07

Re: How to change the direction of an entity?

Posted 06 August 2012 - 12:39 PM

You have enums, use them! Get all that crap out of main. Your turtle is an object. No static!

Just a little cleanup on what you have:
e.g.
class Turtle4 {
	enum Direction {
		TURN_RIGHT = 1,
		TURN_DOWN = 2,
		TURN_LEFT = 3,
		TURN_UP = 4
	}

	enum Command {
		PEN_UP = 1,
		PEN_DOWN = 2,
		MOVE_RIGHT = 3,
		MOVE_LEFT = 4,
		SET_LENGHT = 5,
		PRINT_FLOOR = 6,
		CLEAR_FLOOR = 7,
		END_PROGRAM = 9,
		INVALID_COMMAND = 10
	}

	const int maxFloorSize = 20;
	// this is the state of your object
	// you turtle
	private int[,] floor;
	private bool penDown;
	private Direction direction;
	private int yPos, xPos;

	public Turtle4() {
		// you initialize that state here
		this.penDown = false;
		this.direction = Direction.TURN_RIGHT;
		this.floor = new int[maxFloorSize, maxFloorSize];
		this.yPos = this.xPos = 0;
	}

	// use your enums!!!
	// note, C# methods start with caps
	private Command Menu() {
		Console.Write("Enter the commands to move the turtle as below\n"
			+ "Type 1: for Pen Up\n"
			+ "Type 2: for Pen Down\n"
			+ "type 3: for Move Right\n"
			+ "Type 4: to Move Left\n"
			+ "type 5: to enter the distance\n"
			+ "Type 6: to Print\n"
			+ "type 7: to clear floor\n"
			+ "Type 9: end programm\n"
			+ "\n"
			+ "Please enter the command as per instructions above: "
			);
		Command cmd = Command.INVALID_COMMAND;
		// yeah, the syntax is a little whack
		// it works
		try {
			cmd = (Command)Enum.Parse(typeof(Command), Console.ReadLine());
		} catch {
		}
		return cmd;
	}

	public void Run() {
		bool done = false; // standard game loop, continue until done
		while(!done) {
			switch (Menu()) { // your menu returns command: use it
				case Command.PEN_UP:
					penDown = false;
					break;
				case Command.PEN_DOWN:
					penDown = true;
					break;
				case Command.MOVE_RIGHT:
					direction = Direction.TURN_RIGHT;
					break;
				case Command.MOVE_LEFT:
					direction = Direction.TURN_DOWN;
					break;
				case Command.SET_LENGHT:
					EnterDistance();
					break;
				case Command.PRINT_FLOOR:
					PrintFloor();
					break;
				case Command.CLEAR_FLOOR:
					ClearFloor();
					break;
				case Command.END_PROGRAM:
					done = true;
					break;
				default:
					Console.Write("Unknown command");
					break;
			}
		}
	}

	private void EnterDistance() {
		Console.Write("Enter lenght to move (not exceeding 19): ");
		int lenghtToMove = Int32.Parse(Console.ReadLine());
		MoveTurtle(lenghtToMove);
	}


	private void PrintFloor() {
		for (int y = 0; y < floor.GetLength(0); y++) {
			for (int x = 0; x < floor.GetLength(1); x++) {
				Console.Write(floor[y, x] + " ");
			}
			Console.WriteLine();
		}
	}

	private void ClearFloor() {
		for (int y = 0; y < floor.GetLength(0); y++) {
			for (int x = 0; x < floor.GetLength(1); x++) {
				floor[y, x] = 0;
			}
		}
	}

	// not a bad method to have
	// private void Plot(int x, int y) { floor[y, x] = 1; }

	// or, better still, have a move method that plot depending on pen state
	private void Move(int x, int y) {
		this.xPos = x;
		this.yPos = y;
		if (this.penDown) {
			floor[this.yPos, this.xPos] = 1;
		}
	}

	// you only need the amount to move
	private void MoveTurtle(int tMoveLenght) {
		// your code here
	}

	static void Main(string[] args) {
		new Turtle4().Run();

	}

}



Now, really, what you need is something like this:
// you don't really need the numbers
enum TurnDirection { RIGHT, DOWN, LEFT, UP };

enum Direction { North, South, East, West }



When you turn, you decide what the current direction should be.

Alternately:
int dx, dy; // where value can be -1, 0, or 1




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

#5 rookie0Ken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-August 12

Re: How to change the direction of an entity?

Posted 07 August 2012 - 01:02 AM

Thanks, it helps, though i haven't yet delved too deep into object creation and manipulations am sure as i continue to learn i will fully understand the codes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1