一个C++的21点(BlackJack)游戏

#ifndef POKER_H_
#define POKER_H_

//suits: Games Any of the four sets of 13 playing cards
//(clubs, diamonds, hearts, and spades) in a standard deck,
//the members of which bear the same marks.
//红桃 : H - Heart 桃心(象形), 代表爱情
//黑桃 : S - Spade 橄榄叶(象形), 代表和平
//方块 : D - Diamond 钻石(形同意合), 代表财富
//梅花 : C - Club 三叶草(象形), 代表幸运

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <cctype>

using namespace std;

struct Card
{
	string mark;
	string point;
};

class Poker
{
public:
	const string Mark[4] = { "红桃", "黑桃", "方块", "梅花" };
	const string Point[13] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };

private:
	Card card[52];
	static int num;

public:
	Poker();

	void randomPoker();
	Card& dealPoker();
};

#endif

#include "poker.h"

int Poker::num = 0;

Poker::Poker()
{
	for (int i = 0; i < 52; i++)
	{
		card[i].mark = Mark[i % 4];
		card[i].point = Point[i % 13];
	}
}

void Poker::randomPoker()
{
	srand((unsigned int)time(0));

	for (int i = 0; i < 52; i++)
	{
		Card temp;
		temp = card[i];

		int r = rand() % 52;

		card[i] = card[r];
		card[r] = temp;
	}
}

Card& Poker::dealPoker()
{
	if (num < 52)
		return card[num++];
	else
		cout << "No more cards!\n";
	exit(EXIT_FAILURE);
}

#include "poker.h"

void showBlackjack(const Card[], int);
bool bustBlackjack(const Card[], int);
void winBlackjack(const Card[], const Card[], int);

int main()
{
	Poker blackjack;
	blackjack.randomPoker();

	static int count;

	Card dealer[26], player[26];

	count = 0;
	cout << "【开始发牌】" << endl;

	while (count<2)
	{
		dealer[count] = blackjack.dealPoker();
		player[count++] = blackjack.dealPoker();
	}

	cout << "【庄家】";
	showBlackjack(dealer, 1);
	cout << endl;

	cout << "【玩家】";
	showBlackjack(player, count);
	cout << endl;

	cout << "是否继续发牌?(Y发牌/N结束): ";
	char ch;

	while (cin >> ch)
	{
		if (!isalpha(ch))
		{
			cout << "请输入字母!\n是否继续发牌?(Y发牌/N结束): ";
			continue;
		}

		switch (ch)
		{
		case 'Y':
			dealer[count] = blackjack.dealPoker();
			player[count++] = blackjack.dealPoker();
			cout << "【庄家】";
			showBlackjack(dealer, 1);
			cout << endl;
			cout << "【玩家】";
			showBlackjack(player, count);
			cout << endl;

			if (bustBlackjack(player, count))
				break;

			cout << "是否继续发牌?(Y发牌/N结束): ";
			continue;

		case 'N':
			winBlackjack(dealer, player, count);
			break;

		default:
			cout << "请输入正确的字母!\n是否继续发牌?(Y发牌/N结束): ";
			continue;
		}

		cout << "再来一局?(C继续/Q退出): ";
		if (cin >> ch && ch == 'C')
		{
			count = 0;
			cout << "【开始发牌】" << endl;

			while (count<2)
			{
				dealer[count] = blackjack.dealPoker();
				player[count++] = blackjack.dealPoker();
			}

			cout << "【庄家】";
			showBlackjack(dealer, 1);
			cout << endl;

			cout << "【玩家】";
			showBlackjack(player, count);
			cout << endl;

			cout << "是否继续发牌?(Y发牌/N结束): ";
			continue;
		}
		else if (ch == 'Q')
			break;
	}

	system("pause");
	return 0;
}

void showBlackjack(const Card c[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << c[i].mark << c[i].point << '\0';
	}
}

