急求C++编程高手解决问题,谢谢

2024年12月01日 13:51
有1个网友回答
网友(1):

可以了

#pragma warning(disable:4786)
#include
#include
#include
#include
using namespace std;

#define Random4 (rand() % 4)
#define RandomN (rand() % 13)

const string varity[4] = {"黑桃", "红桃", "草花", "方块"};
const string Num[13] = {"A", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "J", "Q", "K"};

class Games
{
private:
int Players;
vector Poke;
vector > each_num;
int *Sum;
string *Name;
int i;

public:
Games()
{
srand((unsigned)time(NULL));
cout << "多少人参加游戏?(1~7):";
cin >> Players;
Poke.resize(Players+1);
Sum = new int[Players+1];
Name = new string[Players+1];
Name[0] = "庄家";
for(i = 0; i <= Players; i++)
{
Sum[i] = 0;
}
for(i = 1; i <= Players; i++)
{
cout << "输入第" << i <<"位玩家的姓名: ";
cin >> Name[i];
}
}

~Games()
{
delete []Sum;
delete []Name;
}

void InitFirst()
{
int fir, sec;
vector row_num;
for(i = 0; i <= Players; i++)
{
row_num.erase(row_num.begin(), row_num.end());
fir = RandomN+1;
sec = RandomN+1;
row_num.push_back(fir);
row_num.push_back(sec);
each_num.push_back(row_num);
string Col = varity[Random4];
Poke[i].append(Col);
cout << Name[i] << ": ";
i == 0 ? (cout << "<隐藏>") : (cout << Col << Num[fir-1]);
Poke[i].append(Num[fir-1]);
Col = varity[Random4];
Poke[i].append(Col);
Poke[i].append(Num[sec-1]);
cout << "," << Col << Num[sec-1];
if(fir > 10 || sec > 10)
{
if(fir > 10)
fir = 10;
else
sec = 10;
}
Sum[i] += fir+sec;
if(i != 0)
cout << " 总分值: " << Sum[i];
cout << endl;
}

}

void AddPoint()
{
for(i = 1; i <= Players; i++)
{
char sel;
cin.sync();
cin.clear();
while(1)
{
for(vector::iterator it = each_num[i].begin(); it != each_num[i].end(); it++)
{
if(*it == 1)
{
cout << Name[i] << ",你牌中的A是否要作为11(y,n)?";
cin >> sel;
if(sel == 'y' || sel == 'Y')
{
Sum[i] += 10;
cout << Name[i] << ": " << Poke[i] << "总分值: " << Sum[i] << endl;
}
}
}
cout << Name[i] << ",你想再要一张牌吗(y,n)?";
cin >> sel;
if(sel == 'n' || sel == 'N')
break;
else
{
int add = RandomN+1;
string Col = varity[Random4];
Poke[i].append(Col);
Poke[i].append(Num[add-1]);
if(add > 10)
add = 10;
each_num[i].push_back(add);
Sum[i] += add;
}
cout << Name[i] << ": " << Poke[i] << "总分值: " << Sum[i] << endl;
if(Sum[i] > 21)
{
cout << Name[i] << "引爆!" << endl;
break;
}
}
}
}

void Resault()
{
for(vector::iterator it = each_num[0].begin(); it != each_num[0].end(); it++)
{
if(*it == 1)
{
if(Sum[0]+10 < 21)
{
Sum[0] += 10;
}
}
}

while(Sum[0] <= 16)
{
int add = RandomN+1;
string Col = varity[Random4];
Poke[0].append(Col);
Poke[0].append(Num[add-1]);
if(add > 10)
add = 10;
Sum[0] += add;
}
cout << Name[0] << ": " << Poke[0] << "总分值: " << Sum[0] << endl;
if(Sum[0] > 21)
{
cout << "庄家引爆!" << endl;
for(i = 1; i <= Players; i++)
{
if(Sum[i] > 21)
cout << Name[i] << ": 唉,你打平局!" << endl;
else
cout << Name[i] << ": 你赢了!" << endl;
}
}
else
{
for(i = 1; i <= Players; i++)
{
if(Sum[i] > 21 || Sum[i] < Sum[0])
cout << Name[i] << ": 对不起,你输了!" << endl;
else if(Sum[i] > Sum[0])
cout << Name[i] << ": 你赢了!" << endl;
else
cout << Name[i] << ": 唉,你打平局!" << endl;
}
}
}
};

int main()
{
Games one;
one.InitFirst();
one.AddPoint();
one.Resault();
return 0;
}