打牌问题(利用map映射的应用)

瑞神打牌

- 题目要求:现在我们定义牌的顺序,首先,花色是(梅花)<(方片)<(黑桃)<(红桃),(输入时,我们用C,D,S,H分别表示梅花,方片,黑桃,红桃,即其单词首字母)。对于牌面的值,我们规定2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < T < J < Q < K < A。
现在你作为上帝,你要从小到大排序每个人手中的牌,并按照给定格式输出。(具体格式见输出描述和样例输出)。

- 解题思路:自定义结构体node记录牌的花色和数字,都是char类型,结构体数组NN,SS,WW,EE分别记录四个方向的四个人的牌。cmp函数进行排序,花色是C<D<S<H,将S、H单独说明,其余默认字典序升序。运用map函数将char型映射为int型,分别将T,J,Q,K的映射值定义为10,11,12,13,14,方便在cmp函数中只需将对应的int值升序输出。将输入的发牌人N,S,W,E映射为1,2,3,4,进行相应转换调整发牌顺序,将花色和数值分别存入对应数组,调用sort()排序后输出。特别注意输出的格式,不要忘记空格和空行

- 代码实现

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

struct node
{
	char col,number;
};

map<char,char> mp;
node NN[50],SS[50],WW[50],EE[50];

bool cmp(node a,node b){
		if(a.col!=b.col)
		{
			if((a.col=='S'&&b.col=='H')||(a.col=='H'&&b.col=='S'))
				return a.col>b.col;
			else
				return a.col<b.col;
		}	
		else 
			return mp[a.number]<mp[b.number];
			
}


int main()
{
	char aa;
	mp['N']=0;mp['E']=1;mp['S']=2;mp['W']=3;   
	mp['1']=1;mp['2']=2;mp['3']=3;mp['4']=4;mp['5']=5;mp['6']=6;mp['7']=7;mp['8']=8;mp['9']=9;mp['T']=10;mp['J']=11;mp['Q']=12;mp['K']=13;mp['A']=14;
	while(1)
	{
		cin>>aa;
		if(aa=='#')
			break;
		int q=mp[aa];
		int bb,cc,dd;
		switch(q)
		{
			case 0:
				dd=1;break;
			case 1:
				dd=2;break;
			case 2:
				dd=3;break;
			case 3:
				dd=4;break;
			
		}
		char col,number;
		for(int i=0;i<52;i++)
		{
			cin>>col>>number;
			bb=(i+dd)%4;  cc=i/4; 
			switch(bb)
			{
				case 0:
					NN[cc].col=col;
					NN[cc].number=number;
					break;
				case 1:
					EE[cc].col=col;
					EE[cc].number=number;
					break;
				case 2:
					SS[cc].col=col;
					SS[cc].number=number;
					break;
				case 3:
					WW[cc].col=col;
					WW[cc].number=number;
					break;
			}
			
		}
		
		sort(NN,NN+13,cmp);
		sort(EE,EE+13,cmp);
		sort(SS,SS+13,cmp);
		sort(WW,WW+13,cmp);
		
	cout << "South player:" << endl;
		cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
		for (int j = 0; j < 13; j++)
			cout << "|" << SS[j].number << " " <<SS[j].number;
		cout << "|" << endl;
		for (int j = 0; j < 13; j++)
			cout << "|" << " " << SS[j].col << " " ;
		cout << "|" << endl;
		for (int j = 0; j < 13; j++)
			cout << "|" << SS[j].number << " " << SS[j].number;
		cout << "|" << endl;
		cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;

	
		
		cout << "West player:" << endl;
		cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
		for (int j = 0; j < 13; j++)
			cout << "|" << WW[j].number << " " << WW[j].number;
		cout << "|" << endl;
		for (int j = 0; j < 13; j++)
			cout << "|" << " " << WW[j].col << " ";
		cout << "|" << endl;
		for (int j = 0; j < 13; j++)
			cout << "|" << WW[j].number << " " << WW[j].number;
		cout << "|" << endl;
		cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;

		
		
		cout << "North player:" << endl;
		cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
		for (int j = 0; j < 13; j++)
			cout << "|" << NN[j].number << " " << NN[j].number;
		cout << "|" << endl;
		for (int j = 0; j < 13; j++)
			cout << "|" << " " << NN[j].col << " ";
		cout << "|" << endl;
		for (int j = 0; j < 13; j++)
			cout << "|" << NN[j].number << " " << NN[j].number;
		cout << "|" << endl;
		cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;

		
		
		cout << "East player:" << endl;
		cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
		for (int j = 0; j < 13; j++)
			cout << "|" << EE[j].number << " " << EE[j].number;
		cout << "|" << endl;
		for (int j = 0; j < 13; j++)
			cout << "|" << " " << EE[j].col << " ";
		cout << "|" << endl;
		for (int j = 0; j < 13; j++)
			cout << "|" << EE[j].number << " " << EE[j].number;;
		cout << "|" << endl;
		cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
		cout << endl;
	}
				 	
}

- 输入输出:
输入包含多组数据
每组数据的第一行包含一个大写字符,表示发牌员是谁。如果该字符为‘#’则表示输入结束
接下来有两行,每行有52个字符,表示了26张牌,两行加起来一共52张牌。每张牌都由两个字符组成,第一个字符表示花色,第二个字符表示数值。

输出多组数据发牌的结果,每组数据之后需要额外多输出一个空行!!!!!
每组数据应该由24行的组成,输出按照顺时针方向,始终先输出South Player的结果,每位玩家先输出一行即玩家名称(东南西北),接下来五行,第一行和第五行输出固定格式(见样例),第二行和第四行按顺序和格式输出数值(见样例),第三行按顺序和格式输出花色(见样例
)。
—————————————————————————EOF—————————————————————————

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值