java麻将洗牌发牌代码,java麻将洗牌发牌代码大全-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

java麻将洗牌发牌代码,java麻将洗牌发牌代码大全

用java ArrayList集合编写扑克牌洗牌与发牌

import java.util.ArrayList;

创新互联是专业的工布江达网站建设公司,工布江达接单;提供成都做网站、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行工布江达网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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数组编写洗牌程序怎么写?我刚学java,请多指教,谢谢!!!

package xly04;

import java.util.Random;

public class ArrayDemo3 {

/**

* 数组应用:

* 1 随机洗牌

* 2 顺序发牌

*/

public static void main(String[] args) {

String[] cards=new String[]{"黑桃A","黑桃2","黑桃3","黑桃4",

"黑桃5","黑桃6","黑桃7","黑桃8","黑桃9","黑桃10","黑桃J","黑桃Q","黑桃K",

"红桃A","红桃2","红桃3","红桃4","红桃5","红桃6","红桃7","红桃8","红桃9",

"红桃10","红桃J","红桃Q","红桃K","方块A","方块2","方块3","方块4","方块5",

"方块6","方块7","方块8","方块9","方块10","方块J","方块Q","方块K","梅花A",

"梅花2","梅花3","梅花4","梅花5","梅花6","梅花7","梅花8","梅花9","梅花10",

"梅花J","梅花Q","梅花K","大王","小王"};

//Random random=new Random();.......(1)

//int index=random.nextInt(10);//[0,10) %10

//System.out.println(index);

//String card=cards[random.nextInt(cards.length)];//长度为8,随机下标[0,8)

//System.out.println(card);//随机的某张牌

for(int i=cards.length-1;i=1;i--){

int j=new Random().nextInt(i);//或者(1)不注销用int j=random.nextInt(i);

String t=cards[i];

cards[i]=cards[j];

cards[j]=t;

}

for(int i=0;icards.length;i++){

System.out.print(cards[i]+",");

}

System.out.println();//输出回车字符

//顺序发牌:顺序的将每个牌发给每个人

String[] players=new String[]{"马伊琍","文章","六六"};

//int i=0;

//System.out.println(players[i++%players.length]);

//System.out.println(players[i++%players.length]);

//System.out.println(players[i++%players.length]);

//System.out.println(players[i++%players.length]);

//System.out.println(players[i++%players.length]);

//System.out.println(players[i++%players.length]);

for(int i=0;icards.length;i++){

//i=0,1,2,3,4,5,6,7

String card=cards[i];//card 代表每张扑克牌

String player=players[i%players.length];

//player代表每个玩游戏的人

System.out.println(player+":"+card);

}

}

}

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麻将洗牌发牌代码大全
文章位置:http://kswsj.cn/article/hohjio.html

其他资讯