C替换srand和rand

问题描述

我正在尝试使C语言的游戏适合3KB以下的限制,为此,我想删除所有标准库引用。我想在程序中拥有的唯一标头是windows.h,到目前为止,我的表现还不错。

我仅有的外部引用是rand()srand()time()getch()的一个调用。对于3KB以下的游戏来说,这似乎很多,但是时间,rand和srand函数只是为了让游戏开始时可以拥有随机种子。

rand获取数字,srand设置种子,以及时间获取随机种子。因此,如果我可以找到一种方法来摆脱过多的随机生成过程,那么我可以摆脱3/4 std库函数。我不太确定该如何处理getch,但稍后将重点放在这上面。

有人推荐了xorshifts,但似乎需要uint32_t类型的stdint.h。我也尝试使用整数代替,虽然结果数是随机的,但从返回的数字是随机的意义上讲,它是随机的,但是您可以依靠它每次给您一个随机数。我可以给它时间作为一种种子,但是id仍然必须使用time函数。我有什么要忽略的,还是应该使用其他方法?

编辑:根据请求,我的编译标志为-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},{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] = 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,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,arrowPos);
                break;
        }
    }
}

解决方法

这很大程度上取决于您对随机数发生器的要求。如果您只是想要它,以便每次玩游戏时它都会有所不同并且不能完全确定,那么几乎任何随机数生成器都可以。只需在网上查看,您就会找到原始随机生成器的示例。

最简单(但仍然相当不错)的是:

int seed = 123456789;

int rand()
{
  seed = (a * seed + c) % m;
  return seed;
}    

请注意,您需要acm的适当值。我在这里找到它的地方进一步说明:https://stackoverflow.com/a/3062783/6699433

涉及种子时,有一些选择。如果可能,您可以将种子作为参数发送给程序。您可以使用用户输入并测量时间。

此外,请记住可执行文件的大小不会(或者至少通常不会)变大,因为如果不使用标头,则会包含标头。尤其是如果您已激活优化。

,

您可以使用可执行文件的参数,并将哈希argv[1]作为种子。这样,人们可以一遍又一遍又一遍又一遍地面对着同样的问题。例如“仙女座”或“伦敦”。 :)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...