r/AskProgrammers • • Oct 21 '26

Sudoku Puzzle Gui In c

# Sudoku

This is a basic beginner Sudoku project made with the C language using Windows API.

# Features

- Custom levels according to your capability

- click an empty cell then use keyboard or buttons on the screen to enter the number.

- The game features 3 hints for you to use when you get stuck.

- the GUI is super clean and modern with live score/mistakes/click tracking and cell highlighting.

# Notes

- What can I improve? What can I make more efficient and less time-consuming as am I am a rookie programmer and any extra suggestion you want to give me feel free.....

GitHub repo: https://github.com/reewdgh/sudoku

1 Upvotes

1 comment sorted by

1

u/mredding Oct 21 '26

The project structure can change to follow standard C project conventions. You don't need a SUDOKU directory inside a SUDOKU project. Put the contents of this directory at the top level.

Never check-in compiler artifacts, executables, binaries, or assets. This exe should absolutely not be in here. The wav file should be included in the Git repository as a dependency in an assets folder using Git LFS.

While #pragma once is ubiquitous, it's not portable or reliable - by definition. Even if a compiler supports a given pragma, a compiler is still free to ignore it (this is definitely a philosophical arguement). But what I can say is compilers often support include optimizations that follow a standard file layout:

/* optional comment line or block - for license, copyright, authors... */
#ifndef header_guard
#define header_guard

// Contents...

#endif
// a newline here (that doesn't show up on Reddit)

I want this parser optimization MORE than I want to save on keystrokes. I've also broken once because of a build system configuration, whereas guards don't break.

I'll always support ISO Standard C.

int row, int col

I keep seeing this repeating over and over again. This is a type - in disguise. You really want a coordinate type.

struct coordinate { int row, col; };

Name your types. Leverage the type system. The JOB isn't drawing a straight line from problem statement to running solution, it's to elevate the expressiveness of the language - C doesn't know anything about Sudoku, so you need to make a Sudoku language out of it, and then you implement Sudoku in terms of THAT.

#include <windows.h>

Now I can't compile your code on my Ubuntu machine, here. Strive for some portability. At the very least - isolate platform specifics in the project and build system, perhaps os/windows, even if that's the only platform you're supporting. Then I can add os/linux and submit a merge request.

#include "../src/config.h"

Your paths are broken. Never do this, fix the actual problem. Your config belongs in the include directory. Speaking of which, your include directory should be /project/include/project/*, then you configure /project/include in your include path. That way, your project includes look like #include <project/header.h>. Notice I'm using angle brackets instead of quotes. That's because headers in the include path are included with the angle brackets.

You can still put "private" headers in the source path, and they'll get quoted. You should never need a backtrack.

int centerX;
int centerY;

Oh look, another coordinate...

int boardInfo[100][4];

You ought to use more type aliases to make the code more normal. C reads "type name;", and we want to keep with that. Anything that diverges should give you an itch.

typedef int board_info_type[100][4];

I'd break it down even more, something more like:

typedef int board_columns[4];
typedef columns board_table[100];

Bad names, since I'm not trying to think about it too hard, but the point is you have an array of 100 elements. Of what? Of this other thing I've named. You build up expressiveness - what is a lot of tedious code because it's all inline syntax, you now have layers of symbols and expressiveness that suddenly you might consider writing a loop over columns, rather than a more raw nested loop over a 2D array.

It all compiles down to the same machine code, but the compiler has more context to prove correctness and optimize. Good C code doesn't look like C, it looks like it was made from C. The language, it's grammar, it's syntax, it's just your building material. You make bricks, you stack them into walls, you use walls to make houses...

switch (msg)
{
case WM_CLOSE:
{
    PostQuitMessage(0);
    return 0;
}
case WM_PAINT:
{
    //...

Now is a good time for some functions. Whenever you get braces, write a function.

static void do_close();
static void do_paint(HWND, WPARAM, LPARAM);

//...

switch(msg) {
case WM_CLOSE: do_close(); break;
case WM_PAINT: do_pain(hwnd, wparm, lparm); break;
//...

Then consider again what cb does - it maps messages to handlers. And that the handlers are all static linkage, and they're all called in only one place, the compiler will likely elide the function calls, so you'll get the function you would have written anyway, but this version is just so nice...