通过代码实现斗地主过程中的洗牌,发牌和看牌动作
public class PokerDemo {
public static void main(String[] args) {
//创建一个牌盒,也就是定义一个集合对象,用ArrayList集合实现
ArrayList<String> array = new ArrayList<String>();
//往牌盒里面装牌
/*
♦2,♦3,♦4...♦K,♦A
♣2,...
♥2,...
♠2,...
小王,大王
*/
//定义花色数组
String[] colors = {"♦", "♣", "♥", "♠"};
//定义点数数组
String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
for (String color : colors) {
for (String number : numbers) {
array.add(color + number);
}
}
array.add("小王");
array.add("大王");
//洗牌,也就是把牌打撒,用Collections的shuffle()方法实现
Collections.shuffle(array);
// System.out.println(array);
//发牌,也就是遍历集合,给三个玩家发牌
ArrayList<String> lqxArray = new ArrayList<String>();
ArrayList<String> lyArray = new ArrayList<String>();
ArrayList<String> fqyArray = new ArrayList<String>();
ArrayList<String> dpArray = new ArrayList<String>();
for (int i = 0; i < array.size(); i++) {
String poker = array.get(i);
if (i >= array.size() - 3) {
dpArray.add(poker);
} else if (i % 3 == 0) {
lqxArray.add(poker);
} else if (i % 3 == 1) {
lyArray.add(poker);
} else if (i % 3 == 2) {
fqyArray.add(poker);
}
}
//看牌,也就是三个玩家分别遍历自己的牌 lookPoker("林青霞", lqxArray);
lookPoker("柳岩", lyArray);
lookPoker("风清扬", fqyArray);
lookPoker("底牌", dpArray);
}
//看牌的方法
public static void lookPoker(String name, ArrayList<String> array) {
System.out.print(name + "的牌是:");
for (String poker : array) { System.out.print(poker + " ");
}
System.out.println();
}
}