If you go to work and play Fighting Landlords like this, your boss should not scold you.....

Copyright Notice

  • The original author of this article: Gu's younger brother
  • Author's blog address: http://blog.csdn.net/lfdfhl

Wet feet

Originally, I happily wrote code at the workstation. Suddenly, the administrative commissioner asked me to go to the boss. My instinct tells me: I guess it's a crime. Sure enough, as expected; when I arrived at the office, the boss gave me a slap in the face and criticized me: The interns in your department go to work and play games, fighting the landlord, haven't you seen it? How is the work arranged? Do you fish in troubled waters every day?

Insert picture description here

When the boss finished venting, I returned to the office; I just ran into an apologetic intern head-on. Seeing the drooping head of the children's shoes, I was a little angry and wanted to laugh. I wanted to say something; after thinking about it, forget it. Who has never been young? Why should I accuse him too much? So I took him to the conference room and chatted briefly.

"Children's shoes, it's not right to play games at work; even if you want to fish, don't play blatantly on the computer."

"Well, I won't play next time"

"No, you can play."


"You write a program to play. You both exercise programming and waste time."

"No. It's too difficult. I haven't graduated yet."

"It's hard?"


"Come and I will write you a look"

Combing the game of Fighting Landlords

Everyone is very familiar with the game of Doudizhu. Therefore, here is a brief summary of the relevant elements in the game.

game rules

  • 1. 1 deck of playing cards
  • 2, 3 players
  • 3. Each player has 17 cards
  • 4. 3 hole cards

Principle analysis

After understanding the rules, our focus can be on the realization of the game.

Representation of cards

Except for the king and king, each card is composed of suits (for example: clubs, hearts, etc.) and symbols (for example: 2, 3, K, Q, etc.). Therefore, we can define two arrays to represent suits and symbols:

// 牌的花色
public String[] colorArray = { "♠", "♥", "♣", "♦" };
// 牌的符号
public String[] numberArray = { "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2" };

If we combine the array colorArray and the numberArray array, can't we represent the whole set of poker?

Card access

To facilitate storage, we can associate each card with a unique number. With the idea, you need to consider the choice of storage tools. At this point, TreeMap is a very good choice.

Summary of key points:

  • 1. The serial number of the card starts from 1
  • 2. Use the ArrayList collection to store the serial number corresponding to each card
  • 3. Use the TreeMap collection to store each card
// 每张牌所对应的序号
public int cardSerialNumber = 1;
// 存放每张牌所对应的序号的ArrayList集合
public ArrayList<Integer> cardSerialNumberList = new ArrayList<Integer>();
// 存放每张牌的TreeMap集合
public TreeMap<Integer, String> cardTreeMap = new TreeMap<Integer, String>();

Representation of hole cards

Similarly, we leave 3 hole cards and use the TreeSet collection to store them.

// 底牌数
int remainCards = 3;
// 存放底牌的TreeSet集合
TreeSet<Integer> remainCardsTreeSet = new TreeSet<Integer>();

Player's representation

3 players participate in the game, and each player holds 17 poker cards. Therefore, we can use the TreeSet collection to represent the cards held by each player.

// 三位玩家
public TreeSet<Integer> player1CardsTreeSet = new TreeSet<Integer>();
public TreeSet<Integer> player2CardsTreeSet = new TreeSet<Integer>();
public TreeSet<Integer> player3CardsTreeSet = new TreeSet<Integer>();

Landlord game realization

OK, after sorting out the relevant elements in the game, we can start coding.

Game preparation

Before we officially start the game, we need to have a deck of cards, that is to say: we need to place each card in the same set.

	public void prepareCard() {
		// 将大王所对应的序号存放至cardSerialNumberList
		// 将大王存放至cardTreeMap
		cardTreeMap.put(cardSerialNumber, "RED-JOCKER");

		// 将小王所对应的序号存放至cardSerialNumberList
		// 将小王存放至cardTreeMap
		cardTreeMap.put(cardSerialNumber, "BLACK-JOCKER");

		// 将除大小王以外的牌所对应的序号存放至cardSerialNumberList
		// 将除大小王以外的牌存放至cardTreeMap
		for (int i = 0; i < numberArray.length; i++) {
			for (int j = 0; j < colorArray.length; j++) {
				String card = colorArray[j] + numberArray[i];
				cardTreeMap.put(cardSerialNumber, card);


With the playing cards, we need to shuffle the cards before the cards are dealt. Here, use the Collections.shuffle() function to disrupt the original order in cardSerialNumberList.

public void shuffleCard() {


Each player takes turns to get a card, and finally there are three hole cards remaining.

Insert picture description here
	public void dealCard() {
		for (int i = 0; i < cardSerialNumberList.size(); i++) {
			Integer serialNumber = cardSerialNumberList.get(i);
			int index = i % remainCards;
			if (i >= cardSerialNumberList.size() - remainCards) {
			} else if (index == 0) {
			} else if (index == 1) {
			} else if (index == 2) {

Look at the cards

After the cards are dealt, let's take a look at the cards held by each player.

Insert picture description here
	public void showCard() {
		for (Integer i : player1CardsTreeSet) {
			System.out.print(cardTreeMap.get(i) + " ");

		for (Integer i : player2CardsTreeSet) {
			System.out.print(cardTreeMap.get(i) + " ");
		for (Integer i : player3CardsTreeSet) {
			System.out.print(cardTreeMap.get(i) + " ");
		for (Integer i : remainCardsTreeSet) {
			System.out.print(cardTreeMap.get(i) + " ");


Insert picture description here
Insert picture description here

Cards in hand and life

After talking about this small demo to the intern children's shoes, I remembered what the extrajudicial fanatic teacher Zhang San Luoxiang said.

Insert picture description here

Eisenhower's mother said to him: When you are playing cards, even if you catch the worst card, you can't abandon the game. What do you have to do with this card? Finish the call.

Do we often feel that the cards we draw are really bad? However, you should play the worst cards. Sometimes we are surprised to find that the worst cards sometimes win in the end. Is there such a possibility? There is.

If you encounter setbacks in your life, you have two options: one option is to give up the performance; the other option is to perform the established script well.

Reading these, I have a lot of feelings. The cards are dealt randomly, and we can't change them. What we can grasp and change is how to play the cards in our hands. Therefore, young children's shoes: live up to the time, live up to the Qing; live up to the spring, live up to yourself; hurry up and try to make yourself better.