首页 > 代码库 > 扑克牌的洗牌实现

扑克牌的洗牌实现

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

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 =http://www.mamicode.com/ 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;    }}

 

扑克牌的洗牌实现