标签:
一,题目简介:
1、创建一副扑克牌 7------k 加入到集合对象中
2、对扑克牌洗牌
3、定义参与游戏的玩家的人,通过键盘输入,限定人数2-5
4、人数符合要求继续执行,不符合退出
5、对玩家发牌,每个人发五张,对玩家的牌排序
GitHub链接地址:
https://github.com/GY1/test/blob/master/ShowHand(%E6%A2%AD%E5%93%88%E6%B8%B8%E6%88%8F)
梭哈游戏 源代码:
package com.langsin.CollectionsFramework;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;
public class ShowHand {
	private ArrayList<String> pukeList = new ArrayList<String>();
	private Map<String, TreeSet<String>> map = new LinkedHashMap<String, TreeSet<String>>();
	private Map<String, Double> scoreMap = new LinkedHashMap<String, Double>();
	private boolean flag = true;
	// 1.创建扑克牌 7-K
	private void createPuke() {
		String[] point = {"2","3","4","5", "6", "7", "8", "9", "10", "J", "Q", "K" };
		String[] type = { "黑桃", "红桃", "梅花", "方块" };
		for (int i = 0; i < type.length; i++) {
			for (int j = 0; j < point.length; j++) {
				this.pukeList.add(type[i] + point[j]);
			}
		}
	}
	// 2.洗牌
	private void sortedPuke() {
		Random rand = new Random();
		for (int i = 0; i < 100; i++) {
			int index1 = rand.nextInt(this.pukeList.size());
			int index2 = rand.nextInt(this.pukeList.size());
			String puke1 = this.pukeList.get(index1);
			String puke2 = this.pukeList.get(index2);
			this.pukeList.set(index1, puke2);
			this.pukeList.set(index2, puke1);
		}
}
	// 3.创建参加游戏的人
	private void createPlayer() {
		System.out.println("请输入参与游戏的玩家的名称,中间用空格隔开:(人数不得超过8人,少于3人 ,否则程序终止!)");
		Scanner scan = new Scanner(System.in);
		String players = scan.nextLine();
		String[] nums = players.split(" ");
		if (nums.length < 3 || nums.length > 9) {
			System.out.println("参与游戏的玩家人数不符合要求,程序终止!");
			this.flag = false;
			System.exit(0);// (程序终止)
		}
		Comparator<String> comp = new Comparator<String>() {
public int compare(String str1, String str2) {
				int point1 = getPoint(str1.substring(2));
				int point2 = getPoint(str2.substring(2));
				if (point1 < point2) {
					return -1;
				} else if (point1 > point2) {
					return 1;
				} else {
					int type1 = getType(str1.substring(0, 2));
					int type2 = getType(str2.substring(0, 2));
					if (type1 < type2) {
						return -1;
					} else {
						return 1;
					}
				}
			}
		};
		for (int i = 0; i < nums.length; i++) {
			map.put(nums[i], new TreeSet<String>(comp));
		}
}
	private int getPoint(String point) {
		if ("J".equals(point)) {
			return 11;
		} else if ("Q".equals(point)) {
			return 12;
		} else if ("K".equals(point)) {
			return 13;
		} else {
			return Integer.parseInt(point);
		}
	}
	private int getType(String type) {
		if ("黑桃".equals(type)) {
			return 4;
		} else if ("红桃".equals(type)) {
			return 3;
		} else if ("梅花".equals(type)) {
			return 2;
		} else {
			return 1;
		}
	}
	// 4.给玩家发牌
	private void showPuke() {
		for (int i = 0; i < 5; i++) {
			for (String key : map.keySet()) {
				String puke = this.pukeList.remove(0);
				map.get(key).add(puke);
			}
		}
		for (String key : map.keySet()) {
			System.out.println("玩家名称:" + key + ":" + map.get(key));
		}
	}
	// 计算玩家分数,得到获胜玩家
	private void getScoreWinner() {
		for (String key : map.keySet()) {
			TreeSet<String> set = map.get(key);
			scoreMap.put(key, this.score(set));
		}
		List<String> list1=this.getWinner(scoreMap);
		System.out.println("恭喜" + list1.get(0) +"获得最终冠军! ! !"+ " 		冠军分数为:" + list1.get(1));
		Map<String, Double> scoreMap1=this.getScore(scoreMap);
		
		List<String> list2=this.getWinner1(scoreMap1);
		System.out.println("恭喜" + list2.get(0) +"获得亚军!!"+ " 		亚军分数为:" + list2.get(1));
	    Map<String, Double> scoreMap2=this.getScore(scoreMap1);
		
	    List<String> list3=this.getWinner1(scoreMap2);
		System.out.println("恭喜" + list3.get(0) +"获得季军!"+ " 		季军分数为:" + list3.get(1));
	
	}
	
	private List<String> getWinner(Map<String, Double> scoreMap) {
		List<String> list=new ArrayList<String>();
		String winner = null;
		double end = 0;
		for (Map.Entry<String, Double> entry : this.scoreMap.entrySet()) {
			System.out.println("玩家名: " + entry.getKey() + "	" + "玩家获得分值: "
					+ entry.getValue());
			if (end < entry.getValue()) {
				end = entry.getValue();
				winner = entry.getKey();
			}
		}
		list.add(winner);
		list.add(String.valueOf(end));
		return list;
	}
	
