问题描述
我正试图在3KB的限制下将c的扫雷程序安装到c中,以便它可以安装在qr代码中,并且在我添加选项-nostdlib
并对其进行编译之前,该程序一直运行良好已退出,尽管main中有while(1)循环而无法退出。
我在链接器中添加的库为kernel32
,gcc
和msvcrt
(以防您想重新生成它们)。尽管从技术上讲它确实返回了0x0,这意味着它“成功”,但显然不是。否则就不会那样做。我的代码可以在这里看到:
#include <windows.h>
#define WIDTH 100
#define HEIGHT 100
#define BOMBS 801
int xorshf96(int x,int y,int z)
{
unsigned long t;
x ^= x << 16;
x ^= x >> 5;
x ^= x << 1;
t = x;
x = y;
y = z;
z = t ^ x ^ y;
return z;
}
void ExpandGrid(int fullGrid[WIDTH][HEIGHT],int knownGrid[WIDTH][HEIGHT],int blankPos[2])
{
int neighbors[8][2] = {{0,1},{1,0},{0,-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)
{
COORD characterBufferSize = { WIDTH,HEIGHT };
COORD characterPosition = { 0,0 };
SMALL_RECT consoleWriteArea = { 0,WIDTH - 1,HEIGHT - 1 };
CHAR_INFO consoleBuffer[WIDTH][HEIGHT];
HANDLE wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE rHnd = GetStdHandle(STD_INPUT_HANDLE);
int num1,num2,num3 = 0;
SYSTEMTIME systime;
DWORD numEventsRead = 0;
DWORD numEvents = 0;
INPUT_RECORD *eventBuffer;
int wait = 0;
SetConsoleTitle("Minesweeper!");
int startGrid[WIDTH][HEIGHT] = { 0 };
int knownGrid[WIDTH][HEIGHT] = { 0 };
int arrowPos[2] = {0,0};
int bomb[2] = {0,0};
for (int i = 0; i < BOMBS; i++)
{
while (startGrid[bomb[0]][bomb[1]] < -1 || bomb[0] <= 0 || bomb[1] <= 0 || bomb[0] >= WIDTH-1 || bomb[1] >= HEIGHT-1)
{
GetLocalTime(&systime);
num1,num3 = (int) systime.wMilliseconds;
bomb[1] = (xorshf96(num3,num1) % (HEIGHT-1)) + 1;
GetSystemTime(&systime);
num1,num3 = (int) systime.wMilliseconds;
bomb[0] = (xorshf96(num1,num3) % (WIDTH-1)) + 1;
}
startGrid[bomb[0]][bomb[1]] = -9;
startGrid[bomb[0] + 1][bomb[1] + 1]++;
startGrid[bomb[0] + 1][bomb[1]]++;
startGrid[bomb[0]][bomb[1] + 1]++;
startGrid[bomb[0] - 1][bomb[1] + 1]++;
startGrid[bomb[0]][bomb[1] - 1]++;
startGrid[bomb[0] + 1][bomb[1] - 1]++;
startGrid[bomb[0] - 1][bomb[1] - 1]++;
startGrid[bomb[0] - 1][bomb[1]]++;
}
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);
numEvents = 0;
numEventsRead = 0;
GetNumberOfConsoleInputEvents(rHnd,&numEvents);
if (numEvents)
{
eventBuffer = malloc(sizeof(INPUT_RECORD) * numEvents);
ReadConsoleInput(rHnd,eventBuffer,numEvents,&numEventsRead);
}
if(numEventsRead && wait <= 0)
{
wait = 50;
switch (eventBuffer[0].Event.KeyEvent.wVirtualKeyCode)
{
case VK_UP:
arrowPos[0]--;
break;
case VK_DOWN:
arrowPos[0]++;
break;
case VK_LEFT:
arrowPos[1]--;
break;
case VK_RIGHT:
arrowPos[1]++;
break;
case VK_RETURN:
ExpandGrid(startGrid,arrowPos);
break;
}
}
wait--;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)