Example of a poker game with JAVA collection set

  • 2020-05-12 02:40:47
  • OfStack

The root interface in the Collection hierarchy. Collection represents a group of objects, also known as elements of collection. Some collection allow duplicate elements, while others do not. Some of the collection are ordered, while others are disordered. JDK does not provide any direct implementation of this interface: it provides more specific sub-interface implementations, such as Set and List. This interface is typically used to pass collection and operate these collection where the greatest generality is required.

Main content: collection set is used here to simulate the poker game played by the big shots in Hong Kong movies.

1. Rules of the game: two players will each deal two CARDS and compare them. Compare the largest number of points in each player's hand, the size is A-2, and the player with the largest number wins. If the number of points is the same, the suit will be compared, and the size will be black (4), red (3), plum (2), and square (1).

2. Implementation steps:

Create a deck of playing CARDS A-2, a total of 52 CARDS in 4 suits: black (4), red (3), plum (2), and square (1).
Create two players containing player ID and name, licensed Card information;
Shuffle and deal two CARDS to each player;
Compare the size of the CARDS in a player's hand to get the winner;

3. Program implementation

Card Card class: contains the number and suit of a card


package collectiontest.games;
public class Card {
  private Integer id; // The size of the card 
  private Integer type;// The design and color of card 
  
  public Card(Integer id, Integer type) {
    this.id = id;
    this.type = type;
  }
  public Integer getId() {
    return id;
  }
  public void setId(Integer id) {
    this.id = id;
  }
  public Integer getType() {
    return type;
  }
  public void setType(Integer type) {
    this.type = type;
  }
  @Override
  public String toString() {
    return "Card [id=" + id + ", type=" + type + "]";
  }  
}

Poker Poker class: contains poker Card A-2


package collectiontest.games;
public class Poker {
  private Card id2 ;
  private Card id3 ;
  private Card id4 ;
  private Card id5 ;
  private Card id6 ;
  private Card id7 ;
  private Card id8 ;
  private Card id9 ;
  private Card id10 ;
  private Card J ;
  private Card Q ;
  private Card K ;
  private Card A ;
  
  public Poker() {
  }    
  //4 Types: black --4 , red, --3 , mei --2 , square --1
  public Poker(Integer type) {
    this.id2 = new Card(2, type);
    this.id3 = new Card(3, type);
    this.id4 = new Card(4, type);
    this.id5 = new Card(5, type);
    this.id6 = new Card(6, type);
    this.id7 = new Card(7, type);
    this.id8 = new Card(8, type);
    this.id9 = new Card(9, type);
    this.id10 = new Card(10, type);
    this.J = new Card(11, type);
    this.Q = new Card(12, type);
    this.K = new Card(13, type);
    this.A = new Card(14, type);
  }
  public Card getId2() {
    return id2;
  }
  public void setId2(Card id2) {
    this.id2 = id2;
  }
  public Card getId3() {
    return id3;
  }
  public void setId3(Card id3) {
    this.id3 = id3;
  }
  public Card getId4() {
    return id4;
  }
  public void setId4(Card id4) {
    this.id4 = id4;
  }
  public Card getId5() {
    return id5;
  }
  public void setId5(Card id5) {
    this.id5 = id5;
  }
  public Card getId6() {
    return id6;
  }
  public void setId6(Card id6) {
    this.id6 = id6;
  }
  public Card getId7() {
    return id7;
  }
  public void setId7(Card id7) {
    this.id7 = id7;
  }
  public Card getId8() {
    return id8;
  }
  public void setId8(Card id8) {
    this.id8 = id8;
  }

  public Card getId9() {
    return id9;
  }
  public void setId9(Card id9) {
    this.id9 = id9;
  }
  public Card getId10() {
    return id10;
  }
  public void setId10(Card id10) {
    this.id10 = id10;
  }
  public Card getJ() {
    return J;
  }
  public void setJ(Card j) {
    J = j;
  }
  public Card getQ() {
    return Q;
  }
  public void setQ(Card q) {
    Q = q;
  }
  public Card getK() {
    return K;
  }
  public void setK(Card k) {
    K = k;
  }
  public Card getA() {
    return A;
  }
  public void setA(Card a) {
    A = a;
  }
}

Player Player class: contains player ID and name and card information


 package collectiontest.games;
import java.util.ArrayList;
import java.util.List;

public class Player {
  // The players' ID
  private String id ;
  // The player's name 
  private String name ;
  // The player's card 
  private List<Card> pokerType ;
  
  public Player() {  
  }
  public Player(String id, String name, List<Card> pokerType) {
    this.id = id;
    this.name = name;
    this.pokerType = new ArrayList<>();
  }

  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public List<Card> getPokerType() {
    return pokerType;
  }
  public void setPokerType(List<Card> pokerType) {
    this.pokerType = pokerType;
  }  
}

Poker game main category: includes 1) poker creation 2) player creation 3) shuffle 4) deal 5) win or lose


