Landlords game case development
Business needs analysis :
Playing cards against landlords , Shuffle , Licensing , Sort ( Expand knowledge ), Look at cards .
Business : All in all 54 card .
points : "3","4","5","6","7","8","9","10","J","Q","K","A","2"
Design and color : "", "", "", ""
Big and small king : "" , ""
The points should be combined separately 4 Plant designs , One for each king .
fight against landlords : issue 51 card , be left over 3 As a card .
Function realization :
1. Make cards .
2. Shuffle .
3. Definition 3 Players
4. Licensing .
5. Sort ( expand )
6. Look at cards
Code section :
Card class and its attribute method :
public class Card {
private String size;
private String color;
private int index; // The real size of the card
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;
}
}
game Implementation class :
public class GameDemo {
/**
1、 Define a static collection storage 54 Card object
*/
public static List<Card> allCards = new ArrayList<>();
/**
2、 Make cards : Define static code block initialization card data
*/
static {
// 3、 Define the number of points : The number is determined , Type determination , Using arrays
String[] sizes = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
// 4、 Define decor : The number is determined , Type determination , Using arrays
String[] colors = {"", "", "", ""};
// 5、 Combine points and decors
int index = 0; // The size of the record board
for (String size : sizes) {
index++;
for (String color : colors) {
// 6、 Encapsulated into a card object .
Card c = new Card(size, color, index);
// 7、 Put it into the collection container
allCards.add(c);
}
}
// 8 The size king is stored in the collection object "" , ""
Card c1 = new Card("" , "", ++index);
Card c2 = new Card("" , "",++index);
Collections.addAll(allCards , c1 , c2);
System.out.println(" New card :" + allCards);
}
public static void main(String[] args) {
// 9. Shuffle
Collections.shuffle(allCards);
System.out.println(" After the shuffle " + allCards);
// 10. Licensing ( Define three players , Each player's card is also a collection container )
List<Card> lingchuchong = new ArrayList<>();
List<Card> jiumozhi = new ArrayList<>();
List<Card> renyingying = new ArrayList<>();
// 11. Licensing ( Issue from the card set 51 Cards are given to three players , leave 3 Zhang Di card )
for (int i = 0; i < allCards.size() - 3; i++) {
// Get the current card object first
Card c = allCards.get(i);
if (i % 3 == 0) {
// Please take the card with ah Chong
lingchuchong.add(c);
} else if (i % 3 == 1) {
jiumozhi.add(c);
} else if (i % 3 == 2) {
renyingying.add(c);
}
}
// 12. Get the last three cards ( Cut the last three cards into a subset )
List<Card> lastThreeCards = allCards.subList(allCards.size() - 3, allCards.size());
// 13. Sort players' cards ( From big to small )
sortCards(lingchuchong);
sortCards(jiumozhi);
sortCards(renyingying);
// 14. Output player's cards And cards
System.out.println(" Linghuchong's card :"+lingchuchong);
System.out.println(" Jiumozhi's card :"+jiumozhi);
System.out.println(" Ren Yingying's card :"+renyingying);
System.out.println(" Three cards :"+lastThreeCards);
}
/*
public static <T> void sort(List<T> list, Comparator<? super T> c) {
list.sort(c);
}
*/
// public static void sortCards(List<Card> cards) {
// Collections.sort(cards, new Comparator<Card>() {
// @Override
// public int compare(Card o1, Card o2) {
// return o1.getIndex() - o2.getIndex();
// }
// });
//
// }
public static void sortCards(List<Card> cards) {Collections.sort(cards, (o1, o2) -> o2.getIndex() - o1.getIndex());}
}
summary :
First, define a static collection storage 54 Card object , Then initialize the card with a static code block , Combine the decor size of the card and package it into an object , Stored in a static collection container , Use Collections Tools to shuffle , And define three player objects , Send the disordered cards to three objects in order , Rewrite the comparison rules , According to the rules , Press the cards of three objects Descending Sort and output .