当前位置:首页 » 编程语言 » c语言随机抽取一张扑克牌
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言随机抽取一张扑克牌

发布时间: 2022-06-25 04:31:28

A. 求高手做c语言~ 扑克牌游戏 非常急

这个问北大清华的都不一定会

麻烦采纳,谢谢!

B. C语言54扑克问题

这个游戏要保证先手必胜,先手方只需要每次取牌以后让剩余的牌数为5的整数倍+1就可以了,这样对手无论如何都只能拿到最后一张。以下代码VS2019测试通过。

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

// 电脑取牌,返回张数 1~4
int ComputerChoice(int CardsRemain);

// 人取牌,返回张数 1~4
int HumanChoice(int CardsRemain);

// 游戏函数
void Game(int CardsRemain, bool HumanFirst);

void main()
{
int CardsRemain; // 剩余张数
bool HumanFirst; // 人先拿 或 电脑先拿
char c = '/0'; // 先手选择

// 显示游戏规则
printf("游戏规则:\n\n");
printf("54张牌,人和电脑轮流抽,1到4张任选,抽到最后一张牌者败。\n\n");
printf("按任意键继续...\n");
getch();

while (1)
{
CardsRemain = 54;

// 确定谁先拿牌
while (1)
{
system("cls");
printf("\n让电脑先拿牌吗?(Y/N)");
c = getch();
if ((c == 'Y') || (c == 'y'))
{
HumanFirst = false;
break;
}
if ((c == 'N') || (c == 'n'))
{
HumanFirst = true;
break;
}
}
// 开始游戏
Game(CardsRemain, HumanFirst);
getch();
// 再来一次?
while (1)
{
system("cls");
printf("\n\n不服!再来一次?(Y/N)");
c = getch();
if ((c == 'Y') || (c == 'y'))
break;
if ((c == 'N') || (c == 'n'))
return;
}
}
}

// 电脑取牌
int ComputerChoice(int CardsRemain)
{
int i;

// 拿掉n张,使得剩下张数为 5 的整数倍+1,必胜
switch (CardsRemain % 5)
{
case 1:
while (1) // 拿不到最佳张数,就在 1~4 张之间随机拿, 要确保电脑所拿张数不大于剩余张数
{
i = rand() % 4 + 1;
if (i <= CardsRemain)
return i;
}
case 0: return 4;
default: return (CardsRemain % 5) - 1;
}
}

// 人取牌
int HumanChoice(int CardsRemain)
{
int i;

while (1)
{
printf("\n请输入要拿掉的张数[1~4]:");
scanf("%d", &i);
if (i > CardsRemain)
continue;
if ((i >= 1) && (i <= 4))
return i;
}
}

// 游戏函数
void Game(int CardsRemain, bool HumanFirst)
{
int LastComChoice = 0; // 电脑 上轮拿牌张数
int LastManChoice = 0; // 人 上轮拿牌张数
int Rounds = 0; // 回合数
bool HumansTurn = HumanFirst; // 本轮取牌者是 人 还是 电脑。初始值由 HumanFirst 决定

// 初始化随机数
srand(time(0));

system("cls");

// 取牌循环,直到剩下张数为 1
while (CardsRemain > 0)
{
if (HumansTurn) // 轮到人拿牌
{
// 想让电脑互P,把这里 HumanChoice 改成 ComputerChoice, 相应输出文字改成电脑1、2就可以了。
LastManChoice = HumanChoice(CardsRemain);
CardsRemain -= LastManChoice;
printf("\n你拿走 %d 张,剩 %d 张。\n", LastManChoice, CardsRemain);
if (CardsRemain == 0)
{
printf("\n\n你拿到最后 1 张牌,你输了! 按任意键结束游戏...");
getch();
return;
}
}
else // 轮到电脑拿牌
{
LastComChoice = ComputerChoice(CardsRemain);
CardsRemain -= LastComChoice;
printf("\n电脑拿走 %d 张,剩 %d 张。\n", LastComChoice, CardsRemain);
if (CardsRemain == 0)
{
printf("\n\n电脑拿到最后 1 张牌,电脑负! 按任意键结束游戏...");
getch();
return;
}
}

HumansTurn = !HumansTurn; // 更换拿牌者
Rounds++;

if ((Rounds % 2) == 0) // 双方各取一次牌为 1 回合
{
if (HumanFirst)
printf("\n第 %d 轮, 人类:%d张 电脑:%d张 剩下:%d张\n", Rounds / 2, LastManChoice, LastComChoice, CardsRemain);

else
printf("\n第 %d 轮, 电脑:%d张 人类:%d张 剩下:%d张\n", Rounds / 2, LastComChoice, LastManChoice, CardsRemain);

printf("--------------------------------------------------\n");
}
}
}

