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

📄 cardgame.c

📁 《OSEK/VDX汽车电子嵌入式软件编程技术》中光盘的资料
💻 C
📖 第 1 页 / 共 3 页
字号:
         }
         requestHeadToHead = FALSE;
         gameState = GAME_NORMAL;
         WriteDisplay(GameNormalPrompt);
      }
      else{
         ReceiveMessage(RequestModeMessageIn,requestedMode);
         if(*requestedMode == REQUEST_HEAD_TO_HEAD){
            WriteDisplay(RequestMultiPrompt);
            gameState = GAME_AWAITING_RESPONSE;
         }
      }
   }
   else{
      ReceiveMessage(RequestModeMessageIn,requestedMode);
      if(*requestedMode == CANCEL_HEAD_TO_HEAD){
         ChangeMode(SINGLE_PLAYER);
      }
      if(*requestedMode == SWITCH_DEALER){
         gameState = GAME_NORMAL;
         gameRole = GAME_DEALER;
         WriteDisplay(GameNormalPrompt);
         firstShuffle = FALSE;
      }
      if(*requestedMode == DEAL_NEW_HAND){
         if(newGameRequested == TRUE){
            newGameRequested = FALSE;
            if(gameRole == GAME_DEALER){
               newGameReceived = TRUE;
               simulatedKey = 'C';
               SetEvent(ProcessKeyPress,SIM_KEY);
            }
            else{
               gameState = GAME_PLAYER_TURN;
               dealerPosition = playerPosition = 0;
            }   
         }
         else{
            newGameReceived = TRUE;
         }
      }
   }   
   TerminateTask();
}

/************************************************
*
*   Task:         CardReceived
*
*   Description:  Activated whenever a Card message is received
*                 and process the message.
*
************************************************/
TASK(CardReceived)
{
BOOLEAN dealComplete;
char tempBuffer[4];
UINT8 i;
EventMaskType eventMask;

   while(ReceiveMessage(CardMessageIn,receivedCard) == E_OK){
      switch(receivedCard->type){
         case REQUEST_CARD:
            if(gameRole == GAME_DEALER){
               GetResource(CARDDECK);
               playerCards[playerPosition] = DealCard(PLAYER,playerPosition,TRUE);
               ReleaseResource(CARDDECK);
               playerScore += GetCardValue(playerCards[playerPosition]);
               if(playerScore > 21){
                  for(i=0;i<=playerPosition;i++){
                     if(GetCardValue(playerCards[i])==11){
                        playerCards[i] = ACE_IS_ONE;
                        playerScore -= 10;
                        break;
                     }
                  }
               }
               simulatedKey = 'X';
               SetEvent(ProcessKeyPress,SIM_KEY);
            }
            break;
         case END_TURN:
            if(gameRole == GAME_DEALER){
               gameState = GAME_DEALER_TURN;
               WriteDisplayAt(0,8,"*");
               WriteDisplayAt(2,8," ");
            }
            else{
               DisplayCard(dealerCards[0],tempBuffer);
               WriteDisplayAt(0,9,tempBuffer);
               EndGame();
            }
            break;
         case PLAYER_CARD:
            gameState = GAME_PLAYER_TURN;
            playerCards[receivedCard->position]=receivedCard->card;
            if(receivedCard->position > 0){
               playerScore += GetCardValue(playerCards[receivedCard->position]);
            }
            else{
               playerScore = GetCardValue(playerCards[receivedCard->position]);
            }
            DisplayCard(receivedCard->card,tempBuffer);
            if(receivedCard->position < 2){
               WriteDisplayAt(2,9 + (3*receivedCard->position),tempBuffer);
            }
            else{
               WriteDisplayAt(3,9 + (3*(receivedCard->position-2)),tempBuffer);
            }
            CheckScore(&playerScore,receivedCard->position,playerCards);
            break;
         case DEALER_CARD:
            dealerCards[receivedCard->position]=receivedCard->card;
            if(receivedCard->position > 0){
               DisplayCard(receivedCard->card,tempBuffer);
               dealerScore += GetCardValue(dealerCards[receivedCard->position]);
            }
            else{
               WriteDisplay(GameMessage);
               if(GetActiveApplicationMode() == HEAD_TO_HEAD){
                  if(gameRole == GAME_DEALER){
                     WriteDisplayAt(1,1,"*");
                  }
                  else{
                     WriteDisplayAt(3,1,"*");
                  }
               WriteDisplayAt(2,8,"*");
               }
               DisplayCard(BLANK_CARD,tempBuffer);
               dealerScore = GetCardValue(dealerCards[receivedCard->position]);
            }
            if(receivedCard->position < 2){
               WriteDisplayAt(0,9 + (3*receivedCard->position),tempBuffer);
            }
            else{
               WriteDisplayAt(1,9 + (3*(receivedCard->position-2)),tempBuffer);
            }
            CheckScore(&dealerScore,receivedCard->position,dealerCards);
            break;
      }
      
   }
   TerminateTask();
}
/************************************************
*
*   Task:         MonitorNetworkActivity
*
*   Description:  Monitors network activity and sends
*                 state of health messages periodically.
*
************************************************/
TASK(MonitorNetworkActivity)
{
   if(ReadFlag(messageReceived)==TRUE){
      deviceLonely = FALSE;
      ResetFlag(messageReceived);
   }
   nodeStatus->role = gameRole;
   SendMessage(NodeStatusMessageOut,nodeStatus);
   *remainingCardsOut = GetRemainingCards();
   SendMessage(RemainingCardsMessageOut,remainingCardsOut);
   TerminateTask();
}