package collectiontest.games;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class GamsBegin {

  //  Create playing CARDS 
  public Set<Poker> cPoker() {
    
    System.out.println("********** Start creating playing CARDS **********");
    //  create 1 vice poker
    // 4 Types: black --4 , red, --3 , mei --2 , square --1
    Set<Poker> pokers = new HashSet<>();
    Poker[] poker = { new Poker(1), new Poker(2), new Poker(3),
        new Poker(4) };
    /*
     * Collections The use of utility classes 
     * Collections.addAll(pokers, new Poker(1), new Poker(2), new Poker(3),new Poker(4));
     *
     * */
    pokers.addAll(Arrays.asList(poker));

    System.out.println("********** Playing card created successfully **********");

    return pokers;
  }

  //  Create two players 
  public Map<String, Player> cPlayer() {
    
    System.out.println("********** Start building players **********");
    Map<String, Player> map = new HashMap<String, Player>();
    //  Control the number of 
    Integer control = 0;

    System.out.println(" Create two players, as prompted ");
    Scanner console = new Scanner(System.in);
    while (true) {
      System.out.println(" Please enter the first  "+(control+1)+"  A player ID : ");
      String courseId = console.next();

      if (isNumeric(courseId)) {
        System.out.println(" Please enter the first  "+(control+1)+"  Player names: ");
        String courseName = console.next();

        Player players = new Player(courseId, courseName, null);
        // Save the data 
        map.put(courseId, players);

        System.out.println(" Add the first  " + (control + 1) + "  A player  " + courseName
            + "  successful ");
        // The number of add 
        control++;
      } else {
        System.out.println("***** Please enter a number ID*****");
        continue;
      }

      if (control == 2) {
        break;
      }

    }

    System.out.println("********** Player created successfully **********");

    return map;
  }

  //  Determine if the input is a number , Character.isDigit() for java methods 
  public boolean isNumeric(String str) {
    for (int i = 0; i < str.length(); i++) {
      if (!Character.isDigit(str.charAt(i))) {
        return false;
      }
    }
    return true;
  }

  /**
   *  Shuffle the deck   : can also be produced 52 Different random Numbers, shuffling 
   *
   **/
  public List<Card> wPoker(Set<Poker> pokers) {
    System.out.println("********** Began to shuffle **********");
    // using List The order of sorting, after the shuffle to save the order unchanged 
    List<Card> listCard = new ArrayList<>();
    //  using Set The unordered order of the set, the realization of shuffling 
    Set<Card> listSet = new HashSet<>();

    // Save to Set Set, disorder 
    for (Poker pk : pokers) {
      listSet.add(pk.getId2());
      listSet.add(pk.getId3());
      listSet.add(pk.getId4());
      listSet.add(pk.getId5());
      listSet.add(pk.getId6());
      listSet.add(pk.getId7());
      listSet.add(pk.getId8());
      listSet.add(pk.getId9());
      listSet.add(pk.getId10());
      listSet.add(pk.getJ());
      listSet.add(pk.getQ());
      listSet.add(pk.getK());
      listSet.add(pk.getA());
    }

    // Stored in the List A collection of , The orderly 
    for (Card cd : listSet) {
      listCard.add(cd);
      System.out.println(cd);
    }
    
    System.out.println("********** Reshuffle success **********");

    return listCard;
  }

  //  licensing 
  public Map<String, Player> pushPoker(List<Card> listCard,
      Map<String, Player> pMap) {
    System.out.println("********** Licensing began **********");
    
    //  Control ends after each player has dealt two CARDS 
    int control = 0;

    for (Map.Entry<String, Player> entry : pMap.entrySet()) {

      if (control == 0) {
        for (int i = 0; i < 3; i = i + 2) {
          //  licensing 
          entry.getValue().getPokerType().add(listCard.get(i));
        }
        //  update map object 
        pMap.put(entry.getKey(), entry.getValue());
        control++;
      } else if (control == 1) {
        for (int i = 1; i < 4; i = i + 2) {
          //  licensing 
          entry.getValue().getPokerType().add(listCard.get(i));
        }
        //  update map object 
        pMap.put(entry.getKey(), entry.getValue());
        control++;
      } else {
        break;
      }
    }

    System.out.println("********** Licensing success **********");

    return pMap;
  }


  public void compareMatch(Map<String, Player> newMap) {
  
    /* To compare the outcome 
     * 1. First get the biggest player in each player's hand ID And design and color ID . 
     * 2. Compare the two players' biggest names ID The big card wins. 
     * 3. If I have two CARDS ID Equal, comparing the suits of two CARDS ID , design and color ID More to win. 
     * 
     * */ 
    
    List<Player> players = new ArrayList<>();

    //  Get two players 
    for (Map.Entry<String, Player> entry : newMap.entrySet()) {
      players.add(entry.getValue());
    }

    //  The player 1 Information and licences 
    List<Card> playerOne = players.get(0).getPokerType();
    // Get the biggest name ID And design and color 
    Integer oneMaxId = Math.max(playerOne.get(0).getId(), playerOne.get(1)
        .getId());
    Integer oneMaxType = (oneMaxId!=playerOne.get(0).getId()) ? playerOne.get(1).getType() : playerOne.get(0).getType() ;

    //  The player 2 Information and licences 
    List<Card> playerTwo = players.get(1).getPokerType();
    // Get the biggest name ID And design and color 
    Integer twoMaxId = Math.max(playerTwo.get(0).getId(), playerTwo.get(1)
        .getId());
    Integer twoMaxType = (twoMaxId!=playerTwo.get(0).getId()) ? playerTwo.get(1).getType() : playerTwo.get(0).getType() ;

    if (oneMaxId > twoMaxId) {
      System.out.println(" The player   :  " + players.get(0).getName() + "  Win!!!!! ");
    } else if (oneMaxId == twoMaxId) {

      if (oneMaxType > twoMaxType) {
        System.out
            .println(" The player   :  " + players.get(0).getName() + "  Win!!!!! ");

      } else {
        System.out
            .println(" The player   :  " + players.get(1).getName() + "  Win!!!!! ");
      }

    } else {
      System.out.println(" The player   :  " + players.get(1).getName() + "  Win!!!!! ");
    }

    System.out.println("**********************************************");
    System.out.println(" The player   :  " + players.get(0).getName() + " The card is: "
        + showName(playerOne.get(0).getType(), 0) + "--"
        + showName(playerOne.get(0).getId(), 1) + "  "
        + showName(playerOne.get(1).getType(), 0) + "--"
        + showName(playerOne.get(1).getId(), 1));
    System.out.println(" The player   :  " + players.get(1).getName() + " The card is: "
        + showName(playerTwo.get(0).getType(), 0) + "--"
        + showName(playerTwo.get(0).getId(), 1) + "  "
        + showName(playerTwo.get(1).getType(), 0) + "--"
        + showName(playerTwo.get(1).getId(), 1));
  }

  //  The name of the board 
  private String showName(Integer i, Integer type) {
    String str = "";

    //  Display design and color 
    if (type == 0) {
      switch (i) {
      case 1: {
        str = " square ";
        break;
      }
      case 2: {
        str = " The plum blossom ";
        break;
      }
      case 3: {
        str = " Red peach ";
        break;
      }
      case 4: {
        str = " spades ";
        break;
      }

      default: {
        break;
      }
      }

    }

    //  According to digital 
    if (type == 1) {
      if (i < 11) {
        return i.toString();
      } else {
        switch (i) {
        case 11: {
          str = "J";
          break;
        }
        case 12: {
          str = "Q";
          break;
        }
        case 13: {
          str = "K";
          break;
        }
        case 14: {
          str = "A";
          break;
        }

        default: {
          break;
        }
        }
      }
    }

    return str;
  }

  public static void main(String[] args) {
    GamsBegin gb = new GamsBegin();
    
    // 1 Create playing CARDS 
    Set<Poker> pokers = gb.cPoker();

    // 2 Create two players 
    Map<String, Player> pMap = gb.cPlayer();

    // 3 , shuffling 
    List<Card> listCard = gb.wPoker(pokers);

    // 4 And licensing 
    Map<String, Player> newMap = gb.pushPoker(listCard, pMap);

    // 4 Compare winners and losers 
    gb.compareMatch(newMap);

  }
}

Operation results:

********** ********** *********
********** ********** **********
********** ********** ********** ****
Create two players, as prompted
Please enter the first player ID:
Please enter the name of the first player:
Stephen chow
Added the first player Stephen chow successfully
Please enter the second player ID:
Please enter the name of the second player:
Chow yun-fat
Added the second player chow yun-fat successfully
********** ********** *****
********** ********** *****
Card [id=9, type=3]
Card [id=11, type=4]
Card [id=13, type=3]
Card [id=8, type=3]
Card [id=5, type=2]
Card [id=6, type=1]
Card [id=4, type=3]
Card [id=5, type=4]
Card [id=2, type=3]
Card [id=9, type=2]
Card [id=9, type=4]
Card [id=14, type=2]
Card [id=9, type=1]
Card [id=2, type=1]
Card [id=2, type=4]
Card [id=7, type=4]
Card [id=11, type=1]
Card [id=10, type=1]
Card [id=14, type=4]
Card [id=14, type=3]
Card [id=12, type=2]
Card [id=2, type=2]
Card [id=10, type=2]
Card [id=7, type=1]
Card [id=7, type=3]
Card [id=8, type=2]
Card [id=4, type=4]
Card [id=13, type=4]
Card [id=14, type=1]
Card [id=12, type=1]
Card [id=5, type=1]
Card [id=6, type=4]
Card [id=12, type=4]
Card [id=11, type=2]
Card [id=10, type=3]
Card [id=3, type=4]
Card [id=12, type=3]
Card [id=4, type=2]
Card [id=4, type=1]
Card [id=6, type=2]
Card [id=5, type=3]
Card [id=8, type=4]
Card [id=3, type=2]
Card [id=13, type=2]
Card [id=7, type=2]
Card [id=3, type=3]
Card [id=3, type=1]
Card [id=6, type=3]
Card [id=8, type=1]
Card [id=11, type=3]
Card [id=13, type=1]
Card [id=10, type=4]
********** ********** **
********** ********** **********
********** ********** ***
Player: Stephen chow wins!!
**********************************************
Player: Stephen chow's card is: hearts -- 9 hearts -- K
Player: chow yun-fat's card is: spades -- J hearts -- 8


Related articles: