跳到正文

个人记录

猪斗地(bushi)

zjy1437约 6 分钟
原文

这是 2026年10月15日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年9月7日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 6 分钟

版本

  1. v3当前
  2. v2
  3. v1

斗地主(?)

C++
#include<bits/stdc++.h>
#include<windows.h>

#define setTextWHITE SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
#define setTextRED SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY)
#define setTextBLUE SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY)

using namespace std;
mt19937 rng(time(nullptr));
const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
const string suit[4]={"\u2660","\u2663","\u2665","\u2666"};//黑桃,梅花,红桃,方块 
const string nums[20]={"#","3","4","5","6","7","8","9","10","J","Q","K","A","2","joker","JOKER"};
const string pokerHands[20]={"#","Single","Pair","Triple","Triple with One","Triple with Pair","Straight","Double Straight","Plane","Bomb","Four with Two","Four with Two Pairs","Joker Bomb"}
unordered_map<string,int> numToHands;
string cards[55];
class Card{
	private:
		int id;
	public:
		bool operator <(const Card &c)const;
		void print();
		void getID(int x);
};
class Player{
	private:
		vector<Card> handCards;
	public:
		void sortHandCard();
		void printHandCards();
		void getCard(); 
		void playCards();
};
class Competition{
	
	private:
		Card deck[60];
		int deckTop;
		Player player[4];
	public:
		Card draw();
		void printPlayerCards(int x);
		void printHiddenCards();
	Competition(){
		memset(deck,0,sizeof(deck));
		for(int i=1;i<=54;i++)
		{
			//print(i);
			deck[i].getID(i);
		}
		deckTop=54;
		shuffle(deck+1,deck+55,rng);
		for(int j=1;j<=17;j++)
		{
			for(int i=1;i<=3;i++)
			{
				player[i].getCard();
			}
		}
		for(int i=1;i<=3;i++)
		{
			player[i].sortHandCard();
		}
	}
};
Competition comp;
void Card::getID(int x)
{
	id=x;
}
void init()
{
	for(int i=0;i<=3;i++)
	{
		for(int j=1;j<=13;j++)
		{
			//if(i<3)
				cards[i*13+j]=suit[i]+nums[j];
			//else
			//	cards[i*13+j]=suit[i]+" "+nums[j];
		}
	}
	cards[53]=nums[14];
	cards[54]=nums[15];
	for(int i=1;i<=12;i++)
	{
		map[pokerHands[i]]=i;
	}
}
void Card::print()
{
	if(id<=26||id==53)
	{
		setTextBLUE;
		printf("%s",cards[id].c_str());
	}
	else
	{
		setTextRED;
		printf("%s",cards[id].c_str());
	}
	setTextWHITE;
}
bool Card::operator <(const Card &c)const
{
	int a=id,b=c.id;
	if(a==54||b==54||a==53||b==53)
		return a<b;
	if((a%13==0?13:a%13)!=(b%13==0?13:b%13))
		return (a%13==0?13:a%13)<(b%13==0?13:b%13);
	else
		return a/13 < b/13;
}
void Player::sortHandCard()
{
	sort(handCards.begin(),handCards.end());
}
void Player::printHandCards()
{
	for(int i=0;i<handCards.size();i++)
	{
		handCards[i].print();
		putchar(' ');
	}
}
void Player::getCard()
{
	handCards.push_back(comp.draw());
}
Card Competition::draw()
{
	if(deckTop<1)
	{
		printf("ERROR:Deck is empty\n");
		exit(0);
	}
	return deck[deckTop--];
}
void Competition::printPlayerCards(int x)
{
	player[x].printHandCards();
}
void Competition::printHiddenCards()
{
	sort(deck+1,deck+4);
	for(int i=1;i<=3;i++)
	{
		deck[i].print();
		putchar(' ');
	}
}
int main()
{
	SetConsoleOutputCP(65001);
	init();
	for(int j=1;j<=3;j++)
	{
		printf("Player %d: ",j);
		comp.printPlayerCards(j);
		printf("\n"); 
	}
	printf("Hidden Card:");
	comp.printHiddenCards();
	
	return 0;
}