/************************************************
*
*   Task:         SetDeviceLonely
*
*   Description:  Activated whenever a periodic message
*                 from another device is not received.
*
************************************************/
TASK(SetDeviceLonely)
{
   deviceLonely = TRUE;
   TerminateTask();
}

/************************************************
*
*   Task:         CardReceived
*
*   Description:  Activated whenever a Card message is received
*                 and process the message.
*
************************************************/
TASK(LostOpponent)
{
   if(GetActiveApplicationMode() == HEAD_TO_HEAD){
      ChangeMode(SINGLE_PLAYER);
   }
   TerminateTask();
}

/************************************************
*
*   Task:         OpponentMessageReceived
*
*   Description:  Task that is activated when an
*                 opponent message is completely received.
*
************************************************/
TASK(OpponentMessageReceived)
{
DataLength messageSize;
AddressType messageSource;

//   ReceiveMessageFrom(OpponentMessageIn,opponentMessageInBuffer,&messageSize,&messageSource);
   ReceiveMessage(OpponentMessageIn,opponentMessageInBuffer);
   WriteDisplay("\f");
   WriteDisplay(opponentMessageInBuffer);
   gameState = GAME_RECEIVED_OPPONENT_MESSAGE;
   WriteDisplayAt(3,0,OpponentMessageReceivedPrompt);
   CancelAlarm(IncomingMessageAlarm);
   TerminateTask();
}

/************************************************
*
*   Task:         IncomingMessage
*
*   Description:  Task that is activated after the first frame
*                 of an opponent message is received.
*
************************************************/
TASK(IncomingMessage)
{
   WriteDisplay(IncomingMessageNotice);
   SetRelAlarm(IncomingMessageAlarm,10000,0);
   gameStateOld = gameState;
   gameState = GAME_RECEIVING_OPPONENT_MESSAGE;
   TerminateTask();
}

/************************************************
*
*   Task:         AbortMessage
*
*   Description:  Task that is activated when a message is
*                 not received properly.
*
************************************************/
TASK(AbortMessage)
{
   WriteDisplay(GameNormalPrompt);
   gameState = gameStateOld;
   TerminateTask();
}
/************************************************
*
*   Function:     CheckGameTransition
*
*   Inputs:       keyInput - Key that was pressed
*
*   Outputs:      None
*
*   Returns:      Event that occurs based upon the entry in the table
*
*   Description:  Navigates through the table of possible 
*                 game transitions and returns the event
*                 that has occurred based upon the key that
*                 was pressed. If no event, the last entry in
*                 the table returns NO_ACTION.
*
************************************************/
GameTransitions CheckGameTransition(char keyInput)
{
GameTransitionTableType *table = transitions;

	while(table->key != 0){
      if((table->state == gameState) && (table->key == keyInput)){
         break;
      }
      table++;
   }
return table->event;
}

/************************************************
*
*   Function:     EndGame
*
*   Inputs:       None
*
*   Outputs:      Displays scores on LCD
*
*   Returns:      None
*
*   Description:  Calculates scores, displays on LCD.
*
************************************************/
void EndGame(void)
{
char tempDisplay[8];

   sprintf(tempDisplay,"%d",dealerScore);
   WriteDisplayAt(0,18,tempDisplay);   
   sprintf(tempDisplay,"%d",playerScore);
   WriteDisplayAt(2,18,tempDisplay);   
   gameState = GAME_NORMAL;
}

/************************************************
*
*   Function:     CheckScore
*
*   Inputs:       *score - pointer to the current score
*                 position - position in the card array of the last card played
*                 *cards - pointer to the had to be evaluated.
*
*   Outputs:      Modifies score for an Ace
*
*   Returns:      None
*
*   Description:  If the score is over 21, checks if an Ace is in
*                 the hand. If so, adjusts down 10 points and adjusts
*                 Ace value from 11 to 1.
*
************************************************/
void CheckScore(UINT8 *score, UINT8 position, UINT8 * cards)
{
UINT8 i;

   if(*score > 21){
   for(i=0;i<=position;i++){
      if(GetCardValue(cards[i])==11){
         cards[i] = ACE_IS_ONE;
         *score -= 10;
         break;
         }
      }
   }
}

/************************************************
*
*   Function:     GetGameState
*
*   Inputs:       None
*
*   Outputs:      None
*
*   Returns:      Current Game State
*
*   Description:  Determines current game state and returns it.
*
************************************************/
GameState GetGameState(void)
{
   return gameState;
}

/************************************************
*
*   Function:     ConvertKeyCombo
*
*   Inputs:       messageNumber - number that was pressed.
*                 pressedKey - Letter key that was pressed.
*
*   Outputs:      None
*
*   Returns:      Translated key from table
*
*   Description:  Determines a letter from A to Z based upon a number:letter
*                 key press combination.
*
************************************************/
UINT8 ConvertKeyCombo(UINT8 messageNumber,UINT8 pressedKey)
{
KeyConvertTableType *table = ConversionTable;

	while(table->number != 0){
      if((table->number == messageNumber) && (table->position == pressedKey)){
         break;
      }
      table++;
   }
return table->key;
}

#endif /* CARDGAMEC */

⌨️ 快捷键说明

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