⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shadow.cpp

📁 一个游戏 F1:记分模式,过关加500分
💻 CPP
📖 第 1 页 / 共 5 页
字号:
{'k',"by the cooling fans.                                                       "},

{'w',"You hear nothing but a slight echo in the heating ducts."},

{'h',"You hear the sounds of music, but you can't make out any of the words or"},
{'h',"melodies.                                                               "},

{'r',"You hear the sound of driping water and the whispers of a femal voice"},
{'r',"ever so faintly in the background.                                   "},

{'e',"You hear the noises of the outside world muffled by the closed door to the"},
{'e',"South.                                                                    "},

{'o',"You hear nothing but the perpetual hum of all the cooling fans within the"},
{'o',"electronic equipment.                                                    "},
{'X',""},

};

int sentence[8];               // this array holds the current sentence
int num_tokens;                // number of words in current sentecne

// this is the player

player you={"Andre'",10,29,NORTH,{' ',' ',' ',' ',' ',' ',' ',' '},0};

int global_exit=0;             // global exit flag

char global_input[128],        // input string
     global_output[128];       // output string

// F U N C T I O N S //////////////////////////////////////////////////////////

char *Get_Line(char *buffer)
{
// this function gets a single line of input and tolerates white space

int c,index=0;

// loop while user hasn't hit return

while((c=getch())!=13)
     {

// implement backspace

     if (c==8 && index>0)
        {

        buffer[--index] = ' ';
        printf("%c %c",8,8);

        } // end if backspace
     else
     if (c>=32 && c<=122)
        {
        buffer[index++] = c;
        printf("%c",c);

        } // end if in printable range

     } // end while

// terminate string

buffer[index] = 0;

// return pointer to buffer or NULL

if (strlen(buffer)==0)
   return(NULL);
else
return(buffer);

} // end Get_Line

////////////////////////////////////////////////////////////////////////////////

int Get_Token(char *input,char *output,int *current_pos)
{

int index,   // loop index and working index
    start,   // points to start of token
    end;     // points to end of token

// set current positions

index=start=end=*current_pos;

// eat white space

while(isspace(input[index]) || ispunct(input[index]))
     {

     index++;

     } // end while

// test if end of string found

if (input[index]==NULL)
   {
   // emit nothing

   strcpy(output,"");
   return(0);

   } // end if no more tokens

// at this point, we must have a token of some kind, so find the end of it

start = index; // mark front of it
end   = index;

// find end of Token

while(!isspace(input[end]) && !ispunct(input[end]) && input[end]!=NULL)
     {

     end++;

     } // end while

// build up output string

for (index=start; index<end; index++)
    {

    output[index-start] = toupper(input[index]);

    } // end copy string

// place terminator

output[index-start] = 0;

// update current string position

*current_pos  = end;

return(end);

} // end Get_Token

///////////////////////////////////////////////////////////////////////////////

int Extract_Tokens(char *string)
{
// this function breaks the input string down into tokens and fills up
// the global sentence array with the tokens so that it can be processed

int curr_pos=0,      // current position in string
    curr_token=0,    // current token number
    found,           // used to flag if the token is valid in language
    index;           // loop index

char output[16];

// reset number of tokens and clear the sentence out

num_tokens=0;

for (index=0; index<8; index++)
    sentence[index]=0;

// extract all the words in the sentence (tokens)

while(Get_Token(string,output,&curr_pos))
     {

     // test to see if this is a valid token

     for (index=0,found=0; index<NUM_TOKENS; index++)
         {

         // do we have a match?

         if (strcmp(output,language[index].symbol)==0)
            {
            // set found flag

            found=1;

            // enter token into sentence

            sentence[curr_token++] = language[index].value;
        //    printf("\nEntering %s, %d in sentence",
        //                               output,language[index].value);

            break;

            } // end if

         } // end for index

         // test if token was part of language (grammar)

         if (!found)
            {
            printf("\n%s, I don't know what \"%s\" means.",you.name
                                                          ,output);

            // failure

            return(0);

            } // end if not found

         // else

         num_tokens++;

     } // end while

return(1);

} // end Extract_Tokens

///////////////////////////////////////////////////////////////////////////////

