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

📄 blackjack.~cpp

📁 A suite of casino games
💻 ~CPP
字号:
//--------------------------------------------------//blackjack.cpp#include "blackjack.h"#include <cstdlib>#include <iostream>using std::cout;using std::endl;#define BLACKJACK 21//constructorCBlackjack::CBlackjack():numberOfCards(52), computerValueCount(0), humanValueCount(0), isQuit(false), isWin(false), isDraw(false), isBlackjack(false),isHit(true), isStay(false), isPlayAgain(false){    myCards[0] = "2D ";    myCards[1] = "2H ";    myCards[2] = "2C ";    myCards[3] = "2S ";       myCards[4] = "3D ";    myCards[5] = "3H ";    myCards[6] = "3C ";    myCards[7] = "3S ";    myCards[8] = "4D ";    myCards[9] = "4H ";    myCards[10] = "4C ";    myCards[11] = "4S ";      myCards[12] = "5D ";    myCards[13] = "5H ";    myCards[14] = "5C ";    myCards[15] = "5S ";       myCards[16] = "6D ";    myCards[17] = "6H ";    myCards[18] = "6C ";    myCards[19] = "6S ";    myCards[20] = "7D ";    myCards[21] = "7H ";    myCards[22] = "7C ";    myCards[23] = "7S ";       myCards[24] = "8D ";    myCards[25] = "8H ";    myCards[26] = "8C ";    myCards[27] = "8S ";       myCards[28] = "9D ";    myCards[29] = "9H ";    myCards[30] = "9C ";    myCards[31] = "9S ";    myCards[32] = "10D ";    myCards[33] = "10H ";    myCards[34] = "10C ";    myCards[35] = "10S ";    myCards[36] = "JD ";    myCards[37] = "JH ";    myCards[38] = "JC ";    myCards[39] = "JS ";       myCards[40] = "QD ";    myCards[41] = "QH ";     myCards[42] = "QC ";    myCards[43] = "QS ";    myCards[44] = "KD ";     myCards[45] = "KH ";    myCards[46] = "KC ";    myCards[47] = "KS ";       myCards[48] = "AD ";    myCards[49] = "AH ";    myCards[50] = "AC ";    myCards[51] = "AS ";    for(int i = 0; i < 7; ++i)    {        playerHand[i] = "   ";        computerHand[i] = "   ";        }}//modifiersvoid CBlackjack::doMain(){    char myChoice = ' ';    int iHuman = 0;    int iComputer = 0;    clearScreen();    displayTitle();    displayGrid();      cout << "Ready to deal? (Y)es, (N)o: ";    cin >> myChoice;    myChoice = toupper(myChoice);    if(myChoice == 'N')    {        return;    }    do    {        if(isPlayAgain == true)        {            clearHands();            isBlackjack = isDraw = isQuit = isWin = isStay = isPlayAgain = false;            isHit = true;             computerValueCount = humanValueCount = i = 0;            myChoice = ' ';        }        myChoice = ' ';        clearScreen();        displayTitle();        displayGrid();        if(i == 0)        {            dealCards(2, 2, i);            i += 2;        }        else        {            ++i;        }        countValues();        clearScreen();        displayTitle();        displayGrid();        if(humanValueCount > BLACKJACK)        {            myChoice = ' ';            isWin = false;            doQuit();            continue;        }        else if(computerValueCount > BLACKJACK)        {              myChoice = ' ';             isWin = true;            doQuit();            continue;        }         if(isQuit == false)        {            cout << "Human Value Count: " << humanValueCount << endl;            cout << "Computer Value Count: " << computerValueCount << endl;            cout << "Would you like to stay or hit? (S)tay, (H)it: ";            cin >> myChoice;            myChoice = toupper(myChoice);            if(myChoice == 'S')            {                isHit = false;                isStay = true;                if(computerValueCount >= 17)                {                    stay();                }                else if(computerValueCount > 0)                {                    dealCards(1, 0, i);                }            }            else if(myChoice == 'H')            {                isHit = true;                isStay = false;                dealCards(0, 1, i);            }        }    }while(isQuit == false);}void CBlackjack::clearHands(){    for(int i = 0; i < 7; ++i)    {        playerHand[i] = "   ";        computerHand[i] = "   ";    }}void CBlackjack::dealCards(int computerNumber, int humanNumber, int counter){    int tempNumber = 0;    if(counter == 0)    {        for(int i = 0; i < 2; ++i)        {            tempNumber = (rand() % 52);            playerHand[i] = myCards[tempNumber];            tempNumber = (rand() % 52);            computerHand[i] = myCards[tempNumber];        }    }    if(counter > 0)    {       if(computerNumber == 1)       {           tempNumber = (rand() % 52);           computerHand[counter] = myCards[tempNumber];       }       if(humanNumber == 1)       {           tempNumber = (rand() % 52);           playerHand[counter] = myCards[tempNumber];       }    }}void CBlackjack::stay(){    cout << "THE BET IS ON STAY!!!" << endl;    system("sleep 5");    if(computerValueCount > humanValueCount)    {        isWin = false;        doQuit();     }    else if(humanValueCount > computerValueCount)    {        isWin = true;        doQuit();    }    else if(humanValueCount == computerValueCount)    {        isWin = false;        isDraw = true;        doQuit();        isDraw = false;    }}//accessorsvoid CBlackjack::clearScreen()const{    system("clear");}void CBlackjack::displayTitle()const{    cout << "\n\n";    cout << "\t\t\t\t  Blackjack by Armond" << endl;    cout << "\t\t\t\t  ===================" << endl;    cout << "\n\n";}void CBlackjack::displayGrid()const{    cout << "\n\n";    cout << "\t\t\t    " << " ---------------------------" << endl;    cout << "\t\t\t    " << "|         -COMPUTER-        |" << endl;    cout << "\t\t\t    " << "|---------------------------|" << endl;    if(isStay == true && isHit == false)    {        cout << "\t\t\t    " << "|" << computerHand[0] <<"|" << computerHand[1] <<"|" << computerHand[2] <<"|" << computerHand[3] <<"|" << computerHand[4] << "|" << computerHand[5] << "|" << computerHand[6] << "|" << endl;    }    else    {        cout << "\t\t\t    " << "|" << "   " <<"|" << computerHand[1] <<"|" << computerHand[2] <<"|" << computerHand[3] <<"|" << computerHand[4] << "|" << computerHand[5] << "|" << computerHand[6] << "|" << endl;    }    cout << "\t\t\t    " << "|                           |" << endl;    cout << "\t\t\t    " << "|                           |" << endl;    cout << "\t\t\t    " << "|" << playerHand[0] <<"|" << playerHand[1] <<"|" << playerHand[2] <<"|" << playerHand[3] <<"|" << playerHand[4] << "|" << playerHand[5] << "|" << playerHand[6] << "|" << endl;    cout << "\t\t\t    " << "|---------------------------|" << endl;    cout << "\t\t\t    " << "|          -HUMAN-          |" << endl;    cout << "\t\t\t    " << " ---------------------------" << endl;    cout << "\n\n";}void CBlackjack::countValues(){    humanValueCount = computerValueCount = 0;    int i = 0;        while(playerHand[i] != "   ")    {        if(playerHand[i][0] == '2')        {            humanValueCount += 2;        }        else if(playerHand[i][0] == '3')        {            humanValueCount += 3;         }        else if(playerHand[i][0] == '4')        {            humanValueCount += 4;        }        else if(playerHand[i][0] == '5')        {            humanValueCount += 5;        }        else if(playerHand[i][0] == '6')        {            humanValueCount += 6;        }        else if(playerHand[i][0] == '7')        {            humanValueCount += 7;        }        else if(playerHand[i][0] == '8')        {            humanValueCount += 8;        }        else if(playerHand[i][0] == '9')        {            humanValueCount += 9;        }        else if(playerHand[i][0] == '1')        {            humanValueCount += 10;        }        else if(playerHand[i][0] == 'J'             || playerHand[i][0] == 'Q'             || playerHand[i][0] == 'K')        {            humanValueCount += 10;        }        ++i;    }    i = 0;    while(computerHand[i] != "   ")    {        if(computerHand[i][0] == '2')        {            computerValueCount += 2;        }        else if(computerHand[i][0] == '3')        {            computerValueCount += 3;        }        else if(computerHand[i][0] == '4')        {            computerValueCount += 4;        }        else if(computerHand[i][0] == '5')        {            computerValueCount += 5;        }        else if(computerHand[i][0] == '6')        {            computerValueCount += 6;        }        else if(computerHand[i][0] == '7')        {            computerValueCount += 7;        }        else if(computerHand[i][0] == '8')        {            computerValueCount += 8;        }        else if(computerHand[i][0] == '9')        {            computerValueCount += 9;        }        else if(computerHand[i][0] == '1')        {            computerValueCount += 10;        }        else if(computerHand[i][0] == 'J'             || computerHand[i][0] == 'Q'             || computerHand[i][0] == 'K')        {            computerValueCount += 10;        }        ++i;    }    i = 0;}void CBlackjack::doQuit()const{    char myChoice = ' ';       if(isWin == true)    {        cout << "You won!!!" << endl;    }    else if(isWin == false)    {        cout <<"You lost!!!" << endl;    }    else if(isDraw == true)    {        cout << "Draw game!!!" << endl;    }    cout << "Would you like to place another bet? ";    cin >> myChoice;    myChoice = toupper(myChoice);    if(myChoice == 'N')    {        isQuit = true;    }    else    {        isQuit = false;        isPlayAgain = true;    }}//--------------------------------------------------

⌨️ 快捷键说明

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