分析
流程:
1.造一個(gè)牌盒
2.造牌
3.洗牌
4.發(fā)牌
5.看牌
程序分析:
1.創(chuàng)建一個(gè)ArrayList<String>集合對(duì)象
2.新建兩個(gè)數(shù)組(花色,點(diǎn)數(shù)),將兩個(gè)進(jìn)行拼接
3.用Collections.shuffle();
5.發(fā)牌
6.看牌
代碼如下:
public class PokerTest {
public static void main(String[] args) {
//創(chuàng)建一個(gè)牌盒
ArrayList<String> arrayList = new ArrayList<>();
//裝牌
/**
* 黑桃A,....黑桃K
* 紅桃A,....
* 梅花A,....
* 方塊A,....
*/
//定義一個(gè)花色數(shù)組
String[] colors = {"?", "?", "?", "?"};
//定義一個(gè)點(diǎn)數(shù)數(shù)組
String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "K"};
for (String color : colors) {
for (String number : numbers) {
arrayList.add(color.concat(number));
}
}
arrayList.add("大王");
arrayList.add("小王");
//洗牌
Collections.shuffle(arrayList);
//發(fā)牌
ArrayList<String> cmeizu = new ArrayList<>();
ArrayList<String> lwy = new ArrayList<>();
ArrayList<String> huangfeng = new ArrayList<>();
ArrayList<String> diPai = new ArrayList<>();
for (int x = 0; x < arrayList.size(); x++) {
if (x >= arrayList.size() - 3) {
diPai.add(arrayList.get(x));
} else if (x % 3 == 0) {
cmeizu.add(arrayList.get(x));
} else if (x % 3 == 1) {
lwy.add(arrayList.get(x));
} else if (x % 3 == 2) {
huangfeng.add(arrayList.get(x));
}
}
System.out.println(arrayList);
lookPoker("農(nóng)民1:", cmeizu);
lookPoker("農(nóng)民2:", lwy);
lookPoker("地主:", huangfeng);
lookPoker("底牌:", diPai);
}
public static void lookPoker(String name, ArrayList<String> array) {
System.out.println(name + "的牌是:");
for (String s : array) {
System.out.print(s + " ");
}
System.out.println();
}
}
這種做法有一個(gè)問題就是沒有排序
所以第二種做法:
步驟:
1,創(chuàng)建一個(gè)HashMap<ArrayList<Integer>,String>集合
2.創(chuàng)建兩個(gè)數(shù)組String類型(花色,點(diǎn)數(shù)),然后拼接numbers.concat(colors)
3.洗牌
4.發(fā)牌
5.看牌
具體實(shí)現(xiàn)如下:
import java.util.ArrayList;
import java.util.Collections;
public class PokerDemo {
public static void main(String[] args) {
//創(chuàng)建一個(gè)牌盒
ArrayList<String> arrayList = new ArrayList<>();
//裝牌
/**
* 黑桃A,....黑桃K
* 紅桃A,....
* 梅花A,....
* 方塊A,....
*/
//定義一個(gè)花色數(shù)組
String[] colors = {"?", "?", "?", "?"};
//定義一個(gè)點(diǎn)數(shù)數(shù)組
String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "K"};
for (String color : colors) {
for (String number : numbers) {
arrayList.add(color.concat(number));
}
}
arrayList.add("大王");
arrayList.add("小王");
//洗牌
Collections.shuffle(arrayList);
//發(fā)牌
ArrayList<String> cmeizu = new ArrayList<>();
ArrayList<String> lwy = new ArrayList<>();
ArrayList<String> huangfeng = new ArrayList<>();
ArrayList<String> diPai = new ArrayList<>();
for (int x = 0; x < arrayList.size(); x++) {
if (x >= arrayList.size() - 3) {
diPai.add(arrayList.get(x));
} else if (x % 3 == 0) {
cmeizu.add(arrayList.get(x));
} else if (x % 3 == 1) {
lwy.add(arrayList.get(x));
} else if (x % 3 == 2) {
huangfeng.add(arrayList.get(x));
}
}
System.out.println(arrayList);
lookPoker("農(nóng)民1:", cmeizu);
lookPoker("農(nóng)民2:", lwy);
lookPoker("地主:", huangfeng);
lookPoker("底牌:", diPai);
}
public static void lookPoker(String name, ArrayList<String> array) {
System.out.println(name + "的牌是:");
for (String s : array) {
System.out.print(s + " ");
}
System.out.println();
}
}