码迷,mamicode.com
首页 > 其他好文 > 详细

扑克牌的洗牌实现

时间:2014-10-10 13:16:04      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   java   for   sp   

自己随便写的,直接贴个代码吧:

package poker;

import java.io.Serializable;

public class Poker implements Serializable {

    private static final long serialVersionUID = -1531112704784497500L;

    public static final String[] suit = {
        "红", "梅", "方", "黑"
    };

    private String point;

    private int value;

    private String color;

    private boolean isUsed = false;

    public String getPoint() {
        return this.point;
    }

    public void setPoint(String point) {
        this.point = point;
    }

    public int getValue() {
        return this.value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public boolean isUsed() {
        return this.isUsed;
    }

    public void setUsed(boolean isUsed) {
        this.isUsed = isUsed;
    }

    public String getColor() {
        return this.color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "[" + color + "," + point + "]";
    }
}
package poker;

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

public class PokerService {
    private List<Poker> pokerList;

    private void initPoker() {
        pokerList = new ArrayList<Poker>();
        for (int s = 0; s < 4; s++) {
            for (int i = 0; i < 13; i++) {
                Poker poker = new Poker();
                int point = i + 1;
                switch (point) {
                    case 1:
                        poker.setPoint("A");
                        break;
                    case 11:
                        poker.setPoint("J");
                        break;
                    case 12:
                        poker.setPoint("Q");
                        break;
                    case 13:
                        poker.setPoint("K");
                        break;
                    default:
                        poker.setPoint(String.valueOf(point));
                        break;
                }
                poker.setValue(point);
                poker.setColor(Poker.suit[s]);
                pokerList.add(poker);
            }
        }
        Poker smallGhost = new Poker();
        smallGhost.setPoint("small ghost");
        smallGhost.setValue(99);
        pokerList.add(smallGhost);

        Poker bigGhost = new Poker();
        bigGhost.setPoint("big ghost");
        bigGhost.setValue(100);
        pokerList.add(bigGhost);
    }

    private void shufflePoker() {
        Poker temp = new Poker();
        if (pokerList == null || pokerList.size() != 54) {
            return;
        }
        Random random = new Random();
        for (int i = 0; i < 1000; i++) {
            int position = i % 54;
            int position2 = random.nextInt(54);
            if (position != position2) {
                temp = pokerList.get(position);
                pokerList.set(position, pokerList.get(position2));
                pokerList.set(position2, temp);
            }
        }

    }

    public static void main(String[] args) {
        PokerService pService = new PokerService();
        pService.initPoker();
        System.out.println(pService.getPokerList());
        pService.shufflePoker();
        System.out.println(pService.getPokerList());
    }

    public List<Poker> getPokerList() {
        return this.pokerList;
    }

    public void setPokerList(List<Poker> pokerList) {
        this.pokerList = pokerList;
    }
}

 

扑克牌的洗牌实现

标签:style   blog   color   io   os   ar   java   for   sp   

原文地址:http://www.cnblogs.com/happyPawpaw/p/4015410.html

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