Program design thinking C - Swiss God playing cards (multi-keyword sort)

topic

Swiss epidemic because God HRZ bored at home, while he was very powerful, all classes are for him to be able to get water, a water A +, so he was bored, and got three other people: cuckoo East, Teng God and zjm to play cards (Swiss world suffering God for some time now).
Clearly, the Board consists of four individuals, circle. We call for the North East West four directions. Corresponding English is the North, East, South, West. Game total, which is 52 consists of a poker. The beginning, we appoint a dealer (in a truck, with the English initials logo) began licensing, licensing clockwise order, the dealer does not send their own first, but made his next people (clockwise next person). In this way, everyone will get 13 cards.
Now we define the order of the cards, first color is (plum) <(square sheet) <(spades) <(hearts), (input, we use C, D, S, H, respectively plum, square piece, spades, hearts, namely that the first letter of the word). For values of the sign face, we require 2 <3 <4 <5 < 6 <7 <8 <9 <T <J <Q <K <A.
Now you, as God, you have to sort everyone from small to large hands of cards, and set the output format to follow. (See particular output format description and sample output).

Input
Input contains multiple sets of data
of the first line of each data set contains an uppercase character, who represents the dealer yes. If the character is a '#' indicates the end of input.
The next two lines, 52 characters per line, shows 26 cards, two lines add up to a total of 52 cards. Each card consists of two characters, the first character represents the color, the second character indicates values.

Output
output a plurality of sets of data licensing results, each set of data requires additional output after a blank line (i.e., between groups of data lines to use the blank interval, the end of the text is also the need for a blank line).
Each set of data to be composed, the output line 24 in the clockwise direction, always to the output of South Player, i.e. each player to player name output line (truck), followed by five elements, the first and fifth lines output fixation format (see examples), the second and fourth rows in the order and format of the output values (see examples), the third row and the format of the output color sequence (see examples).

Sample Input
N
CTCAH8CJD4C6D9SQC7S5HAD2HJH9CKD3H6D6D7H3HQH4C5DKHKS9
SJDTS3S7S4C4CQHTSAH2D8DJSTSKS2H5D5DQDAH7C9S8C8S6C2C3
#

Sample Output
South player :
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +
| 6 6 | A A | 6 6 | J J | 5 5 | 6 6 | 7 7 | 9 9 | 4 4 | 5 5 | 7 7 | 9 9 | T T|
| C | C | D | D | S | S | S | S | H | H | H | H | H |
|6 6 | A A | 6 6 | J J | 5 5 | 6 6 | 7 7 | 9 9 | 4 4 | 5 5 | 7 7 | 9 9 | T T |
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +
West player :
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +
| 2 2 | 5 5 | 9 9 | K K | 5 5 | 7 7 | 9 9 | 4 4 | T T | J J | A A | 8 8 | A A|
| C | C | C | C | D | D | D | S | S | S | S | H | H |
|2 2 | 5 5 | 9 9 | K K | 5 5 | 7 7 | 9 9 | 4 4 | T T | J J | A A | 8 8 | A A |
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +
North player :
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +
| 3 3 | 4 4 | J J | 2 2 | 3 3 | T T | Q Q | K K | 8 8 | Q Q | K K | 2 2 | 3 3|
| C | C | C | D | D | D | D | D | S | S | S | H | H |
|3 3 | 4 4 | J J | 2 2 | 3 3 | T T | Q Q | K K | 8 8 | Q Q | K K | 2 2 | 3 3 |
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +
East player :
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +
| 7 7 | 8 8 | T T | Q Q | 4 4 | 8 8 | A A | 2 2 | 3 3 | 6 6 | J J | Q Q | K K|
| C | C | C | C | D | D | D | S | S | H | H | H | H |
|7 7 | 8 8 | T T | Q Q | 4 4 | 8 8 | A A | 2 2 | 3 3 | 6 6 | J J | Q Q | K K |
±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - ±- - +

Thinking

This problem can be multi-keyword sort.
The primary key (color): C <D <S < H
Keywords (nominal value): 2 <3 <4 < 5 <6 <7 <8 <9 <T <J <Q <K <A
use of the STL sort function, the preparation of a suitable comparison function:
① when the colors are different, the comparison returns only color values.
② When the same color, comparing the value returned par.

Code

#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <utility>
#include <algorithm>
#include <stdlib.h>
#include <string.h>

using namespace std;

// pair<char, char> p; 第一个是花色,第二个数字

int iconValue(const char c)
{
	switch (c)
	{
	case 'C':
		return 0;
	case 'D':
		return 1;
	case 'S':
		return 2;
	default:	// 'H'
		return 3;
	}
}

int numValue(const char c)
{
	if (c >= '2' && c <= '9') return c - '0';
	switch (c)
	{
	case 'T':
		return 10;
	case 'J':
		return 11;
	case 'Q':
		return 12;
	case 'K':
		return 13;
	default:	// 'A'
		return 14;
	}
}

// 先比较花色,再比较数字
bool cmp(const pair<char, char>& p1, const pair<char, char>& p2)
{
	if (p1.first != p2.first) return iconValue(p1.first) < iconValue(p2.first);
	else return numValue(p1.second) < numValue(p2.second);
}

void printCards(vector<pair<char, char>> v)
{
	cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
	cout << "|";
	for (vector<pair<char, char>>::iterator it = v.begin(); it != v.end(); it++) {
		cout << (*it).second << " " << (*it).second << "|";
	}
	cout << endl;
	cout << "|";
	for (vector<pair<char, char>>::iterator it = v.begin(); it != v.end(); it++) {
		cout << " " << (*it).first << " " << "|";
	}
	cout << endl;
	cout << "|";
	for (vector<pair<char, char>>::iterator it = v.begin(); it != v.end(); it++) {
		cout << (*it).second << " " << (*it).second << "|";
	}
	cout << endl;
	cout << "+---+---+---+---+---+---+---+---+---+---+---+---+---+" << endl;
}

void print(vector<int> v)
{
	vector<int>::iterator i1 = v.begin();
	vector<int>::iterator i2 = v.begin(); i2++;
	while (i2 != v.end()) {
		cout << *i1 << " ";
		i1++; i2++;
	}
	cout << *i1 << endl;
}

int main()
{
	char pos;
	cin >> pos;

	while (pos != '#') {

		string order;

		if (pos == 'N') order = "4123";
		else if (pos == 'E') order = "1234";
		else if (pos == 'S') order = "2341";
		else order = "3412";

		string print_order[] = { "South player:","West player:","North player:" ,"East player:" };

		vector<pair<char, char>> v[4];

		for (int i = 0; i < 13; i++) {
			for (int j = 0; j < 4; j++) {
				char icon, num;
				cin >> icon >> num;
				pair<char, char> p(icon, num);
				v[j].push_back(p);
			}
		}

		for (int i = 0; i < 4; i++) {
			sort(v[i].begin(), v[i].end(), cmp);
		}

		cout << "South player:" << endl;
		int index = order.find(1 + '0');
		printCards(v[index]);

		cout << "West player:" << endl;
		index = order.find(2 + '0');
		printCards(v[index]);

		cout << "North player:" << endl;
		index = order.find(3 + '0');
		printCards(v[index]);

		cout << "East player:" << endl;
		index = order.find(4 + '0');
		printCards(v[index]);

		cout << endl;

		cin >> pos;
	}
}

End

Released five original articles · won praise 0 · Views 47

Guess you like

Origin blog.csdn.net/weixin_43826681/article/details/104690382