本文实例为大家分享了java实现扑克牌牌面小程序的具体代码,供大家参考,具体内容如下
本周分享一个扑克牌牌面小程序如下:
package com.softeem.lmg.one2.work1; import java.util.Objects; /**首先是牌面类 * 5、参考斗地主的游戏规则,完成一个发牌的功能(54张牌,考虑点数,花色;三名玩家,其中地主比其他玩家多3张牌) * @author admin */ public class Card { /**牌面值*/ private String name; /**花色*/ private String flower; /**大小点数*/ private int num; public Card() { } public Card(String name, String flower, int num) { super(); this.name = name; this.flower = flower; this.num = num; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFlower() { return flower; } public void setFlower(String flower) { this.flower = flower; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String toString() { if(Objects.nonNull(flower)){ return name+"-"+num+"-"+flower; } return name+"-"+num; } }
package com.softeem.lmg.one2.work1; import java.util.ArrayList; /**其次是玩家类*/ public class Player { /**玩家ID*/ private int pid; /**玩家姓名*/ private String pname; /**是否是地主*/ private boolean boss; /**牌集合*/ private ArrayList<Card> cards =new ArrayList<>(); public Player() { } public Player(int pid, String pname, boolean boss, ArrayList<Card> cards) { super(); this.pid = pid; this.pname = pname; this.boss = boss; this.cards = cards; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public boolean isBoss() { return boss; } public void setBoss(boolean boss) { this.boss = boss; } public ArrayList<Card> getCards() { return cards; } public void setCards(ArrayList<Card> cards) { this.cards = cards; } public String toString() { return pname+(boss ? "(地主)" : "")+":"+cards; } }
package com.softeem.lmg.one2.work1; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; /** *最后是管理类 */ public class GameManager { /**声明所有牌的集合*/ private static ArrayList<Card> all=new ArrayList<>(); /**用于生成牌的牌面值*/ private static String[] names={"3","4","5","6","7","8","9","10","J","Q","K","A","2","Queen","King",}; /**用于生成牌的花色*/ private static String[] flowers={"红桃","方块","梅花","黑桃"}; /**所有玩家集合*/ private ArrayList<Player> players=new ArrayList<>(); /**声明随机数生成器*/ private static Random randomGen=new Random(); static{ int i = 0; for (; i < names.length-2; i++) { for (int j = 0; j < flowers.length; j++) { Card c=new Card(names[i], flowers[j], i); all.add(c); } } /**将大小王加入*/ all.add(new Card(names[names.length-2],null,i++)); all.add(new Card(names[names.length-1],null,i++)); } /**添加玩家*/ public void addPlayer(){ Scanner sc=new Scanner(System.in); System.out.println("请输入玩家1名称"); String name1=sc.nextLine(); System.out.println("请输入玩家2名称"); String name2=sc.nextLine(); System.out.println("请输入玩家3名称"); String name3=sc.nextLine(); Player p1=new Player(); p1.setPid(1); p1.setPname(name1); Player p2=new Player(); p2.setPid(2); p2.setPname(name2); Player p3=new Player(); p3.setPid(3); p3.setPname(name3); /**将三名玩家分别加入集合*/ players.add(p1); players.add(p2); players.add(p3); } /**随机地主*/ public void randomBoss(){ addPlayer(); int i=randomGen.nextInt(players.size()); players.get(i).setBoss(true); } /**发牌*/ public ArrayList<Player> sendCard(){ randomBoss(); for (Player p : players) { for (int i = 0; i < 17; i++) { int cardIndex=randomGen.nextInt(all.size()); Card card=all.get(cardIndex); p.getCards().add(card); all.remove(card); } } /**最后三张牌都给到地主*/ for (Player p : players) { if(p.isBoss()){ p.getCards().addAll(all); } } return players; } public static void main(String[] args) { ArrayList<Player> players=new GameManager().sendCard(); for (Player pl : players) { System.out.println(pl); } } }
最后控制台结果:
更多有趣的经典小游戏实现专题,分享给大家:
C++经典小游戏汇总
python经典小游戏汇总
python俄罗斯方块游戏集合
JavaScript经典游戏 玩不停
java经典小游戏汇总
javascript经典小游戏汇总
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。