这篇文章主要讲解了java如何实现斗地主发牌系统,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
创新互联自2013年创立以来,是专业互联网技术服务公司,拥有项目成都网站设计、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元天津做网站,已为上家服务,为天津各地企业和个人服务,联系电话:13518219792
玩家类
package com.softeem.example; import java.util.ArrayList; import java.util.List; /** * 玩家类 * */ public class Player { private String name; //名字 private boolean landowner; //是否地主 private List<Card> cards = new ArrayList<>(); //手中的所有牌 public Player() { // TODO Auto-generated constructor stub } public Player(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isLandowner() { return landowner; } public void setLandowner(boolean landowner) { this.landowner = landowner; } public List<Card> getCards() { return cards; } public void setCards(List<Card> cards) { this.cards = cards; } @Override public String toString() { return name+"-是否地主:"+landowner+"->"+cards; } }
牌类
package com.softeem.example; /** * 牌类 * */ public class Card implements Comparable<Card>{ private int num;//点数 private String name;//牌面 private String flower;//花色 public Card() { // TODO Auto-generated constructor stub } public Card(int num, String name, String flower) { super(); this.num = num; this.name = name; this.flower = flower; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getFlower() { return flower; } public void setFlower(String flower) { this.flower = flower; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int compareTo(Card c) { return this.num - c.num; } @Override public String toString() { return name+"-"+flower+"-"+num; } }
package com.softeem.example; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; import java.util.Scanner; public class PokerGame { static List<Card> list = new ArrayList<>(); //所有牌集合 static List<Player> players = new ArrayList<>(); //玩家集合 static int position; //初始化当前发牌的位置 static String[] flowers = {"红桃","方片","梅花","黑桃"}; static String[] names = {"3","4","5","6","7","8","9","10","J","Q","K","A","2","小王","大王"}; /** * 初始化所有牌面和玩家 */ static{ int i = 0; //遍历所有的牌面值(除大小王:大小王无花色) for (; i < names.length - 2; i++) { for (int j = 0; j < flowers.length; j++) { list.add(new Card(i, names[i], flowers[j])); } } list.add(new Card(i++, names[names.length-2], null)); list.add(new Card(i++, names[names.length-1], null)); } /** * 初始化玩家 */ public void initPlayer(){ players.clear(); players.add(new Player("盖伦")); players.add(new Player("艾希")); players.add(new Player("玛尔扎哈")); } /** * 洗牌 */ public void shuffle(){ //索引重置 position = 0; Collections.shuffle(list); Collections.shuffle(list); Collections.shuffle(list); start(); } /** * 发牌 */ public void sendCards(){ //准备玩家 initPlayer(); //随机地主(索引) int index = new Random().nextInt(players.size()); //设置为地主 players.get(index).setLandowner(true); Card now = null; while(position < list.size() - 3){ //获取最上面的一张牌 now = list.get(position); if(position % 3 == 0){ players.get(0).getCards().add(now); }else if(position % 3 == 1){ players.get(1).getCards().add(now); }else{ players.get(2).getCards().add(now); } position++; } //获取集合中的最后三张牌 List<Card> last = list.subList(list.size()-3, list.size()); //将最后三张牌给地主 players.get(index).getCards().addAll(last); //显示 show(); } /** * 显示发牌结果 */ public void show(){ //排序 Collections.sort(players.get(0).getCards()); Collections.sort(players.get(1).getCards()); Collections.sort(players.get(2).getCards()); players.stream().forEach(p->System.out.println(p)); start(); } public void start(){ System.out.println("***********欢乐斗地主***********"); System.out.println("***********【1】洗牌***********"); System.out.println("***********【2】发牌***********"); Scanner sc = new Scanner(System.in); String i = sc.nextLine(); if(!i.matches("^\\d{1}$")){ System.out.println("请输入正确的指令!"); start(); return; } switch(i){ case "1": shuffle(); break; case "2": sendCards(); break; default: System.out.println("指令不正确"); start(); break; } } public static void main(String[] args) { new PokerGame().start(); } }
看完上述内容,是不是对java如何实现斗地主发牌系统有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。
网站标题:java如何实现斗地主发牌系统
地址分享:https://www.cdcxhl.com/article16/jgoedg.html
成都网站建设公司_创新互联,为您提供网站制作、做网站、网站营销、关键词优化、手机网站建设、企业建站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联