炸金花作业

代码 

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
public class Zuoyezhajh {
	private String suit;//花色
    private int rank;//数字
 
    public Zuoyezhajh(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }
 
    public Zuoyezhajh() {
		// TODO Auto-generated constructor stub
	}
 
	public String getSuit() {
        return suit;
    }
 
    public void setSuit(String suit) {
        this.suit = suit;
    }
 
    public int getRank() {
        return rank;
    }
 
    public void setRank(int rank) {
        this.rank = rank;
    }
 
    @Override
    public String toString() {
        return "{ "+suit+" "+rank+"}";		//重写toString方法,调用时产生花色与牌号
    }
    private static final String[] suits = {"♥","♣","♦","♠"};
    
    public List<Zuoyezhajh> takePoker(){
        List<Zuoyezhajh> pokers = new ArrayList<>();
        for (int i = 0; i < 4; i++) {					
            for (int j = 1; j <= 13; j++) {				
            	Zuoyezhajh poker = new Zuoyezhajh(suits[i], j);
                pokers.add(poker);						//通过循环产生52张牌并放入集合中
            }
        }
        return pokers;
    }
    public void shuffle(List<Zuoyezhajh> pokers){
        for (int i = pokers.size()-1; i > 0; i--) {
            Random random = new Random();				//用random随机函数打乱牌序,以便每个人的牌大小不一样
            int index = random.nextInt(i);
            swap(pokers,i,index);
        }
    }
 
    private void swap(List<Zuoyezhajh> pokers, int i, int j){
    	Zuoyezhajh tmp = pokers.get(i);
        pokers.set(i,pokers.get(j));					//调用时,产生随机的花色和数字
        pokers.set(j,tmp);
    }
    public List<List<Zuoyezhajh>> game(List<Zuoyezhajh> pokers){
        List<List<Zuoyezhajh>> hand = new ArrayList<>();
        List<Zuoyezhajh> hand1 = new ArrayList<>();
        List<Zuoyezhajh> hand2 = new ArrayList<>();
        List<Zuoyezhajh> hand3 = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);				
        hand.add(hand3);                      //抓牌,设置每个人抓5张,三个人游戏
 
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
            	Zuoyezhajh removePoker = pokers.remove(0);				//循环控制三个人轮流摸牌,一人摸五张,一共15张
                hand.get(j).add(removePoker);
            }
        }
        return hand;
        
    }
    public static void main(String[] args) {
    	Zuoyezhajh game = new Zuoyezhajh();
        List<Zuoyezhajh> pokers = game.takePoker();
        System.out.println(pokers);
 
        //洗牌
        game.shuffle(pokers);
        System.out.println("洗牌:");
        System.out.println(pokers);
 
        //揭牌
        List<List<Zuoyezhajh>> hand = game.game(pokers);
        System.out.println("揭牌:");
        for (int i = 0; i < hand.size(); i++) {
            System.out.println("第 "+(i+1)+"个人的牌:"+hand.get(i));
        }
        System.out.println("剩下的牌");
        System.out.println(pokers);
    }
    
}

运行结果 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值