bool bustBlackjack(const Card c[], int n)
{
	int count_c1 = 0;
	int count_c2 = 0;

	for (int i = 0; i < n; i++)
	{
		if (c[i].point == "A")
		{
			count_c1 += 1; count_c2 += 11;
		}
		else if (c[i].point == "2")
		{
			count_c1 += 2; count_c2 += 2;
		}
		else if (c[i].point == "3")
		{
			count_c1 += 3; count_c2 += 3;
		}
		else if (c[i].point == "4")
		{
			count_c1 += 4; count_c2 += 4;
		}
		else if (c[i].point == "5")
		{
			count_c1 += 5; count_c2 += 5;
		}
		else if (c[i].point == "6")
		{
			count_c1 += 6; count_c2 += 6;
		}
		else if (c[i].point == "7")
		{
			count_c1 += 7; count_c2 += 7;
		}
		else if (c[i].point == "8")
		{
			count_c1 += 8; count_c2 += 8;
		}
		else if (c[i].point == "9")
		{
			count_c1 += 9; count_c2 += 9;
		}
		else if (c[i].point == "10")
		{
			count_c1 += 10; count_c2 += 10;
		}
		else if (c[i].point == "J")
		{
			count_c1 += 10; count_c2 += 10;
		}
		else if (c[i].point == "Q")
		{
			count_c1 += 10; count_c2 += 10;
		}
		else if (c[i].point == "K")
		{
			count_c1 += 10; count_c2 += 10;
		}
	}

	if (count_c1 > 21)
	{
		cout << "【玩家】 " << count_c1 << "点。\n"
			<< "玩家爆,庄家赢。\n";
		return true;
	}

	return false;
}

void winBlackjack(const Card c[], const Card d[], int n)
{
	int count_c1 = 0;
	int count_c2 = 0;
	int count_d1 = 0;
	int count_d2 = 0;

	for (int i = 0; i < n; i++)
	{
		if (c[i].point == "A")
		{
			count_c1 += 1; count_c2 += 11;
		}
		else if (c[i].point == "2")
		{
			count_c1 += 2; count_c2 += 2;
		}
		else if (c[i].point == "3")
		{
			count_c1 += 3; count_c2 += 3;
		}
		else if (c[i].point == "4")
		{
			count_c1 += 4; count_c2 += 4;
		}
		else if (c[i].point == "5")
		{
			count_c1 += 5; count_c2 += 5;
		}
		else if (c[i].point == "6")
		{
			count_c1 += 6; count_c2 += 6;
		}
		else if (c[i].point == "7")
		{
			count_c1 += 7; count_c2 += 7;
		}
		else if (c[i].point == "8")
		{
			count_c1 += 8; count_c2 += 8;
		}
		else if (c[i].point == "9")
		{
			count_c1 += 9; count_c2 += 9;
		}
		else if (c[i].point == "10")
		{
			count_c1 += 10; count_c2 += 10;
		}
		else if (c[i].point == "J")
		{
			count_c1 += 10; count_c2 += 10;
		}
		else if (c[i].point == "Q")
		{
			count_c1 += 10; count_c2 += 10;
		}
		else if (c[i].point == "K")
		{
			count_c1 += 10; count_c2 += 10;
		}

		if (d[i].point == "A")
		{
			count_d1 += 1; count_d2 += 11;
		}
		else if (d[i].point == "2")
		{
			count_d1 += 2; count_d2 += 2;
		}
		else if (d[i].point == "3")
		{
			count_d1 += 3; count_d2 += 3;
		}
		else if (d[i].point == "4")
		{
			count_d1 += 4; count_d2 += 4;
		}
		else if (d[i].point == "5")
		{
			count_d1 += 5; count_d2 += 5;
		}
		else if (d[i].point == "6")
		{
			count_d1 += 6; count_d2 += 6;
		}
		else if (d[i].point == "7")
		{
			count_d1 += 7; count_d2 += 7;
		}
		else if (d[i].point == "8")
		{
			count_d1 += 8; count_d2 += 8;
		}
		else if (d[i].point == "9")
		{
			count_d1 += 9; count_d2 += 9;
		}
		else if (d[i].point == "10")
		{
			count_d1 += 10; count_d2 += 10;
		}
		else if (d[i].point == "J")
		{
			count_d1 += 10; count_d2 += 10;
		}
		else if (d[i].point == "Q")
		{
			count_d1 += 10; count_d2 += 10;
		}
		else if (d[i].point == "K")
		{
			count_d1 += 10; count_d2 += 10;
		}
	}

	if (count_c1 < count_c2 && count_c2 <= 21)
		count_c1 = count_c2;

	if (count_d1 < count_d2 && count_d2 <= 21)
		count_d1 = count_d2;

	if (count_c1 > 21)
	{
		cout << "【庄家】 " << count_c1 << "点,\n"
			<< "【玩家】 " << count_d1 << "点。\n"
			<< "庄家爆,玩家赢。\n";
	}

	else if (count_c1 >= count_d1)
	{
		cout << "【庄家】 " << count_c1 << "点,\n"
			<< "【玩家】 " << count_d1 << "点。\n"
			<< "庄家赢。\n";
	}

	else if (count_c1 < count_d1)
	{
		cout << "【庄家】 " << count_c1 << "点,\n"
			<< "【玩家】 " << count_d1 << "点。\n"
			<< "玩家赢。\n";
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值