文章目录
- 前言
- 一、自动点名器
- 二、斗地主小游戏
- 1、准备牌
- 2、洗牌
- 3、发牌
- 4、理牌
- 总结
前言
博客仅记录个人学习进度和一些查缺补漏。
学习内容:BV17F411T7Ao
部分内容因没有学到,等待后续更新
一、自动点名器
/***
* 点名器1,N个学生随机点名
*/
ArrayList<Student> arrayList = new ArrayList<>();
Student student_1 = new Student("aaa", "male", 16);
Student student_2 = new Student("bbb", "female", 16);
Student student_3 = new Student("ccc", "female", 16);
Student student_4 = new Student("ddd", "male", 16);
Collections.addAll(arrayList, student_1, student_2, student_3, student_4);
System.out.println(arrayList);
//随机数法
Random rd = new Random();
System.out.println(arrayList.get(rd.nextInt(arrayList.size())).getName());
//打乱法
Collections.shuffle(arrayList);
System.out.println(arrayList.get(0).getName());
/***
* 点名器2,N个学生,70%点名男生,30%点名女生
*/
ArrayList<Student> arrayList = new ArrayList<>();
Student student_1 = new Student("aaa", "male", 16);
Student student_2 = new Student("bbb", "female", 16);
Student student_3 = new Student("ccc", "female", 16);
Student student_4 = new Student("ddd", "male", 16);
Collections.addAll(arrayList, student_1, student_2, student_3, student_4);
System.out.println(arrayList);
//先将男女分开
ArrayList<Student> male_Student = new ArrayList<>();
ArrayList<Student> female_Student = new ArrayList<>();
for (Student student : arrayList) {
if(student.getGender().equals("male")) {
male_Student.add(student);
}else {
female_Student.add(student);
}
}
//创建概率数组并打乱
ArrayList<Integer> probability = new ArrayList<>();
Collections.addAll(probability, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1);
Collections.shuffle(probability);
System.out.println(probability);
//抽取学生
Random rd = new Random();
int x = probability.get(rd.nextInt(probability.size()));
if (x == 0) {
System.out.println(male_Student.get(rd.nextInt(male_Student.size())).getName());
}else {
System.out.println(female_Student.get(rd.nextInt(female_Student.size())).getName());
}
/***
* 点名器3,N个学生,都会点,但是被点到后不会再点了
* 所有人点到后重新开始第二轮
*/
ArrayList<Student> arrayList = new ArrayList<>();
Student student_1 = new Student("aaa", "male", 16);
Student student_2 = new Student("bbb", "female", 16);
Student student_3 = new Student("ccc", "female", 16);
Student student_4 = new Student("ddd", "male", 16);
Collections.addAll(arrayList, student_1, student_2, student_3, student_4);
System.out.println(arrayList);
//实际上直接打乱然后顺序点就行了
//但是此处是想练习集合删除
//点名十轮
for (int i = 0; i < 10; i++) {
//创建临时集合装被点过的学生
ArrayList<Student> arrayList1 = new ArrayList<>();
while (!arrayList.isEmpty()){
Random rd = new Random();
int x = rd.nextInt(arrayList.size());
System.out.println(arrayList.get(x).getName());
arrayList1.add(arrayList.get(x));
arrayList.remove(x);
}
arrayList.addAll(arrayList1);
}
/***
* 点名器4,后续更新
*/
/***
* 点名器5,后续更新
*/
/***
* 集合嵌套
*键是字符串,值是单列集合
*/
//没有特殊要求的情况下默认使用HashMap
HashMap<String , ArrayList<String>> hashMap = new HashMap<>();
ArrayList<String> city1 = new ArrayList<>();
Collections.addAll(city1, "南京市", "扬州市", "苏州市");
ArrayList<String> city2 = new ArrayList<>();
Collections.addAll(city2, "武汉市", "孝感市", "十堰市");
ArrayList<String> city3 = new ArrayList<>();
Collections.addAll(city3, "石家庄市", "唐山市", "保定市");
hashMap.put("江苏省", city1);
hashMap.put("湖北省", city1);
hashMap.put("河北省", city1);
hashMap.forEach(new BiConsumer<String, ArrayList<String>>() {
@Override
public void accept(String s, ArrayList<String> strings) {
System.out.print(s + " = ");
int i = 0;
for (String string : strings) {
System.out.print(string);
if (i != strings.size() - 1) {
System.out.print(",");
i ++;
}
}
System.out.println();
}
});
二、斗地主小游戏
1、准备牌
很明显,一张牌有两个属性,花色和数字
在游戏开始时准备一副牌,并且不会随着开始新的一轮游戏而新创建一副牌,需要使用静态代码块
static {
/**
* 准备牌
*"♦","♣","♥","♠"
*"3","4","5","6","7","8","9","10","J","Q","K","A"
*/
ArrayList<String> colorSet = new ArrayList<>();
ArrayList<String> pointSet = new ArrayList<>();
//添加花色及点数
Collections.addAll(colorSet, "♦","♣","♥","♠");
Collections.addAll(pointSet, "3","4","5","6","7","8","9","10","J","Q","K","A","2");
for (String color : colorSet) {
for (String point: pointSet) {
cardSet.add(new Card(color, point));
}
}
//加入大小王
cardSet.add(new Card("Black", "Joker"));
cardSet.add(new Card("Red", "Joker"));
}
2、洗牌
//洗牌
Collections.shuffle(cardSet);
3、发牌
//发牌
//创建玩家牌库
ArrayList<Card> lord = new ArrayList<>();
ArrayList<Card> player1 = new ArrayList<>();
ArrayList<Card> player2 = new ArrayList<>();
ArrayList<Card> player3 = new ArrayList<>();
//给玩家发牌
CardGet(lord, player1, player2, player3);
public static void CardGet(ArrayList<Card> lord, ArrayList<Card> player1, ArrayList<Card> player2, ArrayList<Card> player3) {
int i = 0;
//发牌
for (int j = 0; j < 3; j++) {
lord.add(cardSet.get(i++));
player1.add(cardSet.get(i++));
player2.add(cardSet.get(i++));
player3.add(cardSet.get(i++));
}
while (i < cardSet.size()) {
player1.add(cardSet.get(i++));
player2.add(cardSet.get(i++));
player3.add(cardSet.get(i++));
}
4、理牌
//理牌
public static void CardSort(ArrayList<Card> card) {
//添加花色及点数
ArrayList<String> colorSet = new ArrayList<>();
ArrayList<String> pointSet = new ArrayList<>();
Collections.addAll(colorSet, "♦","♣","♥","♠","Red","Black");
Collections.addAll(pointSet, "3","4","5","6","7","8","9","10","J","Q","K","A","2","Joker");
card.sort(new Comparator<Card>() {
@Override
public int compare(Card o1, Card o2) {
int i = pointSet.indexOf(o1.getPoint()) - pointSet.indexOf(o2.getPoint());
i = i == 0 ? colorSet.indexOf(o1.getPoint()) - colorSet.indexOf(o2.getPoint()) : i;
return i;
}
});
}
各类定义类型推荐
提示:会频繁出现集合嵌套集合的情况
注意此时Card类继承自jLabel和MouseListener。
后续代码等学完多线程后更新
总结
基本都是集合的运用,包括集合的排序,比较器的书写规则,lambda表达式的熟悉。总体来说都是熟练了就很简单,纯工具。