简单的洗牌算法(不包括大小王)
算法思想:
1.如何去表示一张牌呢?这里我们自己创造一个类当作一种新数据类型表示一张牌。
a:这个类里面包括一个String类型的数据和int数据类型的数据分别存储花色和牌号
b:创建一个构造方法对牌号和花色赋值·
c:进行toString重写
java
public class Crad {
private int mark;
private String suit;
public Crad() {
}
public Crad(String suit,int mark) {
this.mark = mark;
this.suit = suit;
}
@Override
public String toString() {
return "{" + mark + suit + '}'+" ";
}
}
2.我们先得创造一副牌,考虑四种花色,每张花色的张数为13张
a:创造一个数组里面包含了四种花色
b:利用for循环对花色与牌号结合 然后赋值给牌中
java
public static final String []STRING={"♥","♠","♣","♦"};
public List<Crad> setCrad() {
List<Crad> crads= new ArrayList<>();
for (int i = 0; i <STRING.length ; i++) {
for (int j = 1; j <=13 ; j++) {
Crad crad=new Crad(STRING[i],j);
crads.add(crad);
}
}
return crads;
}
3.对于创建的新牌进行洗牌
a:用到了一个类Random类产生随机下表
b:这里采用牌交换的方式来进行洗牌,采用从最后一张牌开始交换,大致过程举例,51下标的牌然后和Radom类随机生成0到50下标的牌进行交换,然后再50下标的牌和Radom类随机生成0到49下标的牌进行交换,以此类推
java
public void shuffle(List<Crad> crads) {
Random random=new Random();
for (int i = crads.size()-1; i >0 ; i--) {
int index=random.nextInt(i);
Crad temp=crads.get(i);
crads.set(i,crads.get(index));
crads.set(index,temp);
}
}
4.游戏玩法
a:三名玩家每名玩家依次发5张牌比大小:
java
public List<List<Crad>> paly(List<Crad> crads) {
List<List<Crad>> head =new ArrayList<>();
List<Crad> head1=new ArrayList<>();
List<Crad> head2=new ArrayList<>();
List<Crad> head3=new ArrayList<>();
head.add(head1);
head.add(head2);
head.add(head3);
for (int i = 0; i <5 ; i++) {
for (int j = 0; j <head.size() ; j++) {
head.get(j).add(crads.remove(0));
}
}
return head;
}
}
代码合并
java
public class Crad {
private int mark;
private String suit;
public Crad() {
}
public Crad(String suit,int mark) {
this.mark = mark;
this.suit = suit;
}
@Override
public String toString() {
return "{" + mark + suit + '}'+" ";
}
}
public class Game {
public static final String []STRING={"♥","♠","♣","♦"};
public List<Crad> setCrad() {
List<Crad> crads= new ArrayList<>();
for (int i = 0; i <STRING.length ; i++) {
for (int j = 1; j <=13 ; j++) {
Crad crad=new Crad(STRING[i],j);
crads.add(crad);
}
}
return crads;
}
// public void disPalyCards () {
// List<Crad> list=setCrad();
// for (int i = 0; i <list.size() ; i++) {
// System.out.print(list.get(i));
// }
// }
public void shuffle(List<Crad> crads) {
Random random=new Random();
for (int i = crads.size()-1; i >0 ; i--) {
int index=random.nextInt(i);
Crad temp=crads.get(i);
crads.set(i,crads.get(index));
crads.set(index,temp);
}
}
public List<List<Crad>> paly(List<Crad> crads) {
List<List<Crad>> head =new ArrayList<>();
List<Crad> head1=new ArrayList<>();
List<Crad> head2=new ArrayList<>();
List<Crad> head3=new ArrayList<>();
head.add(head1);
head.add(head2);
head.add(head3);
for (int i = 0; i <5 ; i++) {
for (int j = 0; j <head.size() ; j++) {
head.get(j).add(crads.remove(0));
}
}
return head;
}
}
//开始玩游戏
public class Test {
public static void main(String[] args) {
Game game = new Game();
List<Crad> list = game.setCrad();
System.out.println(list);
System.out.println();
game.shuffle(list);
System.out.println(list);
List<List<Crad>>game1 =game.paly(list);
for (int i = 0; i <game1.size(); i++) {
System.out.println("head"+(i+1)+":"+game1.get(i) );
}
}
}