1. Functions to be achieved:
Complete a licensing function (54 cards, count points, color; three players, of which the landlord has three more cards than the other players) by referring to the game rules of the landlord.
Random landlord, shuffle, licensing (licensing according to the conventional way A-B-C-A-B-C-A..., not allowed to one-time random 17 to a player), the player's hand card display (requires sorting by points)
2. The thought and process of editing code:
According to the rules of the landlord, there must be cards and players, so we should establish Player and Poke. Player class should contain the following attributes: id, name, ArrayList < Poke >, whether or not the landlord (boss); poker class should contain attributes: flower, point, size index (score), writing methods and implementation in the test class.
Firstly, the Player and Poke are established, and poker and players are added in the compiling method, then random landlord and licensing method, and finally, the card in the player's hand is displayed.
3. Implement functions through code:
Players and poker aren't shown. I'll show you how to test the class directly.
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Random; public class PokeGame { static List<Player> players; static ArrayList<Poke> list = new ArrayList<>(); static String[] flower = {"block","Plum blossom","Spade","Red heart"}; static String[] point = {"3","4","5","6","7","8","9","10", "J","Q","K","A", "2"}; static int bossIndex; static { // Initialize Poker for (int i = 0; i < flower.length; i++) { for (int j = 0; j < point.length; j++) { // Generate a poker object Poke p = new Poke(flower[i], point[j], j); list.add(p); } } // Increase Xiaowang alone list.add(new Poke("Small", "king", 13)); list.add(new Poke("large", "king", 14)); System.out.println(list); // Initialize players players = Arrays.asList(new Player(1, "Liu Bei"), new Player(2, "Guan Yu"), new Player(3, "Zhang Fei")); } /** Random landlord */ public void pushBoss() { Random r = new Random(); int i = r.nextInt(players.size()); // Set the random player as the landlord bossIndex = i; players.get(bossIndex).setBoss(true); } /** Licensing */ public void pushPoke() { Random r = new Random(); //Shuffle the cards Collections.shuffle(list); int ind = 0; for (int i = 0; i < 51; i++) { int index = r.nextInt(list.size()); if (ind < 3) { players.get(ind).getPokes().add(list.get(index)); } else { ind = 0; players.get(ind).getPokes().add(list.get(index)); } ind++; list.remove(index); } // Send the last three cards to the landlord players.get(bossIndex).getPokes().addAll(list); } //Display cards by card size public ArrayList<Poke> show(Player p){ Collections.sort(p.getPokes(),(a,b)->{ return a.getSort() - b.getSort(); }); return p.getPokes(); } public void start() { pushBoss(); pushPoke(); for (Player p : players) { System.out.println(p.getName() + ":" +show(p)); } } public static void main(String[] args) { new PokeGame().start(); } }