```创建扑克牌类
输入代码:
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());
}
}