	private List<String> getWinner1(Map<String, Double> scoreMap) {
		List<String> list=new ArrayList<String>();
		String winner = null;
		double end = 0;
		for (Map.Entry<String, Double> entry : this.scoreMap.entrySet()) {
			if (end < entry.getValue()) {
				end = entry.getValue();
				winner = entry.getKey();
			}
		}
		list.add(winner);
		list.add(String.valueOf(end));
		return list;
	}
private Map<String, Double> getScore(Map<String, Double> scoreMap) {
		
		String winner = null;
		double end = 0;
		for (Map.Entry<String, Double> entry : this.scoreMap.entrySet()) {
			if (end < entry.getValue()) {
				end = entry.getValue();
				winner = entry.getKey();
			}
		}
		scoreMap.remove(winner);
		return scoreMap;
	}
	
	
	private double score(TreeSet<String> set) {
		boolean flag = true;
		String[] pukes = set.toArray(new String[set.size()]);
		for (int i = 0; i < pukes.length - 1; i++) {
			int point1 = this.getPoint(pukes[i].substring(2));
			int point2 = this.getPoint(pukes[i + 1].substring(2));
			if (point1 - point2 != -1) {
				flag = false;
				break;
			}
		}
		if (flag) {
			return  5.0 + this.getPoint(pukes[4].substring(2)) * 0.01;
		}
		int point1 = this.getPoint(pukes[0].substring(2));
		int point2 = this.getPoint(pukes[1].substring(2));
		int point3 = this.getPoint(pukes[2].substring(2));
		int point4 = this.getPoint(pukes[3].substring(2));
		int point5 = this.getPoint(pukes[4].substring(2));
		// 四张相同的卡
		if (point1 == point4 || point2 == point5) {
			return 4.0 + this.getPoint(pukes[2].substring(2)) * 0.01;
		}
		// 三张相同的卡
		if (point1 == point3 || point2 == point4 || point3 == point5) {
			return 3.0 + this.getPoint(pukes[2].substring(2)) * 0.01;
		}
		// 两对两张相同的卡
		if (point1 == point2 && point3 == point4) {
			return 2.0 + this.getPoint(pukes[2].substring(2)) * 0.01+ this.getPoint(pukes[0].substring(2)) * 0.0001+ this.getPoint(pukes[4].substring(2)) * 0.000001;
		}
		if (point1 == point2 && point5 == point4) {
			return 2.0 + this.getPoint(pukes[3].substring(2)) * 0.01+ this.getPoint(pukes[0].substring(2)) * 0.0001+ this.getPoint(pukes[2].substring(2)) * 0.000001;
		}
		if (point3 == point2 && point5 == point4) {
			return 2.0 + this.getPoint(pukes[3].substring(2)) * 0.01+ this.getPoint(pukes[1].substring(2)) * 0.0001+ this.getPoint(pukes[0].substring(2)) * 0.000001;
		}
		// 两张相同卡
		if (point1 == point2) {
			return 1.0+this.getPoint(pukes[0].substring(2))*0.01+this.getPoint(pukes[4].substring(2))*0.0001+this.getPoint(pukes[3].substring(2))*0.000001+this.getPoint(pukes[2].substring(2))*0.00000001;
		}
		if (point2 == point3) {
			return 1.0+this.getPoint(pukes[1].substring(2))*0.01+this.getPoint(pukes[4].substring(2))*0.0001+this.getPoint(pukes[3].substring(2))*0.000001+this.getPoint(pukes[0].substring(2))*0.00000001;
		}
		if (point3 == point4) {
			return 1.0+this.getPoint(pukes[2].substring(2))*0.01+this.getPoint(pukes[4].substring(2))*0.0001+this.getPoint(pukes[1].substring(2))*0.000001+this.getPoint(pukes[0].substring(2))*0.00000001;
		}
		if (point4 == point5) {
			return 1.0+this.getPoint(pukes[3].substring(2))*0.01+this.getPoint(pukes[2].substring(2))*0.0001+this.getPoint(pukes[1].substring(2))*0.000001+this.getPoint(pukes[0].substring(2))*0.00000001;
		}
		return this.getPoint(pukes[4].substring(2))*0.01+this.getPoint(pukes[3].substring(2))*0.0001+this.getPoint(pukes[2].substring(2))*0.000001+this.getPoint(pukes[1].substring(2))*0.00000001+this.getPoint(pukes[0].substring(2))*0.0000000001;
	}
	
	public void init() {
		   
		while (true) {
			this.createPuke();
			this.sortedPuke();
			this.createPlayer();
			if (flag) {
				this.showPuke();
				this.getScoreWinner();
				System.out.println("是否继续进行游戏?Y/N");
				Scanner scan = new Scanner(System.in);
				String idea = scan.nextLine().toUpperCase();
				if (idea.equals("Y")) {
					pukeList.clear();
					map.clear();
					this.init();
				} else {
					System.out.println("拜拜,下次见哦!");
					System.exit(0);
				}
			}
		}
			}
	public static void main(String[] args) {
		new ShowHand().init();
	}
}


问题及解决方案、心得体会:
通过本次实践 对java应用更加熟练,特别是集合框架的运用,set的无序,不可重复性,list的有序,可重复性,map的key-value对,各自特点了解更为深刻。提高了个人能力!
标签:
原文地址:http://www.cnblogs.com/GY-Bky/p/4467648.html