Turtle Graphics Version 2

8

2

Most of you have probably heard of the famous Turtle Graphics program, initially made popular by Logo. This challenge is to implement a Turtle Graphics program in your language of choice.

Rules:

  1. The floor must be a diagonal with dimensions input by the user through std-in in the form x/y, representing the x and y length, respectively. x may not be more than 20 and y may not be more than 45,

  2. When the pen is in the down state, it must draw in both the square (the x/y coordinate) the turtle leaves and the square the turtle enters. This is what would happen in real life, so that is what your program should do.

  3. Initial position and direction of the turtle must be input by the user in the form x,y,d, where x and y are the positions on the x- and y-axis, respectively, and d is the direction, entered as U, D, R, or L (up, down, right, and left, respectively).

  4. You must stop the turtle and display an error message if the turtle hits the wall. In other words, the turtle cannot walk through the wall.

  5. The turtle must include these commands:

RESET - Resets board to blank - turtle goes back to initial position and direction.

DRAW and ERASE - These commands set the turtle to drawing and erasing, respectively.

UP and DOWN - These two commands set the active tool (pen or eraser) position to up and down, respectively.

DIRECTION x - This command turns the turtle to any direction North, Northeast, East, Southeast, South, Southwest, West, or Northwest, represented as the digits 1-8, with North (up) being 1, Northeast being 2...

FORWARD xx - Advances turtle xx squares; FORWARD 15 advances the turtle 15 squares. If the turtle is facing N, E, S, of W, it will draw in x squares in that direction only. If the turtle is facing NE, NW, SE, or SW, it will draw in x squares in both directions; i.e. in FORWARD 1 while facing NE, it will draw this:

 X  
X

DISPLAY - Displays board, using X for filled squares (x/y coordinates) and a blank space for empty squares.

POSITION - Prints turtle's x/y coordinate on the board.

SETPOS x y d - Sets the turtle to the x/y coordinate facing direction d, using the same notation as the DIRECTION command.

STOP - Displays the board, and terminates the program after the user enters the character ~.

Example commands:

DIRECTION 2 FORWARD 1 DIRECTION 4 FORWARD 1:

 X  
X X 

DIRECTION 2 FORWARD 1 DIRECTION 4 FORWARD 4:

 X  
X X  
   X  
    X

DIRECTION 3 FORWARD 5:

XXXXX

This program will use standard scoring rules to try to allow non-esoteric programming languages, such as Java and C++, to compete.

Version 1 is here. It did not require user input of board size and turtle start point, nor did it require diagonal movements.

user10766

Posted 2014-03-07T20:38:43.287

Reputation:

Answers

2

Python 3.3 (354?)

I noticed this as an unanswered question and decided to give it a go:

v=input().split('/')+input().split(',')+[input().split(' '),0,1,'X','U','D','L','R']
w,h,x,y,d,c,p,r,m,n,s,l,e=v
w,h,x,y=int(w),int(h),int(x),int(y)
g=[[' 'for x in range(int(h))]for y in range(int(w))]
while p<len(c):
 i=c[p]
 if r:
  g[y][x]=m
 if i=='RESET':
  w,h,x,y,d,c,p,r,m,n,s,l,e=v
 if i=='DRAW':
  m='X'
 if i=='ERASE':
  m=' '
 if i=='UP':
  r=0
 if i=='DOWN':
  r=1
 if i=='DIRECTION':
  p+=1
  i=int(c[p])
  d=''
  if i in[8,1,2]:
   d+=n
  if i in[4,5,6]:
   d+=s
  if i in[6,7,8]:
   d+=l
  if i in[2,3,4]:
   d+=e
 if i=='FORWARD':
  p+=1
  i=int(c[p])
  for _ in range(i):
   if d.find(n)+1:
    y-=1
   if d.find(s)+1:
    y+=1
   if d.find(l)+1:
    x-=1
   if d.find(e)+1:
    x+=1
   if r:
    g[y][x]=m
 if i=='DISPLAY':
  for z in g:
   print(''.join(z))
 if i=='POSITION':
  print(x,y)
 if i=='SETPOS':
  p+=3
  x,y=c[p-2:p]
  d=''
  if i in[8,1,2]:
   d+=n
  if i in[4,5,6]:
   d+=s
  if i in[6,7,8]:
   d+=w
  if i in[2,3,4]:
   d+=e
 if i=='STOP':
  while input()!='~':
   pass
  exit
 if x>w or x<0 or y>h or y<0:
  print('!')
  exit
 p+=1

PS I am unsure about the scoring setup so my score may be incorrect!

kitcar2000

Posted 2014-03-07T20:38:43.287

Reputation: 2 689

Nice. I guess you win. – None – 2014-05-02T15:55:10.400