void Verb_Parser(void)
{
// this function breaks down the sentence and based on the verb calls the
// appropriate "method" or function to apply that verb
// note: syntactic analysis could be done here, but I decided to place it
// in the action verb functions, so that you can see the way the errors are
// detected for each verb (even though there is a a lot of redundancy)

// what is the verb?

switch(sentence[FIRST_WORD])
      {

      case ACTION_MOVE:
           {
           // call the appropriate function

           Verb_MOVE();

           } break;

      case ACTION_TURN:
           {
           // call the appropriate function

           Verb_TURN();

           } break;

      case ACTION_SMELL:
           {
           // call the appropriate function

           Verb_SMELL();

           } break;

      case ACTION_LOOK:
           {
           // call the appropriate function

           Verb_LOOK();

           } break;

      case ACTION_LISTEN:
           {
           // call the appropriate function

           Verb_LISTEN();

           } break;

      case ACTION_PUT:
           {
           // call the appropriate function

           Verb_PUT();

           } break;

      case ACTION_GET:
           {
           // call the appropriate function

           Verb_GET();

           } break;

      case ACTION_EAT:
           {
           // call the appropriate function

           Verb_EAT();

           } break;

      case ACTION_WHERE:
           {
           // call the appropriate function

           Verb_WHERE();

           } break;

      case ACTION_INVENTORY:
           {
           // call the appropriate function

           Verb_INVENTORY();

           } break;

      case ACTION_EXIT:
           {
           // call the appropriate function

           Verb_EXIT();

           } break;

      default:
             {
             printf("\n%s, you must start a sentence with an action verb!",
                    you.name);

             return;

             } break;

      } // end switch

} // end Verb_Parser

// THE ACTION VERBS ///////////////////////////////////////////////////////////

int Verb_MOVE(void)
{
// this function will figure out which way the player wants to move,
// then move the player and test for syntax errors

int token_index,   // current token being processed
    dx,dy;         // ised to hold translation factors

// these look up tables are used to compute the translation factors
// needed to move the player in the requested direction based on the
// current direction, the problem occurs since the directives are not
// absolute directions, they are relative to the direction the player
// is facing

static int forward_x[]={1,-1,0,0};
static int forward_y[]={0,0,-1,1};

static int backward_x[]={-1,1,0,0};
static int backward_y[]={0,0,1,-1};

static int left_x[]={0, 0,-1,1};
static int left_y[]={-1,1,0,0};

static int right_x[]={0,0,1,-1};
static int right_y[]={1,-1,0,0};

// test if player didn't say which way, if so just move forward
// this functionality was added after the fact so is a slight cludge
// it is accomplished by synthetically inserting the direction "forward" into
// the sentence

if (num_tokens==1)
   {
   // no direction given so assume forward

   sentence[SECOND_WORD] = DIR_2_FORWARD;

   num_tokens++;

   } // end if no direction

   // begin further processing to figure out direction

   // check if the next word is a direction and if so move in that
   // direction

   // first test if the words 'to' or 'to the' are inserted bewteen action
   // verb and noun (object).  In this case the phrase "move to the right"
   // sounds ok and should be passed, but "move to the forward" will also
   // be passed even though it is grammatically incorrent, but that's life

   token_index=1;

   if (Check_For_Phrase(PHRASE_TO_THE,token_index))
      {
      // consume preposition since it has to bearing on the final
      // meaning sentence

      // index token scan to directon

      token_index=3;

      } // end if prep and article
   else
   if (Check_For_Phrase(PHRASE_TO,token_index))
      {
      // consume preposition since it has to bearing on the final
      // meaning sentence

      // index token scan to directon

      token_index=2;

      } // end if prep

   // at this point the token_index is pointing to the direction

   if (sentence[token_index] >= DIR_2_START &&
       sentence[token_index] <= DIR_2_END)
      {
      // at this point we finally know what the user is asking for, so
      // let's do it

      // based on direction asked for do movement and collision detection
      // note: the use of look up tables to decrease the complexity of the
      // conditional logic

      dx=dy=0;

      switch(sentence[token_index])
            {

            case DIR_2_FORWARD:  // move player forward
                 {
                 // compute translation factors using look up tables

                 dx = forward_x[you.direction];
                 dy = forward_y[you.direction];

                 } break;

            case DIR_2_BACKWARD: // move player backward
                 {
                 // compute translation factors using look up tables

                 dx = backward_x[you.direction];
                 dy = backward_y[you.direction];

                 } break;

            case DIR_2_RIGHT:    // parry right
                 {
                 // compute translation factors using look up tables

                 dx = right_x[you.direction];
                 dy = right_y[you.direction];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -