【Java】利用多线程实现斗地主发牌

做这个之前在网看了很久,斗地主发牌的代码小伙伴们大多是用集合来实现的,现在给大家分享一个利用多线程实现的斗地主发牌,代码如下:

扑克类:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Poker {
	// 定义集合,存储扑克
	private static List poker = new ArrayList();
	//区分玩家,即线程
	private int player = (int)(Math.random() * 3 + 1);
	//取牌索引
	private int index = 0;
	//地主
	private int landlord = (int)(Math.random() * 3 + 1);
	
	// 组合扑克牌
	public static void make() {
		String[] colors = { "♠", "♣", "♥", "♦" };
		String[] charsA = { "A" };
		String[] charsJQK = { "J", "Q", "K" };
 
		for (int i = 0; i < colors.length; i++) {
			for (int j = 0; j < charsA.length; j++) {
				poker.add(colors[i] + charsA[j]);
			}
			for (int j = 2; j <= 10; j++) {
				poker.add(colors[i] + j);
			}
			for (int j = 0; j < charsJQK.length; j++) {
				poker.add(colors[i] + charsJQK[j]);
			}
		}
		poker.add("大王");
		poker.add("小王");

		System.out.println(poker);
	}

	// 洗牌
	public static void shuffle() {
		Collections.shuffle(poker);
		System.out.println(poker);
	}

	public static List getPoker() {
		return poker;
	}

	public static void setPoker(List poker) {
		Poker.poker = poker;
	}

	public int getPlayer() {
		return player;
	}

	public void setPlayer(int player) {
		this.player = player;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public int getLandlord() {
		return landlord;
	}

	public void setLandlord(int landlord) {
		this.landlord = landlord;
	}
}

第一个玩家:

import java.util.ArrayList;

public class Player1 implements Runnable{
	private ArrayList data = new ArrayList();

	private Poker poker;
	private String name;
	
	public Player1(Poker poker, String name) {
		this.poker = poker;
		this.name = name;
	}

	@Override
	public void run() {
		while (true) {
			synchronized (poker) {
				if (poker.getIndex() > 50) {
					//如果该玩家是地主,把最后三张发给该玩家
					if (poker.getLandlord() == 1) {
						System.out.println(this.name + "是地主");
						data.add(Poker.getPoker().get(51));
						data.add(Poker.getPoker().get(52));
						data.add(Poker.getPoker().get(53));
					}
					System.out.println(this.name + ":" + this.data);
					break;
				}
				if (poker.getPlayer() == 1) {
					//获取到该摸第几张牌
					int index = poker.getIndex();
					//摸牌
					data.add(Poker.getPoker().get(index));
					System.out.println(this.name + "摸了一张" + Poker.getPoker().get(index));
					//告诉第二个线程进行摸牌
					poker.setPlayer(2);
					poker.setIndex(index + 1);
				}
				try {
					Thread.sleep(10);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}

第二个玩家:

import java.util.ArrayList;

public class Player2 implements Runnable{
	private ArrayList data = new ArrayList();

	private Poker poker;
	private String name;
	
	public Player2(Poker poker, String name) {
		this.poker = poker;
		this.name = name;
	}

	@Override
	public void run() {
		while (true) {
			synchronized (poker) {
				if (poker.getIndex() > 50) {
					if (poker.getLandlord() == 2) {
						System.out.println(this.name + "是地主");
						data.add(Poker.getPoker().get(51));
						data.add(Poker.getPoker().get(52));
						data.add(Poker.getPoker().get(53));
					}
					System.out.println(this.name + ":" + this.data);
					break;
				}
				if (poker.getPlayer() == 2) {
					//获取到该摸第几张牌
					int index = poker.getIndex();
					//摸牌
					data.add(Poker.getPoker().get(index));
					System.out.println(name + "摸了一张" + Poker.getPoker().get(index));
					//告诉第三个线程进行摸牌
					poker.setPlayer(3);
					poker.setIndex(index + 1);
				}
				try {
					Thread.sleep(10);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}

第三个玩家:

import java.util.ArrayList;

public class Player3 implements Runnable{
	private ArrayList data = new ArrayList();

	private Poker poker;
	private String name;
	
	public Player3(Poker poker, String name) {
		this.poker = poker;
		this.name = name;
	}

	@Override
	public void run() {
		while (true) {
			synchronized (poker) {
				if (poker.getIndex() > 50) {
					if (poker.getLandlord() == 3) {
						System.out.println(this.name + "是地主");
						data.add(Poker.getPoker().get(51));
						data.add(Poker.getPoker().get(52));
						data.add(Poker.getPoker().get(53));
					}
					System.out.println(this.name + ":" + this.data);
					break;
				}
				if (poker.getPlayer() == 3) {
					//获取到该摸第几张牌
					int index = poker.getIndex();
					//摸牌
					data.add(Poker.getPoker().get(index));
					System.out.println(name + "摸了一张" + Poker.getPoker().get(index));
					//告诉第一个线程进行摸牌
					poker.setPlayer(1);
					poker.setIndex(index + 1);
				}
				try {
					Thread.sleep(10);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}

测试类:

public class Test {
	public static void main(String[] args) {
		Poker poker = new Poker();
		
		Poker.make(); //制作牌
		Poker.shuffle(); //洗牌
		
		Player1 p1 = new Player1(poker, "张三");
		Player2 p2 = new Player2(poker, "李四");
		Player3 p3 = new Player3(poker, "王五");
		
		Thread pp1 = new Thread(p1);
		Thread pp2 = new Thread(p2);
		Thread pp3 = new Thread(p3);
		
		pp1.start();
		pp2.start();
		pp3.start();
	}
}

控制台:

【Java】利用多线程实现斗地主发牌_第1张图片

思路总结及解释:

  1. 制作一副牌存放到list集合中,然后通过Collections的shuffle()方法打乱扑克牌。
  2. 设置三个玩家然后有顺序的进行摸牌,摸牌的实现方法是通过索引遍历存放整副牌的集合,将对应索引的牌分别放到每个玩家的小集合里,这样就实现了摸牌步骤,那怎么能通过线程实现有顺序的摸呢,这里我首先在扑克类里设置一个玩家变量,然后在具体玩家里用一个判断,第一个玩家的话判断poker.getPlayer() == 1的话,然后将poker.setPlayer(2),玩家二三也一样,如此一来,线程就会按照顺序来执行。
  3. 随机一个地主,顺序发完牌之后还剩三张,将它发给地主。
  4. 最后将三名玩家添加到线程里,开启线程。

你可能感兴趣的:(Java)