Guest User

Untitled

a guest
Feb 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. (Turtle Graphics) The Logo language, which is popular among young computer users, made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the room under the control of a JavaScript program. The turtle holds a pen in one of two positions, up or down. When the pen is down, the turtle traces out shapes as it moves; when the pen is up, the turtle moves about freely without writing anything. In this problem, youll simulate the operation of the turtle and create a computerized sketchpad as well.Use a 20-by-20 array floor thats initialized to zeros. Read commands from an array that contains them. Keep track of the current position of the turtle at all times and of whether the pen is currently up or down. Assume that the turtle always starts at position (0, 0) of the floor, with its pen up. Suppose that the turtle is somewhere near the center of the floor. The following program would draw and print a 12-by-12 square, then leave the pen in the up position:
  2.  
  3. 2,5,12,3,5,12,3,5,12,3,5,12,1,6,9
  4.  
  5. Assume that the turtle always starts facing right at position 0,0 (upper left hand corner) of the floor with its pen up. The set of turtle commands your program must process are as follows:
  6.  
  7. Command Meaning
  8.  
  9. 1 Pen up
  10.  
  11. 2 Pen down = "*"
  12.  
  13. 3 Turn right
  14.  
  15. 4 Turn left
  16.  
  17. 5,10 Move forward 10 spaces (or a number other than 10)
  18.  
  19. 6 Print the 20-by-20 array
  20.  
  21. 9 End of data (sentinel)
  22.  
  23. <!DOCTYPE html>
  24. <html>
  25. <head>
  26. <title>ex10_15</title>
  27. <style>
  28. li {list-style-type: none;}
  29. </style>
  30. </head>
  31.  
  32. <script>
  33. var cols = 20;
  34. var rows = 20;
  35. var multiarray = [];//[[arraySize],[arraySize]];
  36. var text ="";
  37. //var array = [2,5,12,3,5,12,3,5,12,3,5,12,1,6,9];
  38. var emptySpace ='';
  39. var UsedSpace= "";
  40. var right = 0;
  41. var left = 0;
  42. function insertArray(){
  43. var x = document.getElementById("Input").value;
  44. if(x==1){
  45. emptySpace ='';
  46. document.getElementById("Input").value = "";
  47. }
  48. if(x==2){
  49. UsedSpace += "*";
  50. x = 0;
  51. right += 1;
  52. left += 1;
  53. document.getElementById("Input").value = "";
  54. }
  55. if(x==3){
  56. right+=1;
  57. document.getElementById("Input").value = "";
  58. }
  59. if(x==4){
  60. left+=1;
  61. document.getElementById("Input").value = "";
  62. }
  63. if(x==5 || x==10 || x==12){
  64. //UsedSpace += "*";
  65. if(right==1){
  66. window.alert("First");
  67. document.getElementById("Input").value = "";
  68. text += "<ul>";
  69. for(var i= 0; i <= 0; i++){
  70. for(var j = 0; j <= cols ; j++){
  71. text += "<li style="display:inline">" +multiarray[i][j]+"</li>";
  72. }
  73. }
  74. }
  75. if(right==2){
  76. window.alert("Second");
  77. document.getElementById("Input").value = "";
  78. for(var i = 0; i <= rows ; i++){
  79. for(var j = 20; j <= cols ; j++){
  80. text += "<li style="display:inline">" +multiarray[i][j]+"</li>";
  81. }
  82. }
  83. }
  84. if(right==3){
  85. window.alert("Third");
  86. document.getElementById("Input").value = "";
  87. for(var i=20; i = rows ; i){
  88. for(var j = 20 ; j>=0 ; j--){
  89. text += "<li style="display:inline">" +multiarray[i][j]+"</li>";
  90. }
  91. }
  92. }
  93. if(right==4){
  94. window.alert("Fourth");
  95. document.getElementById("Input").value = "";
  96. for(var i = 20 ; i >= 0 ; i--){
  97. for(var j = 0 ; j = 0 ; j){
  98. text += "<li style="display:inline">" +multiarray[i][j]+"</li>";
  99. }
  100. }
  101. text +="</ul>";
  102. }
  103. }
  104. if(x==6){
  105. window.alert("20");
  106. document.getElementById("Input").value = "";
  107. for (var i = 0; i < rows; i++) {
  108. for (var j = 0; j < cols; j++) {
  109. text += multiarray[i][j];
  110. }
  111. }
  112. }
  113. if(x==9){
  114. document.getElementById("Input").value = "";
  115. UsedSpace = "";
  116. }
  117. }
  118. function printturtle(){
  119. window.alert("Finish");
  120. document.getElementById("track").innerHTML = text;
  121. UsedSpace = "";
  122. }
  123. function resetturtletrack(){
  124. text = "";
  125. document.getElementById("track").innerText = "";
  126. right = 0;
  127. left = 0;
  128. UsedSpace = "";
  129. }
  130. function start(){
  131. var submit = document.getElementById("submit");
  132. submit.addEventListener("click",insertArray,false);
  133. var process = document.getElementById("command");
  134. process.addEventListener("click",printturtle,false);
  135. var reset = document.getElementById("reset");
  136. reset.addEventListener("click",resetturtletrack,false);
  137. }
  138. window.addEventListener("load",start,false);
  139. </script>
  140. <body>
  141. <h2>Turtle Graphics Commands</h2>
  142.  
  143. <ul id="menu">
  144. <li>
  145. 1:Pen Up
  146. </li>
  147. <li>
  148. 2:Pen Down
  149. </li>
  150. <li>
  151. 3:Turn Right
  152. </li>
  153. <li>
  154. 4:Turn Left
  155. </li>
  156. <li>
  157. 5,10:Move foward 10 spaces
  158. </li>
  159. <li>
  160. 6:Print 20 by 20 array
  161. </li>
  162. <li>
  163. 9:End of data (sentinel)
  164. </li>
  165. <li>
  166. Note: Enter the move command (5) as two commands
  167. </li>
  168. </ul>
  169. <br>
  170. <input type="text" id="Input"/>
  171. <input type="button" id="submit"value="Submit Command">
  172. <input type="button" id="command" value="Process Commands">
  173. <input type="button" id="reset" value="Reset">
  174. <br>
  175. <div id="track"></div>
  176.  
  177. </body>
  178. </html>
Add Comment
Please, Sign In to add comment