我正在尝试用C语言制作一个不超过3KB的游戏,为此,我想删除所有标准库引用。我希望在我的程序中唯一的头文件是windows.h,到目前为止我做得还不错。
我只有一个外部引用,分别调用rand()、srand()、time()和getch()。对于3KB以下的游戏来说,这看起来似乎很多,但time、rand和srand函数只是为了在游戏开始时拥有一个随机种子。
rand获取数字,srand设置种子,时间获取随机种子。因此,如果我能找到一种方法来摆脱过多的随机生成过程,我就可以摆脱3/4的std库函数。我不太确定如何处理getch,但稍后我会集中讨论这一点。
有些人推荐使用xorshifts,但它似乎需要属于stdint.h的uint32_t类型。我也尝试使用整数代替,虽然结果数是随机的,但它是随机的,因为返回的数是随机的,但您可以依赖它每次都会给出这个随机数。我可以给它时间作为一种种子,但是我仍然必须使用时间函数。有没有什么我忽略了的地方,或者我应该使用不同的方法?
编辑:根据请求,我的编译标志是-ffunction-sections、-fdata-sections、-s和-Os。我唯一的链接器标志是-Wl,--gc-sections,我不确定什么是动态链接库和静态链接库,但我有一个不错的想法。我的代码是12KB,如果我使用UPX,我可以把它降到7KB。我的代码如下:
#include <windows.h>
#define WIDTH 100
#define HEIGHT 100
#define BOMBS 800
struct xorshift_state {
int a;
};
int xorshift(struct xorshift_state *state)
{
int x = state->a;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return state->a = x;
}
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] = blankPos[0]+neighbors[blck][0];
curTile[1] = blankPos[1]+neighbors[blck][1];
if(curTile[0] > WIDTH-1 || curTile[1] > HEIGHT-1 || curTile[0] < 0 || curTile[1] < 0) 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(int argc, char *argv[])
{
COORD characterBufferSize = { WIDTH, HEIGHT };
COORD characterPosition = { 0, 0 };
SMALL_RECT consoleWriteArea = { 0, 0, WIDTH - 1, HEIGHT - 1 };
CHAR_INFO consoleBuffer[WIDTH][HEIGHT];
HANDLE wHnd = GetStdHandle(-11);
int startGrid[WIDTH][HEIGHT] = { 0 };
int knownGrid[WIDTH][HEIGHT] = { 0 };
int arrowPos[2] = {0, 0};
int bomb[2] = {0};
struct xorshift_state seed = {argc == 2 ? (int) argv[1] : 1};
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)
{
bomb[0] = (xorshift(&seed) % WIDTH-1) + 1;
bomb[1] = (xorshift(&seed) % HEIGHT-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 = 10;
}
else
{
consoleBuffer[x][y].Char.AsciiChar = 'o';
consoleBuffer[x][y].Attributes = startGrid[x][y] < 0 ? 4 : 17;
}
}
else
{
consoleBuffer[x][y].Char.AsciiChar = 00;
consoleBuffer[x][y].Attributes = 0;
}
if(arrowPos[0] == x && arrowPos[1] == y)
{
consoleBuffer[x][y].Attributes = 112;
}
}
}
WriteConsoleOutputA(wHnd, *consoleBuffer, characterBufferSize, characterPosition, &consoleWriteArea);
switch(getch())
{
case 72:
arrowPos[0]--;
break;
case 80:
arrowPos[0]++;
break;
case 75:
arrowPos[1]--;
break;
case 77:
arrowPos[1]++;
break;
case '\r':
ExpandGrid(startGrid, knownGrid, arrowPos);
break;
}
}
}发布于 2020-08-24 03:06:10
这在很大程度上取决于你对随机生成器的要求。如果你只是想让游戏在你每次玩的时候都有一点不同,并且不是完全确定的,那么几乎任何随机生成器都可以。只要在网上看看,你就会找到原始随机生成器的例子。
其中最简单的(但仍然很好)是这样的:
int seed = 123456789;
int rand()
{
seed = (a * seed + c) % m;
return seed;
} 请注意,您需要为a、c和m设置好的值。这里进一步解释了我找到它的地方:https://stackoverflow.com/a/3062783/6699433
当涉及到种子时,有一些选择。如果可能,您可以将种子作为参数发送到程序。您可以使用用户输入和测量计时。
此外,请记住,可执行文件的大小不会变大(或者至少在一般情况下不会变大),因为如果不使用头文件,则会包括头文件。如果您激活了优化,则尤其不会。
发布于 2020-08-24 15:20:31
您可以对可执行文件使用参数,并将argv[1]散列为种子。这样,人们就可以一遍又一遍地玩同样的问题,并相互对抗。例如“仙女座”或“伦敦”。:)
https://stackoverflow.com/questions/63550874
复制相似问题