C. 用C语言编程扑克牌搓点游戏,急!

给你修改好了。20分,有点少了,呵呵。
/*游戏:扑克牌搓点游戏
规则:您将随机抽取其中两张和电脑进行对抗,
2张牌相加,个位大的胜出,其中对子比单牌大,
若都是对子,对子大的胜出。
您可以根据提示下注,起始资金均为1000元,
当一方财产小于0时,宣布破产,另一方胜出。
作者:于吉祥
日期:20090220
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_M 1000
enum colour
{
HEI = 0,
HONG,
MEI,
FANG,
};
void display(int number , int sign)
{
printf("%s\n","╭——╮");
switch(number)
{
case 0:
printf("%s\n","│0 │");
break;
case 1:
printf("%s\n","│A │");
break;
case 2:
printf("%s\n","│2 │");
break;
case 3:
printf("%s\n","│3 │");
break;
case 4:
printf("%s\n","│4 │");
break;
case 5:
printf("%s\n","│5 │");
break;
case 6:
printf("%s\n","│6 │");
break;
case 7:
printf("%s\n","│7 │");
break;
case 8:
printf("%s\n","│8 │");
break;
case 9:
printf("%s\n","│9 │");
break;
case 10:
printf("%s\n","│10 │");
break;
case 11:
printf("%s\n","│J │");
break;
case 12:
printf("%s\n","│Q │");
break;
case 13:
printf("%s\n","│K │");
break;
default:
printf("error");
break;
}
// printf("%s\n","│ │");
printf("%s","│ ");
switch(sign)
{
case HEI:
printf("%c",06);
break;
case HONG:
printf("%c",03);
break;
case MEI:
printf("%c",05);
break;
case FANG:
printf("%c",04);
break;
default:
break;
}
printf("%s\n"," │");
printf("%s\n","│ │");
printf("%s\n","╰——╯");
}

int compare(int x[] , int y[])
{
int sign,a,b;
if((x[0] == x[1])&&(y[0] == y[1]))
{
if(x[0] == y[0])
sign = 0;
else if(x[0] > y[0])
sign = 1;
else
sign = -1;
}
else if(x[0] == x[1])
sign = 1;
else if(y[0] == y[1])
sign = -1;
else
{
a = (x[0]+x[1])%10;
b = (y[0]+y[1])%10;
if(a == b)
sign = 0;
else if(a > b)
sign = 1;
else
sign = -1;
}
return sign;
}

void main()
{
int i,chip;
int Per[2],Com[2];
int Mon_Per = MAX_M , Mon_Com = MAX_M;
int Colour_Per[2] , Colour_Com[2];
printf("扑克牌搓点游戏\n");
printf("---------------\n");
system("pause");

while(1)
{
system("cls");
if(Mon_Per <=0)
{
printf("您已身无分文,游戏退出!");
break;
}
if(Mon_Com <=0)
{
printf("电脑已经被你赢光了,恭喜你获胜!");
break;
}

srand( time(NULL) );
for(i=0;i<2;i++)
{
Per[i] = rand()%13+1;
Colour_Per[i] = rand()%4;
Com[i] = rand()%13+1;
Colour_Com[i] = rand()%4;
}

printf("当前余额:你(%d),电脑(%d)\n",Mon_Per,Mon_Com);
// printf("牌已经抽取,你抽到的牌为:%d %d\n",Per[0],Per[1]);
printf("牌已经抽取,你抽到的牌为:\n");
for(i=0;i<2;i++)
display(Per[i],Colour_Per[i]);

printf("请下注:");
RET: scanf("%d",&chip);
if(chip>500)
{
printf("最大可下注为500,请重新下注!\n");
goto RET;
}
Mon_Per -= chip;
Mon_Com -= chip;
if(Mon_Per < 0)
{
printf("您的余额不足,请重新下注!");
Mon_Per += chip;
Mon_Com += chip;
goto RET;
}
if(Mon_Com < 0)
{
printf("电脑余额不足,请重新下注!");
Mon_Per += chip;
Mon_Com += chip;
goto RET;
}

switch(compare(Per,Com))
{
case 0:
printf("平局!");
Mon_Per += chip;
Mon_Com += chip;
break;
case 1:
printf("你赢了!");
Mon_Per += 2*chip;
break;
case -1:
printf("电脑赢了!");
Mon_Com += 2*chip;
break;
default:
printf("系统出错!");
break;
}
// printf("电脑抽到的牌为:%d %d\n",Com[0],Com[1]);
printf("电脑抽到的牌为:\n");
for(i=0;i<2;i++)
display(Com[i],Colour_Com[i]);

system("pause");

}
printf("游戏结束!");
system("pause");
}

D. 编写C语言程序,模拟将一副扑克牌54张随机分给4个人 (如何表达不同花色和大小,如何高效洗牌)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int poke[13][4],joker[2],last_four_card[4]; //定义四类十三张牌、双王
int i,j,card,count,tank[54],player[20],computer[2][20];
bool flag;
srand(time(NULL)); //初始化随机种子
for(i=0;i<54;i++)
tank[i]=-1; //初始化容器
for(i=0;i<54;i++)
{
flag=1;
while(flag)
{
flag=0;
card=rand()%54;
for(j=0;j<=i;j++)
if(card==tank[j]) flag=1;
}
tank[i]=card;
}

count=0;
for(i=0;i<54;i++)
{
if(tank[i]==52) printf("jok ");
else if(tank[i]==53) printf("JOK ");
else if(tank[i]%13==0) printf("%c%c ",tank[i]/13+3,'A');
else if(tank[i]%13==10) printf("%c%c ",tank[i]/13+3,'J');
else if(tank[i]%13==11) printf("%c%c ",tank[i]/13+3,'Q');
else if(tank[i]%13==12) printf("%c%c ",tank[i]/13+3,'K');
else if(tank[i]%13<10) printf("%c%-2d ",tank[i]/13+3,tank[i]%13+1);
count++;
if(count%20==0) printf("\n");
}
count=0;
for(i=0;i<16;i++)
{
player[i]=tank[i];
computer[0][i]=tank[i+16];
computer[1][i]=tank[i+32];
}
for(i=0;i<4;i++)
last_four_card[i]=tank[53-i];
printf("\n\n你的牌是:\n\n");
for(i=0;i<16;i++)
{
if(player[i]==52) printf("jok ");
else if(player[i]==53) printf("JOK ");
else if(player[i]%13==0) printf("%c%c ",player[i]/13+3,'A');
else if(player[i]%13==10) printf("%c%c ",player[i]/13+3,'J');
else if(player[i]%13==11) printf("%c%c ",player[i]/13+3,'Q');
else if(player[i]%13==12) printf("%c%c ",player[i]/13+3,'K');
else if(player[i]%13<10) printf("%c%-2d ",player[i]/13+3,player[i]%13+1);
count++;
if(count%20==0) printf("\n");
}
getchar();
getchar();
return 0;
}

以前无聊写的 给你作参考吧

E. C语言编程题:扑克牌排序问题

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

typedefstructcard{
inttype;//type0:梅花,1:方片,2:红桃,3:黑桃,4:王
intpoint;//type=0--3时,point=2--14,type=4时,point=0--1
}CARD;

voidsort(CARDa[],intn){
inti,j,k;
CARDt;
for(i=0;i<n-1;++i){
k=i;
for(j=i+1;j<n;++j){
if(a[k].point>a[j].point)
k=j;
}
if(k!=i){
t=a[k];
a[k]=a[i];
a[i]=t;
}
}
}

voidshow(CARDa[],intn){
inti;
chartype[4][5]={"梅花","方片","红桃","黑桃"};
charpoint[]="JQKA";
for(i=0;i<n;++i){
if(a[i].type>=0&&a[i].type<=3){
printf("%s",type[a[i].type]);
if(a[i].point<11)printf("%d",a[i].point);
elseprintf("%c",point[a[i].point-11]);
}
else{
if(a[i].point)printf("大王");
elseprintf("小王");
}
}
printf(" ");
}

intHas(CARDa[],intn,CARDCard){
inti;
for(i=0;i<n;++i){
if(a[i].type==Card.type&&a[i].point==Card.point)
return1;
}
return0;
}

intmain(){
CARDarr[5],t;
inti,n=5,m;
srand((unsigned)time(NULL));
for(i=0;i<n;++i){
t.type=rand()%5;
if(t.type==4)t.point=rand()%2;
elset.point=rand()%13+2;
if(Has(arr,i,t))--i;
elsearr[i]=t;
}
m=n;
for(i=0;i<m;++i){//如果有大王,则排在最后
if(arr[i].type==4&&arr[i].point){
t=arr[i];
arr[i]=arr[m-1];
arr[m-1]=t;
--m;
}
}
for(i=0;i<m;++i){//如果有小王,也排在最后
if(arr[i].type==4){
t=arr[i];
arr[i]=arr[m-1];
arr[m-1]=t;
--m;
}
}
sort(arr,m);
show(arr,n);
return0;
}

F. C语言怎么取一个数段的随机数

srand(time(0));
int i,n,data;
n=5;
for( i=0;i<n;i++ )
{
data=rand()%54+1; //得到一个1-54之间的数
printf("%d\n", data ); //1-54分别代表不同的牌,可以约定

}

G. 用简单c语言,随机输出17张扑克牌,包括花色。大小王也要随机。

1、C语言提供了一些库函数来实现随机数的产生。C语言中有三个通用的随机数发生器,分别为 rand函数, random函数, randomize 函数
但是rand函数产生的并不是真意正义上的随机数,是一个伪随机数,是根据一个数,称之为种子,为基准以某个递推公式推算出来的一系数,当这系列数很大的时候,就符合正态公布,从而相当于产生了随机数,但这不是真正的随机数,当计算机正常开机后,这个种子的值是定了的,除非破坏了系统,为了改变这个种子的值,C提供了srand()函数,它的原形是void srand( int a)。
在调用rand函数产生随机数前,必须先利用srand()设好随机数种子,如果未设随机数种子,rand()在调用时会自动设随机数种子为1。一般用for语句来设置种子的个数。


2、例程:

#include"stdio.h"
#include<stdlib.h>
#include<math.h>
intmain()
{
charstr[13][5]={{"A"},{"2"},{"3"},{"4"},{"5"},{"6"},{"7"},{"8"},{"9"},{"10"},{"J"},{"Q"},{"K"}};
intb[]={1,2,3,4};//1红桃2黑桃3放片4梅花
charstr1[5][10]={{"红桃"},{"黑桃"},{"方块"},{"梅花"},{}};
srand(NULL);
inta,c,i;
intarr[17][2]={0};//记录17个数字a[i][0]表示数字a[i][1]表示颜色一共54张牌,4张A--K1大王1小王1-4=>A5-8=>253=>小王54大王,产生不重复的17个数字
intk=0;
while(k<17)
{
a=rand()%54+1;//a<53时候a=(a-1)/4;//1-4=>05-8==>1
//printf("%3d",a);
if(a<53)
{
a=(a-1)/4;
c=rand()%4;
for(i=0;i<17;i++)
{
if(a==arr[i][0]&&c==arr[i][1])
break;
}
if(i<17)
{
arr[k][0]=a;
arr[k][1]=c;
k++;
}
}
if(a==53||a==54)
{
for(i=0;i<17;i++)
{
if(a==arr[i][0])
break;
}
if(i<17)
{
arr[k][0]=a;
arr[k][1]=4;
k++;
}

}
}
for(i=0;i<17;i++)
{
//printf("%s%s ",str1[arr[i][1]],str[arr[i][0]]);
printf("%d",arr[i][0]);
}
}

H. 猜扑克牌的C语言程序设计,急急急!!

作业要自己做呀~~~呵呵,给你一个吧,学C++的时候自己做的一道课后习题。
代码给你拷上来,一些不必要的部分你自己改一下就可以了。
当时随便做的,还可以再进一步优化的。

另:回楼上的,这题主要是三次排序的问题。第一次选择一个行数,然后把那行的元素分成三份到每一行去,第二次再选一行,那么把那一行的第一次就有的三个元素再分布到每行中去,这样,最后每行只剩下一个两次都选择了的元素。那么最后再选一行答案就是唯一的了。

#include<iostream>
#include<string>
#include<time.h>
using namespace std;
namespace variable
{
int used;
static int i;
}
namespace array
{
int used[27]={0};
}

/*自定义函数部分*/
void drawline(int n)//画线函数
{
if(n==1)
cout<<"----------------------------";
else
cout<<"-------------------------------------------------------------------------------";
}
//打印版权信息~~嘿嘿~~~~~自己写的程序~~~标个版权~~
void print(){
int short_line(1);
drawline(short_line);
cout<<"Copyright(c) SehDan.Lee";
drawline(short_line);
cout<<endl;
}
//函数:为名为poker的string类型数组赋初值,作用:生成一副扑克牌
void push_poker(string *str,int len)
{
string list[15];
for(int i=3;i<18;i++)
switch(i)
{
case 10:list[i-3]="10";break;
case 11:list[i-3]='J';break;
case 12:list[i-3]='Q';break;
case 13:list[i-3]='K';break;
case 14:list[i-3]='A';break;
case 15:list[i-3]='2';break;
case 16:list[i-3]="KING2";break;
case 17:list[i-3]="KING1";break;
default:list[i-3]=i+48;
}
string type[4]={"a","b","c","d"};
i=0;
for(int j=0;j<13;j++)
{
for(int k=0;k<4;k++)
{
*(str+i)=type[k]+"-"+list[j];
i++;
}
}
for(i=0;i<2;i++)
*(str+len+i-2)=list[i+13];
}

