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

📄 blackjack.cpp

📁 21点牌的C++课程设计的源代码
💻 CPP
字号:


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////                       RULES:                   //////////////////////////////////////////////////////////////
/////////////////                 
/////////////////                 Deal players and the dealer two initial cards          //////////////////////////////////////
/////////////////                  Hide the dealer's first card                           /////////////////////////////////////
/////////////////                 Deal additional cards to players                       //////////////////////////////////////
/////////////////                  Reveal dealer's first card                             /////////////////////////////////////
/////////////////                 Deal additional cards to dealer                        //////////////////////////////////////
/////////////////                 If dealer is busted                                    //////////////////////////////////////
/////////////////                   Everyone who is not busted wins                        ////////////////////////////////////
/////////////////                 Otherwise                                              //////////////////////////////////////   
/////////////////                   For each player                                        ////////////////////////////////////
/////////////////                    If player isn't busted                                 ///////////////////////////////////
/////////////////                      If player's total is greater than the dealer's total   /////////////////////////////////
/////////////////                       Player wins                                            ////////////////////////////////
/////////////////                      Otherwise if player's total is less than dealer's total ////////////////////////////////
/////////////////                       Player loses                                            ///////////////////////////////  
/////////////////                      Otherwise                                               ////////////////////////////////
/////////////////                       Player pushes                                            //////////////////////////////
/////////////////                 Remove everyone's cards                                 /////////////////////////////////////

//#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <iomanip>
#include "carddefs.h"
using namespace std;

//Define the class of card
class card {
public:
  char abbr[4];
  int name;         //card's name
  int suit;         //card's suit
} the_card;

void deal_player(); //deal the cards to player
void deal_dealer(); //deal the cards to dealer
void deal();        //deal the cards
void doDealer();    //dealer's operation
void erase_hands();  //reset the cards in two sides
int  random(int a,int b,int c=0); 
void rules();      //tackle the blackjack's rule issue
void shuffle();    //shuffle the deck of cards for player and dealer
void printout();   //print out the result
void sleep(int a);  //set up the waiting time
void win_player();  //output the final result if player wins
void win_dealer();  //output the final result if dealer wins


bool cards[13][4];      //define the bool variable cards[name][suit]
int player_total;       //the total value of player's cards
int dealer_total;       //the total value of dealer's cards
char player_hand[32];   //player's hand
char dealer_hand[32];   //dealer's hand
int player_pos;         
int dealer_pos;
bool rules_firstcall;   //if it's the first 
bool player_loses;      //decide whether player loses this round
bool dealer_loses;
bool player_blackjack; //decide whether player gets a blackjack
bool player_stands; //decide whether player stands for the next round
bool game_push;   //decide whether the player and dealer pushes
char c, runAgain;

char PlayAgain()           //decide whether play again
{
	char again='n';
	do{
		cout<<"\nDo you want to play again (Y/y or N/n)  ?  ";
		cin>>again;
		if ((again!='n')&&(again!='N')&&(again!='y')&&(again!='Y'))
		{
			cout<<again<<" is not acceptable, re-enter please"<<endl;
		}
	}while((again!='n')&&(again!='N')&&(again!='y')&&(again!='Y'));
    return(again);
}
void main() {
  cout<<"Welcome to Blackjack, good luck!\n\n";
  cout<<"Press Hit(H/h) or Stay(S/s) to play the game, enjoy!!\n\n";
  srand((unsigned)time(NULL));    //generate randomized seed
  bool playing = true;

    do{                                  // begin do-while loop
    player_total = dealer_total = 0;
    player_pos = dealer_pos = 0;
    player_stands = false;
    player_loses = false;
	dealer_loses = false;
    player_blackjack = false;
    game_push = false;


    shuffle();
    erase_hands();

    // deal player two cards
	
	deal_player();
    deal_player();

    // deal dealer one card
    deal_dealer();
    
	//output the dealer's first card facing down as '??' 
    dealer_hand[dealer_pos] = '?';    
    dealer_hand[dealer_pos+1] = '?';  

    printout();

    rules_firstcall = true;
    rules();

    while(!player_stands && !player_loses) {      //begin while loop
		
      c = 0x00000000;
      while(c!='h'&& c!='H'&& c!='s'&&c!='S'&&c!='x'&&c!='X') c = getchar();
      switch(c) {
        case 'x': cout<<"-->EXIT\n";    //Exit the game
                  playing = false; player_stands = true; break;
		case 'X': cout<<"-->EXIT\n";    //Exit the game
                  playing = false; player_stands = true; break;
        case 'h': cout<<"-->HIT!\n"; sleep(1);  //Choose to hit
                  deal_player(); printout(); rules();
                  if(player_loses) sleep(1); break;
        case 'H': cout<<"-->HIT!\n"; sleep(1);  //Choose to hit
                  deal_player(); printout(); rules();
                  if(player_loses) sleep(1); break;
        case 's': cout<<"-->STAND.\n"; sleep(1);  //Choose to stand
                  player_stands = true; break;
        case 'S': cout<<"-->STAND.\n"; sleep(1);  //Choose to stand
			      player_stands = true; break;
      }
	}                                             //end while loop
	doDealer();
	runAgain= PlayAgain();
	if((runAgain=='y')||(runAgain=='Y')) //to play again
		{
			cout<<endl;
			
			cout<<"================================================================================"<<endl;
		}
		else if ((runAgain=='n')||(runAgain=='N')) //to quit
		{
			cout<<endl;
			break;
		}
  }while((runAgain!='n') && (runAgain!='N'));  //end do-while loop

}

