📄 carddeck.c
字号:
/************************************************
*
* $Copyright 2001 Joseph J. Lemieux ALL RIGHTS RESERVED. $
*
* $Filename: C:\OSEKBook\src\CH11\src\carddeck.c $
*
* Description: Routines to handle the card deck.
*
************************************************/
#ifndef CARDDECKC
#define CARDDECKC
/************************************************
*
* Include files
*
************************************************/
#include "typedefs.h"
#include "os.h"
#include "dispdrv.h"
#include "stdlib.h"
#include "carddeck.h"
/************************************************
*
* Local macros
*
************************************************/
/*
* Special definition for the routine DisplayCard()
* that indicates that the back of the card should
* be displayed.
*/
#define BLANK_CARD 0xFF
/************************************************
*
* Local type definitions
*
************************************************/
/************************************************
*
* Local Function Prototypes
*
************************************************/
/************************************************
*
* Local Variables
*
************************************************/
/*
* cardDeck - Array that holds the current deck
* of cards. One additional entry to avoid overflow.
*/
UINT8 cardDeck[53] = {
0,1,2,3,4,5,6,7,8,9,10,11,12,
13,14,15,16,17,18,19,20,21,22,23,24,25,
26,27,28,29,30,31,32,33,34,35,36,37,38,
39,40,41,42,43,44,45,46,47,48,49,50,51,52
};
/*
* cardValues - These card values translate the raw value
* of each card to the equivalent decimal value.
*/
UINT8 const cardValues[53] = {
11,2,3,4,5,6,7,8,9,10,10,10,10,
11,2,3,4,5,6,7,8,9,10,10,10,10,
11,2,3,4,5,6,7,8,9,10,10,10,10,
11,2,3,4,5,6,7,8,9,10,10,10,10,1
};
/*
* deckStart - pointer to the current start of the deck
*/
UINT8 *deckStart = cardDeck;
UINT8 remainingCards;
UINT8 busyLocation = 0;
/************************************************
*
* Local Constants
*
************************************************/
typedef struct CARD_DISPLAY_TYPEtag {
char string[4];
}CARD_DISPLAY_TYPE;
UINT8 const *deckEnd = &cardDeck[52];
CARD_DISPLAY_TYPE const cardsOutput[52] = {
{"A\360\0"},{"2\360\0"},{"3\360\0"},{"4\360\0"},
{"5\360\0"},{"6\360\0"},{"7\360\0"},{"8\360\0"},
{"9\360\0"},{"10\360\0"},{"J\360\0"},{"Q\360\0"},{"K\360\0"},
{"A\361\0"},{"2\361\0"},{"3\361\0"},{"4\361\0"},
{"5\361\0"},{"6\361\0"},{"7\361\0"},{"8\361\0"},
{"9\361\0"},{"10\361\0"},{"J\361\0"},{"Q\361\0"},{"K\361\0"},
{"A\362\0"},{"2\362\0"},{"3\362\0"},{"4\362\0"},
{"5\362\0"},{"6\362\0"},{"7\362\0"},{"8\362\0"},
{"9\362\0"},{"10\362\0"},{"J\362\0"},{"Q\362\0"},{"K\362\0"},
{"A\363\0"},{"2\363\0"},{"3\363\0"},{"4\363\0"},
{"5\363\0"},{"6\363\0"},{"7\363\0"},{"8\363\0"},
{"9\363\0"},{"10\363\0"},{"J\363\0"},{"Q\363\0"},{"K\363\0"}
};
/*
* Special value of a card that displays the back of the
* dealer's hole card.
*/
CARD_DISPLAY_TYPE const cardBack = {"\365\0"};
/*
* busyDisplay - series of displays that indicate to the player
* that the card deck is being shuffled.
* \364 is the character for a \.
*/
char const busyDisplay[4] [13] = {
"\fSHUFFLING \364",
"\fSHUFFLING |",
"\fSHUFFLING /",
"\fSHUFFLING -"
};
/************************************************
*
* Functions
*
************************************************/
/************************************************
*
* Task: ShuffleCards
*
* Description: Shuffles the carddeck one time.
*
************************************************/
TASK(ShuffleCards)
{
UINT8 count = (UINT8)(((UINT32)rand() * 100u) / RAND_MAX) + 1;
UINT8 location1, location2,tempcard;
deckStart = cardDeck;
remainingCards = 52;
GetResource(CARDDECK);
while((count--)>0){
location1 = ((UINT32)rand() * 52u) / RAND_MAX;
if(location1==52) location1 = 51;
location2 = ((UINT32)rand() * 52u) / RAND_MAX;
if(location2==52) location2 = 51;
tempcard = cardDeck[location1];
cardDeck[location1] = cardDeck[location2];
cardDeck[location2] = tempcard;
}
ReleaseResource(CARDDECK);
WriteDisplay(busyDisplay[busyLocation++]);
if(busyLocation == 4) busyLocation = 0;
TerminateTask();
}
/************************************************
*
* Function: DealCard
*
* Inputs: None
*
* Outputs: Displays next card on LCD
*
* Returns: Card Dealt
*
* Description: Pulls next card from deck and
* displays it on the LCD. Counts the
* number of cards left in the deck.
* If no cards are left, it returns 0
* immediately.
*
************************************************/
UINT8 DealCard(PlayerType player,UINT8 position, BOOLEAN up)
{
UINT8 cardDealt;
char tempBuffer[4];
if(deckStart != deckEnd){
cardDealt = *deckStart;
remainingCards--;
if(up == TRUE){
DisplayCard(*deckStart,tempBuffer);
}
else{
DisplayCard(BLANK_CARD,tempBuffer);
}
deckStart++;
if(player == DEALER){
if(position < 2){
WriteDisplayAt(0,9 + (3*position),tempBuffer);
}
else{
WriteDisplayAt(1,9 + (3*(position-2)),tempBuffer);
}
}
else{
if(position < 2){
WriteDisplayAt(2,9 + (3*position),tempBuffer);
}
else{
WriteDisplayAt(3,9 + (3*(position-2)),tempBuffer);
}
}
}
return cardDealt;
}
/************************************************
*
* Function: DisplayCard
*
* Inputs: card - card to be displayed.
* displayBuffer - buffer that contains characters
*
* Outputs: Displays a card on the LCD
*
* Returns: void
*
* Description: Displays the actual card on the LCD.
* if BLANK_CARD is passed, then the back
* of the card is displayed.
*
************************************************/
void DisplayCard(UINT8 card, char * displayBuffer)
{
if(card == BLANK_CARD){
strcpy(displayBuffer,cardBack);
}
else{
strcpy(displayBuffer,cardsOutput[card].string);
}
}
/************************************************
*
* Function: GetCardValue
*
* Inputs: Card to be evaluated.
*
* Outputs: None
*
* Returns: Value of the card from 2 to 11.
*
* Description: Evaluates the value of the card.
*
************************************************/
UINT8 GetCardValue(UINT8 card)
{
return cardValues[card];
}
/************************************************
*
* Function: GetRemainingCards
*
* Inputs: None
*
* Outputs: None
*
* Returns: Number of cards remaining in the deck.
*
* Description: Determines how many cards remain in the
* deck and returns that value.
*
************************************************/
UINT8 GetRemainingCards(void)
{
return remainingCards;
}
#endif /* C */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -