wemos d1でblackjack


概要

wemos d1でblackjackやってみた。

写真

サンプルコード


#define DEALER          0
#define PLAYER          1
#define SCREENLINECOUNT 1
#define ACELOW          0
#define ACEHIGH         1
char inkey()
{
    char a = ' ';
    while (Serial.available() == 0)
    {
        delay(10);
    }
    switch (Serial.read())
    {
    case 'y':
        a = 'Y';
    break;
    case 'n':
        a = 'N';
    break;
    case 'h':
        a = 'H';
    break;
    case 's':
        a = 'S';
    break;
    default:
    break;
    }
    return a;
}
char getAns(char mesg[])
{
    char ans;
    Serial.print(mesg);
    ans = inkey();
    return toupper(ans);
}
void dispTitle(void)
{
    int i = 0;
    while (i < SCREENLINECOUNT)
    {
        Serial.print("\n");
        i++;
    }
    Serial.print("\n\nStep right up to the Blackjack tables*\n\n");
    return;
}
int dealCard(int * numCards, int cards[52])
{
    int cardDrawn,
        subDraw;
    time_t t;
    subDraw = (rand() % (*numCards));
    cardDrawn = cards[subDraw];
    cards[subDraw] = cards[*numCards - 1];
    (*numCards)--;
    return cardDrawn;
}
void dispCard(int cardDrawn, int points[2])
{
    switch (cardDrawn)
    {
    case(11) :
        Serial.print("Jack ");
        points[ACELOW] += 10;
        points[ACEHIGH] += 10;
    break;
    case(12) :
        Serial.print("Queen ");
        points[ACELOW] += 10;
        points[ACEHIGH] += 10;
    break;
    case(13) :
        Serial.print("King ");
        points[ACELOW] += 10;
        points[ACEHIGH] += 10;
    break;
    default:
        points[ACELOW] += cardDrawn;
        if (cardDrawn == 1)
        {
            Serial.print("Ace ");
            points[ACEHIGH] += 11;
        }
        else
        {
            points[ACEHIGH] += cardDrawn;
            Serial.print(cardDrawn);
            Serial.print(" ");
        }
    }
    return;
}
void findWinner(int total[2])
{
    if (total[DEALER] == 21)
    {
        Serial.println("The house wins.");
        return;
    }
    if ((total[DEALER] > 21) && (total[PLAYER] > 21))
    {
        Serial.println("Nobody wins.");
        return;
    }
    if ((total[DEALER] >= total[PLAYER]) && (total[DEALER] < 21))
    {
        Serial.println("The house wins.");
        return;
    }
    if ((total[PLAYER] > 21) && (total[DEALER] < 21))
    {
        Serial.println("The house wins.");
        return;
    }
    Serial.println("You win!");
    return;
}
void totalIt(int points[2], int total[2], int who)
{
    if ((points[ACELOW] == points[ACEHIGH]) || (points[ACEHIGH] > 21))
    {
        total[who] = points[ACELOW];
    }
    else
    {
        total[who] = points[ACEHIGH];
    }
    if (who == PLAYER)
    {
        Serial.print("You have a total of ");
        Serial.println(total[PLAYER]);
    }
    else
    {
        Serial.print("The house stands with a total of ");
        Serial.println(total[DEALER]);
    }
    return;
}
void playerGetsCard(int *numCards, int cards[52], int playerPoints[2])
{
    int newCard;
    newCard = dealCard(numCards, cards);
    Serial.print("You draw: ");
    dispCard(newCard, playerPoints);
}
void dealerGetsCard(int *numCards, int cards[52], int dealerPoints[2])
{
    int newCard;
    newCard = dealCard(numCards, cards);
    Serial.print("The dealer draws: ");
    dispCard(newCard, dealerPoints);
}
void initCardsScreen(int cards[52], int playerPoints[2], int dealerPoints[2], int total[2], int *numCards)
{
    int sub,
        val = 1;
    *numCards = 52;
    for (sub = 0; sub <= 51; sub++)
    {
        val = (val == 14) ? 1 : val;
        cards[sub] = val;
        val++;
    }
    for (sub = 0; sub <= 1; sub++)
    {
        playerPoints[sub] = dealerPoints[sub] = total[sub] = 0;
    }
    dispTitle();
    return;
}
void main0()
{
    int numCards;
    int cards[52],
        playerPoints[2],
        dealerPoints[2],
        total[2];
    char ans;
    do
    {
        initCardsScreen(cards, playerPoints, dealerPoints, total, &numCards);
        dealerGetsCard(&numCards, cards, dealerPoints);
        Serial.print("\n");
        playerGetsCard(&numCards, cards, playerPoints);
        playerGetsCard(&numCards, cards, playerPoints);
        do
        {
            ans = getAns("Hit or stand (H/S)? ");
            if (ans == 'H')
            {
                playerGetsCard(&numCards, cards, playerPoints);
            }
        } while (ans != 'S');
        totalIt(playerPoints, total, PLAYER);
        do
        {
            dealerGetsCard(&numCards, cards, dealerPoints);
        } while (dealerPoints[ACEHIGH] < 17);
        totalIt(dealerPoints, total, DEALER);
        findWinner(total);
        ans = getAns("\nPlay again (Y/N)? ");
    } while (ans == 'Y');
}
void setup()
{
    Serial.begin(115200);
    while (!Serial) delay(250);
    Serial.print("ok");
    main0();
}
void loop()
{
    delay(5000);
}

以上。