作者在 2011-07-26 09:10:13 发布以下内容
/*21点
计算电脑是否要牌100*(21-n)/21 + rand()%50 >=50 ? 要牌 :不要
*/
#include <stdio.h>
#include <time.h>
#include <string.h>
typedef enum point {ace=1,two,three,four,five,six,seven,eight,nine,ten,jack,queen,king}point;
point puker[52];
typedef struct player{
char name[16];
point pk[11];
int num;
int flag;
int sign;
}player;
player computer,people;
int cnt = 0;
void initial(player *com, player *peo) //初始化玩家
{
int i;
int j = 0;
for(i=0; i<52; i++)
{
j++;
if(j>13) j = 1;
puker[i] = j;
}
for(i=0; i<11; i++)
{
com->pk[i] = 0;
peo->pk[i] = 0;
}
strcpy(com->name,"computer");
com->flag = 0;
com->sign = 0;
peo->flag = 0;
peo->sign = 0;
}
/* 洗牌,随机抽取两张牌,交换位置,执行1000次 */
void shuffle() //洗牌
{
srand(time(NULL));
int i,j,k;
point temp;
for(i=0;i<1000;i++)
{
j=rand()%52;
k=rand()%52;
temp=puker[j];
puker[j]=puker[k];
puker[k]=temp;
}
}
void distribute(player *a) //发牌
{
int m;
for(m=0;m<11;m++)
{
if(a->pk[m]==0)
{
a->pk[m]=puker[cnt];
if(strcmp(a->name,"computer")!=0)
{
switch(a->pk[m])
{
case 11:printf("%s拿到%c\n",a->name,'J');break;
case 12:printf("%s拿到%c\n",a->name,'Q');break;
case 13:printf("%s拿到%c\n",a->name,'K');break;
default:printf("%s拿到%d\n",a->name,a->pk[m]);break;
}
}
break;
}
}
cnt++; //发一张牌后,指向后面的一张牌
}
void complay(player *p) //电脑玩牌
{
if(p->flag==0) //电脑目前还未决定是否继续要牌
{
p->num=0;
int i;
for(i=0; i<11; i++) //任何一方拿到的最多的牌数未11张
{
p->num+=p->pk[i];
}
if(p->num>21) //电脑点数大于21,函数返回
{
p->sign=1;
return;
}
int k;
k=(100*(21-p->num)/21+rand()%50); //产生随机数,计算是否要牌
if(k>=50)
{
printf("computer要牌\n");
distribute(p);
}
else if(k<50)
{
printf("computer不要牌\n");
p->flag=1;
}
}
else //已经确定电脑不要牌了
{
printf("computer不要牌\n");
}
}
void peoplay(player *p) //玩家玩牌
{
if (p->flag == 0)
{
int i;
p->num=0; //计算玩家点数
for(i=0; i<11; i++)
{
p->num += p->pk[i];
}
printf("%s现在有 %d点\n",p->name,p->num);
if(p->num>21) //玩家点数大于21点,函数返回
{
p->sign=1;
return;
}
char a;
printf("******是否继续要牌? y/n \n====================");
scanf("%c",&a);
scanf("%*[^\n]");scanf("%*c"); //清空输入缓冲区
if(a=='y') //玩家要牌
{
distribute(p);
}
else if(a!='y')
{
printf("不要牌\n");
p->flag=1;
}
}
else if(p->flag==1) //玩家不要牌
{
printf("%s不要牌\n",p->name);
}
}
int main()
{
printf("请输入玩家的姓名:");
scanf("%s",people.name);
scanf("%*[^\n]");scanf("%*c"); //清空输入缓冲区
initial(&computer, &people);
shuffle();
distribute(&people); //第一次给玩家发牌
distribute(&computer); //第一次给电脑发牌
printf("给computer发牌\n");
do
{
peoplay(&people); //电脑玩牌
if (people.sign == 1) break;
complay(&computer); //电脑玩牌
if (computer.sign == 1) break;
}while(computer.flag==0 || people.flag==0); //循环终止条件是电脑爆了或电脑、玩家有都不要牌了
if(people.sign==1) //玩家爆了
{
printf("%s现在有%d点,%s现在有%d点\n",computer.name,computer.num,people.name,people.num);
printf("%s爆了\n",people.name);
}
else if(computer.sign==1) //电脑爆了
{
printf("%s现在有%d点,%s现在有%d点\n",computer.name,computer.num,people.name,people.num);
printf("computer爆了\n");
}
else if(computer.num==people.num) //都没爆而且点数相同
{
printf("%s现在有%d点,%s现在有%d点\n",computer.name,computer.num,people.name,people.num);
printf("平局!\n");
}
else //都没爆,谁点数大谁赢
{
printf("%s现在有%d点,%s现在有%d点\n",computer.name,computer.num,people.name,people.num);
printf("%s胜利了\n",(computer.num>people.num ? computer.name : people.name));
}
return 0;
}
计算电脑是否要牌100*(21-n)/21 + rand()%50 >=50 ? 要牌 :不要
*/
#include <stdio.h>
#include <time.h>
#include <string.h>
typedef enum point {ace=1,two,three,four,five,six,seven,eight,nine,ten,jack,queen,king}point;
point puker[52];
typedef struct player{
char name[16];
point pk[11];
int num;
int flag;
int sign;
}player;
player computer,people;
int cnt = 0;
void initial(player *com, player *peo) //初始化玩家
{
int i;
int j = 0;
for(i=0; i<52; i++)
{
j++;
if(j>13) j = 1;
puker[i] = j;
}
for(i=0; i<11; i++)
{
com->pk[i] = 0;
peo->pk[i] = 0;
}
strcpy(com->name,"computer");
com->flag = 0;
com->sign = 0;
peo->flag = 0;
peo->sign = 0;
}
/* 洗牌,随机抽取两张牌,交换位置,执行1000次 */
void shuffle() //洗牌
{
srand(time(NULL));
int i,j,k;
point temp;
for(i=0;i<1000;i++)
{
j=rand()%52;
k=rand()%52;
temp=puker[j];
puker[j]=puker[k];
puker[k]=temp;
}
}
void distribute(player *a) //发牌
{
int m;
for(m=0;m<11;m++)
{
if(a->pk[m]==0)
{
a->pk[m]=puker[cnt];
if(strcmp(a->name,"computer")!=0)
{
switch(a->pk[m])
{
case 11:printf("%s拿到%c\n",a->name,'J');break;
case 12:printf("%s拿到%c\n",a->name,'Q');break;
case 13:printf("%s拿到%c\n",a->name,'K');break;
default:printf("%s拿到%d\n",a->name,a->pk[m]);break;
}
}
break;
}
}
cnt++; //发一张牌后,指向后面的一张牌
}
void complay(player *p) //电脑玩牌
{
if(p->flag==0) //电脑目前还未决定是否继续要牌
{
p->num=0;
int i;
for(i=0; i<11; i++) //任何一方拿到的最多的牌数未11张
{
p->num+=p->pk[i];
}
if(p->num>21) //电脑点数大于21,函数返回
{
p->sign=1;
return;
}
int k;
k=(100*(21-p->num)/21+rand()%50); //产生随机数,计算是否要牌
if(k>=50)
{
printf("computer要牌\n");
distribute(p);
}
else if(k<50)
{
printf("computer不要牌\n");
p->flag=1;
}
}
else //已经确定电脑不要牌了
{
printf("computer不要牌\n");
}
}
void peoplay(player *p) //玩家玩牌
{
if (p->flag == 0)
{
int i;
p->num=0; //计算玩家点数
for(i=0; i<11; i++)
{
p->num += p->pk[i];
}
printf("%s现在有 %d点\n",p->name,p->num);
if(p->num>21) //玩家点数大于21点,函数返回
{
p->sign=1;
return;
}
char a;
printf("******是否继续要牌? y/n \n====================");
scanf("%c",&a);
scanf("%*[^\n]");scanf("%*c"); //清空输入缓冲区
if(a=='y') //玩家要牌
{
distribute(p);
}
else if(a!='y')
{
printf("不要牌\n");
p->flag=1;
}
}
else if(p->flag==1) //玩家不要牌
{
printf("%s不要牌\n",p->name);
}
}
int main()
{
printf("请输入玩家的姓名:");
scanf("%s",people.name);
scanf("%*[^\n]");scanf("%*c"); //清空输入缓冲区
initial(&computer, &people);
shuffle();
distribute(&people); //第一次给玩家发牌
distribute(&computer); //第一次给电脑发牌
printf("给computer发牌\n");
do
{
peoplay(&people); //电脑玩牌
if (people.sign == 1) break;
complay(&computer); //电脑玩牌
if (computer.sign == 1) break;
}while(computer.flag==0 || people.flag==0); //循环终止条件是电脑爆了或电脑、玩家有都不要牌了
if(people.sign==1) //玩家爆了
{
printf("%s现在有%d点,%s现在有%d点\n",computer.name,computer.num,people.name,people.num);
printf("%s爆了\n",people.name);
}
else if(computer.sign==1) //电脑爆了
{
printf("%s现在有%d点,%s现在有%d点\n",computer.name,computer.num,people.name,people.num);
printf("computer爆了\n");
}
else if(computer.num==people.num) //都没爆而且点数相同
{
printf("%s现在有%d点,%s现在有%d点\n",computer.name,computer.num,people.name,people.num);
printf("平局!\n");
}
else //都没爆,谁点数大谁赢
{
printf("%s现在有%d点,%s现在有%d点\n",computer.name,computer.num,people.name,people.num);
printf("%s胜利了\n",(computer.num>people.num ? computer.name : people.name));
}
return 0;
}