//函数:检测生成的扑克牌是否已被使用
int chk_used(int *p,int i)
{
for(int j=0;j<27;j++)
{
if(*(p+j)==i)
{
return 1;
break;
}
}
return 0;
}

//函数:标记已使用的扑克牌
void used(int *p,int i)
{
*(p+variable::i)=i;
variable::i++;
}

//数组置零
void set_int_array(int *p,int len)
{
for(int i=0;i<len;i++)
*(p+i)=0;
}

//抽取扑克
void set_poker(string *str,string *p_poker,int n)
{

for(int i=0;i<n;i++)//随机抽取n张牌
{
srand((unsigned)time(NULL));//以系统时间为种子,产生不同的随机值
variable::used=rand()%54;
while(chk_used(array::used,variable::used)==1){variable::used=rand()%54;}//确保产生不重复的随机值
used(array::used,variable::used);//标记产生的随机值,确保下次不再出现
*(p_poker+i)=*(str+variable::used);
}
variable::i=0;
set_int_array(array::used,27);
}

//出题函数
void print_area(string *str,int times,int row,int row_2,int *r)
{
int p_row,i,n_list[27]={0};
for(i=0;i<27;i++)
n_list[i]=i;
if(times!=1)
{
//接下来处理第一次收集的row信息
int t;
for(i=0;i<3;i++)
{
t=n_list[i];
n_list[i]=n_list[(row-1)*9+i];
n_list[(row-1)*9+i]=t;
}
for(i=0;i<3;i++)
{
t=n_list[i+12];
n_list[i+12]=n_list[(row-1)*9+i+3];
n_list[(row-1)*9+i+3]=t;
}
for(i=0;i<3;i++)
{
t=n_list[i+24];
n_list[i+24]=n_list[(row-1)*9+i+6];
n_list[(row-1)*9+i+6]=t;
}
//第二次收集到的row信息(row_2)
if(times==3)
{
for(i=0;i<3;i++)
{
t=n_list[(row_2-1)*12+i];
n_list[(row_2-1)*12+i]=n_list[i*9];
n_list[i*9]=t;
}
for(i=0;i<27;i++)
*(r+i)=n_list[i];
}
}
for(i=0;i<27;i++)
{
if(i%9==0)
cout<<endl<<"Line "<<i/9+1<<":";
cout.width(8);
cout<<*(str+n_list[i]);
}
cout<<endl;
}
/*主函数*/
void main()
{
int again=1;
int times,row,row_2,row_last,r[27],long_line=0;
string poker[54],p_poker[27];
push_poker(poker,54);
while(again)
{
times=1,row=0,row_2=0,row_last=0;
print();
set_poker(poker,p_poker,27);
while(times!=4)
{
drawline(long_line);
print_area(p_poker,times,row,row_2,r);
drawline(long_line);
cout<<endl;
if(!row)
cout<<"Remember a card,and tell me what line it reside in(1/2/3):";
else
cout<<"What line the card you remembered reside in now(1/2/3):";
if(!row)
cin>>row;
else if(!row_2)
cin>>row_2;
times++;
}
int final;
cin>>final;
drawline(long_line);
cout<<"\nYour remembered card is: "<<p_poker[r[(final-1)*9]]<<endl;
cout<<"Donot believe it? Try again?(1 for yes,0 for no)";
cin>>again;
cout<<"\n\n\n";
if(again!=1&&again!=0)
{
cout<<"error!";
break;
}
}
}

I. 用C语言编写一个52张扑克牌随即洗牌的程序 并写上注释

定义一个int p[52],里面的元素就是1~52,然后,直接 for循环i:1~52,每次随机一个数 m=rand()%52 ,swap(p[i],p[m]),就ok了

J. c语言程序设计扑克牌游戏

定义一个结构类型表示一张牌,结构包含3个成员,第一个成员char:取值2,3~K,A表示牌名字,第二个成员int:取值2~14表示牌真实大小。第三个成员:结构链表指针。
写一个初始化函数,定义52大小的结构数组,成员值初值分别和牌对应,遍历数组并将每个元素的链表指针依次指向下一个元素地址。这样得到一个初始链表。(相当于一盒新牌)
所有涉及随机数都用rand函数,洗牌分四份就是循环取随机数m=1~n,n是随循环自减,初值52,直到n变成0。每随一次循环就从初始链表中遍历取出对应第m个节点,并从初始链表中将这个节点断开(既前一个节点指针直接指向后一个节点指针)。每取13张就组成一个新的链表。这样获得4个新链表分别表示4个玩家。
最后出牌就是分别遍历自己的链表,利用循环取牌比较结构数值大小。(取出的牌要从链表断开和上面一样,你把取出节点写成独立函数就能反复使用)。