三个人看牌:将看牌封装成一个功能(独立的 代码块)
代码示例
package Poker;
import java.util.ArrayList;
import java.util.Collections;
public class Poker {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 创建牌盒
ArrayList arrayList = new ArrayList();
// 装牌
String[] colors = { "♥", "♠", "♣", "♦" };
String[] numbers = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
for (String color : colors) {
for (String number : numbers) {
String poker = color.concat(number);// ♥A ♥2 ♥3 ♥4 ♥5...
arrayList.add(poker);
}
}
arrayList.add("大王");
arrayList.add("小王");
// 洗牌
Collections.shuffle(arrayList);
// 发牌
ArrayList player1 = new ArrayList();
ArrayList player2 = new ArrayList();
ArrayList player3 = new ArrayList();
ArrayList diPai = new ArrayList();
for (int i = 0; i < arrayList.size(); i++) {// arrayList.size()==54
if (i % 3 == 0) {
player1.add(arrayList.get(i));
} else if (i % 3 == 1) {
player2.add(arrayList.get(i));
} else if (i % 3 == 2) {
player3.add(arrayList.get(i)); // 添加牌
} else if (i >= arrayList.size() - 3) {
diPai.add(arrayList.get(i));
}
}
lookPoker("玩家1", player1);
lookPoker("玩家2", player2);
lookPoker("玩家3", player3);
}
public static void lookPoker(String name, ArrayList array) {
System.out.print(name + "的牌是:");
for (String string : array) {
System.out.print(string + " ");
}
System.out.println();
}
}
5)看牌 封装功能
代码示例
package Poker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class Poker2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// HashMap集合存储编号和牌
HashMap hm = new HashMap();
// ArrayList集合存储编号
ArrayList array = new ArrayList();
// 装牌
String[] colors = { "♥", "♠", "♣", "♦" };
String[] numbers = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
// 从0开始编号,将编号和编号对应的牌都存储到HashMap集合中,同时往ArrayList单独存储编号
int index = 0;
// 拼接
for (String number : numbers) {// A
for (String color : colors) {
String poker = color.concat(number);// ♠A ♥A ♦A ♣A ♠B ♥B ♦B ♣B
hm.put(index, poker);
array.add(index);
index++;
}
}
// 装小王和大王
hm.put(index, "小王");
array.add(index);
index++;
hm.put(index, "大王");
array.add(index);
// 不能++了,角标越界
Collections.shuffle(array);// 洗存储编号的集合
// 发牌
TreeSet player1 = new TreeSet();
TreeSet player2 = new TreeSet();
TreeSet player3 = new TreeSet();
TreeSet diPai = new TreeSet();
for (int x = 0; x < array.size(); x++) {
// 获取到每一个元素
if (x >= array.size() - 3) {
diPai.add(array.get(x)); // array.get(x)获取的是编号 TreeSet里面存储的也是编号,对编号进行排序
} else if (x % 3 == 0) {
// 玩家1
player1.add(array.get(x));
} else if (x % 3 == 1) {
// 玩家2
player2.add(array.get(x));
} else if (x % 3 == 2) {
player3.add(array.get(x));
}
}
lookPoker("玩家1", player1, hm); // hm为牌,顺序不变,用array里面的编号变代替牌盒变
lookPoker("玩家2", player2, hm);
lookPoker("玩家3", player3, hm);
lookPoker("底牌", diPai, hm);
}
public static void lookPoker(String name, TreeSet ts, HashMap hm) {
System.out.println(name + "的牌为:");
for (Integer key : ts) {// 遍历玩家的牌
String value = hm.get(key);// 通过编号找牌
System.out.print(value + " ");
}
System.out.println();
}
}