为了账号安全,请及时绑定邮箱和手机立即绑定

简单java发牌洗牌小程序

标签:
Java

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

}

}

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消