Computer Science, asked by dhanamvijay2002, 8 months ago

Write a program which simulates the movement of a turtle as follows:

At the beginning, the turtle is at the origin, i.e. point (0,0).  It is facing in the positive x direction. For the purpose of this exercise, let us assume that the coordinate axes are drawn as in mathematics, i.e. the x axis points in the East direction, and the y axis in the North direction.

The user can type commands to move the turtle.  The commands are:

     F : This causes the turtle to move 1 unit in the direction it

     is currently facing.


     L : This causes the turtle to turn left by 90 degrees.


     E : This causes the program to print the current coordinates of

     the turtle and stop execution.


You can assume that the user will not type any other letters besides the ones shown. Note that while there is no command to turn right, we can get the effect by using LLL, i.e. turning left thrice.
As an example, if the user input is "FFLLLFFLFE", then it will cause the turtle to move to (3,-2) and the program will print "3 -2" and stop.


Answers

Answered by Anonymous
3

# Python program

import turtle #Outside_In

wn = turtle.Screen()

wn.bgcolor("light green")

wn.title("Turtle")

skk = turtle.Turtle()

skk.color("blue")

def sqrfunc(size):

for i in range(4):

skk.fd(size)

skk.left(90)

size = size-5

sqrfunc(146)

sqrfunc(126)

sqrfunc(106)

sqrfunc(86)

sqrfunc(66)

sqrfunc(46)

sqrfunc(26)

import turtle #Inside_Out

wn = turtle.Screen()

wn.bgcolor("light green")

skk = turtle.Turtle()

skk.color("blue")

def sqrfunc(size):

for i in range(4):

skk.fd(size)

skk.left(90)

size = size + 5

sqrfunc(6)

sqrfunc(26)

sqrfunc(46)

sqrfunc(66)

sqrfunc(86)

sqrfunc(106)

sqrfunc(126)

sqrfunc(146)

#avi

Answered by sarahssynergy
0

Given is the program as required in C++ programming language.

Explanation:

  • here we first take user input for the turtle's movements in an array
  • than we run a loop over this array and apply nested if-else for each type of movements as described in the question accordingly.  
  • PROGRAM:                                                                                                  
  • # include<iostream>
  • using namespace std;
  • int main(){
  • int x=0,y=0,ang=90,l,i,q;
  • char s[1000];
  • cout<<"Enter the further movements of the turtle";
  • cin>>s;
  • for(i=0;s[i]!='\0';i++){
  • if(s[i]=='L') {
  •          ang=ang+90;  }
  • else if(s[i]=='F')  {
  •          quad=angle/90;
  •          if(quad%4==1){
  •                       x=x+1;  }
  •          else if(quad%4==2){
  •                        y=y+1;}
  •          else if(quad%4==3){
  •                        x=x-1;     }
  •          else {
  •                        y=y-1;   }
  •                          }
  • else if(s[i]=='E') {
  •          break; }     }
  • cout<<"Current co- ordinates of the turtle are: "<<x<<" "<<y<<endl;
  • }              

Similar questions