用java实现斗地主发牌过程
本文实例为大家分享了java实现斗地主发牌过程的具体代码,供大家参考,具体内容如下
一、牌类实现
定义牌的相关属性,点数、花色、大小
package com.softeem.lesson18.doudizhu;
import java.util.Objects;
public class Card {
//牌面值
private String name;
//花色
private String flower;
//点数
private int num;
public Card() {
// TODO Auto-generated constructor stub
}
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;
}
@Override
public String toString() {
if(Objects.nonNull(flower)) {
return name+"-"+num+"-"+flower;
}
return name+"-"+num;
}
}
二、玩家类
玩家类中定义了万家的相关属性,名字,ID,还定义了一个牌类数组,将每位玩家的牌加入到数组中。
package com.softeem.lesson18.doudizhu;
import java.util.ArrayList;
public class Player {
private int id;
private String name;
private boolean boss;
private ArrayList<Card> cards = new ArrayList<>();
public Player() {
}
public Player(int id, String name, boolean boss, ArrayList<Card> cards) {
super();
this.id = id;
this.name = name;
this.boss = boss;
this.cards = cards;
}
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 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;
}
@Override
public String toString() {
return name+(boss?"(地主)" : "") + cards;
}
}
三、功能类
功能类中实现了牌的分发,每人17张牌,然后地主多三张牌的实现,以及对玩家进行随机分配地主等功能。
package com.softeem.lesson18.doudizhu;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class GameManage {
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<Player>();
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() {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个玩家的名字:");
String name1 = sc.nextLine();
System.out.println("请输入第二个玩家的名字:");
String name2 = sc.nextLine();
System.out.println("请输入第三个玩家的名字:");
String name3 = sc.nextLine();
Player p1 = new Player();
p1.setId(1);
p1.setName(name1);
Player p2 = new Player();
p2.setId(2);
p2.setName(name2);
Player p3 = new Player();
p3.setId(3);
p3.setName(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 void sendCard() {
randomBoss();
//对玩家遍历
for(Player p : players) {
//先为每一位玩家随机发17张牌
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);
}
}
for(Player p : players) {
System.out.println(p);
}
}
public static void main(String[] args) {
new GameManage().sendCard();
}
}
更多有趣的经典小游戏实现专题,分享给大家:
C++经典小游戏汇总
python经典小游戏汇总
python俄罗斯方块游戏集合
JavaScript经典游戏 玩不停
java经典小游戏汇总
javascript经典小游戏汇总
以上是 用java实现斗地主发牌过程 的全部内容, 来源链接: utcz.com/z/317676.html