Java之斗地主

本篇主要介绍通过集合的存取,来实现斗地主的发牌模式,并没有实现斗地主的完整功能。
按照斗地主的规则,完成洗牌发牌的动作。
在这里插入图片描述
具体规则:
1. 组装54张扑克牌
2. 将54张牌顺序打乱
3. 三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。
4. 查看三人各自手中的牌(按照牌的大小排序)、底牌
手中扑克牌从大到小的摆放顺序:大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3

代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class MyDouDiZhu {

	public static void main(String[] args) {
		
		HashMap hashMap = new HashMap<>();
		ArrayList list = new ArrayList<>();
		String[] puke = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
		String[] huase = {"♥","♠","♦","♣"};
		
		//存牌
		hashMap.put(0, "大王");
		list.add(0);
		hashMap.put(1, "小王");
		list.add(1);
		
		int count = 2;
		for (String pu : puke) {
			for (String hua : huase) {
				hashMap.put(count, hua+pu);
				list.add(count);
				count++;
			}
		}
		
		//洗牌
		Collections.shuffle(list);
		
		//玩家
		ArrayList player1 = new ArrayList<>();
		ArrayList player2 = new ArrayList<>();
		ArrayList player3 = new ArrayList<>();
		ArrayList bottom = new ArrayList<>();
		
		//发牌
		for (int i = 0; i < list.size(); i++) {
			
			if (i<3) {
				bottom.add(list.get(i));
			}
			
			if (i>=3&&i%3==0) {
				player1.add(list.get(i));
			}else if (i>=3&&i%3==1) {
				player2.add(list.get(i));
			}else if (i>=3&&i%3==2) {
				player3.add(list.get(i));
			}
		}
		
		//看牌
		playLookPuke(player1,hashMap,"玩家一:");
		playLookPuke(player2,hashMap,"玩家二:");
		playLookPuke(player3,hashMap,"玩家三:");
		playLookPuke(bottom,hashMap,"底牌:");
		
		
	}
	//看牌方法以及排序
	private static void playLookPuke(ArrayList player1, HashMap hashMap, String playname) {
		//排序
		Collections.sort(player1);
		
		System.out.println(playname);
		for (int key : player1) {
			String s = hashMap.get(key);
			System.out.print(s+" ");
		}
		System.out.println();
		
		
	}

}

结果:
Java之斗地主_第1张图片

你可能感兴趣的:(Java,Java,斗地主,集合)