21点扑克牌游戏
#include
//#include
#include
//#include
#include
using namespace std;
//定义扑克类
class game
{
private:
int wins,lose,draw,money;
int bet,playerturns,cputurns;
float player,cpu,card;//此处CARD在和BET交换数据的时候会有警告,可多定义一个INT型避免。
char flag;
public:
game();
void results(float player,float cpu,int bet,int &money,int &draw,int &wins,int &lose);
void wait(int milli);
void sign();
void special(float &player,float &cpu);
void rules();
void pause();
float random(long hign,long low);
void print(int wins,int lose,int draw,int money);
void replay(char &flag);
void hit(float &total);
void results(float player,int cpu,int bet,int &money,int &draw,int &win,int &lose);
void BET(int &bet,int &money);
void deal(float &player,float &cpu,int &playerturns,int &cputurns);
void deal2(float &player,float &cpu,int &playerturns,int &cputurns);
void ask();
};
//---------------------------------------------------------------------
//完成虚构函数
game::game()
{
wins=0;
lose=0;
draw=0;
money=0;
player=0.0;
cpu=0.0;
bet=0;
playerturns=0;
cputurns=0;
card=0;
flag='a';
}
//---------------------------------------------------------------------
void game::rules()
{
cout<<"\t*********************************************************"<
pause();//此处暂停几秒
ask();
}
//---------------------------------------------------------------------
void game::ask()
{
cout<
if((flag=='y')||(flag=='Y')) //判断是否进入游戏
{
cout<<"\t\t\t你将有100元开始这个游戏"<
cout<<"\t\t\t";
pause();
}
else exit(1);
BET(bet,money);
deal(player,cpu,playerturns,cputurns);//开始处理
}
//---------------------------------------------------------------------
void game::BET(int &bet,int &money)
{
int sign;
system("cls");
if(money<=0)
{
cout<<"\t\t\t你已经没有赌注了,请关闭后重新开始"<
}
cout<<"\t\t\t你现在有:"<
do{ //判断输入赌注是否正确
sign=0;
cin>>bet;
if((bet<0)&&(bet*-1<=money))
{
bet=bet*-1;
break;
}
else if((bet>0)&&(bet<=money))
break;
else
{
cout<<"输入错误,请重新输入:";
sign=1;
}
}while(sign=1);
money=money-bet;
}
//---------------------------------------------------------------------
void game::deal(float &player,float &cpu,int &playerturns,int &cputurns)
{
float playercard1,playercard2,cpucard1,cpucard2;
playerturns=2;
cputurns=2;
playercard1=random(13,1); //开始利用随机数分牌
cout<<"\n\t\t\t正在处理中"<
playercard2=random(13,1);
wait(150);
cpucard1=random(13,1);
wait(350);
cpucard2=random(13,1);
if(playercard1>=10)
{
playercard1=0.5;
}
if(playercard2>=10)
{
playercard2=0.5;
}
if(cpucard1>=10)
{
cpucard1=0.5;
}
if(cpucard2>=10)
{
cpucard2=0.5;
}
player=playercard1+playercard2;
cpu=cpucard1+cpucard2;
cout<<"\t\t\t你的点数为:"<
cout<<"["<
cout<<"[*]"<<"["<
}
//---------------------------------------------------------------------
void game::deal2(float &player,float &cpu,int &playerturns,int &cputurns)
{
do{
cout<<"\t\t\t你想继续要牌么?";
cin>>flag;
if(((flag=='o')||(flag=='O'))&&(playerturns<5)) //判断是否高级玩家
{
do
{
card=random(13,1);
if(card>10)
{
card=0.5;
}
cout<<"\t\t\t下张牌是:"<
if((flag=='p')||(flag=='P'))
{
playerturns++;
player=player+card;
cout<<"\t\t\t你现在的点数是:"<
if(playerturns>=5)
{
cout<<"\t\t\t\n你的牌已经到了五张,不能再要牌了"<
}while(flag=='p');
}
else if((flag=='y')||(flag=='Y')) //此处设置普通玩家
{
playerturns++;
if(player>21)
{
cout<<"\t\t\t你暴庄了,你输了";
lose++;
replay(flag);
}
if(playerturns>5)
{
cout<<"\t\t\t你的牌已经到了五张,不能再要牌了"<
else if((playerturns<6)&&((flag=='y')||(flag=='Y')))
{
cout<
}
}
}while((flag=='y')||(flag=='Y'));
cout<<"\t\t\t你想追加赌注么?"<
if((flag=='y')||(flag=='Y'))
{
cout<<"\t\t\t请输入你想追加的赌注:";
do{
cin>>card;
if(money==0) //判断是否能追加赌注
{
cout<<"\t\t\t你已经没有赌注了,电脑开始要牌。"<
}
if((card)>money)
{
cout<<"\t\t\t你的赌注不够。"<
}while((card)>money);
bet=bet+card;
money=money-card;
}
for(;(cpu<16)&&(cputurns<5);cputurns++) //给电脑发牌
{
cout<
if(cpu>21)
{
cpu=cpu-card;
break;
}
}
cout<
{
special(player,cpu);
}
results(player,cpu,bet,money,draw,wins,lose);
replay(flag);
print(wins,lose,draw,money);
cout<
//---------------------------------------------------------------------
void game::hit(float &total) //分牌函数
{
float card;
card=random(13,1);
if(card>=10)
{
card=0.5;
}
total=total+card;
cout<<"\t\t\t要的牌是:"<
//---------------------------------------------------------------------
void game::pause()
{
cout<<"按任意键继续"<
}
//---------------------------------------------------------------------
void game::wait(int milli)
{
clock_t start; //调用TIME.H内函数
start=clock();
while((clock()-start)
}
//---------------------------------------------------------------------
float game::random(long high,long low) //得到随机数
{
float ran;
srand((unsigned)time(NULL));
ran= rand()%(high-(low-1))+low;
return(ran);
}
//---------------------------------------------------------------------
void game::replay(char &flag)
{
cout<<"\n\t\t\t你想继续玩这个游戏么:";
cin>>flag;
while((flag=='y')||(flag=='Y')) //此处设置游戏循环
{
BET(bet,money);
deal(player,cpu,playerturns,cputurns);
}
print(wins,lose,draw,money);
}
//---------------------------------------------------------------------
void game::print(int wins,int lose,int draw,int money) //输出结果
{
cout<<"\t\t\t赢:"<
}
//---------------------------------------------------------------------
void game::results(float player,float cpu,int bet,int &money,int &draw,int &wins,int &lose)
{
if(cpu==player) //输出结果
{
cout<<"\t\t\t点数相同,庄家赢"<
}
if(player>21)
{
cout<<"\t\t\t你暴庄了,加油"<
}
else
{
if(cpu
cout<<"\n\t\t\t恭喜,你赢了";
money=money+(bet*2);
wins++;
}
}
if(cpu>21)
{
cout<<"\t\t\t庄家加油"<
{
cout<<"\n\t\t\t你赢了";
wins++;
money=money+(bet*2);
}
}
else
{
if(cpu>player)
{
cout<<"\t\t\t你输了"<
}
}
}
//---------------------------------------------------------------------
void game::sign()
{
cout<<"\n\n\n\n\t\t\t新手第一次写,多多指教"<
}
//---------------------------------------------------------------------
void game::special(float &player,float &cpu)
/*如果要五张牌还不超过21点而对方没到五张牌,那就算赢.如果两个人都五张牌且都没
有超过21点,那点数小的赢.*/
{
if((player<=21)&&(cpu<=21)&&(playerturns==6)&&(cputurns<5))
{
cout<<"\n\t\t\t恭喜,你赢了";
money=money+(bet*2);
wins++;
}
if((player<=21)&&(cpu<=21)&&(playerturns<6)&&(cputurns==5))
{
cout<<"\t\t\t你输了"<
}
if((player<=21)&&(cpu<=21)&&(playerturns==6)&&(cputurns==5))
{
if(player
cout<<"\n\t\t\t恭喜,你赢了";
money=money+(bet*2);
wins++;
}
else
{
cout<<"\t\t\t你输了"<
}
}
replay(flag);
}
//---------------------------------------------------------------------
void main()
{
game deck;
deck.rules();
}
//---------------------------------------------------------------------
经典的猜数字小游戏,简单明了,希望对你有帮助。
#include
#include
#include
#include
using namespace std;
#define NUM 4
// 随机产生4个数字,作为答案供使用者猜测
void answer(int *a, int s)
{
int i, j;
srand(time(NULL));
for (i = 0; i < s; ++i)
{
a[i] = rand()%10;
// 确保当前产生的随机数与前面产生的不同
for (j = 0; j < i; ++j)
if (a[i] == a[j])
{
a[i] = rand()%10;
j = -1;
continue;
}
}
}
void main(void)
{
int a[NUM], g[NUM]={0, 0, 0, 0}, i, j, n, x, y;
char choice;
answer(a, NUM);
cout << "***Welcome To This Little Game***" << endl << endl;
cout << "Please input a 4-digit number: " << endl;
while (1)
{
x = y = 0;
i = NUM-1;
do
{
cin >> n;
} while (n < 0 || n > 9999); // 判断用户输入的数字是否有效,无效重新接收输入
// 分解数字
while (n)
{
g[i--] = n%10; n /= 10;
}
// 验证输入
for (i = 0; i < NUM; ++i)
{
if (a[i] == g[i])
x += 1;
else
{
for (j = 0; j < NUM; ++j)
if (a[i] == g[j])
y += 1;
}
}
// 判断输入数字是否猜对
if (4 == x)
{
cout << "Congratulations!" << endl;
break;
}
else
{
cout << "Sorry, you haven't guess the right number!" << endl;
cout << x << 'A' << y << 'B' << endl;
cout << "Do you want to continue(y or n)?" << endl;
cin >> choice;
if (choice != 'y' && choice != 'Y')
{
cout << "Right number is:" << a[0] << a[1] << a[2] << a[3] << endl;
break;
}
}
}
cout << "exiting.." << endl;
}