21点扑克游戏

#include<iostream>
#include<map>
#include<string>
#include<time.h>


using namespace std;


class Game
{
public:
Game() :nPair(4), nComputer(0), nPeople(0){}
void Start();
private:
void ShowResWin()
{
cout << "系统:" << sComputer << endl;
cout << "玩家:" << sPeople << endl;
cout << "你赢了!" << endl;
}
void ShowResLose()
{
cout << "系统:" << sComputer << endl;
cout << "玩家:" << sPeople << endl;
cout << "你输了!" << endl;
}
bool deal();
void Shuffle();//洗牌
string GetPoker(int * n);//取牌
int  nPair;//扑克副数
int  nRemain;//剩余数量
map<string, int> Poker;//扑克牌
string sComputer, sPeople;
int nComputer, nPeople;//电脑,人的点数;


};
void Game::Shuffle()
{
nRemain = nPair * 52;
char ch1 = '\3';//递增依次为红桃,方片,梅花,黑桃
char ch2 = '2';//2-9计数
string str;
for (int i = 0; i < 4; ++i)
{
str += ch1 + i;
str += 'A';
Poker[str] = nPair;//数量初试化为副数
str.clear();
for (int j = 0; j < 8; ++j)
{
str += ch1 + i;
str += ch2 + j;
Poker[str] = nPair;
str.clear();


}
str += ch1 + i;
str += "10";
Poker[str] = nPair;
str.clear();
str += ch1 + i;
str += 'J';
Poker[str] = nPair;
str.clear();
str += ch1 + i;
str += 'Q';
Poker[str] = nPair;
str.clear();
str += ch1 + i;
str += 'K';
Poker[str] = nPair;
str.clear();
}
}


string Game::GetPoker(int * n)
{
reGetPoker:


int roll_n1 = rand() % 4;//随机获取红桃,黑桃,方片,梅花
int roll_n2 = rand() % 13;//随机大小
char ch1 = '\3' + roll_n1;
char ch2 = '1';
string str;


str += ch1;


switch (roll_n2)
{
case 0: str += 'A'; break;
case 9:str += "10"; break;
case 10:str += 'J';    break;
case 11:str += 'Q'; break;
case 12:str += 'K'; break;
default:
ch2 += roll_n2;
str += ch2;
break;
}
if (0 != Poker[str])
{
--Poker[str];
--nRemain;
if (roll_n2 > 9)
roll_n2 = 9;
*n += roll_n2 + 1;
return str;
}
else
{
if (nRemain < 13)//牌数小于13,重新洗牌
{
Shuffle();
}
goto reGetPoker;
}
}


bool Game::deal()
{
int IsContinue;
string sTmp;


sComputer += GetPoker(&nComputer);
sTmp = sComputer;
sPeople += GetPoker(&nPeople);
sPeople += "   ";
while (nComputer < 17)
{
if (-1 != sComputer.find('A') && nComputer + 9 < 22 && nComputer + 9 > 16)
{
nComputer += 9;
}
else
{
sComputer += "   ";
sComputer += GetPoker(&nComputer);
}


}
sPeople += GetPoker(&nPeople);
cout << "按键1继续要牌,按键2结束要牌,其他按键退出程序:" << endl;
while (true)
{
cout << "系统:" << sTmp << endl;
cout << "玩家:" << sPeople << endl;




cin >> IsContinue;
if (IsContinue == 1)
{
sPeople += "   ";
sPeople += GetPoker(&nPeople);


if (nPeople > 21)
{
ShowResLose();
break;
}
}
else if (IsContinue == 2)
{
if (nPeople > 21)//玩家点数超过21
ShowResLose();
else if (nComputer > 21)//电脑点数超过21
ShowResWin();
else
{
if (-1 != sPeople.find('A'))//如果有A
{
if ((nPeople > nComputer) || (nPeople + 9 > nComputer && nPeople + 9 < 22))
ShowResWin();
else
ShowResLose();
}
else
{
if ((nPeople > nComputer))
ShowResWin();
else
ShowResLose();
}
}


break;
}
else //输入1,2之外的值,结束游戏
return false;
}
sPeople.clear();
sComputer.clear();
nPeople = 0;
nComputer = 0;


return true;
}


void Game::Start()
{
cout << "输入1-8设置扑克数:" << endl;
cin >> nPair;
if (nPair < 1 || nPair > 8)//输入过大或者过小,自动设置为默认值
nPair = 4;
Shuffle();
while (deal())
;
}
int main()
{
srand((unsigned)time(NULL));
Game g1;
g1.Start();






return 0;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值