如何正确实施作弊码?

发布于 2024-08-26 00:27:22 字数 983 浏览 4 评论 0原文

一般来说,实施作弊代码的最佳方法是什么? 我想到了 WinForms 应用程序,其中作弊代码可以解锁复活节彩蛋,但实现细节并不相关。

我想到的最好方法是为每个代码保留索引 - 让我们考虑一下著名的 DOOM 代码 - IDDQD 和 IDKFA,在一个虚构的 C# 应用程序中。

string[] CheatCodes = { "IDDQD", "IDKFA"};
int[] CheatIndexes = { 0, 0 };
const int CHEAT_COUNT = 2;
void KeyPress(char c)
{
    for (int i = 0; i < CHEAT_COUNT; i++) //for each cheat code
    {
        if (CheatCodes[i][CheatIndexes[i]] == c)
        { //we have hit the next key in sequence
            if (++CheatIndexes[i] == CheatCodes[i].Length) //are we in the end?
            {
                //Do cheat work
                MessageBox.Show(CheatCodes[i]);
                //reset cheat index so we can enter it next time
                CheatIndexes[i] = 0; 
            }
        }
        else //mistyped, reset cheat index
            CheatIndexes[i] = 0; 
    }
}

这是正确的做法吗?

编辑:也许我应该做的最糟糕的事情就是包含来自我头脑中的第一个作弊代码作为示例。我真的不想想要看到 Doom 的源代码或其实现,而是这个问题的一般解决方案。

what would be the best way to implement kind of cheat codes in general?
I have WinForms application in mind, where a cheat code would unlock an easter egg, but the implementation details are not relevant.

The best approach that comes to my mind is to keep index for each code - let's consider famous DOOM codes - IDDQD and IDKFA, in a fictional C# app.

string[] CheatCodes = { "IDDQD", "IDKFA"};
int[] CheatIndexes = { 0, 0 };
const int CHEAT_COUNT = 2;
void KeyPress(char c)
{
    for (int i = 0; i < CHEAT_COUNT; i++) //for each cheat code
    {
        if (CheatCodes[i][CheatIndexes[i]] == c)
        { //we have hit the next key in sequence
            if (++CheatIndexes[i] == CheatCodes[i].Length) //are we in the end?
            {
                //Do cheat work
                MessageBox.Show(CheatCodes[i]);
                //reset cheat index so we can enter it next time
                CheatIndexes[i] = 0; 
            }
        }
        else //mistyped, reset cheat index
            CheatIndexes[i] = 0; 
    }
}

Is this the right way to do it?

Edit: Probably the worst thing I should have done was to include the first cheat codes that came from the top of my head as an example. I really did not want to see Doom's source code or their implementation, but general solution to this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谢绝鈎搭 2024-09-02 00:27:22

为什么不下载 DOOM 源代码并亲自看看呢? =)
http://www.doomworld.com/idgames/?id=14576

Why not download the DOOM source and see for yourself? =)
http://www.doomworld.com/idgames/?id=14576

以可爱出名 2024-09-02 00:27:22

我认为这个更容易理解一点,尽管你原来的可能会比这个表现更好:

using System.Collections.Generic;

void KeyPress(char c)
{
    string[] cheatCodes = { "IDDQD", "IDKFA"};
    static Queue<char> buffer; //Contains the longest number of characters needed
    buffer.Enqueue(c);
    if (buffer.Count() > 5) //Replace 5 with whatever your longest cheat code is
        buffer.Dequeue();
    bufferString = new System.String(buffer.ToArray());
    foreach(string code in cheatCodes) {
        if (bufferString.EndsWith(code)) {
            //Do cheat work
        }
    }
}

I think this one's a bit easier to understand, though your original will probably perform better than this one:

using System.Collections.Generic;

void KeyPress(char c)
{
    string[] cheatCodes = { "IDDQD", "IDKFA"};
    static Queue<char> buffer; //Contains the longest number of characters needed
    buffer.Enqueue(c);
    if (buffer.Count() > 5) //Replace 5 with whatever your longest cheat code is
        buffer.Dequeue();
    bufferString = new System.String(buffer.ToArray());
    foreach(string code in cheatCodes) {
        if (bufferString.EndsWith(code)) {
            //Do cheat work
        }
    }
}
同尘 2024-09-02 00:27:22

这是来自 doom 源代码的 DOOM 作弊实现:

#define SCRAMBLE(a) \
((((a)&1)<<7) + (((a)&2)<<5) + ((a)&4) + (((a)&8)<<1) \
 + (((a)&16)>>1) + ((a)&32) + (((a)&64)>>5) + (((a)&128)>>7))

int cht_CheckCheat ( cheatseq_t* cht, char key )
{
    int i;
    int rc = 0;

    if (firsttime)
    {
        firsttime = 0;
        for (i=0;i<256;i++) cheat_xlate_table[i] = SCRAMBLE(i);
    }

    if (!cht->p)
        cht->p = cht->sequence; // initialize if first time

    if (*cht->p == 0)
        *(cht->p++) = key;
    else if
        (cheat_xlate_table[(unsigned char)key] == *cht->p) cht->p++;
    else
        cht->p = cht->sequence;

    if (*cht->p == 1)
        cht->p++;
    else if (*cht->p == 0xff) // end of sequence character
    {
        cht->p = cht->sequence;
        rc = 1;
    }

    return rc;
}

here is the DOOM cheat implementation from the doom source:

#define SCRAMBLE(a) \
((((a)&1)<<7) + (((a)&2)<<5) + ((a)&4) + (((a)&8)<<1) \
 + (((a)&16)>>1) + ((a)&32) + (((a)&64)>>5) + (((a)&128)>>7))

int cht_CheckCheat ( cheatseq_t* cht, char key )
{
    int i;
    int rc = 0;

    if (firsttime)
    {
        firsttime = 0;
        for (i=0;i<256;i++) cheat_xlate_table[i] = SCRAMBLE(i);
    }

    if (!cht->p)
        cht->p = cht->sequence; // initialize if first time

    if (*cht->p == 0)
        *(cht->p++) = key;
    else if
        (cheat_xlate_table[(unsigned char)key] == *cht->p) cht->p++;
    else
        cht->p = cht->sequence;

    if (*cht->p == 1)
        cht->p++;
    else if (*cht->p == 0xff) // end of sequence character
    {
        cht->p = cht->sequence;
        rc = 1;
    }

    return rc;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文