• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

关于java洗牌发牌小程序

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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());

}

}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
java基于TCP/IP协议的局域网聊天小程序发布时间:2022-07-18
下一篇:
FinClip小程序+Rust(三):一个加密钱包发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap