c++21点游戏

欢迎光临程序代写小店https://item.taobao.com/item.htm?spm=a230r.1.14.59.255028c3ALNkZ0&id=586797758241&ns=1&abbucket=15#detail

欢迎点击链接加入群聊【程序代写-接单群】共同致富:https://jq.qq.com/?_wv=1027&k=5WxihsL 

群号:733065427

可直接联系客服QQ交代需求:953586085

/*
21点游戏
*/
#include <iostream>
#include <cstdlib>
#include <algorithm>

using namespace std;

const int NUM_CARDS = 52;
const char suit[4] = { 'H','S','D','C' };
const char* type[13] ={ "2","3","4","5","6","7","8","9","10","J","Q","K","A" };
const int value[13] = { 2,3,4,5,6,7,8,9,10,10,10,10,11 };

void shuffleCards(int cards[]);//洗牌
void showCard(int id);//显示牌
int cardValue(int id);//牌的值
void showHand(int hand[], int numCards);//显示手上的牌
int scoring(int hand[], int numCards);//计算分数
//////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
	if (argc < 2) {
		cout << "Something wrong - Please input the seed value." << endl;
		return 1;//保证在运行exe的时候后面输入随机数种子的值
	}
	int seed = atoi(argv[1]);
	srand(seed);

	int cards[52];//存储着所有的牌
	int dealerHand[9];//dealer手中的牌
	int playerHand[9];//player手中的牌

	char playFlag = 'y';//如果输入y继续,否则退出
	while (playFlag == 'y')
	{
		shuffleCards(cards);//洗牌

		int dealerNumCards = 0;//在手中的牌数
		int playerNumCards = 0;
		int deckNumCards = 0;//在deck中牌的序号

		playerHand[playerNumCards++] = cards[deckNumCards++];//连发两张牌
		dealerHand[dealerNumCards++] = cards[deckNumCards++];
		playerHand[playerNumCards++] = cards[deckNumCards++];
		dealerHand[dealerNumCards++] = cards[deckNumCards++];

		cout << "Dealer:" << "? ";//显示dealer的牌,第一张打?
		showCard(dealerHand[1]);
		cout << endl;

		cout << "Player:";//显示player的牌
		showHand(playerHand, 2);
		cout << endl;

		while (true)
		{
			if (scoring(playerHand, playerNumCards)>21)
			{
				break; //如果大于21,结束
			}
			else if (scoring(playerHand, playerNumCards) == 21)
			{
				while (scoring(dealerHand, dealerNumCards)<17)
				{
					dealerHand[dealerNumCards++] = cards[deckNumCards++];//继续发牌
				}
				break;
			}
			else {
				cout << "Type 'h' to hit and 's' to stay:" << endl;
				char input;
				cin >> input;
				if (input == 'h')
				{
					playerHand[playerNumCards++] = cards[deckNumCards++];//游戏继续
					cout << "Player:";
					showHand(playerHand, playerNumCards);//显示player得到的这张牌
					cout << endl;
				}
				else if (input == 's')
				{
					while (scoring(dealerHand, dealerNumCards)<17)
					{
						dealerHand[dealerNumCards++] = cards[deckNumCards++]; //持续给dealer发牌,直到结束条件出现
					}
					break;
				}
			}
		}
		cout << "Dealer: ";
		showHand(dealerHand, dealerNumCards);
		cout << endl;
		/*
		判断谁赢
		*/
		if (scoring(playerHand, playerNumCards)>21) // player busts
		{
			cout << "Player busts \nLose\n";
			cout << scoring(playerHand, playerNumCards) << " ";
			cout << scoring(dealerHand, dealerNumCards) << endl;

		}

		else if (scoring(dealerHand, dealerNumCards)>21)// dealer busts 
		{
			cout << "Dealer busts \nWin\n";
			cout << scoring(playerHand, playerNumCards) << " ";
			cout << scoring(dealerHand, dealerNumCards) << endl;
		}

		else if (scoring(playerHand, playerNumCards)> scoring(dealerHand, dealerNumCards))// player win
		{
			cout << "Win ";
			cout << scoring(playerHand, playerNumCards) << " ";
			cout << scoring(dealerHand, dealerNumCards) << endl;
		}
		else if (scoring(playerHand, playerNumCards)< scoring(dealerHand, dealerNumCards)) //dealer win
		{
			cout << "Lose ";
			cout << scoring(playerHand, playerNumCards) << " ";
			cout << scoring(dealerHand, dealerNumCards) << endl;
		}
		else //tie
		{
			cout << "Tie ";
			cout << scoring(playerHand, playerNumCards) << " ";
			cout << scoring(dealerHand, dealerNumCards) << endl;
		}
		//是否继续游戏
		cout << "\nPlay again? [y / n]\n";
		cin >> playFlag;
	}
	return 0;
}
//////////////////////////////////////////////////////////////////////////
//洗牌
void shuffleCards(int cards[])
{
	for (int j = 0; j<52; j++)  //将牌初始化
	{
		cards[j] = j;
	}
	for (int i = 51; i >= 1; i--) 
	{
		int j = rand() % (i + 1);//通过交换来洗牌
		int temp;
		temp = cards[i];
		cards[i] = cards[j];
		cards[j] = temp;
	} 
}
//将牌显示出来,显示形式如:K-C  (King of clubs), 2-H (2 of hearts)
//前面是具体的数字: 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A
//后面是类型: H, D, C, S
void showCard(int id)
{
	cout << type[id % 13] << "-" << suit[id / 13] << " ";
}
//返回牌的值,ACE的值姑且为11,后面会根据总的分数进行处理
int cardValue(int id)
{
	return value[id % 13];
}
//将手中的牌显示出来
void showHand(int hand[], int numCards)
{
	for (int i = 0; i < numCards; i++) 
	{
		showCard(hand[i]);
	}
}

//计算总的分数,如果超过21,依次查找ACE,并在总的分数上减10
int scoring(int hand[], int numCards)
{
	int scoresSum = 0;
	for (int i = 0; i < numCards; i++) 
	{
		scoresSum = scoresSum + cardValue(hand[i]);
	} 

	for (int i = 0; i< numCards; i++) 
	{
		if (cardValue(hand[i]) == 11 && scoresSum > 21) 
		{
			scoresSum = scoresSum - 10;//不会改变ACE的值,而是改变总的分数
		}
	}
	return scoresSum;
}


猜你喜欢

转载自blog.csdn.net/qq_24624539/article/details/87883809