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

📄 carddeck.c

📁 《OSEK/VDX汽车电子嵌入式软件编程技术》中光盘的资料
💻 C
字号:
/************************************************
*
*	$Copyright    2001 Joseph J. Lemieux  ALL RIGHTS RESERVED. $
*
*	$Filename: C:\OSEKBook\src\CH05\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
*
************************************************/

/************************************************
*
*	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
   };
/*
*  deckStart - pointer to the current start of the deck
*/
UINT8 *deckStart = cardDeck;

/*
*  Location in array busyDisplay that shows the progress
*  of the shuffle
*/
UINT8 busyLocation = 0;

/************************************************
*
*	Local Constants
*
************************************************/
typedef struct CARD_DISPLAY_TYPEtag {
   char string[4];
   }CARD_DISPLAY_TYPE;
/*
*  Constant pointer to the end of the card deck.
*
*/
UINT8 const *deckEnd = &cardDeck[52];
/*
*  Array of strings that define how a card is displayed
*  on the LCD. A value of \36x is translated to a special 
*  character of 0 to 0x0f. These special characters are
*  set when the display is initialized. 
*  \360 is the character for Heart
*  \361 is the character for Diamond
*  \362 is the character for Spade
*  \363 is the character for Club
*/
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"}
   };

/*
*  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;
   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;
   }
   strcpy(displayBuffer,busyDisplay[busyLocation++]);
   if(busyLocation == 4) busyLocation = 0;
   ActivateTask(OutputDisplay);
   TerminateTask();
}

/************************************************
*
*   Function:     DealCard
*
*   Inputs:       None
*
*   Outputs:      Displays next card on LCD
*
*   Returns:      Number of cards left in deck.
*
*   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(void)
{
   if(deckStart != deckEnd){
      strcpy(displayBuffer,cardsOutput[*deckStart++].string);
      ActivateTask(OutputDisplay);
   }
   return (UINT8) (cardDeck + sizeof(cardDeck) - deckStart);
}

#endif /* C */

⌨️ 快捷键说明

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