943,616 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3117
  • C++ RSS
Jun 3rd, 2004
1

Urgent! Change using class

Expand Post »
I need to change this code i have written into class

I need it done urgently, i have tried, and failed numerous times.

C++ Syntax (Toggle Plain Text)
  1. /* Assignment 2.cpp
  2. Michael Ainsworth
  3. 15737944
  4. 15.05.2004
  5. Assignment 2.cpp
  6. Computerized Sketchpad
  7. */
  8.  
  9. //Preprocessor Section
  10. #include <iostream>
  11. #include <stdlib.h>
  12.  
  13. using namespace std;
  14.  
  15. const int MAXCOMMANDS = 100;
  16. const int SIZE = 20; // grid is SIZE * SIZE
  17. const int RIGHT = 0; // facing right
  18. const int DOWN = 1; // facing down
  19. const int LEFT = 2; // facing left
  20. const int UP = 3; // facing up
  21.  
  22. //Main Program Section
  23. void ClearFloor(int floor[SIZE][SIZE])
  24. { // set the floor array to all clear
  25. for (int x = 0; x < SIZE; x++)
  26. {
  27. for (int y = 0; y < SIZE; y++)
  28. {
  29. floor[y][x] = 0;
  30. }
  31. }
  32. }
  33.  
  34. void ClearCommands(int commandArray[MAXCOMMANDS][2])
  35. { // empty the commands array
  36. for (int i = 0; i < MAXCOMMANDS; i++)
  37. {
  38. commandArray[i][0] = 0;
  39. commandArray[i][1] = 0;
  40. }
  41. }
  42.  
  43. void GetCommands(int commandArray[MAXCOMMANDS][2])
  44. {
  45. int count = 0; // count of commands so far received
  46. char buff[255]; // temp buffer for storing info in
  47. int command; // command request
  48. int distance; // distance to move if request is of type 5
  49. int i; // temp value
  50. char loadfile; // should we load from file
  51. bool bLoadFile; // are we loading from file
  52. FILE *fp = NULL; // file pointer we are reading from
  53.  
  54. cout << "Computerized Sketchpad" << endl;
  55.  
  56.  
  57. cout << "Would you like to load your commands from file (y/n) ? ";
  58. cin >> loadfile;
  59.  
  60. if (loadfile == 'y' || loadfile == 'Y')
  61. bLoadFile = true;
  62. else
  63. bLoadFile = false;
  64.  
  65. if (!bLoadFile)
  66. { // not loading from file so request manual command entry
  67. cout << endl;
  68. cout << "Please enter the commands you wish to execute, 1 per line." << endl << endl << "Enter 9 to finish" << endl << endl
  69. << "Command Meanings" << endl
  70. << "1:Pen up" << endl
  71. << "2:Pen down" << endl
  72. << "3:Turn Right" << endl
  73. << "4:Turn Left" << endl
  74. << "5,10: Move Forward 10 spaces" << endl
  75. << "6:Print the 20-by-20 array" << endl << endl
  76. << "(in this example)(eg. 5,5 would move the turtle forward 5 spaces)" << endl << endl;
  77.  
  78. }
  79.  
  80. else
  81. { // load commands from file
  82. do
  83. { // loop until we get a valid file
  84. cout << endl << "Type name of file with type of file example (test.txt)!" << endl << endl
  85. << "Filename?" ;
  86. cin >> buff;
  87. fp = fopen(buff, "rt"); // open the file as text
  88. if (fp == NULL)
  89. { // failed to open specified file, let them know
  90. cout << "Unable to open file '" << buff << "' Please try again" << endl;
  91. }
  92. }
  93. while (fp == NULL);
  94. }
  95.  
  96. do
  97. { // loop through until we get a command 9, end of file or reach MAXCOMMANDS
  98. command = 0;
  99. distance = 0;
  100. cout << "Command " << count + 1 << " : ";
  101. if (bLoadFile)
  102. { // load from file so get next line
  103. if (fgets(buff, 255, fp) == NULL)
  104. { // no line read, set buffer to 9 to trigger exit
  105. strcpy(buff, "9\n");
  106. }
  107. cout << buff; // output to screen what command we got from the file
  108. }
  109. else
  110. { // get command from user
  111. cin >> buff;
  112. }
  113. i = sscanf(buff, "%i, %i", &command, &distance);
  114. switch (i)
  115. { // how many items did we parse from the command
  116. case 1: // must be a 1,2,3,4,6 or 9 command
  117. if ((command > 0 && command < 5) || (command == 6) || (command == 9))
  118. { // is the commands we know
  119. commandArray[count][0] = command;
  120. commandArray[count][1] = 0;
  121. count++;
  122. }
  123. else if (command == 5)
  124. { // command 5 with no distance, complain
  125. cout << "Move command must have a distance component (eg 5,10), please re-enter" << endl;
  126. }
  127. else
  128. { // invalid command, complain
  129. cout << "unable to parse line '" << buff << "', please re-enter" << endl;
  130. }
  131. break;
  132. case 2: // must be a 5 command
  133. if (command == 5)
  134. { // it is a 5 command
  135. commandArray[count][0] = command;
  136. commandArray[count][1] = distance;
  137. count++;
  138. }
  139. else
  140. { // complain
  141. cout << "unable to parse line '" << buff << "', please re-enter" << endl;
  142. }
  143. break;
  144. default: // unknown, complain
  145. cout << "unable to parse line '" << buff << "', please re-enter" << endl;
  146. break;
  147. }
  148. }
  149. while ((command != 9) && (count < MAXCOMMANDS - 1));
  150.  
  151. system("pause"); // pause so they can see all commands before processing
  152.  
  153. commandArray[MAXCOMMANDS - 1][0] = 9; // just in case set the last entry in array to end command
  154.  
  155. if (fp != NULL) // close the file if it is open
  156. fclose(fp);
  157.  
  158. return;
  159. }
  160.  
  161. // Direction the pen is moving
  162. // 3 (up)
  163. // ^
  164. // |
  165. // (left) 2 <-+-> 0 (right)
  166. // |
  167. // v
  168. // 1 (down)
  169.  
  170. int turnRight(int direction)
  171. {
  172. // right -> down -> left -> up -> right ....
  173. direction++;
  174.  
  175. if (direction > UP) // rotated past up so reset to facing right
  176. direction = RIGHT;
  177.  
  178. return direction;
  179. }
  180.  
  181. int turnLeft(int direction)
  182. {
  183. // up -> left -> down -> right -> up ...
  184. direction--;
  185.  
  186. if (direction < RIGHT) // rotated past right so reset to facing up
  187. direction = UP;
  188.  
  189. return direction;
  190. }
  191.  
  192. void printArray(int floor[SIZE][SIZE])
  193. { // draw the sketch pad
  194. for (int y = 0; y < SIZE; y++)
  195. {
  196. for (int x = 0; x < SIZE; x++)
  197. {
  198. if (floor[y][x] == 0)
  199. cout << " "; // empty grid spot
  200. else
  201. cout << "*"; // active grid spot
  202. }
  203. cout << endl;
  204. }
  205. }
  206.  
  207. void movePen(bool penDown, int floor[SIZE][SIZE], int direction, int distance, int &x, int &y)
  208. {
  209. int xmov = 0;
  210. int ymov = 0;
  211. int value;
  212.  
  213. if (penDown == false)
  214. { // pen up so set floor values changed to 0 (unset)
  215. value = 0;
  216. }
  217. else
  218. { // pen down so set floor values to 1 (set)
  219. value = 1;
  220. }
  221.  
  222. switch (direction)
  223. { // assuming top,left is 0,0
  224. case LEFT: // xpos will decrease when moving left
  225. xmov = -1;
  226. break;
  227. case RIGHT: // xpos will increase when moving right
  228. xmov = 1;
  229. break;
  230. case UP: // ypos will descrease if moving up
  231. ymov = -1;
  232. break;
  233. case DOWN: // ypos will increase if moving down
  234. ymov = 1;
  235. break;
  236. }
  237.  
  238. // change the set/unset value of the current pen position
  239. floor[y][x] = value;
  240.  
  241. for(int i = 0; i < distance; i++)
  242. {
  243. x += xmov; // move our x position
  244. y += ymov; // move our y position
  245.  
  246. if (x < 0)
  247. x = 0;
  248. if (x >= SIZE)
  249. x = SIZE - 1;
  250. if (y < 0)
  251. y = 0;
  252. if (y >= SIZE)
  253. y = SIZE - 1;
  254.  
  255. // set/unset the value of the spot we have moved to.
  256. floor[y][x] = value;
  257. }
  258.  
  259. return;
  260. }
  261.  
  262. int main(int argc, char* argv[])
  263. {
  264. int floor[SIZE][SIZE] = {0}; // size of the drawing space on screen
  265. int command = 0; // records current command
  266. int distance = 0; // distance to be moved on the screen
  267. int direction = 0; // current direction
  268. int count = 0; // loop counter
  269. int x, y; // x & y pos on sketchpad
  270. int commandArray[MAXCOMMANDS][2] = {0}; // array to record a set of commands
  271. bool penDown = false; // pen position
  272. char again = 'n'; // program flag
  273.  
  274.  
  275. do
  276. {
  277. // initialize variables each time
  278. command = 0;
  279. distance = 0;
  280. direction = 0;
  281. count = 0;
  282. x = -1;
  283. y = -1;
  284. penDown = false;
  285. ClearFloor(floor);
  286. ClearCommands(commandArray);
  287. system("cls");
  288.  
  289. // get the commands
  290. GetCommands(commandArray);
  291. system("cls");
  292.  
  293. // retrieve first command
  294. command = commandArray[count][0];
  295.  
  296. while ( (command != 9))
  297. { // loop through commands until we hit marker
  298. switch ( command )
  299. {
  300. case 1: // penDown
  301. penDown = false;
  302. break;
  303. case 2: // penUp
  304. penDown = true;
  305. break;
  306. case 3: // turn right
  307. direction = turnRight(direction);
  308. break;
  309. case 4: // turn left
  310. direction = turnLeft(direction);
  311. break;
  312. case 5: // move the pen
  313. distance = commandArray[count][1];
  314. movePen(penDown, floor, direction, distance, x, y);
  315. break;
  316. case 6: // draw the sketch pad
  317. cout << endl << "The drawing is:" << endl << endl;
  318. printArray(floor);
  319. break;
  320. }
  321. command = commandArray[++count][0];
  322. }
  323.  
  324. cout << endl << "Do you want to draw again (y/n)? ";
  325. cin >> again;
  326. }
  327. while ( (again == 'y') || (again == 'Y') );
  328.  
  329. return 0;
  330. }

Thankyou, Mike
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Kaiser_Sosae is offline Offline
2 posts
since Jun 2004
Jun 3rd, 2004
0

Re: Urgent! Change using class

If anyone can help, i will greatfully give them a 50mb email account.

Thanks again
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Kaiser_Sosae is offline Offline
2 posts
since Jun 2004

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: Good literature on c++?
Next Thread in C++ Forum Timeline: Serial Port





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


Follow us on Twitter


© 2011 DaniWeb® LLC