Home  >  Article  >  Java  >  Poker game example of JAVA collection collection

Poker game example of JAVA collection collection

高洛峰
高洛峰Original
2017-01-23 16:54:071449browse

Collection The root interface in the hierarchy. Collection represents a set of objects, which are also called elements of the collection. Some collections allow duplicate elements, while others do not. Some collections are ordered, while others are unordered. The JDK does not provide any direct implementation of this interface: it provides implementations of more specific subinterfaces such as Set and List. This interface is typically used to pass collections and operate on them wherever maximum generality is required.

Main content: Collection is used here to simulate the poker game played by the big guys in Hong Kong movies.

1. Game rules: Two players are dealt two cards each and compared. Compare the highest number of cards in each player's hand, the size is A-2, the player with the higher number of points wins. If the points are the same, the suits will be compared, and the suits will be black (4), red (3), plum (2), square (1), and the one with the bigger suit wins.

2. Implementation steps:

Create a deck of playing cards A-2, with four suits of black (4), red (3), plum (2), and square (1), a total of 52 Cards;
Create two players including player ID, name, and card information;
Shuffle the cards and deal two cards to each of the two players;
Compare the size of the cards in the players' hands to determine the winner ;

3. Program implementation

Card class: Contains the number and suit of the card

package collectiontest.games;
public class Card {
  private Integer id; //牌的大小
  private Integer type;//牌的花色
   
  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 class: Contains playing card 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、红--3、梅--2、方--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: includes player ID and name, card information held

package collectiontest.games;
import java.util.ArrayList;
import java.util.List;
 
public class Player {
  //玩家的ID
  private String id ;
  //玩家姓名
  private String name ;
  //玩家所持牌
  private List pokerType ;
   
  public Player() {  
  }
  public Player(String id, String name, List 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 getPokerType() {
    return pokerType;
  }
  public void setPokerType(List pokerType) {
    this.pokerType = pokerType;
  }  
}

Poker game main class: includes 1) Poker creation 2) Player creation 3) Shuffling 4) Dealing 5) Compare the outcome

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 {
 
  // 创建扑克牌
  public Set cPoker() {
     
    System.out.println("**********开始创建扑克牌**********");
    // 创建一副poker
    // 四个类型:黑--4、红--3、梅--2、方--1
    Set pokers = new HashSet<>();
    Poker[] poker = { new Poker(1), new Poker(2), new Poker(3),
        new Poker(4) };
    /*
     * Collections工具类的使用
     * Collections.addAll(pokers, new Poker(1), new Poker(2), new Poker(3),new Poker(4));
     *
     * */
    pokers.addAll(Arrays.asList(poker));
 
    System.out.println("**********扑克牌创建成功**********");
 
    return pokers;
  }
 
  // 创建两个玩家
  public Map cPlayer() {
     
    System.out.println("**********开始创建玩家**********");
    Map map = new HashMap();
    // 控制数量
    Integer control = 0;
 
    System.out.println("创建两名玩家,根据提示创建");
    Scanner console = new Scanner(System.in);
    while (true) {
      System.out.println("请输入第 "+(control+1)+" 个玩家ID:");
      String courseId = console.next();
 
      if (isNumeric(courseId)) {
        System.out.println("请输入第 "+(control+1)+" 个玩家姓名:");
        String courseName = console.next();
 
        Player players = new Player(courseId, courseName, null);
        //保存数据
        map.put(courseId, players);
 
        System.out.println("添加第 " + (control + 1) + " 个玩家 " + courseName
            + " 成功");
        //数量自加
        control++;
      } else {
        System.out.println("*****请输入数字ID*****");
        continue;
      }
 
      if (control == 2) {
        break;
      }
 
    }
 
    System.out.println("**********玩家创建成功**********");
 
    return map;
  }
 
  // 判断输入是否为数字, Character.isDigit()为java方法
  public boolean isNumeric(String str) {
    for (int i = 0; i < str.length(); i++) {
      if (!Character.isDigit(str.charAt(i))) {
        return false;
      }
    }
    return true;
  }
 
  /**
   * 洗牌 :也可以产生52个不同随机数,实现洗牌
   *
   **/
  public List wPoker(Set pokers) {
    System.out.println("**********开始洗牌**********");
    //利用List的有序排序,洗牌之后保存顺序不变
    List listCard = new ArrayList<>();
    // 利用Set集合的无序排序,实现洗牌
    Set listSet = new HashSet<>();
 
    //保存到Set集合,无序
    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());
    }
 
    //保存在List集合,有序
    for (Card cd : listSet) {
      listCard.add(cd);
      System.out.println(cd);
    }
     
    System.out.println("**********洗牌成功**********");
 
    return listCard;
  }
 
  // 发牌
  public Map pushPoker(List listCard,
      Map pMap) {
    System.out.println("**********发牌开始**********");
     
    // 控制每人发两张牌后结束
    int control = 0;
 
    for (Map.Entry entry : pMap.entrySet()) {
 
      if (control == 0) {
        for (int i = 0; i < 3; i = i + 2) {
          // 发牌
          entry.getValue().getPokerType().add(listCard.get(i));
        }
        // 更新map对象
        pMap.put(entry.getKey(), entry.getValue());
        control++;
      } else if (control == 1) {
        for (int i = 1; i < 4; i = i + 2) {
          // 发牌
          entry.getValue().getPokerType().add(listCard.get(i));
        }
        // 更新map对象
        pMap.put(entry.getKey(), entry.getValue());
        control++;
      } else {
        break;
      }
    }
 
    System.out.println("**********发牌成功**********");
 
    return pMap;
  }
 
 
  public void compareMatch(Map newMap) {
   
    /*比较胜负
     * 1.首先取得每个玩家手中最大牌的ID和花色ID。
     * 2.比较俩玩家手中最大牌的ID大小,牌大者获胜。
     * 3.如果两张牌的ID相等,在比较两张牌的花色ID,花色ID更大着获胜。
     * 
     * */
     
    List players = new ArrayList<>();
 
    // 获得两个玩家
    for (Map.Entry entry : newMap.entrySet()) {
      players.add(entry.getValue());
    }
 
    // 玩家一信息和所持牌
    List playerOne = players.get(0).getPokerType();
    //获得最大牌的ID和花色
    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() ;
 
    // 玩家二信息和所持牌
    List playerTwo = players.get(1).getPokerType();
    //获得最大牌的ID和花色
    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("玩家 : " + players.get(0).getName() + " 获胜!!");
    } else if (oneMaxId == twoMaxId) {
 
      if (oneMaxType > twoMaxType) {
        System.out
            .println("玩家 : " + players.get(0).getName() + " 获胜!!");
 
      } else {
        System.out
            .println("玩家 : " + players.get(1).getName() + " 获胜!!");
      }
 
    } else {
      System.out.println("玩家 : " + players.get(1).getName() + " 获胜!!");
    }
 
    System.out.println("**********************************************");
    System.out.println("玩家 : " + players.get(0).getName() + "的牌是:"
        + 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("玩家 : " + players.get(1).getName() + "的牌是:"
        + showName(playerTwo.get(0).getType(), 0) + "--"
        + showName(playerTwo.get(0).getId(), 1) + "  "
        + showName(playerTwo.get(1).getType(), 0) + "--"
        + showName(playerTwo.get(1).getId(), 1));
  }
 
  // 显示牌的名称
  private String showName(Integer i, Integer type) {
    String str = "";
 
    // 显示花色
    if (type == 0) {
      switch (i) {
      case 1: {
        str = "方块";
        break;
      }
      case 2: {
        str = "梅花";
        break;
      }
      case 3: {
        str = "红桃";
        break;
      }
      case 4: {
        str = "黑桃";
        break;
      }
 
      default: {
        break;
      }
      }
 
    }
 
    // 显示数字
    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、创建扑克牌
    Set pokers = gb.cPoker();
 
    // 2、创建两个玩家
    Map pMap = gb.cPlayer();
 
    // 3、洗牌
    List listCard = gb.wPoker(pokers);
 
    // 4、发牌
    Map newMap = gb.pushPoker(listCard, pMap);
 
    // 4、比较胜负
    gb.compareMatch(newMap);
 
  }
}

Running results:

************Start creating playing cards**********
**********Poker cards created successfully************
************Start creating player********* *
Create two players and follow the prompts
Please enter the 1st player ID:
Please enter the 1st player name:
Stephen Chow
Add the 1st player Stephen Chow successfully
Please enter the 2nd player ID:
Please enter the 2nd player name:
Chow Yun-fat
Add the 2nd player Chow Yun-fat successfully
************The player was created successfully **********
************Start shuffling **********
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]
**********Shuffle successfully************
************ *Dealing starts************
**********Dealing successfully**********
Player: Stephen Chow wins! !
************************************************
Player: Stephen Chow’s card is: Hearts--9 Hearts--K
Player: Chow Yun-fat’s cards are: Spades--J Hearts--8

The above is the entire article The content, I hope it will be helpful to everyone's learning, and I also hope that everyone will support the PHP Chinese website.

For more articles related to JAVA collection poker game examples, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn