Reduce size of minesweeper program to fit in qr code

I saw a guy who fit snake in a qr code and I wanted to do the same thing but with minesweeper. The maximum amount of data a qr code can hold is 3kb. My c program, without any compiler magic, takes up 58kb.

A tiny bit over the limit, haha. So before I started performing alchemy with the compiler and compression programs, I was wondering if there was a couple of stuff I could do beforehand and optimize it to make the process easier. My code can be seen below:

#include <windows.h>

#define WIDTH 100
#define HEIGHT 100
#define BOMBS 799

#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77

HANDLE wHnd;
HANDLE rHnd;

void SetGrid(int grid[WIDTH][HEIGHT])
{
int bomb[2] = { abs(rand() % WIDTH-1) + 1,
abs(rand() % HEIGHT-1) + 1 };

for (int i = 0; i < BOMBS; i++)
{
while (grid[bomb[0]][bomb[1]] < -1 || bomb[0] == 0 || bomb[1] == 0 || bomb[0] >= WIDTH-1 || bomb[1] >= HEIGHT-1)
{
bomb[0] = abs(rand() % WIDTH-1) + 1;
bomb[1] = abs(rand() % HEIGHT-1) + 1;
}

grid[bomb[0]][bomb[1]] = -9;

grid[bomb[0] + 1][bomb[1] + 1]++;
grid[bomb[0] + 1][bomb[1]]++;
grid[bomb[0]][bomb[1] + 1]++;
grid[bomb[0] - 1][bomb[1] + 1]++;
grid[bomb[0]][bomb[1] - 1]++;
grid[bomb[0] + 1][bomb[1] - 1]++;
grid[bomb[0] - 1][bomb[1] - 1]++;
grid[bomb[0] - 1][bomb[1]]++;
}
}

void ExpandGrid(int fullGrid[WIDTH][HEIGHT], int knownGrid[WIDTH][HEIGHT], int blankPos[2])
{
int neighbors[8][2] = {{0,1}, {1,0}, {1,1},
{0,-1},        {-1,0},
{-1,-1},{-1,1},{1,-1}};
int curTile[2];

knownGrid[blankPos[0]][blankPos[1]] = 1;
if(fullGrid[blankPos[0]][blankPos[1]] != 0) return;

for(int blck = 0; blck < 8; ++blck)
{
curTile[0] = abs(blankPos[0]+neighbors[blck][0]);
curTile[1] = abs(blankPos[1]+neighbors[blck][1]);
if(curTile[0] > WIDTH-1 || curTile[1] > HEIGHT-1) continue;

if(fullGrid[curTile[0]][curTile[1]] == 0 && knownGrid[curTile[0]][curTile[1]] == 0)
{
knownGrid[curTile[0]][curTile[1]] = 1;
ExpandGrid(fullGrid, knownGrid, curTile);
}
else if(fullGrid[curTile[0]][curTile[1]] > 0) knownGrid[curTile[0]][curTile[1]] = 1;
}
}

int main(void)
{

SMALL_RECT windowSize = { 0, 0, WIDTH - 1, HEIGHT - 1 };

COORD characterBufferSize = { WIDTH, HEIGHT };
COORD characterPosition = { 0, 0 };
SMALL_RECT consoleWriteArea = { 0, 0, WIDTH - 1, HEIGHT - 1 };

CHAR_INFO consoleBuffer[WIDTH][HEIGHT];

wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
rHnd = GetStdHandle(STD_INPUT_HANDLE);

SetConsoleTitle("Minesweeper!");

SetConsoleWindowInfo(wHnd, TRUE, &windowSize);

srand((unsigned int)time(NULL));

int startGrid[WIDTH][HEIGHT] = { 0 };
int knownGrid[WIDTH][HEIGHT] = { 0 };
SetGrid(startGrid);

int startCoord[2] = {0, 0};
int arrowPos[2] = {0, 0};

ExpandGrid(startGrid, knownGrid, startCoord);

while(1)
{
if (arrowPos[0] > WIDTH-1) arrowPos[0] = WIDTH-1;
if (arrowPos[0] < 0) arrowPos[0] = 0;
if (arrowPos[1] > HEIGHT-1) arrowPos[1] = HEIGHT-1;
if (arrowPos[1] < 0) arrowPos[1] = 0;

for (int x = 0; x < WIDTH; ++x)
{
for (int y = 0; y < HEIGHT; ++y)
{

if (knownGrid[x][y] == 1)
{
if (startGrid[x][y] > 0)
{
consoleBuffer[x][y].Char.AsciiChar = '0' + startGrid[x][y];
consoleBuffer[x][y].Attributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
}
else
{
consoleBuffer[x][y].Char.AsciiChar = 'o';
consoleBuffer[x][y].Attributes = (startGrid[x][y] < 0 ? FOREGROUND_RED : FOREGROUND_BLUE) | FOREGROUND_INTENSITY;
}
}
else
{
consoleBuffer[x][y].Char.AsciiChar = 00;
consoleBuffer[x][y].Attributes = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
}

if(arrowPos[0] == x && arrowPos[1] == y)
{
consoleBuffer[x][y].Attributes = BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN;
}
}
}

WriteConsoleOutputA(wHnd, consoleBuffer, characterBufferSize, characterPosition, &consoleWriteArea);

switch(getch())
{
case KEY_UP:
arrowPos[0]--;
break;
case KEY_DOWN:
arrowPos[0]++;
break;
case KEY_LEFT:
arrowPos[1]--;
break;
case KEY_RIGHT:
arrowPos[1]++;
break;
case '\r':
ExpandGrid(startGrid, knownGrid, arrowPos);
break;
}
}
}

• Your program is probably not the problem -- it's the libraries that are getting dragged in. You might want to see if there's a way to explore what symbols link in what other symbols. Could you, for example, gain anything by using different I/O calls? Or writing your own random? Commented Aug 20, 2020 at 22:47
• If possible, you could use a different file format for your executable file, or you could even use a custom file format. In that case, it might be better to program in assembler. Maybe you could also use some cheap compression algorithm. Commented Oct 24, 2020 at 8:57