Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a fighting landlord with Java

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to write a fighting landlord with Java". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to write a fighting landlord with Java".

Train of thought:

1. To create a HashMap, the key is the number and the value is the card.

two。 Create an ArrayList and store the number.

3. Create an array of colors and points.

4. Starting at 0, the number is stored in the HashMap, and the corresponding cards are stored. At the same time, store the number in ArrayList.

5. Shuffle (shuffle is numbered), using Collections's shuffl () method to achieve.

6. Licensing (also issued with numbers, in order to ensure that the numbers are sorted, create a TreeSet collection.

7. Define the method to look at the cards (traverse the TreeSet collection, get the number, and go to the HashMap collection to find the corresponding cards)

8. Call the card-watching method

Specific code implementation: 1. Create collection pack playing cards / / create HashMap collection key is numbered with Integer value is card String HashMap hm=new HashMap (); / / create ArrayList collection to store serial number ArrayList list=new ArrayList (); / / create color array and point array String [] color= {"♠", "♦", "♥", "♣"} String [] number= {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"}; / / stores numbers and corresponding cards in the HashMap collection starting from 0. At the same time, store the serial number int index=0; / / enhanced For circular storage for (String num:number) {for (String col:color) {hm.put (index,col+num); list.add (index); index++ in the ArrayList collection. }} / / 52 cards have been saved and only big Wang is left. Now add hm.put (index, "Xiao Wang"); list.add (index); index++; hm.put (index, "King"); list.add (index). / / the above operation enables 54 playing cards to be put into a collection container. two。 Shuffle and shuffle / shuffle (shuffle is numbered), which is realized by collections's shuffle () method. Collections.shuffle (list); / / Licensing TreeSet receiver names TreeSet PDD=new TreeSet (); TreeSet DaSiMa=new TreeSet (); TreeSet LuBenWei=new TreeSet (); / / three cards TreeSet finalCard=new TreeSet (); for (int xcards); finalCard.add (3) {finalCard.add (a) } else if (x% 3 = = 0) {PDD.add (a);} else if (x% 3 = = 1) {DaSiMa.add (a);} else {LuBenWei.add (a);}} 3. Define the method of looking at cards / / define the method of looking at cards (traversing the TreeSet collection, getting the number, and going to the HashMap collection to find the corresponding card) public static void lookPoker (String name,TreeSet ts,HashMap hm) {System.out.print (the hand of name+ "is:"); / / traversal card is the traversal index for (Integer key:ts) {String poker = hm.get (key) System.out.print (poker+ ");} System.out.println ();} original code: package simulates fighting landlords; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.TreeSet / * demand: the functions of shuffling, dealing and looking at cards in the process of fighting landlords are realized through the program, and the cards should be sorted in order to facilitate the reading of cards. Idea: 1: create a HashMap collection. The key is the number and the value is the card. 2: create an Arraylist collection to store numbers. 3: create an array of colors and points. 4: starting from 0, store numbers in the HashMap collection and store the corresponding cards. At the same time, store numbers in the ArrayList collection. 5: shuffle (shuffle is numbered), realized by collections's shuffle () method. 6: deal (the number is also issued, in order to ensure that the number is sorted, create the TreeSet collection to receive 7: define the method to look at the cards (traverse the TreeSet collection to get the number Go to the HashMap collection to find the corresponding card) 8: call the method to see the card * / public class ChinesePoker {public static void main (String [] args) {/ / create the HashMap collection key is numbered with Integer value is card String HashMap hm=new HashMap () / / create an ArrayList collection to store the number ArrayList list=new ArrayList (); / / create an array of colors and dots String [] color= {"♠", "♦", "♥", "♣"} String [] number= {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"}; / / stores numbers and corresponding cards in the HashMap collection starting from 0. At the same time, store the serial number int index=0; / / enhanced For circular storage for (String num:number) {for (String col:color) {hm.put (index,col+num); list.add (index); index++ in the ArrayList collection. }} / / 52 cards have been saved and only big Wang is left. Now add hm.put (index, "Xiao Wang"); list.add (index); index++; hm.put (index, "King"); list.add (index). / / shuffle (shuffle is numbered), which is realized by the shuffle () method of collections. Collections.shuffle (list); / / Licensing TreeSet receiver names TreeSet PDD=new TreeSet (); TreeSet DaSiMa=new TreeSet (); TreeSet LuBenWei=new TreeSet (); / / three cards TreeSet finalCard=new TreeSet (); for (int xcards); finalCard.add (3) {finalCard.add (a) } else if (x% 3 = = 0) {PDD.add (a);} else if (x% 3 = = 1) {DaSiMa.add (a);} else {LuBenWei.add (a) }} / / call lookPoker ("PDD", PDD,hm); lookPoker ("Big Sima", DaSiMa,hm); lookPoker ("Lu Benwei", LuBenWei,hm); lookPoker ("cards", finalCard,hm) } / / define the method of looking at cards (traversing the TreeSet collection, getting the number, and going to the HashMap collection to find the corresponding card) public static void lookPoker (String name,TreeSet ts,HashMap hm) {System.out.print (the hand of name+ "is:") / / the traversal card is the traversal index for (Integer key:ts) {String poker = hm.get (key); System.out.print (poker+ "");} System.out.println () }} Thank you for your reading. The above is the content of "how to write a fight against landlords with Java". After the study of this article, I believe you have a deeper understanding of the problem of how to write a fight against landlords with Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 247

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report