fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define WORD_LENGTH 5
  7. #define MAX_TRIES 6
  8. #define CATEGORIES 3
  9. #define WORDS_PER_CAT 5
  10.  
  11. /* ANSI colors */
  12. #define GREEN "\033[1;32m"
  13. #define YELLOW "\033[1;33m"
  14. #define GRAY "\033[1;37m"
  15. #define RESET "\033[0m"
  16.  
  17. int main() {
  18.  
  19. char categoryNames[CATEGORIES][20] = {
  20. "Places",
  21. "Animals",
  22. "Objects"
  23. };
  24.  
  25. char words[CATEGORIES][WORDS_PER_CAT][WORD_LENGTH + 1] = {
  26. { "mecca", "paris", "milan", "dubai", "cairo" },
  27. { "tiger", "horse", "sheep", "zebra", "camel" },
  28. { "chair", "table", "phone", "glass", "clock" }
  29. };
  30.  
  31. srand(time(NULL));
  32.  
  33. int catIndex = rand() % CATEGORIES;
  34. char secret[WORD_LENGTH + 1];
  35. strcpy(secret, words[catIndex][rand() % WORDS_PER_CAT]);
  36.  
  37. char guesses[MAX_TRIES][WORD_LENGTH + 1];
  38. int tries = 0;
  39. int win = 0;
  40.  
  41. printf("=== WORDLE GAME ===\n");
  42. printf("Category: %s\n", categoryNames[catIndex]);
  43. printf("Guess the 5-letter word (%d tries)\n\n", MAX_TRIES);
  44.  
  45. while (tries < MAX_TRIES && !win) {
  46.  
  47. printf("Try %d: ", tries + 1);
  48. scanf("%5s", guesses[tries]);
  49.  
  50. system("clear"); /* Linux / Mac */
  51. /* system("cls"); */ /* Windows */
  52.  
  53. printf("=== WORDLE GAME ===\n");
  54. printf("Category: %s\n\n", categoryNames[catIndex]);
  55.  
  56. /* Print table */
  57. for (int r = 0; r < MAX_TRIES; r++) {
  58. for (int c = 0; c < WORD_LENGTH; c++) {
  59.  
  60. if (r < tries + 1) {
  61. if (guesses[r][c] == secret[c]) {
  62. printf(GREEN "[%c]" RESET, guesses[r][c]);
  63. }
  64. else if (strchr(secret, guesses[r][c])) {
  65. printf(YELLOW "[%c]" RESET, guesses[r][c]);
  66. }
  67. else {
  68. printf(GRAY "[%c]" RESET, guesses[r][c]);
  69. }
  70. }
  71. else {
  72. printf("[ ]");
  73. }
  74. }
  75. printf("\n");
  76. }
  77.  
  78. int correct = 0;
  79. for (int i = 0; i < WORD_LENGTH; i++)
  80. if (guesses[tries][i] == secret[i])
  81. correct++;
  82.  
  83. if (correct == WORD_LENGTH)
  84. win = 1;
  85.  
  86. tries++;
  87. printf("\n");
  88. }
  89.  
  90. if (win)
  91. printf(GREEN "Bravo! You found the word: %s\n" RESET, secret);
  92. else
  93. printf("Game Over! The word was: %s\n", secret);
  94.  
  95. return 0;
  96. }
Success #stdin #stdout #stderr 0.01s 5288KB
stdin
Standard input is empty
stdout
=== WORDLE GAME ===
Category: Places
Guess the 5-letter word (6 tries)

Try 1: === WORDLE GAME ===
Category: Places

[][][][][]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]

Try 2: === WORDLE GAME ===
Category: Places

[][][][][]
[][][0][7][[]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]

Try 3: === WORDLE GAME ===
Category: Places

[][][][][]
[][][0][7][[]
[�][][][][G]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]

Try 4: === WORDLE GAME ===
Category: Places

[][][][][]
[][][0][7][[]
[�][][][][G]
[S][�][�][][]
[ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ]

Try 5: === WORDLE GAME ===
Category: Places

[][][][][]
[][][0][7][[]
[�][][][][G]
[S][�][�][][]
[][][][][]
[ ][ ][ ][ ][ ]

Try 6: === WORDLE GAME ===
Category: Places

[][][][][]
[][][0][7][[]
[�][][][][G]
[S][�][�][][]
[][][][][]
[][][`][k][%]

Game Over! The word was: milan
stderr
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.