您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)java如何實(shí)現(xiàn)斗地主發(fā)牌功能的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
具體內(nèi)容如下
參考斗地主的游戲規(guī)則,完成一個發(fā)牌的功能(54張牌,考慮點(diǎn)數(shù),花色;三名玩家,其中地主比其他玩家多3張牌)
要求實(shí)現(xiàn)以下功能:
1、洗牌
2、發(fā)牌(發(fā)牌按照常規(guī)方式A-B-C-A-B-C-A…,不允許一次性隨機(jī)17張給某個玩家)
3、玩家手中牌的展示(要求按點(diǎn)數(shù)實(shí)現(xiàn)排序)
運(yùn)用面向?qū)ο蟮某绦蛟O(shè)計(jì)思維分為撲克、玩家和游戲三部分,撲克類屬性有花色,點(diǎn)數(shù),次序;玩家類屬性有玩家id,昵稱,是否地主,牌的集合;游戲過程分為洗牌,隨機(jī)產(chǎn)生地主,發(fā)牌,完成牌的排序,玩家手里的牌展示功能
package com.lesson17.homework; /** * @author HUC * */ /**撲克類*/ public class Poker { /** 撲克花色 */ private String flower; /** 撲克點(diǎn)數(shù) */ private String pointer; /** 撲克順序 */ private int sort; public Poker(String flower, String pointer, int sort) { super(); this.flower = flower; this.pointer = pointer; this.sort = sort; } public String getFlower() { return flower; } public void setFlower(String flower) { this.flower = flower; } public String getPointer() { return pointer; } public void setPointer(String pointer) { this.pointer = pointer; } public int getSort() { return sort; } public void setSort(int sort) { this.sort = sort; } @Override public String toString() { return flower + pointer; } package com.lesson17.homework; public class Player { /** 玩家id */ private int id; /** 玩家昵稱 */ private String name; /** 玩家拿到的牌 */ private ArrayList<Poker> pokers = new ArrayList<>(); /** 玩家是否地主 */ private boolean boss; public Player(int id, String name) { super(); this.id = id; this.name = name; } 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 ArrayList<Poker> getPokers() { return pokers; } public void setPokers(ArrayList<Poker> pokers) { this.pokers = pokers; } public boolean isBoss() { return boss; } public void setBoss(boolean boss) { this.boss = boss; } package com.lesson17.homework; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Random; public class PokerGame { static List<Player> players; static ArrayList<Poker> list = new ArrayList<>(); static String[] flowers = {"方塊","梅花","黑桃","紅心"}; static String[] pointers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; int bossIndex; //地主索引 static{ //初始化撲克 for (int i = 0; i < flowers.length; i++) { for (int j = 0; j < pointers.length; j++) { //生成一張撲克對象 Poker p = new Poker(flowers[i], pointers[j], j); list.add(p); } } //單獨(dú)將大小王加入集合 list.add(new Poker("小", "王", 13)); list.add(new Poker("大", "王", 14)); //初始化玩家 players = Arrays.asList( new Player(1, "劉備"), new Player(2, "關(guān)羽"), new Player(3, "張飛")); } /**洗牌*/ public void washPoker(){ Collections.shuffle(list); } /**隨機(jī)地主*/ public void pushBoss(){ Random r = new Random(); bossIndex = r.nextInt(players.size()); //設(shè)置被隨機(jī)到的玩家為地主 players.get(bossIndex).setBoss(true); } /**依次發(fā)牌*/ public void pushPoker(){ for (int i = 0; i < 51; i+=3) { players.get(0).getPokers().add(list.get(i)); players.get(1).getPokers().add(list.get(i+1)); players.get(2).getPokers().add(list.get(i+2)); } for(int i = 51;i < 54 ;i++){ //將最后剩余的牌(3張)全部給地主 players.get(bossIndex).getPokers().add(list.get(i)); } } /**排序*/ public void sortPoker() { Collections.sort(players.get(0).getPokers(), (a,b)->a.getSort()-b.getSort()); Collections.sort(players.get(1).getPokers(), (a,b)->a.getSort()-b.getSort()); Collections.sort(players.get(2).getPokers(), (a,b)->a.getSort()-b.getSort()); } public void start(){ //隨機(jī)地主 pushBoss(); //洗牌 washPoker(); //發(fā)牌 pushPoker(); //排序 sortPoker(); //展示牌 for(Player p : players){ System.out.println(p.getPokers()); } } public static void main(String[] args) { new PokerGame().start(); } }
結(jié)果
感謝各位的閱讀!關(guān)于“java如何實(shí)現(xiàn)斗地主發(fā)牌功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。