您好,登錄后才能下訂單哦!
這篇文章主要講解了java如何實現斗地主發牌系統,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
玩家類
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如何實現斗地主發牌系統有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。