java實現撲克牌牌面小程式
阿新 • • 發佈:2020-11-24
本文例項為大家分享了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],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經典小遊戲彙總
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。