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個玩家。
最後出牌就是分別遍歷自己的鏈表,利用循環取牌比較結構數值大小。(取出的牌要從鏈表斷開和上面一樣,你把取出節點寫成獨立函數就能反復使用)。