扑克随机发牌

简介

       斗地主是大家都熟悉的一个游戏,其原理也很简单,就是将51扑克牌平均分到三个人手中,然后将剩下的三张牌分配到地主手中,其代码就根据这个原理进而实现发牌功能。

玩家类

       给玩家定义一个序号,姓名,再定义一个地主属性,代码如下:

import java.util.ArrayList;

public class Player {

	private int id;
	private String name;
	private ArrayList pokers = new ArrayList<>();
	private boolean boss;
	
	public Player() {
		
	}

	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 getPokers() {
		return pokers;
	}

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

	public boolean isBoss() {
		return boss;
	}

	public void setBoss(boolean boss) {
		this.boss = boss;
	}
}

扑克类:

扑克类包括花色,数字,sort用于记录扑克

public class Poker {

	private String flower;
	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;
	}
}

发牌:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * 
 * @author Huaazai
 *
 */
public class PokerGame {

	static List players;
	static ArrayList 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);
			}
		}

		// 单独将双王加进去
		list.add(new Poker("小", "王", 13));
		list.add(new Poker("大", "王", 14));

		// 初始化玩家
		players = Arrays.asList(new Player(1, "刘备"), new Player(2, "关羽"), new Player(3, "张飞"));
	}

	// 随机地主
	public void pushBoss() {
		Random r = new Random();
		bossIndex = r.nextInt(players.size());
		// 设置随机到的玩家为地主
		players.get(bossIndex).setBoss(true);
	}

	// 发牌
	public void pushPoker() {
		Random r = new Random();
		for (int n = 0; n < players.size(); n++) {
			for (int i = 0; i < 17; i++) {
				int index = r.nextInt(list.size());
				players.get(n).getPokers().add(list.get(index));
				list.remove(index);
			}
		}

		// 将最后剩的牌给地主
		players.get(bossIndex).getPokers().addAll(list);
	}

	public void start() {
		pushBoss();
		pushPoker();
		for (Player p : players) {
			System.out.println(p.getName()+p.getPokers());
		}
	}

	public static void main(String[] args) {
		new PokerGame().start();
	}
}

运行结果:

扑克随机发牌_第1张图片

你可能感兴趣的:(小白)