java洗牌代码大全 洗牌算法java
用java ArrayList集合编写扑克牌洗牌与发牌
import java.util.ArrayList;
创新互联建站主要从事成都做网站、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务三江侗,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
import java.util.Random;
public class PokerDemo {
ArrayListCard poker = new ArrayListCard();
Player[] players;
int n = 4;
String[] name = {"Wang", "Zhang", "Li", "Yuan"};
public PokerDemo() {
players = new Player[n];
for(int i = 0; i players.length; i++)
players[i] = new Player(name[i]);
for(int i = 0; i 4; i++)
for(int j = 0; j 13; j++)
poker.add(new Card(i, j));
System.out.println("洗牌前: " + poker);
xipai();
System.out.println("洗牌后: " + poker);
fapai();
System.out.println("发牌后: ");
for(int i = 0; i players.length; i++)
System.out.println(players[i]);
}
public void xipai() {
Random r = new Random();
ArrayListCard tmp = new ArrayListCard();
while(!poker.isEmpty()) {
int index = r.nextInt(poker.size());
Card card = poker.get(index);
tmp.add(card);
poker.remove(index);
}
poker = new ArrayListCard(tmp);
}
public void fapai() {
int p = 0;
while(!poker.isEmpty()) {
Card card = poker.get(0);
players[p].add(card);
p = (p + 1) % n;
poker.remove(0);
}
}
public static void main(String[] args) {
new PokerDemo();
}
}
class Card {
int val, sty;
final String[] value = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
final String[] style = {"黑桃", "红心", "梅花", "方块"};
public Card(int s, int v) {
sty = s;
val = v;
}
public String toString() {
return style[sty]+ " " + value[val];
}
}
class Player {
ArrayListCard cards = new ArrayListCard();
String name;
public Player(String n) {
name = n;
}
public void add(Card card) {
cards.add(card);
}
public String toString() {
return name + ":\t" + cards;
}
}
java 中如何实现一副扑克牌洗牌的功能。如果无法书写完整的代码,可以用文字描述出自己的实现思路
1.定义牌类
public class Card{
public int id;//牌大小
public String number;//牌值名称
public String color;//花色
}
2.定义牌组,用ArrayList存放所有牌
ArrayListCard cardGroup =new ArrayListCard();
3.定义花色枚举
public static final String[] COLORS=new String[]{"方块","梅花","红桃","黑桃"};
4.定义牌值大小枚举
public static fina String[] CARDVALUES=new String[]{"A","1","2","3",
"4","5","6","7","8","9","10","J","Q","K","King_Small","King_Big"};
5.初始化牌组,这就是一副新牌,没切过,己经排好序的
public void initCardGroup(){
//加入A~K
for(int i=0;i13;i++){
for (int j=0;j4;j++){
//生成一张牌的对象
Card card=new Card();
card.id=i;
card.color=COLORS[j];
card.munber=CARDVALUES[i];
//将对象加入牌组
cardGroup.add(card);
}
}
//加入大小王
Card card_k1=new Card();
card_k1.id=13;
card_k1.color="King_Small";
card_k1.number="King_Small";
cardGroup.add(card_k1);
Card card_k2=new Card();
card_k2.id=14;
card_k2.color="King_big";
card_k2.number="King_big";
cardGroup.add(card_k2);
}
6. 洗牌,将原牌组打乱,得到新的牌组
public ArrayListCard flushCardsGroup(ArrayListCard src){
ArrayListCard result=new ArrayListCard();
while(src.size0){
int size=src.size();
//以size为限,生成0~size之间的随机数
Random r=new Random();
int index=r.nextInt(size);
//对index做个检查
if(index0){index=0;}
if(indexsize-1){index=size-1;}
//index就是捡出来的牌,加到新的数组中,从原来数组中删除
Card card=src.get(index);
result.add(card);
card.remove(index);
}
return result;
}
java洗牌算法问题
你指的是Card里的toString方法吧
public String toString() {
String aa = suit + " " + num + " ";
return aa;
}
toString这个方法一般在 System.out.print时使用,这个是打印出String,JVM就是默认调用类的toSting方法
注:所有类都有toString方法,默认是当前对象的hashcode,即内存地址
所以在发牌是打印
public void dealcard()//发牌
{
for(int i=0;i52;i++)
{
if(i%4==0i!=0){
System.out.println(); //每发4张牌输出换行
}
// 就是这里,默认调用card的toString方法
System.out.print(card[i]); //依次输出 发的牌
}
}
文章题目:java洗牌代码大全 洗牌算法java
URL标题:http://cdiso.cn/article/hpphes.html