本文目录一览:
- 1、用java撰写一个简单的洗牌程序,可在文本模式下显示洗牌结果
- 2、java扑克牌洗牌程序,求抽可以抽出特定牌的次数
- 3、java洗牌算法问题
- 4、用JAVA写个代码,实现52张牌随机分给四个人
- 5、java 中如何实现一副扑克牌洗牌的功能。如果无法书写完整的代码,可以用文字描述出自己的实现思路
用java撰写一个简单的洗牌程序,可在文本模式下显示洗牌结果
//牌类
public class Card {
private int face; //牌的面值
private int suit; //牌的花色
public Card(int face, int suit) {
super();
this.face = face;
this.suit = suit;
}
public int getFace() {
return face;
}
public void setFace(int face) {
this.face = face;
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit;
}
}
//模拟洗牌和发牌
public class Test {
public static void main(String[] args) {
Card[] deck = new Card[52];
fillDeck(deck);
deal(deck);
System.out.println();
}
private static void fillDeck(Card[] deck) {
int i, j=0;
for (i = 0; i 52; i++) {
j = (int) (Math.random() * 52);
deck[i]=new Card(j%13,j/13);
}
}
private static void deal(Card[] deck) {
int k1, k2;
System.out.println();
for (k1 = 0, k2 = k1 + 26; k1 26; k1++, k2++) {
if (k1 % 13 == 0)
System.out.println("\n第" + (k1 / 13 + 1) +
"组牌:\t\t\t第"+ (k1 / 13 + 3) + "组牌:");
System.out.print("\nCard: " + deck[k1].getFace() +
" Suit: "+ deck[k1].getSuit());
System.out.print("\t\t\tCard: " + deck[k2].getFace() +
" Suit: "+ deck[k2].getSuit());
}
}
}
java扑克牌洗牌程序,求抽可以抽出特定牌的次数
package zhidao;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
/**
* 发牌游戏
*/
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 = POKERS.clone ();
/**
* 发到哪张牌 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 = current.split ("\\" + T)[0].trim ();
int col = Integer.parseInt (current.split ("\\" + T )[1].trim ());
if (null == results[m][col])
{
results[m][col] = pk + " ";
}
else
{
results[m][col] += pk + " ";
}
number--;
}
for ( int i = 0; i results.length; 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 )
{
Arrays.sort (temp, new ComparatorString ()
{
@Override
public int compare ( String o1, String o2 )
{
int index1 = orialPokers.indexOf (o1 + T);
int index2 = orialPokers.indexOf (o2 + T);
if (index1 index2)
{
return -1;
}
else if (index1 index2)
{
return 1;
}
else
{
return 0;
}
}
});
}
/**
* 洗牌
*/
private static void shufflePokers ( String[] POKERS )
{
Collections.shuffle (Arrays.asList (POKERS));
}
public static void main ( String[] args )
{
System.out.print ("输入人数:");
Scanner scanner = new Scanner (System.in);
int n = scanner.nextInt ();
scanner.close ();
String[] pColors = POKER_COLOR.split ("\\" + T );
String[][] results = new String[n][pColors.length];
shufflePokers (POKERS);
drawPoker (n, results);
final String poker = Arrays.toString (ORIAL_POKERS);
System.out.println ("\n输出如下:\n");
for (int i = 0; i results.length; i++)
{
System.out.println ("第" + ( i + 1 ) + "个人:");
for ( int j = 0; j results[i].length; j++ )
{
String[] temp = results[i][j].trim ().split ("\\s+");
sortPokers (poker, temp);
System.out.println (pColors[j] + ":" + Arrays.toString (temp).replaceAll ("[\\,\\[\\]]", ""));
}
System.out.println ();
}
}
}
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写个代码,实现52张牌随机分给四个人
java内的Collections类自带了一个shuffle洗牌算法。
static void shuffle(List? list)
使用默认随机源对指定列表进行置换。
static void shuffle(List? list, Random rnd)
使用指定的随机源对指定列表进行置换。
现在你可以把52张牌放进一个List里,调用他的shuffle算法打乱顺序。
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;
}