Advertisement
ZoriaRPG

String Processing Example: Riddle Minigame

Sep 19th, 2019
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include "std.zh"
  2. hero script kb_riddle
  3. {
  4.     const int SFX_WON = 20;
  5.     const int SFX_LOSS = 28;
  6.     const int BUFFER_SIZE = 20;
  7.     void run()
  8.     {
  9.  
  10.         int StringBuffer[BUFFER_SIZE];
  11.         int lower[BUFFER_SIZE];
  12.         TraceS("Riddle Game String Processor:\n");
  13.         Game->TypingMode = true;
  14.         int index; int won;
  15.        
  16.         while(1)
  17.         {
  18.             while(!Input->Key[KEY_ENTER])
  19.             {
  20.                 for(int i = KEY_A; i<=KEY_SPACE; ++i)
  21.                 {
  22.                    
  23.                     if(Input->ReadKey[i])
  24.                     {
  25.                         if(i == KEY_BACKSPACE && index > 0)
  26.                         {
  27.                             --index;
  28.                             StringBuffer[index] = 0;
  29.                         }
  30.                         else
  31.                         {
  32.                             if ( index < ( BUFFER_SIZE-1 ) )
  33.                             {
  34.                                 LogPrint("KeyToChar is: %d \n", KeyToChar(i));
  35.                                 StringBuffer[index] = KeyToChar(i);
  36.                                 ++index;
  37.                             }
  38.                         }
  39.                        
  40.                         TraceS(StringBuffer);
  41.                     }
  42.                 }
  43.                 Screen->DrawString(6, 8, 20, 2, 1, -1, 0, "I am a word that indicates cardinal directions.", 128);
  44.                 Screen->DrawString(6, 8, 40, 2, 1, -1, 0, "Type, then press ENTER to guess...", 128);
  45.                 Screen->DrawString(6, 20, 60, 0, 1, -1, 0, StringBuffer, 128);
  46.                 Waitframe();
  47.             }
  48.             Screen->DrawString(6, 8, 20, 2, 1, -1, 0, "I am a word that indicates cardinal directions.", 128);
  49.             Screen->DrawString(6, 8, 40, 2, 1, -1, 0, "Type, then press ENTER to guess...", 128);
  50.             Screen->DrawString(6, 20, 60, 0, 1, -1, 0, StringBuffer, 128);
  51.  
  52.        
  53.             strcpy(lower, StringBuffer);
  54.             TraceNL(); TraceS(StringBuffer); TraceNL();
  55.             TraceS(lower); TraceNL();
  56.             //convert to lowercase as a precaution
  57.             utol(lower);
  58.             TraceS(lower); TraceNL();
  59.             //process
  60.             if ( !(strcmp(lower, "news")) || !(strcmp(lower, "sewn")) )
  61.             {
  62.                 won = 1;
  63.             }
  64.             else won = 2;
  65.             if ( won == 1 )
  66.             {
  67.                 Game->TypingMode = false;
  68.                 Game->PlaySound(SFX_WON);
  69.                 while(!Input->ReadKey[KEY_Q])
  70.                 {
  71.                     Screen->DrawString(6, 8, 20, 0, 1, -1, 0, "You win! Press Q to exit.", 128);
  72.                     ++Game->NumDeaths;
  73.                     Waitframe();
  74.                 }
  75.                 Game->End();
  76.             }
  77.             else if ( won == 2 )
  78.             {
  79.                 won = 0;
  80.                
  81.                 Game->PlaySound(SFX_LOSS);
  82.                
  83.                 while(!Input->ReadKey[KEY_Y])
  84.                 {
  85.                     Screen->DrawString(6, 8, 20, 0, 1, -1, 0, "Fail. Press Y to try again.", 128);
  86.                     Waitframe();
  87.                 }
  88.                
  89.                 for ( int q = 0; q < 20; ++q ) StringBuffer[q] = 0;
  90.                 index = 0;
  91.             }
  92.             Waitframe();
  93.         }
  94.         Game->TypingMode = false;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement