2021-10-14 simulated landlords (Application of collection)

Posted by m@tt on Sun, 17 Oct 2021 06:15:07 +0200

catalogue

demand

thinking

program

One Card

The constants of decor, number and size King define the Constant class

Poker class

Player class

Card grabbing main program

technological process:

Output results

demand

Sort out the JAVA set knowledge with the program simulating the landlords' grasping cards. The program requirements are as follows:

1. Generate a set of 54 playing cards and shuffle the order of cards;
2. Three people play against the landlord: Player 1, player 2 is a farmer, and player 3 is the landlord;
3. The three players hold cards respectively, leave three cards to the landlord, and show the cards obtained by the three players respectively.

thinking

  • Generate 54 playing cards, create the class of playing cards, create the member variable of collection type in the class, and place 54 cards. The number is 1-13; The designs and colors include spades, hearts, plum blossoms, square pieces and king of size.
  • Shuffle the colors and order. Provide a shuffle member method in the poker class, and call this member method to realize the order of continental arrangement.
  • The member variables of the three players include name, player type, etc; The player class needs to create a member variable of the collection type to save the cards owned by the player.
  • When the three players catch cards, they should leave three cards to the landlord. The player class is required to create a member method to catch cards and traverse the poker object, including 17 farmers and 20 landlords.
  • Display the cards obtained by three players respectively, and create a member method to display the captured cards in the player class.

program

One Card

Define decors and numbers

public class Card {

    //Decor
    private String color;

    //number
    private String number = "";

    public Card() {

    }
    
    public Card(String color) {
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    @Override
    public String toString() {
        return this.color + this.number;
    }
}

The constants of decor, number and size King define the Constant class

Defines the decor, number array, and size king

public class Constant {

     //Card decors: spades, hearts, clubs, diamonds
     static String[] COLORS={"♠","♡","♣","♢"};

     //Card number
     static String[] NUMBERS={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

     static String RED_JOKER="king";
     static String BLACK_JOKER="Xiao Wang";
}

Poker class

It defines the member variable of the collection type, which stores 54 cards (single card object) and the number of the whole deck of cards.

Define the shuffle member method, and use the static method shuffle of the Collections tool class to randomly disrupt the order of cards.

By rewriting the toString method, the traversal output of playing cards is carried out.

public class Poker {

    private List<Card> cards;

    private Integer cardCount;

    //shuffle the cards
    public void shuffle() {
        Collections.shuffle(this.cards);
    }

    public Poker() {

        List<Card> cardList = new ArrayList<>();
        for (int i = 0; i < Constant.COLORS.length; i++) {
            for (int j = 0; j < Constant.NUMBERS.length; j++) {
                Card card = new Card();
                card.setColor(Constant.COLORS[i]);
                card.setNumber(Constant.NUMBERS[j]);
                cardList.add(card);
            }
        }
        cardList.add(new Card(Constant.RED_JOKER));
        cardList.add(new Card(Constant.BLACK_JOKER));
        this.cards = cardList;
        this.cardCount = cardList.size();
    }

    public List<Card> getCards() {
        return cards;
    }

    public void setCards(List<Card> cards) {
        this.cards = cards;
    }

    public Integer getCardCount() {
        return cardCount;
    }

    @Override
    public String toString() {
        String poker = "[";
        Iterator<Card> iterator = this.cards.iterator();
        while (iterator.hasNext()) {
            poker += iterator.next().toString()+",";
        }
        poker += "]";
        return poker;
    }
}

Player class

Defines the member variables name and type, and defines the member variables of the collection type to store the cards caught by the player.

Define the draw member method and put the caught cards into the member variable cards.

By rewriting the toString method, traverse and output the cards caught by the player.

public class Player {

    // Player name
    private String name;

    // Player type: farmer / landlord
    private String type;

    //Caught cards
    private List<Card> cards;

    //Grab cards
    public void draw(Card card) {
        this.cards.add(card);
    }

    public Player(String name, String type) {
        this.name = name;
        this.type = type;
        this.cards = new ArrayList<>();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<Card> getCards() {
        return cards;
    }

    public void setCards(List<Card> cards) {
        this.cards = cards;
    }

    @Override
    public String toString() {
        String poker = "[";
        Iterator<Card> iterator = this.cards.iterator();
        while (iterator.hasNext()) {
            poker += iterator.next().toString() + ",";
        }
        poker += "]";
        return this.getType() + ":" + this.getName() + "Catch" + this.cards.size() + "Card:" + poker;
    }
}

Card grabbing main program

technological process:

  1. Generate a new deck of playing cards
  2. shuffle the cards
  3. Generate three players: two farmers and one landlord
  4. Draw cards in turn until there are three cards left
  5. The landlord caught the cards
  6. Show the cards caught by the three players respectively
public class PlayDemo {

    public static void main(String[] args) {

        //Generate a deck of playing cards and shuffle them
        Poker poker = new Poker();
        poker.shuffle();
        System.out.println("Poker total" + poker.getCardCount() + "only:" + poker.toString());

        //Generate three players and set two as farmers and one as landlord
        int playCount = 3;
        Player player1 = new Player("Player 1", "farmer");
        Player player2 = new Player("Player 2", "farmer");
        Player player3 = new Player("Player 2", "landlord");

        //Three players grab cards in order
        int i;
        for (i = 0; i < poker.getCardCount() - playCount; i++) {
            if (i % playCount == 0) {
                player1.draw(poker.getCards().get(i));
            } else if (i % playCount == 1) {
                player2.draw(poker.getCards().get(i));
            } else if (i % playCount == 2) {
                player3.draw(poker.getCards().get(i));
            }
        }

        //The landlord takes the remaining cards
        while (i < poker.getCardCount()) {
            player3.draw(poker.getCards().get(i));
            i++;
        }

        //Show the cards obtained by three players
        System.out.println(player1.toString());
        System.out.println(player2.toString());
        System.out.println(player3.toString());

    }

}

Topics: Java Algorithm