目录
Collections:是一个用来操作集合的工具类
斗地主游戏
Collections提供的常用静态方法
Collections只能支持对List集合进行排序
package com.xinbao.d6_parameter;
public class Student implements Comparable{
private String name;
private double score;
private int age;
@Override
public int compareTo(Student o) {
return Double.compare(this.score,o.score);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", score=" + score +
", age=" + age +
'}';
}
public Student() {
}
public Student(String name, double score, int age) {
this.name = name;
this.score = score;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.xinbao.d6_parameter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CollectionsTest1 {
public static void main(String[] args) {
List list = new ArrayList<>();
Collections.addAll(list,"JUdy","Bob","Cindy","Jilly","DAvid");
System.out.println(list);//[JUdy, Bob, Cindy, Jilly, DAvid]
Collections.shuffle(list);//打乱元素
System.out.println(list);//[JUdy, Cindy, Bob, Jilly, DAvid]
Collections.sort(list);//排序
System.out.println(list);//[Bob, Cindy, DAvid, JUdy, Jilly]
List students = new ArrayList<>();
Student s1 = new Student("紫霞仙子",99.9,20);
Student s2 = new Student("至尊宝",99.5,20);
Student s3 = new Student("牛魔王",99.3,25);
Student s4 = new Student("月光宝盒",88,888);
Collections.addAll(students,s1,s2,s3,s4);
System.out.println(students);
Collections.sort(students);
// Collections.sort(students, new Comparator() {
// @Override
// public int compare(Student o1, Student o2) {
// return o1.getAge() - o2.getAge();
// }
// });
Collections.sort(students, ( o1, o2) -> o1.getAge() - o2.getAge());
System.out.println(students);
}
}
.总共有54张牌
点数:"3","4","5","6","7","8","9","10","J"," Q "," K "," A ","2"
花色:♦, ♣,♥,♠
大小王:,
斗地主:发出51张牌,剩下3张做为底牌。
分析实现
在启动游戏房间的时候,应该提前准备好54张牌接着,需要完成洗牌、发牌、对牌排序、看牌
package com.xinbao.d3_collection_test;
public class Card {
private String number;
private String color;
private int size;//大小
public Card() {
}
public Card(String number, String color, int size) {
this.number = number;
this.color = color;
this.size = size;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
@Override
public String toString() {
return number + color + "";
}
}
package com.xinbao.d3_collection_test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Room {
private List allCards = new ArrayList<>();
public Room(){
//1、做出54张牌,存入allCards集合中去
//点数
String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","k","A","2"};
//花色
String[] colors = {"♠","♥","♦","♣"};
//遍历点数,再遍历花色,构造Card类,添加大小
int size = 0;
for (String number : numbers) {
size++;
for (String color : colors) {
allCards.add(new Card(number,color,size));
}
}
//单独存入大小王
Card c1 = new Card("","",++size);
Card c2 = new Card("","",++size);
Collections.addAll(allCards,c1,c2);
System.out.println("新牌:" + allCards);
}
public void start() {
//1、洗牌
Collections.shuffle(allCards);
System.out.println("洗牌后" + allCards);
//2、发牌
List player1 = new ArrayList<>();
List player2 = new ArrayList<>();
List player3 = new ArrayList<>();
for (int i = 0; i < allCards.size()-3; i++) {
Card c = allCards.get(i);
if (i % 3 == 0){
player1.add(c);
}else if (i % 3 == 1){
player2.add(c);
}else if (i % 3 == 2){
player3.add(c);
}
}
//3、对玩家牌排序
sortCards(player1);
sortCards(player2);
sortCards(player3);
System.out.println("玩家1:" + player1);
System.out.println("玩家2:" + player2);
System.out.println("玩家3:" + player3);
//4、看牌
List lastThreeCards = allCards.subList(allCards.size()-3, allCards.size());//,50,51,52
System.out.println("底牌:" + lastThreeCards);
player3.addAll(lastThreeCards);
sortCards(player3);
System.out.println("玩家3抢到地主" + player3);
}
private void sortCards(List cards){
Collections.sort(cards, new Comparator() {
@Override
public int compare(Card o1, Card o2) {
return o2.getSize() - o1.getSize();//降序
}
});
}
}
package com.xinbao.d3_collection_test;
import java.util.List;
public class Gamedemo {
public static void main(String[] args) {
Room room = new Room();
//游戏启动
room.start();
}
}
E:\JVsoft\Java\jdk-17\bin\java.exe -javaagent:E:\JVsoft\IntelliJIDEA2021.1.1\lib\idea_rt.jar=51741:E:\JVsoft\IntelliJIDEA2021.1.1\bin -Dfile.encoding=UTF-8 -classpath E:\JVsoft\code\out\production\collection-map-stream-app com.xinbao.d3_collection_test.Gamedemo
新牌:[3♠, 3♥, 3♦, 3♣, 4♠, 4♥, 4♦, 4♣, 5♠, 5♥, 5♦, 5♣, 6♠, 6♥, 6♦, 6♣, 7♠, 7♥, 7♦, 7♣, 8♠, 8♥, 8♦, 8♣, 9♠, 9♥, 9♦, 9♣, 10♠, 10♥, 10♦, 10♣, J♠, J♥, J♦, J♣, Q♠, Q♥, Q♦, Q♣, k♠, k♥, k♦, k♣, A♠, A♥, A♦, A♣, 2♠, 2♥, 2♦, 2♣, , ]
洗牌后[2♠, Q♥, 8♠, A♦, J♦, Q♠, 7♦, 4♣, k♣, J♣, 2♦, 2♥, 3♥, 10♠, 4♦, 8♥, A♠, 5♠, 8♣, 6♥, 6♣, 9♠, k♦, 3♠, k♠, 5♥, 5♣, 9♥, 3♦, 6♦, 8♦, 2♣, Q♦, 5♦, J♥, 7♥, k♥, Q♣, 7♣, 4♥, 10♥, 10♦, A♥, , 7♠, 3♣, 9♦, A♣, 10♣, , 9♣, 4♠, J♠, 6♠]
玩家1:[2♠, A♦, A♥, k♠, k♥, J♣, 10♣, 9♠, 9♥, 8♥, 8♣, 8♦, 7♦, 5♦, 4♥, 3♥, 3♣]
玩家2:[, , 2♦, 2♣, A♠, k♦, Q♥, Q♣, J♦, J♥, 10♠, 10♥, 9♦, 6♥, 5♥, 4♣, 3♦]
玩家3:[2♥, A♣, k♣, Q♠, Q♦, 10♦, 9♣, 8♠, 7♥, 7♣, 7♠, 6♣, 6♦, 5♠, 5♣, 4♦, 3♠]
底牌:[4♠, J♠, 6♠]
玩家3抢到地主[2♥, A♣, k♣, Q♠, Q♦, J♠, 10♦, 9♣, 8♠, 7♥, 7♣, 7♠, 6♣, 6♦, 6♠, 5♠, 5♣, 4♦, 4♠, 3♠]
Process finished with exit code 0