DEV Community

Arthur
Arthur

Posted on

CodleC: Building a Wordle Clone in C

πŸ‘‹ Hey DEV Community!

I recently built CodleC β€” a Wordle-inspired game in pure C that runs in your terminal with ANSI colors, hints, and JSON persistence. Here’s how I did it!


What is CodleC?

A 5-letter word-guessing game where:

🟩 Green = Correct letter & position.

🟨 Yellow = Correct letter, wrong position.

⬜ Gray = Letter not in the word.

Features:

βœ… 3 difficulty levels (Easy: 7 guesses, Hard: 5).
βœ… Limited hints (4 per game, 30s cooldown).
βœ… Virtual keyboard (shows letter status).
βœ… Saves results in results.json.


Key Technical Challenges

1. Terminal Colors (ANSI Escape Codes)

Used ANSI codes for cross-platform colors:

#define GREEN "\033[42m\033[30m"  
printf("%s V %s Correct!", GREEN, RESET);  
Enter fullscreen mode Exit fullscreen mode

Windows fix:

#include <windows.h>  
SetConsoleOutputCP(CP_UTF8);  // Enable UTF-8 
Enter fullscreen mode Exit fullscreen mode

2. Real-Time Input (No "Enter" Needed)

Windows: _getch() (from conio.h).

Linux/macOS: termios raw mode.

char get_char() {  
    #ifdef _WIN32  
        return _getch();  
    #else  
        // Disable input buffering  
        struct termios old, new;  
        tcgetattr(0, &old);  
        new = old;  
        new.c_lflag &= ~(ICANON | ECHO);  
        tcsetattr(0, TCSANOW, &new);  
        char c = getchar();  
        tcsetattr(0, TCSANOW, &old);  
        return c;  
    #endif  
}  

Enter fullscreen mode Exit fullscreen mode

3. Hint System with Cooldown

time_t last_hint_time;  

if (time(NULL) - last_hint_time >= 30) {  
    reveal_random_letter();  
} else {  
    printf("Wait %d sec!", 30 - (time(NULL) - last_hint_time));  
}  
Enter fullscreen mode Exit fullscreen mode

Cool Features

  • JSON Persistence
{"word": "CRANE", "guesses": 3, "difficulty": "HARD"}
Enter fullscreen mode Exit fullscreen mode

Try It Yourself!

πŸ”— GitHub: https://github.com/DamiaoArth/CodleC

Includes:

Full source code (main.c).
Pre-built binaries (Windows/Linux).
Portuguese word lists (easy to swap).


πŸ’¬ Discussion
Ever built a terminal game? Share your experiences below!

Ideas to improve CodleC? (Thinking of adding a global leaderboard!)


References:

ANSI Escape Codes
Wordle (NYT)
Letreco
TERMOOO

c #gamedev #terminal #wordle #opensource


PS: Can you beat my high score of 3 guesses on Hard mode?

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more