2010-03-24 10 views
9

¿Cuál sería la mejor manera de implementar el tipo de códigos de trucos en general? Tengo la aplicación WinForms en mente, donde un código trampa podría desbloquear un huevo de pascua, pero los detalles de implementación no son relevantes.¿Cómo implementar correctamente los códigos de trampa?

El mejor enfoque que me viene a la mente es mantener el índice para cada código; consideremos los famosos códigos DOOM - IDDQD e IDKFA, en una aplicación C# ficticia.

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; 
    } 
} 

¿Es esta la manera correcta de hacerlo?

Editar: Probablemente lo peor que debería haber hecho es incluir los primeros códigos de trucos que vinieron de la parte superior de mi cabeza como un ejemplo. Realmente no quería querer ver el código fuente de Doom o su implementación, sino una solución general a este problema.

Respuesta

6

Creo que éste es un poco más fácil de entender, sin embargo, el documento será probablemente un mejor desempeño que éste:

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 
     } 
    } 
} 
+0

pero su código probablemente no funcionará con códigos de longitud variable, ¿verdad? – Axarydax

+0

@Axarydax: Claro que funciona. Verifique nuevamente :) –

+0

mi mal. No vi la parte "EndsWith";) – Axarydax

11

¿Por qué no descargar la fuente DOOM y comprobarlo usted mismo? =) http://www.doomworld.com/idgames/?id=14576

+0

básicamente es más o menos lo mismo, aunque más estructurado y elaborado. Pero qué más se puede esperar de ID soft :) – Axarydax

+1

Su código es, de hecho, me da un nuevo respeto por ese juego. – MHarrison

+0

Haha. Siempre me gusta cuando una respuesta inteligente como esta es realmente la mejor solución al problema +1 –

1

aquí es el DOOM engañar aplicación de la fuente de la fatalidad:

#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; 
} 
Cuestiones relacionadas