LoginSignup
0
0

More than 5 years have passed since last update.

wemos d1でblackjack その2

Posted at

概要

wemos d1でblackjackやってみた。

写真

b.JPG

サンプルコード


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;
}
int shuff(int cards[])
{
    int t;
    int i;
    int desk[52];
    for (i = 0; i < 52; i++) desk[i] = (i / 13 + 3) * 100 + i % 13 + 1;
    for (i = 0; i < 52; i++)
    {
        do
        {
            t = rand() % 52;
        } while (desk[t] == 0);
        cards[i] = desk[t];
        desk[t] = 0;
    }
    return 0;
}
int convert_jkq(int a)
{
    if ((a % 100 == 11) || (a % 100 == 12) || (a % 100 == 13)) return (a / 100) * 100 + 10;
    else return a;
}
void pic(int num)
{
    char fl;
    int po_num;
    fl = num / 100;
    po_num = num % 100;
    switch (po_num)
    {
        case 1:
        {
            Serial.print("*******\n");
            Serial.print("*     *\n");
            Serial.print("*  ");
            Serial.print(fl);
            Serial.print("  *\n*   A *\n");
            Serial.print("*     *\n");
            Serial.print("*******\n");
            break;
        }
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
        case 10:
        {
            Serial.print("*******\n");
            Serial.print("*     *\n");
            Serial.print("*  ");
            Serial.print(fl);
            Serial.print("  *\n*  ");
            Serial.print(po_num);
            Serial.print(" *\n*     *\n");
            Serial.print("*******\n");
            break;
        }
        case 11:
        {
            Serial.print("*******\n");
            Serial.print("*     *\n");
            Serial.print("*  ");
            Serial.print(fl);
            Serial.print("  *\n*   J *\n");
            Serial.print("*     *\n");
            Serial.print("*******\n");
            break;
        }
        case 12:
        {
            Serial.print("*******\n");
            Serial.print("*     *\n");
            Serial.print("*  ");
            Serial.print(fl);
            Serial.print("  *\n*   Q *\n");
            Serial.print("*     *\n");
            Serial.print("*******\n");
            break;
        }
        case 13:
        {
            Serial.print("*******\n");
            Serial.print("*     *\n");
            Serial.print("*  ");
            Serial.print(fl);
            Serial.print("  *\n*   K *\n");
            Serial.print("*     *\n");
            Serial.print("*******\n");
            break;
        }
    }
}
int play(void)
{
    int i;
    int psum = 0;
    int bsum = 0;
    int pcards[5] = {0};
    int bcards[5] = {0};
    int cards[52];
    char go_on;
    char d;
    Serial.print("Welcome to SimpleBlackJack!\n" "Anytime you can press Ctrl+C to exit.\n" "Enjoy! Press Enter to go on......\n");
    do
    {
        go_on = inkey();
    } while (go_on != 'y');
    Serial.print("\n");
    shuff(cards);
    pcards[0] = cards[0];
    pcards[1] = cards[1];
    bcards[0] = cards[2];
    bcards[1] = cards[3];
    Serial.print("One of computer's cards:\n");
    pic(bcards[0]);
    Serial.print("\n");
    Serial.print("Cards of player:\n");
    pic(pcards[0]);
    pic(pcards[1]);
    i = 0;
    for (i = 0; i < 2; i++)
    {
        if (pcards[i] % 100 == 1)
        {
            Serial.print("choose A value of the card ");
            Serial.print(i + 1);
            Serial.print(", input 'y' for 11 or 'n' for 1 :\n");
            do
            {
                d = inkey();
            } while (d != 'y' && d != 'n');
            if (d == 'y')
            {
                Serial.print("You've chosen value 11 for card A.\n");
                psum = psum + 11;
            }
            else if (d == 'n')
            {
                Serial.print("You've chosen value 1 for card A.\n");
                psum = psum + 1;
            }
        }
        else if (convert_jkq(pcards[i]) % 100 == 10) psum = psum + 10;
        else psum = psum + pcards[i] % 100;
        if (psum > 21)
        {
            Serial.print("Sum of player's cards now: ");
            Serial.print(psum);
            Serial.print("Computer win!\n");
            return 1;
        }
        else if (psum == 21)
        {
            Serial.print("Sum of player's cards now: ");
            Serial.print(psum);
            Serial.print("Player win!\n");
            return 0;
        }
    }
    Serial.print("Sum of player's cards now: ");
    Serial.println(psum);
    i = 0;
    for (i = 0; i < 3; i++)
    {
        char j = 'n';
        Serial.print("Want more cards? Input y or n:\n");
        do
        {
            j = inkey();
        } while (j != 'y' && j != 'n');
        if (j == 'y')
        {
            Serial.print("You've got another card now.\n");
            pcards[i + 2] = cards[i + 4];
            Serial.print("and your card is: ");
            Serial.println(i + 3);
            pic(pcards[i + 2]);
            if (pcards[i + 2] % 100 == 1)
            {
                Serial.print("Choose A value of the card ");
                Serial.print(i + 3);
                Serial.print(", input 'y' for 11 or 'n' for 1:\n");
                do
                {
                    d = inkey();
                } while (d != 'y' && d != 'n'); 
                if (d == 'y')
                {
                    Serial.print("You've chosen value 11 for card A.\n");
                    psum = psum + 11;
                }
                else if (d == 'n')
                {
                    Serial.print("You've chosen value 1 for card A.\n");
                    psum = psum + 1;
                }
            }
            else if (convert_jkq(pcards[i + 2]) % 100 == 10) psum = psum + 10;
            else psum = psum + pcards[i + 2] % 100;
            if (psum > 21)
            {
                Serial.print("Sum of player's cards now: ");
                Serial.println(psum);
                Serial.print("Computer win!\n");
                return 1;
            }
            else if (psum == 21)
            {
                Serial.print("Sum of player's cards now: ");
                Serial.println(psum);
                Serial.print("Player win!\n");
                return 0;
            }
            else 
            {
                Serial.print("Sum of player's cards now: ");
                Serial.println(psum);
            }
        }
        else
        {
            Serial.print("Sum of player's cards now: ");
            Serial.println(psum);
            break;
        }
    }
    if (i == 3)
    {
        Serial.print("Player win! Because the sum of your 5 cards is no larger than 21! So lucky!\n");
        return 0;
    }
    Serial.print("Computer's cards:\n");
    pic(bcards[0]);
    pic(bcards[1]);
    if (bcards[0] % 100 + bcards[1] % 100 == 2)
    {
        bsum = 12;
        Serial.print("Sum of computer's cards now: ");
        Serial.println(bsum);
    }
    else if ((convert_jkq(bcards[0])) % 100 + (convert_jkq(bcards[1])) % 100 ==1)
    {
        bsum = 21;
        Serial.print("Sum of computer's cards now: ");
        Serial.print(bsum);
        Serial.print("Computer win!\n");
        return 1;
    }
    else if (bcards[0] % 100 == 1 || bcards[1] % 100 == 1)
    {
        bsum=(bcards[0] + bcards[1]) % 100 + (rand() % 2) * 10;
        Serial.print("Sum of computer's cards now: ");
        Serial.println(bsum);
    }
    else
    {
        bsum = (convert_jkq(bcards[0])) % 100 + (convert_jkq(bcards[1])) % 100;
        Serial.print("Sum of computer's cards now: ");
        Serial.println(bsum);
    }
    for (i = 0; i < 3 && bsum < 17; i++)
    {
        bcards[i + 2] = cards[i + 7];
        Serial.print("Computer's card is: ");
        Serial.println(i + 3);
        pic(bcards[i + 2]);
        if (bcards[i + 2] % 100 == 1)
        {
            if (bsum + 11 <= 21)
            {
                Serial.print("Computer has chosen A as 11\n");
                bsum = bsum + 11;
                Serial.print("Sum of computer's cards now: ");
                Serial.println(bsum);
            }
            else
            {
                Serial.print("Computer has chosen A as 1\n");
                bsum = bsum + 1;
                Serial.print("Sum of computer's cards now: ");
                Serial.println(bsum);
            }
        }
        else
        {
            bsum = bsum + convert_jkq(bcards[i + 2]) % 100;
            Serial.print("Sum of computer's cards now: ");
            Serial.println(bsum);
        }
    }
    if (i == 3)
    {
        Serial.print("Computer win! Because the sum of its 5 cards is no larger than 21! So lucky!\n");
        return 1;
    }
    if (bsum > 21 || psum > bsum)
    {
        Serial.print("Player win!\n");
        return 0;
    }
    else if (psum == bsum)
    {
        Serial.print("Oh, player and computer get the same score!\n");
        return 3;
    }
    else if (psum < bsum)
    {
        Serial.print("Computer win!\n");
        return 1;
    }
    return 3;
}
void main0(void)
{
    char again;
    play();
    Serial.print("\nAnd now would you like to play again? Input 'y' or 'n':\n");
    do
    {
        again = inkey();
    } while (again != 'y' && again != 'n');
    if (again == 'y')
    {
        Serial.print("\nOK, let's go again!\n\n");
        main0();
    }
}
void setup()
{
    Serial.begin(115200);
    while (!Serial) delay(250);
    Serial.print("ok");
    main0();
}
void loop()
{
    delay(5000);
}




以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0