java编程实现斗地主发牌

 编写一个自动发牌程序,模拟三人斗地主的摸牌场景。首先要给出提示,谁首先开始摸牌,并且摸牌要和现实摸牌一样,三人循环摸牌,
 最后还要剩余三张底牌,同时给出地主牌,摸到地主牌的玩家拥有三张底牌。三张底牌三人都可以看到。当三张底牌派发给地主后提示玩家摸牌结束
实现思路
        (1)首先将一副牌的四种花色和对应的牌面值随机组合放进Set集合,因为Set集合是非重复集合,所以无需考虑重复的问题,
      另外,因为每个牌面值出现的次数只能是四次,所以,当该牌面值出现了四次以后,将该牌面删除。
        (2)获取洗牌结束的牌组(链表,用Set集合作为初始化数据集),随机额抽取三张牌,作为底牌,不对玩家展示,
        并从剩余的牌组中随机选取一个张牌,作为地主牌,对所有人展示但不移动其位置。
        (3)顺序循环发牌,直到牌组没有牌为止,将底牌展示并发给地主。提示玩家发牌结束。

public class Homework1 {
		public static void main(String[] args) {
			//Poker poker = new Poker();
			HashSet<Poker>set = new HashSet<>();
			
	        // 创建花色数组和点数数组
	        // 定义一个花色数组
	        String[] colors = { "黑桃", "红桃", "梅花", "方块" };
	        // 定义一个点数数组
	        String[] faces = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
	                "K", "A", "2", };
	        
			int i = 0;//开始零张牌
			while(true){
				Random random = new Random();
				int a = random.nextInt(4);
				int b = random.nextInt(13);
				Poker poker = new Poker(faces[b],colors[a]);
				boolean c = set.add(poker);
				if (c==true) {
					i++;
				}
				if (i==52) {
					break;
				}
			}
			set.add(new Poker("大王"));
			set.add(new Poker("小王"));
		
		//获取洗牌之后的牌组
		LinkedList<Poker> linkedList = new LinkedList<>();
		//迭代器遍历对象,将对象存入linkedList
		Iterator<Poker>iterator = set.iterator();
		while (iterator.hasNext()) {
			Poker poker = (Poker) iterator.next();
			linkedList.add(poker);
		}
			
		String string = "正在打乱扑克牌......";
		for(int n = 0;n<string.length();n++)
		{
			 System.out.print(string.charAt(n));
			 try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		System.out.println();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Collections.shuffle(linkedList); //乱序

		String string1 = "打乱后的扑克牌为:";
		for(int n = 0;n<string1.length();n++)
		{
			 System.out.print(string1.charAt(n));
			 try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println();
		System.out.println(linkedList);
		
		System.out.println();
		System.out.println("正在获取底牌......");
		System.out.println();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		LinkedList<Poker>linkedList2 = new LinkedList<>();//底牌三张
		while(linkedList2.size() < 3){
			int random = new Random().nextInt(linkedList.size());
			if (linkedList.get(random)!=null) {			
			linkedList2.add(linkedList.remove(random));	

			}
		}
		System.out.println("底牌是:"+linkedList2);
		System.out.println();
		System.out.println("正在获取地主牌......");
		System.out.println();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//地主牌:
		int x = new Random().nextInt(linkedList.size()-3);
		Poker diZhu = linkedList.get(x);
		System.out.println("地主牌是:"+diZhu);
		LinkedList<Poker> player1 = new LinkedList<>();
		LinkedList<Poker> player2 = new LinkedList<>();
		LinkedList<Poker> player3 = new LinkedList<>();
		
		for(int n = 0;n<linkedList.size();n++){
			if (n%3==0) {
				player1.add(linkedList.get(n));			
			}else if (n%3==1) {
				player2.add(linkedList.get(n));			
				
			}else {
				player3.add(linkedList.get(n));							
			}
		}
		System.out.println();
		System.out.println("正在获取玩家牌:");
		System.out.println();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("玩家一牌:"+player1);
		System.out.println("玩家二牌:"+player2);
		System.out.println("玩家三牌:"+player3);	
		System.out.println();
		
		for(int n = 0;n < player1.size();n++){
			if (player1.get(n)==diZhu) {
				System.out.println("地主是:玩家一");	
				player1.addAll(linkedList2);
				break;
			}else if(player2.get(n)==diZhu){
				System.out.println("地主是:玩家二");	
				player2.addAll(linkedList2);
				break;			
			}else if(player3.get(n)==diZhu){
				System.out.println("地主是:玩家三");	
				player3.addAll(linkedList2);
				break;						
			}
		}
		System.out.println();
		System.out.println("正在加载最后的牌......");
		System.out.println();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//最后:
		System.out.println("最后的牌为:");
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("玩家一牌:"+player1);
		System.out.println("玩家二牌:"+player2);
		System.out.println("玩家三牌:"+player3);	
	}
		
}

class Poker{
	String color;//花色
	String faces;//牌面
	public Poker() {
	}
	public Poker(String faces,String color) {
		this.color = color;
		this.faces = faces;
	}
	public Poker(String faces) {
		this.faces = faces;
		this.color = "";
	}
	@Override
	public String toString() {
		//return "Poker [color=" + color + ", faces=" + faces + "]";
		return color + faces;
	}	
	@Override
	public int hashCode() {
		return color.hashCode()+faces.hashCode();//花色和牌面相同哈希值和相同则认为哈希值相同
	}
	//花色和牌面都相同,则认为是一个对象
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Poker)) {
			throw new ClassCastException();
		}
		Poker poker = (Poker)obj;
		return color.equals(poker.color)&&faces.equals(poker.faces);
	}
}


 

猜你喜欢

转载自blog.csdn.net/qq_35334203/article/details/81810273
今日推荐