赞
踩
算法思想:
a:这个类里面包括一个String类型的数据和int数据类型的数据分别存储花色和牌号
b:创建一个构造方法对牌号和花色赋值·
c:进行toString重写
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循环对花色与牌号结合 然后赋值给牌中
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;
}
a:用到了一个类Random类产生随机下表
b:这里采用牌交换的方式来进行洗牌,采用从最后一张牌开始交换,大致过程举例,51下标的牌然后和Radom类随机生成0到50下标的牌进行交换,然后再50下标的牌和Radom类随机生成0到49下标的牌进行交换,以此类推
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);
}
}
a:三名玩家每名玩家依次发5张牌比大小:
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 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) ); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。