fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h> // Windows ke liye, agar online compiler mein na ho toh getchar() use kar sakte hain
  4. #include <stdbool.h>
  5.  
  6. #define WIDTH 20
  7.  
  8. void printGame(int position) {
  9. system("cls"); // Screen clear karta hai (Windows). Online compiler me kaam na kare toh use "\n" se screen clear karen
  10.  
  11. for (int i = 0; i < WIDTH; i++) {
  12. if (i == position)
  13. printf("M"); // Mario ko M se represent kar rahe hain
  14. else
  15. printf("-");
  16. }
  17. printf("\n");
  18. }
  19.  
  20. int main() {
  21. int position = WIDTH / 2;
  22. char input;
  23. bool running = true;
  24.  
  25. printf("Use 'a' to move left, 'd' to move right, 'q' to quit.\n");
  26.  
  27. while (running) {
  28. printGame(position);
  29.  
  30. input = getchar();
  31.  
  32. if (input == 'a' || input == 'A') {
  33. if (position > 0)
  34. position--;
  35. } else if (input == 'd' || input == 'D') {
  36. if (position < WIDTH - 1)
  37. position++;
  38. } else if (input == 'q' || input == 'Q') {
  39. running = false;
  40. }
  41.  
  42. // Input buffer clear karne ke liye
  43. while ((input = getchar()) != '\n' && input != EOF) {}
  44. }
  45.  
  46. printf("Game over!\n");
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0.03s 25684KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>  // Windows ke liye, agar online compiler mein na ho toh getchar() use kar sakte hain
#include <stdbool.h>

#define WIDTH 20

void printGame(int position) {
    system("cls");  // Screen clear karta hai (Windows). Online compiler me kaam na kare toh use "\n" se screen clear karen

    for (int i = 0; i < WIDTH; i++) {
        if (i == position)
            printf("M");  // Mario ko M se represent kar rahe hain
        else
            printf("-");
    }
    printf("\n");
}

int main() {
    int position = WIDTH / 2;
    char input;
    bool running = true;

    printf("Use 'a' to move left, 'd' to move right, 'q' to quit.\n");

    while (running) {
        printGame(position);

        input = getchar();

        if (input == 'a' || input == 'A') {
            if (position > 0)
                position--;
        } else if (input == 'd' || input == 'D') {
            if (position < WIDTH - 1)
                position++;
        } else if (input == 'q' || input == 'Q') {
            running = false;
        }

        // Input buffer clear karne ke liye
        while ((input = getchar()) != '\n' && input != EOF) {}
    }

    printf("Game over!\n");
    return 0;
}