189 8069 5689

java如何实现斗地主游戏

这篇文章主要讲解了java如何实现斗地主游戏,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

创新互联建站主营平陆网站建设的网络公司,主营网站建设方案,重庆App定制开发,平陆h5小程序定制开发搭建,平陆网站营销推广欢迎平陆等地区企业咨询

思路:

1、创建一个扑克牌的实体类Poker,设置了四个参数:花色、数字、牌值(判断大小)、是否地主牌,实现getset方法和构造方法;
2、创建一个玩家的实体类Player,设置了四个参数: 初始牌集合,排序后牌集合,牌值集合,是否地主,实现getset方法和构造方法;
3、洗牌:循环嵌套花色数组跟数字数组生成52个Poker,手动加入大小王Poker,放进map(int,Poker)里面,利用Collections.shuffle随机排序map的key,再根据key生成排序后的Poker集合
4、发牌:生成3个玩家对象,拿到洗牌后的集合,前51张牌为玩家牌,后3张为地主牌,在51张牌中随机一张为地主牌,遍历玩家牌分配个每一个Player,同时根据玩家牌的牌值生成集合grades存进Player对象中,将grades转为int数组并进行升序排序,根据grade取得新Poker集合分配给玩家。

Poker类:

package com.company;

public class Poker {
 private String color;
 private String number;
 private int grade;
 boolean isLandowners;

 public boolean isLandowners() {
 return isLandowners;
 }

 public void setLandowners(boolean landowners) {
 isLandowners = landowners;
 }

 public int getGrade() {
 return grade;
 }

 public void setGrade(int grade) {
 this.grade = grade;
 }

 public String getColor() {
 return color;
 }

 public void setColor(String color) {
 this.color = color;
 }

 public String getNumber() {
 return number;
 }

 public void setNumber(String number) {
 this.number = number;
 }

 public Poker() {
 }

 public Poker(String color, String number ,int grade,boolean isLandowners) {
 this.color = color;
 this.number = number;
 this.grade=grade;
 this.isLandowners=isLandowners;
 }


 public String toString() {
 return this.getColor()+this.getNumber();
 }
}

Player类:

package com.company;

import java.util.ArrayList;
import java.util.LinkedList;

public class Player {
 private ArrayList pokers;
 private boolean isLandower;
 private ArrayList newPokers;
 private LinkedList grades;

 public LinkedList getGrades() {
 return grades;
 }

 public void setGrades(LinkedList grades) {
 this.grades = grades;
 }

 public ArrayList getNewPokers() {
 return newPokers;
 }

 public void setNewPokers(ArrayList newPokers) {
 this.newPokers = newPokers;
 }

 public boolean isLandower() {
 return isLandower;
 }

 public void setLandower(boolean landower) {
 isLandower = landower;
 }

 public Player() {
 }

 public ArrayList getPokers() {
 return pokers;
 }

 public void setPokers(ArrayList pokers) {
 this.pokers = pokers;
 }
}

洗牌:

/*洗牌*/
 public static LinkedList getPokerList() {
 LinkedList pokers = new LinkedList();
 LinkedList newPokers = new LinkedList();
 ArrayList arrayList = new ArrayList<>();
 int[] arr;

 /*生成54张扑克牌(Poker对象)*/
 /*用嵌套循环生成从方块3到黑桃2的Poker对象,放进Poker的LinkedList保证顺序*/
 String[] colors = {"♦", "♣", "♥", "♠"};
 String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
 for (int i = 0; i < numbers.length; i++) {
 for (int j = 0; j < colors.length; j++) {
 Poker p = new Poker(colors[j], numbers[i], (4 * i) + j + 1, false);
 pokers.add(p);
 }
 }
 /*添加大小王*/
 pokers.add(new Poker("black", "☆", 53, false));
 pokers.add(new Poker("red", "☆", 54, false));

 /*将生成的54张扑克牌从1到54放进map*/
 for (int z = 1; z <= pokers.size(); z++) {
 map.put(z, pokers.get(z - 1));
 }
 /*遍历Map,将每个Poker对应的key放进 ArrayList arrayList */
 Set> set = map.entrySet();
 Iterator it = set.iterator();
 while (it.hasNext()) {
 Map.Entry e = (Map.Entry) it.next();
 arrayList.add(e.getKey());
 }
 /*利用Collections.shuffle随机排序key*/
 Collections.shuffle(arrayList);
 /*将乱序后的key对于的Poker放进newPokers里面 */
 for (Integer i : arrayList) {
 newPokers.add(map.get(i));
 }

 return newPokers;
 }

发牌:

/*发牌*/
 /*根据玩家人数生成Player,默认num=3*/
 private static void sentPoker(int num) {
 /*获取洗牌后的LinkedList,保证顺序*/
 LinkedList Allpokers = getPokerList();
 ArrayList pokers = new ArrayList<>();
 ArrayList BottomPokers = new ArrayList<>();

 /*生成玩家对象*/
 for (int i = 0; i < num; i++) {
 Player p = new Player();
 players.add(p);
 }
 /*前50张牌为玩家牌,后三张为底牌*/
 for (int p = 0; p < Allpokers.size(); p++) {
 if (p <= 50) {
 pokers.add(Allpokers.get(p));
 } else {
 BottomPokers.add(Allpokers.get(p));
 }
 }
 /*玩家牌中随机一张为地主牌*/
 Random rand = new Random();
 int s = rand.nextInt(51);
 pokers.get(s).setLandowners(true);
 System.out.println("地主牌: " + pokers.get(s));

 /*根据num生成Player对象*/
 for (int j = 0; j < num; j++) {
 /*玩家的牌值集合*/
 LinkedList grades = new LinkedList<>();
 /*玩家的手牌集合*/
 ArrayList playerPokers = new ArrayList();

 /*根据玩家个数分配玩家牌*/
 for (int z = j; z < pokers.size(); z = z + num) {
 playerPokers.add(pokers.get(z));
 grades.add(pokers.get(z).getGrade());
 players.get(j).setPokers(playerPokers);
 players.get(j).setGrades(grades);
 }
 /*根据玩家牌中的地主牌设置玩家是否地主*/
 for (Poker p : players.get(j).getPokers()) {
 if (p.isLandowners() == true) {
  players.get(j).setLandower(true);
 }
 }
 /*根据玩家的牌值集合生成数组*/
 Integer[] gs = new Integer[players.get(j).getGrades().size()];
 grades.toArray(gs);
 /*排序数组,由低到高*/
 Arrays.sort(gs);

 /*排序后的玩家牌*/
 ArrayList newPokers = new ArrayList<>();
 for (int g = 0; g < gs.length; g++) {
 Poker p = map.get(gs[g]);
 newPokers.add(p);
 }
 players.get(j).setNewPokers(newPokers);
 System.out.println("玩家" + (j + 1) + ": " + players.get(j).getNewPokers() + " 是否地主:" + players.get(j).isLandower());
 }

 System.out.print("底牌为: ");
 for (Poker p : BottomPokers) {
 System.out.print(p + " ");
 }
 }

效果如下:

java如何实现斗地主游戏

看完上述内容,是不是对java如何实现斗地主游戏有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。


分享名称:java如何实现斗地主游戏
链接地址:http://cdxtjz.cn/article/iggcip.html

其他资讯