fork download
  1. #include <iostream>
  2. #include <windows.h>
  3. using namespace std;
  4. int main()
  5. {
  6. int n, m, x, y;
  7. bool L_pressed = false, R_pressed = false;
  8. bool U_pressed = false, D_pressed = false;
  9. cout << "Please input your map size (n, m): ";
  10. cin >> n >> m;
  11. cout << "Please input robot place (x, y): ";
  12. cin >> x >> y;
  13. while (true) {
  14. // A boolean to record there's any key be pressed.
  15. bool changed = false;
  16.  
  17. // Left Key
  18. int L_key = GetKeyState(VK_LEFT);
  19. // if L_key >= 0 (More specifically, 0 or 1), means
  20. // L_key now is not being pressed. Let L_pressed be false.
  21. // else, let L_pressed be true, and also set changed to be true.
  22. if (L_key >= 0)
  23. L_pressed = false;
  24. else if (!L_pressed) {
  25. changed = true, L_pressed = true;
  26. // if the robot's position can still go left, we let it go.
  27. if (y != 0)
  28. y--;
  29. }
  30.  
  31. // RIGHT Key
  32. int R_key = GetKeyState(VK_RIGHT);
  33. if (R_key >= 0)
  34. R_pressed = false;
  35. else if (!R_pressed) {
  36. changed = true, R_pressed = true;
  37. if (y != m-1)
  38. y++;
  39. }
  40. // UP Key
  41. int U_key = GetKeyState(VK_UP);
  42. if (U_key >= 0)
  43. U_pressed = false;
  44. else if (!U_pressed) {
  45. changed = true, U_pressed = true;
  46. if (x != 0)
  47. x--;
  48. }
  49. // DOWN Key
  50. int D_key = GetKeyState(VK_DOWN);
  51. if (D_key >= 0)
  52. D_pressed = false;
  53. else if (!D_pressed) {
  54. changed = true, D_pressed = true;
  55. if (x != n-1) {
  56. x++;
  57. }
  58. }
  59. // If there's any key be pressed, print the map.
  60. if (changed) {
  61. /*
  62.   // Use "====" to seperate two printed map.
  63.   for (int j = 0; j < m; j++) {
  64.   cout << "=";
  65.   }
  66.   cout << endl;
  67.   */
  68. // Use cls command to clean the input.
  69. system("cls");
  70. for (int i = 0; i < n; i++) {
  71. for (int j = 0; j < m; j++) {
  72. if (i == x && j == y)
  73. cout << '@';
  74. else
  75. cout << '.';
  76. }
  77. cout << endl;
  78. }
  79. }
  80. }
  81. return 0;
  82. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:10: fatal error: windows.h: No such file or directory
 #include <windows.h>
          ^~~~~~~~~~~
compilation terminated.
stdout
Standard output is empty