void win_player() {
  cout<<"\n***You Win!!***\n";
  sleep(1);
}

void win_dealer() {
  cout<<"\n***I Win!!***\n";
  sleep(1);
}


void doDealer() {
 
  if(player_blackjack) {
    win_player();
    return;
  }

  if(player_loses) {
    win_dealer();
    return;
  }

  deal_dealer();
  printout();
  sleep(2);    //wait for two seconds

  while(dealer_total < player_total && dealer_total<17) {
		  deal_dealer();
		  printout();
		  
          sleep(2);   //wait for two seconds
  }
  
  if(dealer_total==player_total) {         //Push
    cout<<"**It was a Push**\n";
    game_push = true;
    return;
  }

  if(dealer_total>21) {                    //Bust
    cout<<"*I went Bust!*\n";
    win_player();
    return;
  }
  if( dealer_total > player_total)        
    win_dealer();
  else 
    win_player();
  return;

}

void erase_hands() {

  for(int i=0;i<32;i++) player_hand[i] = dealer_hand[i] = NULL;

}

void rules() {
  if(rules_firstcall) {
    if(player_total==21) {
      cout<<"BLACKJACK!!  You win!! \n";

      player_blackjack = true;
      player_stands = true;
      sleep(2);
    }
  } 
  else {
    if(player_total==21) {
      cout<<"-->21!!--Player Stands.\n";
      player_stands = true;
      sleep(2);
    } 
	else if(player_total>21) {
      cout<<"*You Bust*\n";
      player_loses = true;
    }
  }
  rules_firstcall = false;
  return;
}

void shuffle() {
  for(int i=0;i<12;i++)
    for(int j=0;j<3;j++)
      cards[i][j] = false;

}

void deal_player() {
  deal();

  int i = 0;
  while(the_card.abbr[i]!=NULL) {
    player_hand[player_pos] = the_card.abbr[i];
    i++; 
	player_pos++;
  }
  player_hand[player_pos] = ' ';
  player_pos++;

  switch(the_card.name) {
    case TWO: case THREE: case FOUR: case FIVE: case SIX:      //the card(2-10) value is the same to their name
    case SEVEN: case EIGHT: case NINE: case TEN:
            player_total += the_card.name + 1; break;
    case JACK: case QUEEN: case KING: player_total+= 10; break;//the card(J,Q,K) value is 10
    case ACE:  player_total += 11;break;                       //the card(A) value is 11
  }


}

void deal_dealer() {
  deal();
  int i = 0;
  while(the_card.abbr[i]!=NULL) {
    dealer_hand[dealer_pos] = the_card.abbr[i];
    i++; 
	dealer_pos++;
  }
  dealer_hand[dealer_pos] = ' ';
  dealer_pos++;

  switch(the_card.name) {
    case TWO: case THREE: case FOUR: case FIVE: case SIX:
    case SEVEN: case EIGHT: case NINE: case TEN:
            dealer_total += the_card.name + 1; break;
    case JACK: case QUEEN: case KING: dealer_total+= 10; break;
    case ACE: dealer_total+=11; break;
  }
}

void deal() {

   int suit, name;
   do{
   name = random(1,13,-1);   //get a random number from 0-12 inclusive for card name 
   suit = random(1,4,-1);    //get a random number from 0-3 inclusive for card suit
   } while(cards[name][suit]);

   the_card.name = name;
   the_card.suit = suit;
   cards[name][suit] = true;

   // setup card abbr
   for(int i=0;i<4;i++) the_card.abbr[i] = NULL;

   switch(name) {
      case ACE: the_card.abbr[0] = 'A'; break;
      case TWO: case THREE: case FOUR: case FIVE:
      case SIX: case SEVEN: case EIGHT: case NINE:
                the_card.abbr[0] = '0' + name + 1; break;
      case TEN: the_card.abbr[0] = '1';
                the_card.abbr[1] = '0'; break;
      case JACK:the_card.abbr[0] = 'J'; break;
      case QUEEN: the_card.abbr[0] = 'Q'; break;
      case KING: the_card.abbr[0] = 'K'; break;
   }
   if(name==TEN) the_card.abbr[2] = suit + 3;
   else the_card.abbr[1] = suit + 3;

}

void printout() {
    
	cout<<setw(100)<<"\nYour Hand: "<<setw(49)<<"Dealer's Hand: \n"<<"----------"<<setw(51)<<"--------------- \n"<<&player_hand[0]<<setw(45)
		<<&dealer_hand[0]<<"\n\n"<<"TOTAL : "<<player_total<<setw(42)<<"TOTAL : "<<dealer_total<<"\n\n";
    
}

void sleep(int a=1) {
  if(a<1) return;
  int i;
  for(int x=0;x<a;x++) {
    i = (unsigned)time(NULL);
    while(i==time(NULL));
  }
}

int random(int a, int b, int c) {
  int value = 0;

  for(int i=0;i<a;i++) value += rand() % b + 1;
  return value+c;
}

⌨️ 快捷键说明

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