📄 cardgame.c
字号:
/************************************************
*
* $Copyright 2001 Joseph J. Lemieux ALL RIGHTS RESERVED. $
*
* $Filename: C:\OSEKBook\src\CH12\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;
/************************************************
*
* Local Function Prototypes
*
************************************************/
GameTransitions CheckGameTransition(char keyInput);
void EndGame(void);
void CheckScore(UINT8 *score, UINT8 position, UINT8 * cards);
/************************************************
*
* Local Variables
*
************************************************/
/*
* Current state of the game. Default is GAME_NORMAL
*/
GameState gameState = 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;
/************************************************
*
* 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";
/*
* 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_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},
{0,0,NO_ACTION}
};
GameTransitionTableType const gameTransitionTableMultiple[] = {
{GAME_NORMAL,'D',START_SHUFFLING},
{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},
{0,0,NO_ACTION}
};
/************************************************
*
* 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;
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){
i=simulatedKey;
eventMask &= ~SIM_KEY;
}
else{
i=*keyValue;
}
switch(CheckGameTransition(i)){
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);
break;
case NEW_GAME:
ActivateTask(DealCards);
if(GetRemainingCards() < GAME_MINIMUM_CARDS){
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;
}
}while(shuffleComplete == FALSE);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -