Black Jack 21点游戏 C++
Black Jack is a poker game
· Process:
1. Define the card or shuffle the card
2. Player get 2 cards
3. Player get points
4. Player determines whether to continue to ask for cards
5. If player continue ask for cards, player get 1 card, the player points accumulated
6. If player don’t ask for cards, count the player points
7. Dealer get 2 cards
8. Dealer determines whether to continue to ask for cards
9. If dealer continue ask for cards, dealer get 1 card, the dealer points accumulated
10. If dealer don’t ask for cards, count the dealer points
11. Compare the player points with the dealer points
12. The round is end
· Module: class
Poker Card
Player
Dealer
Compare the Points
代码块:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
#define MAXVALUE 200
//Build the playcard class
class PlayCard
{
public:
//Define the poker and poker points, or reshuffle.
void initSet();
//Output the whole playcard and points
void showCard();
string randomPlay();
//Get the playcard points of the current role
void getValue(string &getPoints, int &userPoints);
protected:
string colors[4];
string numbers[13];
string arr[52];
string currentCard;
int cardPoints[52];
int currentPoints;
int cardCount;
};
//Build the player class
class Player: public PlayCard
{
public:
//return the points of the player
int pointsCount(int sign);
void getResult();
int playerPoints;
int sumCount;
};
//Build the dealer class
class Dealer: public PlayCard
{
public:
//return the points of the dealer
int pointsCount(int sign);
void getResult();
int dealerPoints;
int sumCount;
};
//Build the compare class, used for compare the player points with the dealer points
class Compare: public Player, public Dealer{
public:
void compare(int &playerpoints, int &dealerpoints);
};
int main()
{
PlayCard playCard;
playCard.initSet();
Player player;
Dealer dealer;
Compare result;
for(int i = 0; i < MAXVALUE; i++){
player.getResult();
dealer.getResult();
result.compare(player.sumCount, dealer.sumCount);
int next;
cout<<"\nNext Round? 1.Yes 2.No : ";
cin>>next;
if(next==1) {
system("cls");
continue;
}
else break;
}
system("pause");
return 0;
}
void PlayCard::initSet(){
string col[4] = {
"\003", "\004", "\005", "\006"};
string num[13] = {
"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
cardCount = 52;
//Assign decors and points to the array
for(int i = 0; i < 4; i++){
colors[i]=col[i];
}
for(int i = 0; i < 13; i++){
numbers[i]=num[i];
}
//Mix decors and points into an array
int index = 0;
string tempCombin;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 13; j++){
if(j < 8){
tempCombin = colors[i].append(numbers[j]);
arr[index++] = tempCombin;
colors[i].erase(colors[i].end()-1);
} else if(j == 8){
tempCombin = colors[i].append(numbers[j]);
arr[index++] = tempCombin;
colors[i].erase(colors[i].end()-2);
colors[i].erase(colors[i].end()-1);
} else {
tempCombin = colors[i].append(numbers[j]);
arr[index++] = tempCombin;
colors[i].erase(colors[i].end()-1);
}
}
}
//Convert playcards to integer points
for(int i = 0, number = 2; i < cardCount; i++){
if(i == 0 || i == 13 || i == 26 || i == 39){
number = 2;
}
if(i == 12 || i == 25 || i == 38 || i == 51){
cardPoints[i] = 11;
continue;
}
if(number > 10){
number = 10;
}
cardPoints[i] = number++;
}
}
void PlayCard::showCard(){
for(int i = 0; i < 52; i++){
if(i%13 == 0){
cout<<endl;
}
cout<<arr[i]<<" ";
}
cout<<endl;
for(int i = 0; i < 52; i++){
if(i%13 == 0){
cout<<endl;
}
cout<<cardPoints[i]<<" ";
}
cout<<endl;
}
string PlayCard::randomPlay(){
//Give a random seed as an array subscript for the whole deck of cards
unsigned seed;
seed = time(0);
srand(seed);
int index = rand()%51;
//Obtain the number of points of the current card and set it empty.
//If the current card is empty, re issue the random seed.
//If the number of empty cards is less than the half number of the whole
//deck (i.e. 26), shuffle the cards again.
for(int i = 0; i < MAXVALUE; i++){
if(cardCount >= 26 && arr[index] != " "){
currentCard = arr[index];
currentPoints = cardPoints[index];
arr[index] = " ";
cardCount--;
break;
} else if(cardCount >= 26 && arr[index] == " "){
index = rand()%51;
} else {
initSet();
}
}
return currentCard;
}
void PlayCard::getValue(string &getPoints, int &userPoints){
getPoints = randomPlay();
cout<<getPoints<<" ";
if(currentPoints == 11 && (userPoints+currentPoints)>21){
currentPoints = 1;
userPoints += currentPoints;
} else if(currentPoints == 11 && (userPoints+currentPoints)<=21){
userPoints += currentPoints;
} else {
userPoints += currentPoints;
}
}
int Player::pointsCount(int sign){
string getPoints;
playerPoints = 0;
cout<<"Player Card: ";
//sign=0, first random play
//sign=1, next random play
if(sign == 0){
for(int i = 0; i < 2; i++){
getValue(getPoints, playerPoints);
}
} else if(sign == 1){
getValue(getPoints, playerPoints);
}
cout<<endl;
return playerPoints;
}
void Player::getResult(){
int choice;
sumCount = 0;
sumCount += pointsCount(0);
cout<<"Player Points: "<<sumCount<<endl;
for(int i = 0; i < MAXVALUE; i++){
if(sumCount < 21){
cout<<"Do you want to continue? 1.Yes 2.No : ";
cin>>choice;
if(choice == 1){
sumCount += pointsCount(1);
cout<<"Player Points: "<<sumCount<<endl;
} else if(choice == 2){
cout<<"Player Points: "<<sumCount<<endl;
break;
} else {
cout<<"Error! Retry!\n";
continue;
}
} else if(sumCount == 21){
break;
} else {
break;
}
}
cout<<endl;
}
int Dealer::pointsCount(int sign){
string getPoints;
dealerPoints = 0;
cout<<"Dealer Card: ";
//sign=0, first random play
//sign=1, next random play
if(sign == 0){
for(int i = 0; i < 2; i++){
getValue(getPoints, dealerPoints);
}
} else if(sign == 1){
getValue(getPoints, dealerPoints);
}
cout<<endl;
return dealerPoints;
}
void Dealer::getResult(){
int choice;
sumCount = 0;
sumCount += pointsCount(0);
cout<<"Dealer Points: "<<sumCount<<endl;
for(int i = 0; i < MAXVALUE; i++){
if(sumCount < 21){
cout<<"Do you want to continue? 1.Yes 2.No : ";
cin>>choice;
if(choice == 1){
sumCount += pointsCount(1);
cout<<"Dealer Points: "<<sumCount<<endl;
} else if(choice == 2){
cout<<"Dealer Points: "<<sumCount<<endl;
break;
} else {
cout<<"Error! Retry!\n";
continue;
}
} else if(sumCount == 21){
break;
} else {
break;
}
}
cout<<endl;
}
void Compare::compare(int &playerpoints, int &dealerpoints){
if((playerpoints<=21)&&(dealerpoints<=21)&&(playerpoints>dealerpoints)){
cout<<"Player Win!!!"<<endl;
}
if((playerpoints<=21)&&(dealerpoints<=21)&&(dealerpoints>playerpoints)){
cout<<"Dealer Win!!!"<<endl;
}
if((playerpoints<=21)&&(dealerpoints<=21)&&(playerpoints==dealerpoints)){
cout<<"Draw!"<<endl;
}else if(playerpoints>21){
cout<<"Dealer Win!!!"<<endl;
}else if(dealerpoints>21){
cout<<"Player Win!!!"<<endl;
}
}
还没有评论,来说两句吧...