谁会c语言编程发牌洗牌程序
发布网友
发布时间:2022-05-24 12:07
我来回答
共2个回答
热心网友
时间:2023-10-12 18:56
这个文件包含了生成牌,发牌,洗牌等操作,不过是Java写的,但是程序语言和C还是有很多相通的,你知道了原理自然能自己做
/**
* 文件名:Card.java 2014-1-24 上午8:46:59
* @author Administrator
*/
package cc.icoc.javaxu.card.bean;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**扑克牌卡片类
* 创建时间: 2014-1-24 上午8:46:59
*
* @author 许仕永
* 项目名称: PucCard
* 文件名: Card.java
* 编码: GBK
* @Description:
* @JKD JDK 1.6.0_21
* @version v1.0
* @TODO
*/
public class Card implements Comparable
{
int cardType; //花色
int cardNum; //点数
//存储全部的卡牌
public static List cardsList = new ArrayList();
public Card(int cardNum,int cardType)
{
this.cardNum = cardNum;
this.cardType = cardType;
}
/**初始化卡牌**/
public static void initCards()
{
// String[] types = new String[]{"黑桃","红桃","梅花","方片","小王","大王"};
int[] types = new int[]{4,3,2,1,5,6};
/**
* * 54张,
* 红桃,黑桃,梅花,方块各1--13 13张
* 大小王2张
* 2+13x4=54
*/
for ( int j = 0; j < 4; j++ )
{
for ( int i = 1; i <= 13 ; i++ )
{
cardsList.add(new Card(i, types[j]));
}
}
//大小王的初始化
cardsList.add(new Card(14, types[4]));
cardsList.add(new Card(15, types[5]));
}
/**洗牌 以避免抽牌时抽到的卡牌次序时初始化卡牌的次序**/
public static void cleanCards()
{
Random random = new Random();
//洗牌次数控制在卡牌张数的一半+20以内的随机浮动次数
for ( int i = 0; i < (cardsList.size() / 2 + random.nextInt(20)); i++ )
{
int from = random.nextInt(cardsList.size());
int to = random.nextInt(cardsList.size());
Object temp;
if(to != from)
{
temp = cardsList.get(to);
cardsList.set(to, cardsList.get(from));
cardsList.set(from, temp);
}
}
}
@Override
public String toString()
{
// int[] types = new int[]{4,3,2,1,5,6};
String[] types = new String[]{"♠","♥","♣","♢","大王","小王"};
String typeString = null;
switch (cardType)
{
case 4 :typeString = types[0];break;
case 3 :typeString = types[1];break;
case 2 :typeString = types[2];break;
case 1 :typeString = types[3];break;
case 5 :typeString = types[5];break;
case 6 :typeString = types[4];break;
default :
break;
}
return "Card [" + typeString + " " + cardNum + "]";
}
@Override
public int compareTo(Object o)
{
//黑桃、红桃、梅花、方片、小王、大王 对象 4,3,2,1,5,6
//大小次序 大王、小王、2、A、K、Q、J、10...3。
Card card = (Card)o;
if((card.cardNum == 2) && (this.cardNum != 2))
{
if((this.cardNum < 14))
{
return 1;
}
else if(this.cardNum >= 14)
{
return -1;
}
}
else if((this.cardNum == 2) && (this.cardNum == 2))
{
return card.cardType - this.cardType;
}
if((card.cardNum == 1) && (this.cardNum != 1))
{
if((this.cardNum < 13))
{
return 1;
}
else if(this.cardNum >= 13)
{
return -1;
}
}
else if((this.cardNum == 1) && (this.cardNum == 1))
{
return card.cardType - this.cardType;
}
if((this.cardNum == card.cardNum))
{
return this.cardType - card.cardType;
}
return card.cardNum - this.cardNum;
}
}
热心网友
时间:2023-10-12 18:57
洗牌就是把数组中值随机切换位置,发牌很简单顺序把数组的值给几个人即可