1. Demand
Use a set object to store a deck of playing cards, disrupt the order of all playing cards, and then distribute them to three players and cards represented by the set, and print the set contents of players and cards.
2. Steps
- Buy a deck of playing cards and combine the colors and numbers to generate all ordinary cards. Manually add size King
- Shuffle: use the shuffle() method of the Collections tool class to shuffle the order of cards
- Deal: traverse the deck, distribute each card to three player sets, and leave three cards as cards
- Card reading: print the contents of each player set separately
2.1. Buy cards
Analysis 1
- Create numeric array: nums
- Create Decor array: colors
- Loop nested concatenation of two arrays
- Concat method of String class: colors [i] Concat (Num s [J]) (X.concat(Y) outputs XY; Y. Concat (x) counts (the result is YX)
package cn.itcast.demo2; public class SendPokerTest { public static void main(String[] args) { //Create an array of numbers and decors String[] nums = {"3","4","5","6","7","8","9","K","Q","J","A","2"}; String[] colors = {"♣","♦","♥","♠"}; //Splice two arrays for (int i = 0; i < nums.length; i++) { for (int j = 0; j < colors.length; j++) { colors[j].concat(nums[i]); System.out.print(colors[j].concat(nums[i])+" "); } } } }
Analysis 2
- Define a double column set, set the number with the key, and the value indicates the specific card rule: the smaller the number, the smaller the card (how to set the number for cards with different colors and the same size, and why to set the number)
- Define a single column set to store numbers
- Loop nested two arrays, add cards to the double column set, and add numbers to the single column set
- Manually add size King
package cn.itcast.demo2; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class SendPokerTest { public static void main(String[] args) { //Create an array of numbers and decors String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; String[] colors = {"♣","♦","♥","♠"}; //Define a num to represent the number int num = 0; //Define a two column set that stores cards and card numbers Map<Integer,String> pokers = new HashMap<>(); //Define a single column set to store card numbers List<Integer> list = new ArrayList<>(); //Loop nested concatenation of two arrays for (String number : numbers) {//External circulation traversal deck for (String color : colors) {//Inner loop traversal Decor //Splice two arrays as the values of a two column set String poker = color + number ; //Add it to the two column set pokers.put(num,poker); //Adds a number to a single column set list.add(num); //The number will increase by 1 for each addition num++; } } //Print 52 cards currently generated System.out.println("The 52 cards in the double row set are:" + pokers); //Print numbers in a single column set System.out.println("The current 52 numbers are:" + list); } }
2.2. shuffle the cards
- Shuffle the cards by using the shuffle () method in the Collections tool class
2.3. Licensing
-
Create 4 collection objects, three representing player collection objects and one representing cards collection objects
-
Licensing action:
-
Take the remainder of the index and 3. If the remainder is 0, the card will be issued to the first player. If the remainder is 1, it will be issued to the second player, and the remainder is 2, it will be issued to the third player
-
If only the last three cards are left, they will be stored in the card set object
if(i >= list.size()-3) { dipai.add(list[i]); }
-
-
Display board
package cn.itcast.demo2; import java.util.*; public class SendPokerTest { public static void main(String[] args) { //Create an array of numbers and decors String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; String[] colors = {"♣","♦","♥","♠"}; //Define a num to represent the number int num = 0; //Define a two column set that stores cards and card numbers Map<Integer,String> pokers = new HashMap<>(); //Define a single column set to store card numbers List<Integer> list = new ArrayList<>(); //Loop nested concatenation of two arrays for (String number : numbers) {//External circulation traversal deck for (String color : colors) {//Inner loop traversal Decor //Splice two arrays as the values of a two column set String poker = color + number ; //Add it to the two column set pokers.put(num,poker); //Adds a number to a single column set list.add(num); //The number will increase by 1 for each addition num++; } } //Add size King pokers.put(num,"Xiao Wang"); list.add(num++); pokers.put(num,"king"); list.add(num); //Print all cards System.out.println("The 54 cards in the double row set are:" + pokers); //Print numbers in a single column set System.out.println("54 Number:" + list); //shuffle the cards Collections.shuffle(list); System.out.println("Shuffled number:" + list); //Licensing //1. Create 4 collection objects List<Integer> player1 = new ArrayList<>(); List<Integer> player2 = new ArrayList<>(); List<Integer> player3 = new ArrayList<>(); List<Integer> restPoker = new ArrayList<>(); //2. Traverse the list after disorder and deal cards for (int i = 0; i < list.size(); i++) { //Gets the index sequence number in the list Integer pokerNum = list.get(i); //Licensing according to index if (i >= list.size() - 3) { restPoker.add(list.get(i)); } else if (i % 3 == 0) { player1.add(list.get(i)); } else if (i % 3 == 1) { player2.add(list.get(i)); } else if (i % 3 == 2) { player3.add(list.get(i)); } } System.out.println("player1 :" + player1); System.out.println("player2 :" + player2); System.out.println("player3 :" + player3); System.out.println("restPoker :" + restPoker); } }
2.4. Look at the cards
Define a way to look at cards
- Arrange the player's cards and cards in order from small to large
- Traverse the single column set, and use the elements in the single column set to obtain the values in the double column set
- Defines a variable length StringBuilder object for splicing strings
- Returns a string with leading and trailing spaces removed
- The method is called in the main method.
package cn.itcast.demo2; import java.util.*; public class SendPokerTest { public static void main(String[] args) { //Create an array of numbers and decors String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; String[] colors = {"♣","♦","♥","♠"}; //Define a num to represent the number int num = 0; //Define a two column set that stores cards and card numbers Map<Integer,String> pokers = new HashMap<>(); //Define a single column set to store card numbers List<Integer> list = new ArrayList<>(); //Loop nested concatenation of two arrays for (String number : numbers) {//External circulation traversal deck for (String color : colors) {//Inner loop traversal Decor //Splice two arrays as the values of a two column set String poker = color + number ; //Add it to the two column set pokers.put(num,poker); //Adds a number to a single column set list.add(num); //The number will increase by 1 for each addition num++; } } //Add size King pokers.put(num,"Xiao Wang"); list.add(num++); pokers.put(num,"king"); list.add(num); //Print all cards System.out.println("The 54 cards in the double row set are:" + pokers); //Print numbers in a single column set System.out.println("54 Number:" + list); //shuffle the cards Collections.shuffle(list); System.out.println("Shuffled number:" + list); //Licensing //1. Create 4 collection objects List<Integer> player1 = new ArrayList<>(); List<Integer> player2 = new ArrayList<>(); List<Integer> player3 = new ArrayList<>(); List<Integer> restPoker = new ArrayList<>(); //2. Traverse the list after disorder and deal cards for (int i = 0; i < list.size(); i++) { //Gets the index sequence number in the list Integer pokerNum = list.get(i); //Licensing according to index if (i >= list.size() - 3) { restPoker.add(list.get(i)); } else if (i % 3 == 0) { player1.add(list.get(i)); } else if (i % 3 == 1) { player2.add(list.get(i)); } else if (i % 3 == 2) { player3.add(list.get(i)); } } //Output player cards and cards System.out.println("player1 :" + printPoker(player1,pokers)); System.out.println("player2 :" + printPoker(player2,pokers)); System.out.println("player3 :" + printPoker(player3,pokers)); System.out.println("restPoker :" + printPoker(restPoker,pokers)); } /*Define a method for watching cards * Method name: printPoker() * Parameter list: output specific cards according to the number * List<Integer> nums, Map<Integer,String> poker*/ public static String printPoker(List<Integer> nums,Map<Integer,String> pokers) { //Arrange the cards from small to large Collections.sort(nums); //Traverse the single column set and obtain the specific cards in the double column set according to the set element number StringBuilder sb = new StringBuilder(); for (Integer num : nums) { //Obtain the specific cards in the double column set according to the elements obtained in the list set (i.e. the keys in the double column set) String eachPoke = pokers.get(num); //Splice the values of three players and cards in the deck set sb.append(eachPoke + " "); } String str = sb.toString(); return str.trim(); } }