#include <stdioh>
#include <stdlibh>
#include <timeh>
typedef struct poker
{
int num;
char color[7];
}node;
void creat(node card[4][13])
{
srand(1);
int i,j,flag=0;
node p;
for(i = 0; i < 4; i++)
for(j = 0; j < 13; j++)
{
do
{
card[i][j]num = rand()%52+1;
// 这里跟你改了一下,比较判断的时候用取地址的方式比较
// 简单。
for(p = card[0]; p < &card[i][j]; p++)
{
if((i==0) && (j==0))
break;
if(p->num == card[i][j]num)
{
flag = 1;
break;
}
}
// 这里很容易忘记取到正确的结果时重置flag
if ( &card[i][j] == p)
{
flag = 0;
}
}while (flag);
}
}
void main()
{
void creat(node card[4][13]);
node card[4][13];
int i,j;
creat(card);
for(i=0;i<4;i++)
for(j=0;j<13;j++)
{
printf("%d ",card[i][j]num);
if(j==12)
printf("\n");
}
}
首先你要明白flag这个变量是用来干什么的,它是用来标记是否要再随机取一个值,为1则表示要,为0表示不要再取值了。如果不加那段if语句将flag重置为0则会进入死循环,这就是为什么你停在那里不动的原因。
for(p = card[0]; p < &card[i][j]; p++)
这句话中是确定了p 不小于 &card[i][j],不小于不一定就是大于,还有等于的情况,其实p现在的值就是&card[i][j]了。
下次遇到这样的逻辑问题,你可以进行调试,用VC++6的调试方法就是按下 F10 然后你就是可以在main函数里看到一个箭头的东西,每按一次F10就会向下走一步,然后你可以看有一个叫做Varialbes的窗口,里面的值是会变化的,这是我们观察程序中变量变化的窗口。遇到函数,我们按下F11就可以进入函数中执行了,查看 调试窗口 和 调试 那里有更为详细的功能。
package zhidao;
import javautilArrays;
import javautilCollections;
import javautilComparator;
import javautilScanner;
/
发牌游戏
/
public class PokerGame
{
/
共有1幅扑克牌,不包括大王和小王共52张牌。
/
public static final int POKER_NUMBER = 52;
/
特殊分隔符 _
/
private static final String T = "_";
/
52张牌<br />
顺序为黑桃、红心、草花、方块
/
public static final String[] POKERS =
{
"A" + T + "0", "A" + T + "1", "A" + T + "2", "A" + T + "3",
"2" + T + "0", "2" + T + "1", "2" + T + "2", "2" + T + "3",
"3" + T + "0", "3" + T + "1", "3" + T + "2", "3" + T + "3",
"4" + T + "0", "4" + T + "1", "4" + T + "2", "4" + T + "3",
"5" + T + "0", "5" + T + "1", "5" + T + "2", "5" + T + "3",
"6" + T + "0", "6" + T + "1", "6" + T + "2", "6" + T + "3",
"7" + T + "0", "7" + T + "1", "7" + T + "2", "7" + T + "3",
"8" + T + "0", "8" + T + "1", "8" + T + "2", "8" + T + "3",
"9" + T + "0", "9" + T + "1", "9" + T + "2", "9" + T + "3",
"10" + T + "0", "10" + T + "1", "10" + T + "2", "10" + T + "3",
"J" + T + "0", "J" + T + "1", "J" + T + "2", "J" + T + "3",
"Q" + T + "0", "Q" + T + "1", "Q" + T + "2", "Q" + T + "3",
"K" + T + "0", "K" + T + "1", "K" + T + "2", "K" + T + "3"
};
/
原始的扑克牌
/
public static final String[] ORIAL_POKERS = POKERSclone ();
/
发到哪张牌 0
/
public static int currentIndex = 0;
/
四种花色
/
public static final String POKER_COLOR = "黑桃" + T + "红心" + T + "草花" + T + "方块";
/
发牌
@param n int 一共几个人玩牌
@param results 储存发牌结果的数组
/
public static void drawPoker (int n, String[][] results)
{
int m = n - 1;
if (n == 0)
{
return;
}
int number = (POKER_NUMBER - currentIndex) / n;
while (number > 0)
{
String current = POKERS[currentIndex++];
String pk = currentsplit ("\\" + T)[0]trim ();
int col = IntegerparseInt (currentsplit ("\\" + T )[1]trim ());
if (null == results[m][col])
{
results[m][col] = pk + " ";
}
else
{
results[m][col] += pk + " ";
}
number--;
}
for ( int i = 0; i < resultslength; i++ )
{
for ( int j = 0; j < results[i]length; j++ )
{
if (null == results[i][j])
{
results[i][j] = "";
}
}
}
drawPoker (--n, results);
}
/
给牌排序
@param orialPokers String 原始扑克牌
@param temp String[] 每张花色的扑克牌数组
/
private static void sortPokers ( final String orialPokers, String[] temp )
{
Arrayssort (temp, new Comparator<String> ()
{
@Override
public int compare ( String o1, String o2 )
{
int index1 = orialPokersindexOf (o1 + T);
int index2 = orialPokersindexOf (o2 + T);
if (index1 > index2)
{
return -1;
}
else if (index1 < index2)
{
return 1;
}
else
{
return 0;
}
}
});
}
/
洗牌
/
private static void shufflePokers ( String[] POKERS )
{
Collectionsshuffle (ArraysasList (POKERS));
}
public static void main ( String[] args )
{
Systemoutprint ("输入人数:");
Scanner scanner = new Scanner (Systemin);
int n = scannernextInt ();
scannerclose ();
String[] pColors = POKER_COLORsplit ("\\" + T );
String[][] results = new String[n][pColorslength];
shufflePokers (POKERS);
drawPoker (n, results);
final String poker = ArraystoString (ORIAL_POKERS);
Systemoutprintln ("\n输出如下:\n");
for (int i = 0; i < resultslength; i++)
{
Systemoutprintln ("第" + ( i + 1 ) + "个人:");
for ( int j = 0; j < results[i]length; j++ )
{
String[] temp = results[i][j]trim ()split ("\\s+");
sortPokers (poker, temp);
Systemoutprintln (pColors[j] + ":" + ArraystoString (temp)replaceAll ("[\\,\\[\\]]", ""));
}
Systemoutprintln ();
}
}
}
#include<stdlibh>
int pokers[53];
int array[502];
int shiwei;
int gewei;
int rseed;
int i=0;
int j=0;
void fun(int n) //用于将编号对应为牌面输出
{ switch(n%100 )
{
case 1: cout<< ‘A’<<’\t’;break ;
case 2: cout<< 2<<’\t’;break ;
case 3: cout<< 3<<’\t’;break ;
case 4: cout<< 4<<’\t’;break ;
case 5: cout<< 5<<’\t’;break ;
case 6: cout<< 6<<’\t’;break ;
case 7: cout<< 7<<’\t’;break ;
case 8: cout<< 8<<’\t’;break ;
case 9: cout<< 9<<’\t’;break ;
case 10: cout<< 10<<’\t’;break ;
case 11: cout<< ‘J’<<’\t’;break ;
case 12: cout<< ‘Q’<<’\t’;break ;
case 13: cout<< ‘K’<<’\t’;break ;
}
}
int main(){
//随机产生一副牌
while(true)
{
shiwei = rand()% (4) + 1;//产生十位数
gewei = rand()%(12)+1;//产生个位数
if(shiwei==5){
gewei = rand()%(1)+1;
}
rseed= shiwei100+gewei;//用于产生一个101 ~ 502 间的机选整数
if(array[rseed]==1)
continue;
else{
array[rseed]==1;
pokers[rseed]==rseed;
i++;
}
if(i==54)
break;
}
for(j=0;j<=53;j++){
switch(pokers[j]/100)
{
case 5 :if(pokers[i]%500==1)
cout<<(char)1<<’\t’;
else cout<<(char)2<<’\t’
break ;
case 1: cout<<(char)6; fun( pokers[ i ]);break;
case 2: cout<<(char)3; fun( pokers[ i ]);break;
case 3: cout<<(char)5; fun( pokers[ i ]);break;
case 4: cout<<(char)4; fun( pokers[ i ]);break;
}
}
}
#include <stdioh>
#include <stdlibh>
#include <timeh>
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;
}
以前无聊写的 给你作参考吧
以上就是关于c语言 洗牌程序全部的内容,包括:c语言 洗牌程序、java扑克牌洗牌程序,求抽可以抽出特定牌的次数、设计一电脑洗牌程序,将54张扑克牌等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)