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:
This post has been edited by Atli: 06 August 2012 - 04:15 AM

New Topic/Question
Reply



MultiQuote








|