案例:斗地主游戏
一、需求
- 在启动游戏房间的时候,应该提前准备好54张牌,完成洗牌、发牌、牌排序、逻辑。
二、分析
- 当系统启动的同时需要准备好数据的时候,就可以用静态代码块了。
- 洗牌就是打乱牌的顺序。
- 定义三个玩家,依次发出51张牌。
- 给玩家的牌进行排序(拓展)。
- 输出每个玩家的牌数据。
package com.app.d6_Collection_test;
public class Card {
private String size;
private String color;
private int index;
public Card() {
}
public Card(String size, String color, int index) {
this.size = size;
this.color = color;
this.index = index;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
@Override
public String toString() {
return size + color;
}
}
package com.app.d6_Collection_test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class GameDemo {
public static List<Card> allCards = new ArrayList<>();
static {
String[] sizes = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
String[] colors = {"♠", "♥", "♣", "♦"};
int index = 0;
for (String size : sizes) {
index++;
for (String color : colors) {
Card card = new Card(size, color, index);
allCards.add(card);
}
}
Collections.addAll(allCards, new Card("小", "🃏", ++index), new Card("大", "🃏", ++index));
System.out.println("新牌:" + allCards);
}
public static void main(String[] args) {
Collections.shuffle(allCards);
System.out.println("洗牌后:" + allCards);
List<Card> sunwukong = new ArrayList<>();
List<Card> zhubajie = new ArrayList<>();
List<Card> shawujing = new ArrayList<>();
for (int i = 0; i < allCards.size() - 3; i++) {
Card card = allCards.get(i);
if (i % 3 == 0) {
sunwukong.add(card);
} else if (i % 3 == 1) {
zhubajie.add(card);
}else if (i % 3 == 2){
shawujing.add(card);
}
}
List<Card> lastThreeCards = allCards.subList(allCards.size() - 3, allCards.size());
sortCards(sunwukong);
sortCards(zhubajie);
sortCards(shawujing);
System.out.println("-------------------------------------------------------------------------------------");
System.out.println("孙悟空:" + sunwukong);
System.out.println("猪八戒:" + zhubajie);
System.out.println("沙悟净:" + shawujing);
System.out.println("三张底牌:" + lastThreeCards);
}
private static void sortCards(List<Card> cards) {
Collections.sort( cards, ( o1, o2) -> o2.getIndex() - o1.getIndex() );
}
}
新牌:[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♦, 小🃏, 大🃏]
洗牌后:[6♥, 6♠, 2♦, 8♦, K♦, 2♣, 8♥, Q♣, 7♣, J♥, 5♥, 3♣, K♣, 8♠, Q♥, 9♦, Q♦, 4♣, 10♠, 小🃏, 3♥, K♠, 5♠, 9♠, 9♣, 4♠, 10♣, A♣, 3♠, 7♦, K♥, 7♠, 大🃏, 5♦, A♦, 6♦, J♦, 6♣, 7♥, 2♥, 3♦, 5♣, Q♠, 4♦, 10♦, A♠, 9♥, 2♠, J♠, 10♥, A♥, J♣, 8♣, 4♥]
-------------------------------------------------------------------------------------
孙悟空:[2♥, A♣, A♠, K♣, K♠, K♥, Q♠, J♥, J♦, J♠, 10♠, 9♦, 9♣, 8♦, 8♥, 6♥, 5♦]
猪八戒:[小🃏, A♦, K♦, Q♣, Q♦, 10♥, 9♥, 8♠, 7♠, 6♠, 6♣, 5♥, 5♠, 4♠, 4♦, 3♠, 3♦]
沙悟净:[大🃏, 2♦, 2♣, 2♠, A♥, Q♥, 10♣, 10♦, 9♠, 7♣, 7♦, 7♥, 6♦, 5♣, 4♣, 3♣, 3♥]
三张底牌:[J♣, 8♣, 4♥]
Process finished with exit code 0