欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVA collection集合之扑克牌游戏实例

程序员文章站 2024-03-12 16:20:56
collection 层次结构中的根接口。collection表示一组对象,这些对象也称为collection的元素。一些 collection 允许有重复的元素,而另一些...

collection 层次结构中的根接口。collection表示一组对象,这些对象也称为collection的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。jdk 不提供此接口的任何直接 实现:它提供更具体的子接口(如 set 和 list)实现。此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。

主要内容:这里使用collection集合,模拟香港电影中大佬们玩的扑克牌游戏。

1、游戏规则:两个玩家每人手中发两张牌,进行比较。比较每个玩家手中牌最大的点数,大小由a-2,点数大者获胜。如果点数相同,则比较花色,大小由黑(4)、红(3)、梅(2)、方(1),花色大者获胜。

2、实现步骤:

创建一副扑克牌a-2,四种花色黑(4)、红(3)、梅(2)、方(1)共52张牌;
创建两个玩家包含玩家id和姓名、所持牌card信息;
洗牌并向两位玩家各发两张牌;
比较玩家手中牌大小,得出获胜者;

3、程序实现

牌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类:包含扑克牌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类:包含玩家id和姓名、所持卡片信息

 package collectiontest.games;
import java.util.arraylist;
import java.util.list;

public class player {
  //玩家的id
  private string id ;
  //玩家姓名
  private string name ;
  //玩家所持牌
  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;
  }  
}

扑克牌游戏主类:包含1)扑克牌创建 2)玩家创建 3)洗牌 4)发牌 5)比较胜负

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<poker> cpoker() {
    
    system.out.println("**********开始创建扑克牌**********");
    // 创建一副poker
    // 四个类型:黑--4、红--3、梅--2、方--1
    set<poker> 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<string, player> cplayer() {
    
    system.out.println("**********开始创建玩家**********");
    map<string, player> map = new hashmap<string, player>();
    // 控制数量
    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<card> wpoker(set<poker> pokers) {
    system.out.println("**********开始洗牌**********");
    //利用list的有序排序,洗牌之后保存顺序不变
    list<card> listcard = new arraylist<>();
    // 利用set集合的无序排序,实现洗牌
    set<card> 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<string, player> pushpoker(list<card> listcard,
      map<string, player> pmap) {
    system.out.println("**********发牌开始**********");
    
    // 控制每人发两张牌后结束
    int control = 0;

    for (map.entry<string, player> 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<string, player> newmap) {
  
    /*比较胜负
     * 1.首先取得每个玩家手中最大牌的id和花色id。
     * 2.比较俩玩家手中最大牌的id大小,牌大者获胜。
     * 3.如果两张牌的id相等,在比较两张牌的花色id,花色id更大着获胜。
     * 
     * */ 
    
    list<player> players = new arraylist<>();

    // 获得两个玩家
    for (map.entry<string, player> entry : newmap.entryset()) {
      players.add(entry.getvalue());
    }

    // 玩家一信息和所持牌
    list<card> 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<card> 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<poker> pokers = gb.cpoker();

    // 2、创建两个玩家
    map<string, player> pmap = gb.cplayer();

    // 3、洗牌
    list<card> listcard = gb.wpoker(pokers);

    // 4、发牌
    map<string, player> newmap = gb.pushpoker(listcard, pmap);

    // 4、比较胜负
    gb.comparematch(newmap);

  }
}

运行结果:

**********开始创建扑克牌**********
**********扑克牌创建成功**********
**********开始创建玩家**********
创建两名玩家,根据提示创建
请输入第  1 个玩家id:
请输入第  1 个玩家姓名:
周星驰
添加第 1 个玩家  周星驰 成功
请输入第  2 个玩家id:
请输入第  2 个玩家姓名:
周润发
添加第 2 个玩家  周润发 成功
**********玩家创建成功**********
**********开始洗牌**********
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]
**********洗牌成功**********
**********发牌开始**********
**********发牌成功**********
玩家 : 周星驰 获胜!!
**********************************************
玩家 : 周星驰的牌是:红桃--9   红桃--k
玩家 : 周润发的牌是:黑桃--j   红桃--8

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。