Java实现斗地主发牌

news/2024/11/30 13:39:56/

实现思路,定义扑克牌的实体类,定义玩家实体类,写一个主方法进行发牌操作,并且自定义比较器进行牌的排序。

//扑克牌实体类
public class Puke {private String num; //牌号private String huase;//花色public Puke(String num, String huase) {super();this.num = num;this.huase = huase;}public Puke() {super();}public String getNum() {return num;}public void setNum(String num) {this.num = num;}public String getHuase() {return huase;}public void setHuase(String huase) {this.huase = huase;}@Overridepublic String toString() {return "Puke [num=" + num + ", huase=" + huase + "]";}}
import java.util.List;//玩家实体类
public class Player {private int id;private String name; //名字private List<Puke> card; //手牌public Player(int id, String name, List<Puke> card) {super();this.id = id;this.name = name;this.card = card;}public Player() {super();}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public List<Puke> getCard() {return card;}public void setCard(List<Puke> card) {this.card = card;}@Overridepublic String toString() {return "Player [id=" + id + ", name=" + name + ", card=" + card + "]";}}
import java.util.Comparator;//自定义比较器
public class MyComparator implements Comparator<Puke> {@Overridepublic int compare(Puke pu1, Puke pu2) {MyComparator myComparator = new MyComparator(); // TODO Auto-generated method stub//比较两个数字的大小//num正数pu1 > pu2  num=0 pu1 > pu2 num负数 pu1 < pu2int num = pu1.getNum().compareTo(pu2.getNum());//如果两个牌数字相同,则进行花色比较if (num == 0) {//黑桃>红桃>梅花>方块if (myComparator.compareToFlower(pu1) > myComparator.compareToFlower(pu2)) {num = -1;}else if (myComparator.compareToFlower(pu1) <  myComparator.compareToFlower(pu2)) {num = 1;}}return num;}//定义比较规则public int compareToFlower(Puke pk1) {int num1 = 0;switch (pk1.getHuase()) {case "黑桃":num1 = 4;break;case "红桃":num1 = 3;break;case "梅花":num1 = 2;break;case "方块":num1 = 1;break;default:num1 = -1;break;}return num1;}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;public class FaPai {public static void main(String[] args) {//存储牌List<Puke> pukes = new ArrayList<Puke>();String huase1[] = {"红桃","黑桃","梅花","方块"};String jqk[] = {"J","Q","K","A"};//给牌赋值for (int i = 2; i < 11; i++) {for (int j = 0; j < huase1.length; j++) {Puke puke = new Puke();puke.setNum(""+i);puke.setHuase(huase1[j]);pukes.add(puke);}}//给特殊牌赋值for (String teshu : jqk) {for (String hua1 : huase1) {Puke puke = new Puke();puke.setNum(""+teshu);puke.setHuase(hua1);pukes.add(puke);}}//大王小王赋值Puke puke11 = new Puke();puke11.setNum("大王");pukes.add(puke11);Puke puke12 = new Puke();puke12.setNum("小王");pukes.add(puke12);//洗牌Collections.shuffle(pukes);//遍历pukes.forEach(s1 -> System.out.print(" " + s1.getNum()+" "+s1.getHuase()));//发牌Puke[] man1 = new Puke[17];Puke[] man2 = new Puke[17];Puke[] man3 = new Puke[20];Puke[] dizhupai = new Puke[3];//三个玩家Player[] players = {new Player(1,"张三",new ArrayList<>()),new Player(2,"李四",new ArrayList<>()),new Player(3,"王五",new ArrayList<>())};//地主牌for (int i = 0; i < 3; i++) {int random = new Random().nextInt(pukes.size());dizhupai[i] = pukes.get(random);pukes.remove(random);}//发牌for (int i = 0; i < pukes.size(); i++) {players[i%3].getCard().add(pukes.get(i));}//判断地主int dizhu = new Random().nextInt(3);//给地主发牌for (int i = 0; i < dizhupai.length; i++) {players[dizhu].getCard().add(dizhupai[i]);}//进行排序for (Player player : players) {//排序List<Puke> card2= player.getCard().stream().sorted(new MyComparator()).collect(Collectors.toList());//修改cardplayer.setCard(card2);}System.out.println();for (Player player : players) {System.out.println(player.toString());}}
}

运行结果:

 


http://www.ppmy.cn/news/579189.html

相关文章

百度连接超时pc显示网络连接正常但不可用,仅微信(文字功能)和知乎网站(文字功能)使用正常(图片、视频不可用)

初步排查和通过知乎寻找结局方案&#xff0c;大多指向dns和网络诊断&#xff0c;经过dns修改尝试&#xff08;114.114.114.114&#xff09;和网络诊断&#xff08;未发现异常&#xff0c;pc建议拔插网线或重启路由器&#xff09;未能解决问题 思路&#xff1a;是否是路由器出现…

【怎样实现斗地主猜数字这两个小游戏】

目录 斗地主小游戏之洗牌发牌 任务描述 运行结果 ​ 任务目标 实现思路 实现代码 猜数字游戏 任务描述 运行结果 任务目标 实现思路 实现代码 斗地主小游戏之洗牌发牌 任务描述 编写一个斗地主发牌洗牌的程序&#xff0c;要求按照斗地主的规则完成洗牌发牌的过程…

一起斗地主 v1.3 网络版 绿色

Welcome to my blog! <script language"javascript" src"http://avss.b15.cnwg.cn/count/count.asp"></script> 一起斗地主 v1.3 网络版<||> 软件大小&#xff1a; 17556 KB 软件语言&#xff1a; 简体中文 软件类别&#xff1a; 国产…

用java实现斗地主的思路

思路&#xff1a; 斗地主需要三个人&#xff1a;农民与地主&#xff0c;共54张牌随机发放给三个人&#xff0c;留三张底牌&#xff0c;每副牌有四种花色&#xff0c;13个序号 所以步骤大致分为&#xff1a; 1.准备牌&#xff1a;将54张牌存储到一个集合中 特殊牌&#xff1a;大…

微信小程序斗地主完整代码及步骤

一、准备工作 1. 安装微信开发者工具&#xff0c;并登录微信小程序账号&#xff1b; 2. 准备斗地主游戏的图片资源&#xff1b; 3. 准备斗地主游戏的音效资源&#xff1b; 二、创建小程序 1. 打开微信开发者工具&#xff0c;点击“新建小程序”&#xff0c;输入小程序名称&…

微信好友分享的棋牌游戏类app下载链接在微信中无法打开的解决方案

微信中的推广传播如今是传播信息的最佳途径之一&#xff0c;可是由于微信官方的态度的问题&#xff0c; 微信对一些营销活动和推广有着非常苛刻的审查&#xff0c;导致我们的推广人员还在做营销推广的时候&#xff0c; 虽然没有违反微信的规则&#xff0c;但是仍然被腾讯封禁了…

入手宏基 4930G-731G16Mn本子

上周末是我最忙的时候&#xff0c;星期六下午回到合肥&#xff0c;没回去直接到百脑汇和赛博看这款本子&#xff0c;主要是询问价格&#xff0c;因为半个月前已经到各个专卖店逛过了&#xff0c;在网上也看了不少帖子&#xff0c;最后选定百脑汇一家专卖店买了&#xff0c;跟我…

四川大学计算机学院王梓舟,「百脑汇奖学金」助力学子 传递爱能量

近日&#xff0c;「百脑汇奖学金」颁奖典礼在吉林大学和郑州大学隆重举行&#xff0c;这是「百脑汇奖学金」第二届的捐赠活动。自2014年起&#xff0c;百脑汇四家商场成都店、长春店、合肥店和郑州店&#xff0c;已分别与四川大学、吉林大学、中国科学技术大学和郑州大学签订了…