C++实现德州扑克游戏(和电脑一起玩)

事先声明,本人坚决反对赌博,对众多程序员助纣为虐,远赴东南亚等地编写赌博网站的行为也很反感,更有甚者,使用python进行黑客行为,非法爬虫,真正实现了“C++从入门到入土”,“python从入门到入狱”.今年,已经有法规规定杜绝上架一切德州扑克游戏,本人最近想看看C++语法,就写个小程序练练手,只有简单的人机功能,参考了《轻松学会C++》部分代码,但核心部分全是本人创作,代码只供交流学习用,严禁一切非法用途。其他代码和程序在本人博客的资源里,和以前一样,有一些不影响大局的小bug,比如全局变量和局部变量等等,本人无意修复。
eval.cpp

#include<string>
using namespace std;
class Eval {
public:
	Eval(Card* pCards);
	double rank_hand();
private:
	int rankCounts[13];
	//int type;
	//int max;
	int suitCounts[4];
	int has_reps(int n);
	bool is_straight();
	bool verify_straight(int n);
	bool is_flush();
	bool is_two_pair();
};
Eval::Eval(Card* pCards) {
	for (int i = 0;i < 13;++i) {
		rankCounts[i] = 0;
	}
	for (int i = 0;i < 4;++i) {
		suitCounts[i] = 0;
	}
	for (int i = 0;i < 5;++i) {
		int r = pCards[i].rank;
		int s = pCards[i].suit;
		++rankCounts[r];
		++suitCounts[s];
	}
}
double Eval::rank_hand() {
	double score,first, second,third,fourth,fifth=0;
	if (is_straight() && is_flush()) {
		for (int i = 12;i >= 0;i--) {
			if (rankCounts[i]!= 0) {
				first = i *1.0/ 20;
				score = 10 + first;
				break;
			}
		}
		cout << "同花顺" << endl;
	}
	//这里仅仅考虑一副牌的情况,所以4条只需考虑4张牌的点数
	else if (has_reps(4)) {
		for (int i = 0;i < 13;++i) {
			if (rankCounts[i] == 4) {
				first = i *1.0/ 20;
				score = 9 + first;
				break;
			}
		}
		cout << "四条" << endl;
	}
    //同上,只考虑3条的点数
	else if (has_reps(3) && has_reps(2)) {
		for (int i = 0;i < 13;++i) {
			if (rankCounts[i] == 3) {
				first = i *1.0/ 20;
				score = 8 + first;
				break;
			}
		}
		cout<<"葫芦";
	}
	else if (is_flush()) {
		for (int i = 12;i >= 0;i--) {
			if (rankCounts[i] != 0) {
				first = i *1.0/ 20;
				for (int j = i - 1;j >= 0;j--) {
					if (rankCounts[j] != 0) {
						second = j *1.0/ 400;
						for (int k = j - 1;k >= 0;k--) {
							if (rankCounts[k] != 0) {
								third = k*1.0 / 8000;
								for (int l = k - 1;l >= 0;l--) {
									if (rankCounts[k] != 0) {
										fourth = l *1.0/ 160000;
										for (int m = l - 1;m >= 0;m--) {
											fifth = m*1.0 / 3200000;
										}
									}
								}
							}
						}
					}

				}
			}
		}
		cout<<"同花";
		score = 7 + first+second+third+fourth+fifth;
	}
	else if (is_straight()) {
		for (int i = 12;i >= 0;i--) {
			if (rankCounts[i] != 0) {
				first = i*1.0 / 20;
				break;
			}
		}
		cout<<"顺子";
		score = 6 + first;
	}
	else if (has_reps(3)) {
		for (int i = 0;i < 13;++i) {
			if (rankCounts[i] == 3) {
				first = i *1.0/ 20;
				break;
			}
		}
		cout<< "三条";
		score = 5 + first;
	}
	else if (is_two_pair()) {
		for (int i = 12;i >=0;--i) {
			if (rankCounts[i] == 2) {
				first = i *1.0/ 20;
				for (int j = i - 1;j >= 0;--j) {
					if (rankCounts[j] == 2) {
						second = j *1.0/ 400;
						for (int k = 12;k >= 0;--k) {
							if (rankCounts[k] == 1) {
								third = k *1.0/ 8000;
							}
						}
					}
				}
			}
		}
		cout<<"两对";
		score = 4 + first+second+third;
	}
	else if (has_reps(2)) {
		for (int i = 12;i >= 0;--i) {
			if (rankCounts[i] == 2) {
				first = i*1.0 / 20;
				for (int j = 12;j >= 0;--j) {
					if (rankCounts[j] != 0) {
						second = j*1.0 / 400;
						for (int k = j - 1;k >= 0;--k) {
							if (rankCounts[k] != 0) {
								third = k*1.0 / 8000;
								for (int l = k - 1;l >= 0;--l) {
									if (rankCounts[l] != 0) {
										fourth = l*1.0 / 160000;
									}
								}
							}
						}
					}
				}
			}
		}
		cout<<"一对";
		score = 3+ first+second+third+fourth;
	}
	else {
	for (int i = 12;i >= 0;i--) {
		if (rankCounts[i] != 0) {
			first = i*1.0 / 20;
			for (int j = i - 1;j >= 0;j--) {
				if (rankCounts[j] != 0) {
					second = j*1.0 / 400;
					for (int k = j - 1;k >= 0;k--) {
						if (rankCounts[k] != 0) {
							third = k*1.0 / 8000;
							for (int l = k - 1;l >= 0;l--) {
								if (rankCounts[l] != 0) {
									fourth = l *1.0/ 160000;
									for (int m = l - 1;m >= 0;m--) {
										if (rankCounts[m] != 0) {
											fifth = m * 1.0 / 3200000;
											score = 2.0 + first + second + third + fourth;
										}
									}
									
								}
							}
						}
					}
				}

			}
		}
	}
		cout<<"高牌";
	}
	return score;
}
int Eval::has_reps(int n) {
	for (int i = 0;i < 13;++i) {
		if (rankCounts[i] == n) {
			return true;
		}
	}
	return false;
}
bool Eval::is_straight() {
	for (int i = 0;i <= 8;++i) {
		if (rankCounts[i] == 1) {
			return verify_straight(i);
		}
	}
	return false;
}
bool Eval::verify_straight(int n) {
	for (int i = n + 1;i < n + 5;++i) {
		if (rankCounts[i] != 1) {
			return false;
		}
	}
	return true;
}
bool Eval::is_flush() {
	for (int i = 0;i < 4;++i) {
		if (suitCounts[i] == 5) {
			return true;
		}
	}
	return false;
}
bool Eval::is_two_pair() {
	int n = 0;
	for (int i = 0;i < 13;++i) {
		if (rankCounts[i] == 2) {
			++n;
		}
	}
	return n == 2;
}

card.cpp

#include <string>
using namespace std;
class Card {
public:
	Card() {}
	Card(int r, int s) { rank = r;suit = s; }
	int rank;
	int suit;
	string display();
};
string Card::display() {
	static const string aRanks[] = { "2","3","4","5","6","7","8","9","10","J","Q","K","A" };
	static const string aSuits[] = { "梅花","方块","红桃","黑桃" };
	return aSuits[suit] + aRanks[rank] + ".";
}

deck.cpp

#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class Deck {
public:
	int iCard;
	Deck();
	Card deal_a_card();
private:
	int cards[52];
	void shuffle();
};
Deck::Deck() {
	srand(time(NULL));
	for (int i = 0;i < 52;++i) {
		cards[i] = i;
	}
	shuffle();
}
void Deck::shuffle() {
	iCard = 0;
	for (int i = 51;i > 0;--i) {
		int j = rand() % (i + 1);
		int temp = cards[i];
		cards[i] = cards[j];
		cards[j] = temp;
	}
}
Card Deck::deal_a_card() {
	if (iCard > 42) {
		cout << endl << "牌不够了,正在重新洗牌..." << endl;
		shuffle();
	}
	int r = cards[iCard] % 13;
	int s = cards[iCard++] / 13;
	return Card(r, s);
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

returnadsss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值