943,940 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 712
  • C++ RSS
Apr 30th, 2006
0

been at this for 5 hrs now and can't figure it out please help...

Expand Post »
I have to create a 20 x 20 array (turtle program), I am sure you guys get these alot.. I have been working on this for way too long and I am not getting it.. I have read the chapter again and still am lost.. here is the code I have so far. I can't figure out where and how to get it to show the menu options so you can tell what you have the option of hitting to get the "turtle" to go where you want it and also how to display the grid after the turtle has moved... please help.. thank you,


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int penup(int &pen);
  7. int pendown(int &pen);
  8. int left_turn(int &dir);
  9. int right_turn(int &dir);
  10. int move(int &current_y,int &current_x);
  11. int getcommand(int &command);
  12.  
  13. const int y=20;
  14. const int x=20;
  15. int dir=1;
  16. int floor[y][x];
  17. int current_y=0;
  18. int current_x=0;
  19. int pen=1;
  20. int moves;
  21. int command;
  22. int i;
  23. int j;
  24.  
  25.  
  26. int main()
  27. {
  28.  
  29. int again=1;
  30.  
  31. if(again==1)
  32. {
  33. int dir=1;
  34. int floor[y][x];
  35. int current_y=0;
  36. int current_x=0;
  37. int pen=1;
  38. }
  39.  
  40.  
  41. for(i=0;i<=y;++i)
  42. {
  43. for(j=0;j<=x;++j)
  44. {
  45. floor[i][j]=0;
  46. }
  47. }
  48.  
  49.  
  50. while(again!=2)
  51. {
  52. do
  53. {
  54. if(pen==1)
  55. {
  56. cout << "current pen position is up!" << endl;
  57. }
  58. else
  59. {
  60. cout << "current pen position is down!" << endl;
  61. }
  62. if(dir==0)
  63. {
  64. cout<< "current direction is up!" << endl;
  65. }
  66. else if(dir==1)
  67. {
  68. cout << "current direction is right!" << endl;
  69. }
  70. else if (dir==2)
  71. {
  72. cout << "current direction is down!" << endl;
  73. }
  74. else if(dir==3)
  75. {
  76. cout << "current direction is left!" << endl;
  77. }
  78. cout << "current position are: (" << current_x << " ," <<
  79. current_y << ")\n\n";
  80.  
  81. cout << "please enter your command: ";
  82. cin >> command;
  83. cout << "\n\n";
  84.  
  85. getcommand(command);
  86.  
  87.  
  88. }while(command!=9);
  89.  
  90. cout << "do you want to draw again? (1=yes , 2=no)";
  91. cin >> again;
  92.  
  93. if(again!=1||2)
  94. {
  95. while(again!=1||2)
  96. {
  97. cout << "That was an incorrect input!\n" << "do you want to draw again? (1=yes , 2=no)";
  98. cin>>again;
  99. }
  100. }
  101.  
  102.  
  103. }
  104. return 0;
  105.  
  106. }
  107.  
  108. int getcommand(int &command)
  109. {
  110. if (command==1)
  111. {
  112. penup(pen);
  113. }
  114. else if (command==2)
  115. {
  116. pendown(pen);
  117. }
  118. else if (command==3)
  119. {
  120. right_turn(dir);
  121. }
  122. else if (command==4)
  123. {
  124. left_turn(dir);
  125. }
  126. else if (command==5)
  127. {
  128. cout << "please enter how many moves you want: ";
  129. cin >> moves;
  130. move(current_y,current_x);
  131. }
  132. else if (command==6)
  133. {
  134. system("cls");
  135. for(i=0;i<=y;++i)
  136. {
  137. for(j=0;j<=x;++j)
  138. {
  139. if(floor[i][j]==0)
  140. {
  141. cout << " ";
  142. }
  143. else if(floor[i][j]==1)
  144. {
  145. cout << "*";
  146. }
  147. }
  148. cout << endl;
  149. }
  150. }
  151. else if (command==9)
  152. {
  153. }
  154.  
  155. else
  156. {
  157. cout << "you did not put a correct command. please try again.";
  158.  
  159. }
  160. return command;
  161. }
  162.  
  163.  
  164. int penup(int &pen)
  165. {
  166. pen=1;
  167.  
  168. return pen;
  169. }
  170. int pendown(int &pen)
  171. {
  172. pen=2;
  173.  
  174. return pen;
  175. }
  176. int left_turn(int &dir)
  177. {
  178. --dir;
  179. if(dir<0)
  180. {
  181. dir=3;
  182. }
  183. return dir;
  184. }
  185. int right_turn(int &dir)
  186. {
  187. ++dir;
  188. if(dir>3)
  189. {
  190. dir=0;
  191. }
  192. return dir;
  193. }
  194. int move(int &current_y,int &current_x)
  195. {
  196. if(pen==1)
  197. {
  198. if(dir==0)
  199. {
  200. while((moves>0)&&(current_y>=0))
  201. {
  202. --current_y;
  203. --moves;
  204. }
  205. }
  206. else if(dir==1)
  207. {
  208. while((moves>0)&&(current_x<=19))
  209. {
  210.  
  211. ++current_x;
  212. --moves;
  213. }
  214. }
  215. else if(dir==2)
  216. {
  217. while((moves>0)&&(current_y<=19))
  218. {
  219.  
  220. ++current_y;
  221. --moves;
  222. }
  223. }
  224. else if(dir==3)
  225. {
  226. while((moves>0)&&(current_x>=0))
  227. {
  228.  
  229. --current_x;
  230. --moves;
  231. }
  232. }
  233. }
  234. else if(pen==2)
  235. {
  236. if(dir==0)
  237. {
  238. while((moves>0)&&(current_y>=0))
  239. {
  240.  
  241. floor[current_y][current_x]=1;
  242. --current_y;
  243. --moves;
  244. }
  245. }
  246. else if(dir==1)
  247. {
  248. while((moves>0)&&(current_x<=19))
  249. {
  250.  
  251. floor[current_y][current_x]=1;
  252. ++current_x;
  253. --moves;
  254. }
  255. }
  256. else if(dir==2)
  257. {
  258. while((moves>0)&&(current_y<=19))
  259. {
  260.  
  261. floor[current_y][current_x]=1;
  262. ++current_y;
  263. --moves;
  264. }
  265. }
  266. else if(dir==3)
  267. {
  268. while((moves>0)&&(current_x>=0))
  269. {
  270.  
  271. floor[current_y][current_x]=1;
  272. --current_x;
  273. --moves;
  274. }
  275. }
  276.  
  277. }
  278. return current_y,current_x;
  279. }
Last edited by Dave Sinkula; Apr 30th, 2006 at 7:04 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
totalnewb++ is offline Offline
4 posts
since Apr 2006
Apr 30th, 2006
0

Re: been at this for 5 hrs now and can't figure it out please help...

Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: need help creating a "stack" of cards
Next Thread in C++ Forum Timeline: c / c++ crocover





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC