C++模拟扑克牌的实现

#include <iostream>
#include <ctime>

using namespace std;

enum
{
	SPADES,
	HEARTS,
	CLUBS,
	DIAMONDS,
	JOKER
};

class Poker
{
	char m_type;
	int m_point;
public:
	Poker() :
		m_type(0),
		m_point(0)
	{

	}

	Poker(char type, int point) :
		m_type(type),
		m_point(point)
	{

	}

	void makePoker(char type, int point)
	{
		m_type = type;
		m_point = point;

		if (m_type == JOKER)
		{
			m_point += 13;
		}
	}

	void outputPoker()
	{
		char * type[5] = { "黑桃", "红桃", "梅花", "方片", "" };
		char * point[16] = { "", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "小王", "大王" };

		printf("%s%s", type[m_type], point[m_point]);
	}

	bool cmppoker(Poker tmp)
	{
		return (m_point < tmp.m_point) || (m_point == tmp.m_point && m_type > tmp.m_type);
	}

	bool isEff()
	{
		if (m_type == JOKER && (m_point == 14 || m_point == 15))
		{
			return true;
		}

		if ((unsigned char)m_type > 3 || (unsigned char)m_point - 1 > 13)
		{
			return false;
		}

		return true;
	}

};

class Player
{
	Poker m_HandCard[18];
	int m_size;
public:
	Player() :
		m_size(0)
	{

	}

	void getCard(Poker newCard)
	{
		int i;
		
		for (i = m_size; i > 0 && m_HandCard[i - 1].cmppoker(newCard); --i)
		{
			m_HandCard[i] = m_HandCard[i - 1];
		}

		m_HandCard[i] = newCard;
		m_size++;
	}

	void showCard()
	{
		for (auto & i : m_HandCard)
		{
			i.outputPoker();
			putchar(' ');
		}

		putchar('\n');
	}
};

//测试
void PokerTest()
{
	srand((unsigned)time(NULL));

	Player p1;
	Poker tmp(0, 3);

	int i;
	for (i = 0; i < 18; ++i)
	{
		tmp.makePoker(rand() % 4, rand() % 13 + 1);
		p1.getCard(tmp);
	}

	p1.showCard();
}

int randnum(Poker * cardHeap)
{
	int tmp = rand() % 54;
	while (1)
	{
		if (cardHeap[tmp].isEff())
		{
			return tmp;
		}
		else
		{
			tmp++;
			if (tmp == 54)
			{
				tmp = 0;
			}
		}

	}
}

int main()
{
	Poker tmp[54];
	int j = 0;

	Player A;
	Player B;
	Player C;

	for (auto & i : tmp)
	{
		i.makePoker(j / 13, j % 13 + 1);
		j++;
	}

	srand((unsigned)time(NULL));

	int delcard;

	int i;
	for (i = 0; i < 18; ++i)
	{
		delcard = randnum(tmp);
		A.getCard(tmp[delcard]);
		tmp[delcard].makePoker(-1, -1);

		delcard = randnum(tmp);
		B.getCard(tmp[delcard]);
		tmp[delcard].makePoker(-1, -1);

		delcard = randnum(tmp);
		C.getCard(tmp[delcard]);
		tmp[delcard].makePoker(-1, -1);
	}

	A.showCard();
	B.showCard();
	C.showCard();

	//PokerTest();

	system("pause");
	return 0;
}
发布了235 篇原创文章 · 获赞 28 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44781107/article/details/103387423