943,908 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 572
  • C++ RSS
Jul 19th, 2008
0

Moveable character?

Expand Post »
Hey.
I am wanting to make this program so that the user can move the position of his characters. I have made a attempt but i am confused about some bits.

C++ Syntax (Toggle Plain Text)
  1. // Code Shark
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. char character = 'X', movement;
  9.  
  10. do {
  11. system("Cls");
  12.  
  13. cout << "Do you want your character to move [L]eft, [R]ight, [U]p or";
  14. cout << " [D]own or [E]xit: ";
  15. cin >> movement;
  16.  
  17. switch (movement)
  18. {
  19. case 'L':
  20. cout << "\b" << character;
  21. break;
  22.  
  23. case 'R':
  24. cout << " " << character;
  25. break;
  26.  
  27. case 'U':
  28. //cout << ???;
  29. break;
  30.  
  31. case 'D':
  32. cout << endl << character;
  33. /* the thing is if i just go endl here the character will
  34.   go straight to the start of the new line and most likelys
  35.   the character will be spaces away from it. */
  36. break;
  37.  
  38. case 'E':
  39. break;
  40. }
  41. }
  42. while(movement != 'E');
  43.  
  44. cin.get();
  45.  
  46. return 0;
  47. }

Some of the problems include: At the start of the Do loop the characters position will be deleted in my code because of the system clear. Another worry is that when the user wants to move up or down the character will be brought to the start of the line and that may not be directly under/above the last position. If you could help i would be very graceful.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Code Shark is offline Offline
14 posts
since Jul 2008
Jul 19th, 2008
0

Re: Moveable character?

Click to Expand / Collapse  Quote originally posted by Code Shark ...
Hey.
I am wanting to make this program so that the user can move the position of his characters. I have made a attempt but i am confused about some bits.

C++ Syntax (Toggle Plain Text)
  1. // Code Shark
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. char character = 'X', movement;
  9.  
  10. do {
  11. system("Cls");
  12.  
  13. cout << "Do you want your character to move [L]eft, [R]ight, [U]p or";
  14. cout << " [D]own or [E]xit: ";
  15. cin >> movement;
  16.  
  17. switch (movement)
  18. {
  19. case 'L':
  20. cout << "\b" << character;
  21. break;
  22.  
  23. case 'R':
  24. cout << " " << character;
  25. break;
  26.  
  27. case 'U':
  28. //cout << ???;
  29. break;
  30.  
  31. case 'D':
  32. cout << endl << character;
  33. /* the thing is if i just go endl here the character will
  34.   go straight to the start of the new line and most likelys
  35.   the character will be spaces away from it. */
  36. break;
  37.  
  38. case 'E':
  39. break;
  40. }
  41. }
  42. while(movement != 'E');
  43.  
  44. cin.get();
  45.  
  46. return 0;
  47. }

Some of the problems include: At the start of the Do loop the characters position will be deleted in my code because of the system clear. Another worry is that when the user wants to move up or down the character will be brought to the start of the line and that may not be directly under/above the last position. If you could help i would be very graceful.

I think the way to go is to keep track of the (x,y) coordinates of the character. When you move the character, decrement or increment the x or y coordinate of the character, then display it. The program below is a modification of your program where the character is represented by 'O' and it can move around in a 5 x 5 character grid. Empty spaces are denoted by 'X'. (xPos,yPos) represent the character's current location and the 5 x 5 grid is redrawn each trip through the do-while loop.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int main ()
  6. {
  7. char grid[5][5];
  8. for (int i = 0; i < 5; i++)
  9. {
  10. for (int j = 0; j < 5; j++)
  11. {
  12. grid[i][j] = 'X';
  13. }
  14. }
  15. char movement;
  16. int xPos = 2;
  17. int yPos = 2;
  18.  
  19. do
  20. {
  21. grid[xPos][yPos] = 'O';
  22. system("Cls");
  23.  
  24. for (int i = 0; i < 5; i++)
  25. {
  26. for (int j = 0; j < 5; j++)
  27. {
  28. cout << grid[j][i];
  29. }
  30.  
  31. cout << endl;
  32. }
  33.  
  34. cout << "Do you want your character to move [L]eft, [R]ight, [U]p or";
  35. cout << " [D]own or [E]xit: ";
  36. cin >> movement;
  37.  
  38. grid[xPos][yPos] = 'X';
  39.  
  40. switch (movement)
  41. {
  42. case 'L':
  43. if (xPos > 0)
  44. {
  45. xPos--;
  46. }
  47. break;
  48.  
  49. case 'R':
  50. if (xPos < 4)
  51. {
  52. xPos++;
  53. }
  54.  
  55. case 'U':
  56. if (yPos > 0)
  57. {
  58. yPos--;
  59. }
  60. break;
  61.  
  62. case 'D':
  63. if (yPos < 4)
  64. {
  65. yPos++;
  66. }
  67. break;
  68.  
  69. case 'E':
  70. break;
  71. }
  72. }
  73. while(movement != 'E');
  74.  
  75. cin.get();
  76.  
  77. return 0;
  78. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

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: Although it's not really important...
Next Thread in C++ Forum Timeline: Reading user input file into an array





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


Follow us on Twitter


© 2011 DaniWeb® LLC