📄 cardgame.c
字号:
/************************************************
*
* $Copyright 2001 Joseph J. Lemieux ALL RIGHTS RESERVED. $
*
* $Filename: C:\OSEKBook\src\CH13\src\cardgame.c $
*
* Description: Main routine to handle the processing
* of the card game.
*
************************************************/
#ifndef CARDGAMEC
#define CARDGAMEC
/************************************************
*
* Include files
*
************************************************/
#include "typedefs.h"
#include "os.h"
#include "init.h"
#include "keypad.h"
#include "dispdrv.h"
#include "carddeck.h"
#include "cardgame.h"
DeclareTask(OutputDisplay);
/************************************************
*
* Local macros
*
************************************************/
/************************************************
*
* Local type definitions
*
************************************************/
/*
* Defines the types of transitions that occur for the state
* machine. The event is processed in the function
* ProcessKeyPress
*/
typedef struct GameTransitionTableTypetag {
GameState state;
char key;
GameTransitions event;
}GameTransitionTableType;
/*
* Defines the type of structure that converts a number:position
* press on keypad into a character from A to Z.
*/
typedef struct KeyConvertTableTypetag{
UINT8 number;
UINT8 position;
UINT8 key;
}KeyConvertTableType;
/************************************************
*
* Local Function Prototypes
*
************************************************/
GameTransitions CheckGameTransition(char keyInput);
void EndGame(void);
void CheckScore(UINT8 *score, UINT8 position, UINT8 * cards);
UINT8 ConvertKeyCombo(UINT8 messageNumber,UINT8 pressedKey);
/************************************************
*
* Local Variables
*
************************************************/
/*
* Current state of the game. Default is GAME_NORMAL
*/
GameState gameState = GAME_NORMAL;
GameState gameStateOld = GAME_NORMAL;
/*
* Player specific variables - total score, position in hand (1-5),
* and actual cards in hand.
*/
UINT8 dealerScore,playerScore;
UINT8 dealerPosition, playerPosition;
UINT8 dealerCards[5], playerCards[5];
/*
* simulatedKey - used to simulate a key press when a message is received.
*/
UINT8 simulatedKey;
/*
* Pointer used to navigate the game transitions table
*/
GameTransitionTableType * transitions;
/*
* State variables used to determine state of various parameters
*/
static BOOLEAN requestHeadToHead,firstShuffle,newGameRequested,newGameReceived;
/*
* Role played by this device. Defaults to a Player role.
*/
static UINT8 gameRole = GAME_PLAYER;
/*
* Flag that indicates whether the device is alone on the network.
*/
static BOOLEAN deviceLonely = FALSE;
/*
* Flag indicating that a message has been received.
*/
BOOLEAN messageReceived = FALSE;
/*
* Variables that define the size of the message that is
* sent from one player to the other.
*/
DataLength messageSize;
UINT8 messageNumber;
/*
* Variable that points to the recipient of the message. The
* recipient will change depending upon the node
*/
#ifdef NODE0
AddressType messageOpponent = OpponentMessageNode1;
#else
AddressType messageOpponent = OpponentMessageNode0;
#endif
/************************************************
*
* Local Constants
*
************************************************/
/*
* Messages that are shown on the display for various actions.
*/
char const GameMessage[] = "\fDealer:\n\nPlayer:";
char const RequestModeAwaitPrompt[] = "\fWaiting for Response\n Press # to Cancel";
char const GameNormalPrompt[] = "\fPress D to Shuffle";
char const RequestMultiPrompt[] = "\fDo you want to play\n HEAD_TO_HEAD?\n A=YES B=NO";
char const WaitingPrompt[] = "\fWaiting for Shuffle";
char const OpponentMessagePrompt[] = "\fEnter Message then #";
char const OpponentMessageReceivedPrompt[] = "Press # to Clear";
char const IncomingMessageNotice[] = "\fIncomingMessage";
/*
* Table of Transitions - Both single and head to head games
*/
GameTransitionTableType const gameTransitionTableSingle[] = {
{GAME_NORMAL,'D',START_SHUFFLING},
{GAME_NORMAL,'C',NEW_GAME},
{GAME_NORMAL,'*',REQUEST_MULTIPLE},
{GAME_NORMAL,'#',CREATE_OPPONENT_MESSAGE},
{GAME_PLAYER_TURN,'A',DEAL_PLAYER_CARD},
{GAME_PLAYER_TURN,'B',END_PLAYER_TURN},
{GAME_PLAYER_TURN,'D',START_SHUFFLING},
{GAME_DEALER_TURN,'D',START_SHUFFLING},
{GAME_AWAITING_RESPONSE,'#',CANCEL_REQUEST},
{GAME_AWAITING_RESPONSE,'A',ACCEPT_REQUEST},
{GAME_AWAITING_RESPONSE,'B',REJECT_REQUEST},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'1',ENTER_SPACE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'2',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'3',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'4',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'5',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'6',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'7',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'8',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'9',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'0',ENTER_PERIOD},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'A',ENTER_LETTER_EXACT},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'B',ENTER_LETTER_EXACT},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'C',ENTER_LETTER_EXACT},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'D',ENTER_LETTER_EXACT},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'#',SEND_OPPONENT_MESSAGE},
{GAME_RECEIVED_OPPONENT_MESSAGE,'#',CLEAR_OPPONENT_MESSAGE},
{0,0,NO_ACTION}
};
GameTransitionTableType const gameTransitionTableMultiple[] = {
{GAME_NORMAL,'D',START_SHUFFLING},
{GAME_NORMAL,'#',CREATE_OPPONENT_MESSAGE},
{GAME_PLAYER_TURN,'A',REQUEST_CARD_DEALT},
{GAME_PLAYER_TURN,'B',RELINQUISH_TURN},
{GAME_DEALER_WAIT,'X',SEND_PLAYER},
{GAME_DEALER_TURN,'A',DEAL_MULTI_CARD},
{GAME_DEALER_TURN,'B',RELINQUISH_TURN},
{GAME_NORMAL,'C',NEW_MULTI_GAME},
{GAME_NORMAL,'*',CANCEL_MULTI},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'1',ENTER_SPACE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'2',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'3',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'4',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'5',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'6',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'7',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'8',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'9',ENTER_LETTER_RANGE},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'0',ENTER_PERIOD},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'A',ENTER_LETTER_EXACT},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'B',ENTER_LETTER_EXACT},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'C',ENTER_LETTER_EXACT},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'D',ENTER_LETTER_EXACT},
{GAME_CONSTRUCT_OPPONENT_MESSAGE,'#',SEND_OPPONENT_MESSAGE},
{GAME_RECEIVED_OPPONENT_MESSAGE,'#',CLEAR_OPPONENT_MESSAGE},
{0,0,NO_ACTION}
};
/*
* Table of Key Translations - When the Number:Letter is pressed
* on the keypad, the third entry is displayed. Only used to send
* messages.
*/
KeyConvertTableType ConversionTable[] = {
{'2','A','A'},
{'2','B','B'},
{'2','C','C'},
{'2','D','C'},
{'3','A','D'},
{'3','B','E'},
{'3','C','F'},
{'3','D','F'},
{'4','A','G'},
{'4','B','H'},
{'4','C','I'},
{'4','D','I'},
{'5','A','J'},
{'5','B','K'},
{'5','C','L'},
{'5','D','L'},
{'6','A','M'},
{'6','B','N'},
{'6','C','O'},
{'6','D','O'},
{'7','A','P'},
{'7','B','Q'},
{'7','C','R'},
{'7','D','S'},
{'8','A','T'},
{'8','B','U'},
{'8','C','V'},
{'8','D','V'},
{'9','A','W'},
{'9','B','X'},
{'9','C','Y'},
{'9','D','Z'},
{0,'A',' '}
};
/************************************************
*
* Functions
*
************************************************/
/************************************************
*
* Function: InitGame
*
* Inputs: type - Type of initialization
*
* Outputs: Mode specific variable are set here
*
* Returns: None
*
* Description: This routine initializes the state of the
* game based upon the type of mode and the device
* which is either Node0 or Node1
*
************************************************/
void InitGame(InitType type)
{
if(GetActiveApplicationMode() == HEAD_TO_HEAD){
transitions = (GameTransitionTableType *)gameTransitionTableMultiple;
#ifdef NODE0
gameRole = GAME_DEALER;
firstShuffle = FALSE;
#else
gameRole = GAME_PLAYER;
firstShuffle = TRUE;
#endif
dealerPosition = playerPosition = 0;
newGameRequested = newGameReceived = FALSE;
}
else{
transitions = (GameTransitionTableType *)gameTransitionTableSingle;
gameRole = GAME_DEALER;
firstShuffle = FALSE;
}
gameState = GAME_NORMAL;
requestHeadToHead = FALSE;
}
/************************************************
*
* Task: ProcessKeyPress
*
* Description: Main dispatching task that processes
* all key presses as they occur.
*
************************************************/
TASK(ProcessKeyPress)
{
EventMaskType eventMask;
BOOLEAN shuffleComplete,dealComplete;
UINT8 i,pressedKey;
char tempBuffer[4];
while(1){
WaitEvent(KEYPRESS|SIM_KEY);
GetEvent(ProcessKeyPress,(EventMaskRefType)&eventMask);
ClearEvent(KEYPRESS|SIM_KEY);
while((ReceiveMessage(KeyPressMessage,keyValue) == E_OK)||((eventMask & SIM_KEY) != 0)){
if((eventMask&SIM_KEY)!=0){
pressedKey=simulatedKey;
eventMask &= ~SIM_KEY;
}
else{
pressedKey=*keyValue;
}
switch(CheckGameTransition(pressedKey)){
case START_SHUFFLING:
if(firstShuffle == FALSE){
ActivateTask(DealCards);
ActivateTask(ShuffleCards);
gameState = GAME_SHUFFLING;
shuffleComplete = FALSE;
do{
WaitEvent(KEYPRESS|SHUFFLED);
GetEvent(ProcessKeyPress,(EventMaskRefType)&eventMask);
if((eventMask & KEYPRESS)!= 0){
ClearEvent(KEYPRESS);
while(ReceiveMessage(KeyPressMessage,keyValue) == E_OK){
if(*keyValue == '#'){
CancelAlarm(ShuffleAlarm);
SetEvent(DealCards,ABORT_SHUFFLE);
shuffleComplete = TRUE;
WriteDisplay(GameNormalPrompt);
}
}
}
else{
ClearEvent(SHUFFLED);
shuffleComplete = TRUE;
if(GetActiveApplicationMode() == HEAD_TO_HEAD){
firstShuffle = TRUE;
WaitEvent(CARDS_DEALT);
ClearEvent(CARDS_DEALT);
dealtCard->type = DEALER_CARD;
dealtCard->position = 0;
dealtCard->card = dealerCards[0];
ClearEvent(TRANSMIT_COMPLETE);
SendMessage(CardMessageOut,dealtCard);
WaitEvent(TRANSMIT_COMPLETE);
ClearEvent(TRANSMIT_COMPLETE);
dealtCard->position = 1;
dealtCard->card = dealerCards[1];
SendMessage(CardMessageOut,dealtCard);
WaitEvent(TRANSMIT_COMPLETE);
ClearEvent(TRANSMIT_COMPLETE);
dealtCard->type = PLAYER_CARD;
dealtCard->position = 0;
dealtCard->card = playerCards[0];
SendMessage(CardMessageOut,dealtCard);
WaitEvent(TRANSMIT_COMPLETE);
ClearEvent(TRANSMIT_COMPLETE);
dealtCard->position = 1;
dealtCard->card = playerCards[1];
SendMessage(CardMessageOut,dealtCard);
}
}
}while(shuffleComplete == FALSE);
}
break;
case DEAL_PLAYER_CARD:
GetResource(CARDDECK);
playerCards[playerPosition] = DealCard(PLAYER,playerPosition,TRUE);
ReleaseResource(CARDDECK);
playerScore += GetCardValue(playerCards[playerPosition]);
CheckScore(&playerScore,playerPosition,playerCards);
if((++playerPosition == 5) || (playerScore > 21)){
DisplayCard(dealerCards[0],tempBuffer);
WriteDisplayAt(0,9,tempBuffer);
EndGame();
}
break;
case END_PLAYER_TURN:
gameState = GAME_DEALER_TURN;
ActivateTask(DealerTurn);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -