码迷,mamicode.com
首页 > 编程语言 > 详细

关于java洗牌发牌小程序

时间:2016-11-27 22:37:01      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:stat   ati   扑克   shu   cut   override   void   this   ret   

package play.card;

public class Card {
public String num;
public String suit;
public Card(String num,String suit) {
this.num = num;
this.suit = suit;
}

public Card() {
super();
}

@Override
public String toString() {
String str = suit+" "+ num;
return str;
}
}

package play.card;

import java.util.ArrayList;
import java.util.List;

public class Player {
public String name;
public List<Card> cards;
public Player(String name) {
super();
this.name = name;
}

public Player() {
super();
}

//获取手牌
public void getCard(Card card){
if (cards == null) {
cards = new ArrayList<Card>();
}
cards.add(card);
}

@Override
public String toString() {
return name + ":" + cards.toString();
}
}

package play.card;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DeskOfCard {
List<Card> cards;
//创建一副手牌
public void initCard(){
String [] nums = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String [] suits = {"方块","梅花","红桃","黑桃"};
cards = new ArrayList<Card>();
//先创建四种花色的牌

for (int i = 0; i < 52; i++) {
cards.add(new Card(nums[i%13],suits[i%3]));
}
System.out.println("------------创建扑克牌-----------------");
System.out.println("-------------扑克牌创建成功-------------");
System.out.println("全部扑克牌:");
System.out.println(cards);
}
public static void main(String[] args) {
DeskOfCard test = new DeskOfCard();
test.initCard();//初始化
Collections.shuffle(test.cards);//洗牌
//创建玩家
Player pl1 = new Player("Cute");
Player pl2 = new Player("Jane");
Player pl3 = new Player("katte");
//发牌
for (int i = 0; i < 52; i++) {
if(i%3==0){
pl1.getCard(test.cards.get(i));
}
if(i%3==1){
pl2.getCard(test.cards.get(i));
}
if(i%3==2){
pl3.getCard(test.cards.get(i));
}
}
System.out.println("玩家拿到手的牌为:");
System.out.println(pl1.toString());
System.out.println(pl2.toString());
System.out.println(pl3.toString());

}

}

关于java洗牌发牌小程序

标签:stat   ati   扑克   shu   cut   override   void   this   ret   

原文地址:http://www.cnblogs.com/mengjie1001/p/6107030.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!