一个简单粗糙的C++发牌程序

//程序设计思想:定义puke类,生成54个对象,然后将对象随机发到3位玩家手中
//实际上只定义了两位玩家,第三位玩家的牌就是前两位玩家剩下的
//将玩家手中的牌进行排序和输出
#include 
#include
#include
#include
using namespace std;


class puke//定义扑克类
{
public:
	int id;
	int kind;
	int number;
};
void swap(int &a, int &b)//交换函数,实现两个数之间的交换
{
	a = a + b;
	b = a - b;
	a = a - b;
}
void bubblesort(puke* player[], int length)//冒泡排序,对玩家手中的牌进行排序
{
	for (int i = 0; i < 18; i++)
	{
		for (int j = 0; j < 18 - 1 - i; j++)
		{
			if (player[j]->id > player[j + 1]->id)
			{
				swap(player[j]->id, player[j + 1]->id);
				swap(player[j]->kind, player[j + 1]->kind);
				swap(player[j]->number, player[j + 1]->number);
			}
		}
	}
}
void show(puke* player[],int length)//输出玩家手中的牌
{
	for (int i = 0; i < 18; i++)
	{
		player[i]->number++;
		if (player[i]->kind == 0)
		{
			if (player[i]->number <= 10)
			{
				cout << "梅花" << player[i]->number << "  ";
			}
			else if (player[i]->number == 11)
			{
				cout << "梅花" << "J"<< "  ";
			}
			else if (player[i]->number == 12)
			{
				cout << "梅花" << "Q" << "  ";
			}
			else if (player[i]->number == 13)
			{
				cout << "梅花" << "K" << "  ";
			}
			else if (player[i]->number == 14)
			{
				cout << "大王" << "  ";
			}
		}
		else if (player[i]->kind == 1)
		{
			if (player[i]->number <= 10)
			{
				cout << "方块" << player[i]->number << "  ";
			}
			else if (player[i]->number == 11)
			{
				cout << "方块" << "J" << "  ";
			}
			else if (player[i]->number == 12)
			{
				cout << "方块" << "Q" << "  ";
			}
			else if (player[i]->number == 13)
			{
				cout << "方块" << "K" << "  ";
			}
			else if (player[i]->number == 14)
			{
				cout << "小王" << "  ";
			}
		}
		else if (player[i]->kind == 2)
		{
			if (player[i]->number <= 10)
			{
				cout << "红桃" << player[i]->number << "  ";
			}
			else if (player[i]->number == 11)
			{
				cout << "红桃" << "J" << "  ";
			}
			else if (player[i]->number == 12)
			{
				cout << "红桃" << "Q" << "  ";
			}
			else if (player[i]->number == 13)
			{
				cout << "红桃" << "K" << "  ";
			}
		}
		else if (player[i]->kind == 3)
		{
			if (player[i]->number <= 10)
			{
				cout << "梅花" << player[i]->number << "  ";
			}
			else if (player[i]->number == 11)
			{
				cout << "梅花" << "J" << "  ";
			}
			else if (player[i]->number == 12)
			{
				cout << "梅花" << "Q" << "  ";
			}
			else if (player[i]->number == 13)
			{
				cout << "梅花" << "K" << "  ";
			}
			else if (player[i]->number == 14)
			{
				cout << "大王" << "  ";
			}
		}
	}
}
int main()
{
	puke* newpuke[54];
	for (int i = 0; i < 54; i++)
	{
		newpuke[i] = new puke();
		newpuke[i]->id = i;
		if (i % 4 == 0)
		{
			newpuke[i]->kind = 0;//设置扑克牌花色为梅花
		}
		else if (i % 4 == 1)
		{
			newpuke[i]->kind = 1;//设置扑克牌花色为方块
		}
		else if (i % 4 == 2)
		{
			newpuke[i]->kind = 2;//设置扑克牌花色为黑桃
		}
		else if (i % 4 == 3)
		{
			newpuke[i]->kind = 3;//设置扑克牌花色为红桃
		}
		newpuke[i]->number = i / 4;//设置扑克牌大小
		//cout << newpuke[i]->kind<<  newpuke[i]->number<id != 54)
	{
		int end = 54;
		for (int i = 0; i < 18; i++)
		{
			player1[i] = new puke();
			int start = rand() % end;
			if (newpuke[start]->id != 54)
			{
				int tempend = --end;
				player1[i]->id = newpuke[start]->id;
				player1[i]->kind = newpuke[start]->kind;
				player1[i]->number = newpuke[start]->number;
				swap(newpuke[start]->id, newpuke[tempend]->id);
				swap(newpuke[start]->kind, newpuke[tempend]->kind);
				swap(newpuke[start]->number, newpuke[tempend]->number);
				newpuke[end]->id = 54;
				//cout << player1[i]->id<<"  ";
			}
		}
		//cout << endl;
		for (int i = 0; i < 18; i++)
		{
			player2[i] = new puke();
			int start = rand() % end;
			if (newpuke[start]->id != 54)
			{
				int tempend = --end;
				player2[i]->id = newpuke[start]->id;
				player2[i]->kind = newpuke[start]->kind;
				player2[i]->number = newpuke[start]->number;
				swap(newpuke[start]->id, newpuke[tempend]->id);
				swap(newpuke[start]->kind, newpuke[tempend]->kind);
				swap(newpuke[start]->number, newpuke[tempend]->number);
				newpuke[end]->id = 54;
				//cout << player2[i]->id << "  ";
			}
		}
		//cout << endl;
		//for (int i = 0; i < 18; i++)
		//	{
		//		cout << newpuke[i]->id<<"  ";
		//	}
		tempid = newpuke[0]->id;
		newpuke[0]->id = 54;
	}
	bubblesort(player1,18);
	bubblesort(player2, 18);
	newpuke[0]->id = tempid;
	bubblesort(newpuke, 18);
	cout << "玩家1持牌 "<

程序执行结果如下:

一个简单粗糙的C++发牌程序_第1张图片


你可能感兴趣的:(一个简单粗糙的C++发牌程序)