一,题目简介:

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对,各自特点了解更为深刻。提高了个人能力!

软件工程实践作业2 --梭哈游戏(java) 实践报告的更多相关文章

  1. 实践作业4:Web测试实践(小组作业)每日任务记录2

    实践作业4:Web测试实践(小组作业)每日任务记录2 会议时间:2017年12月22日 会议地点:东九教学楼自习区 主  持  人:王晨懿 参会人员:王晨懿.余晨晨.郑锦波.杨潇.侯欢.汪元 记  录 ...

  2. 实践作业4:Web测试实践(小组作业)每日任务记录5

    (一)今日任务更新 本次小组作业均已完成! 本组文件最终pdf文件(文件稍大,请耐心等待加载):https://files.cnblogs.com/files/ruanshuo170204/Web测试 ...

  3. 实践作业4:Web测试实践(小组作业)每日任务记录4

    昨天周日平安夜,给大家都放了假,故昨日博客未更新,今天回复博客更新. (一)今日任务更新 编号 人员 任务更新 1 侯欢 已经完成了对两个网站基本功能的分析,已形成基本功能分析报告. 2 余晨晨 上次 ...

  4. 实践作业4:Web测试实践(小组作业)每日任务记录3

    会议时间:2017年12月23日 会议地点:东九教学楼自习区 主  持  人:王晨懿 参会人员:王晨懿.余晨晨.郑锦波.杨潇.侯欢.汪元 记  录  人:王晨懿 会议议题:小组作业第二阶段 下面是今天 ...

  5. 实践作业4:Web测试实践(小组作业)每日任务记录1

    会议时间:2017年12月21日会议地点:东九教学楼自习区主 持 人:王晨懿参会人员:王晨懿.余晨晨.郑锦波.杨潇.侯欢.汪元记 录 人:王晨懿会议议题:小组作业熟悉和任务分配 (一)选择待测产品 我 ...

  6. 假期实践作业:从IT角度看地铁

    实习时间:2016/02/23——2016/02/26 实习地点:京港地铁14号线 实习报告: 大学四年过得真快,转眼就大三了,大学前两年半的生活可谓多姿多彩,从不懂计算机到对编程感兴趣,期待得最多的 ...

  7. Android游戏开发实践(1)之NDK与JNI开发01

    Android游戏开发实践(1)之NDK与JNI开发01 NDK是Native Developement Kit的缩写,顾名思义,NDK是Google提供的一套原生Java代码与本地C/C++代码&q ...

  8. 20135231 JAVA实验报告三:敏捷开发与XP实践

    ---恢复内容开始--- JAVA实验报告三:敏捷开发与XP实践 20135231 何佳 实验内容 1. XP基础 2. XP核心实践 3. 相关工具 实验要求 1.没有Linux基础的同学建议先学习 ...

  9. 2017-2018-2 20179205《网络攻防技术与实践》第十一周作业 SQL注入攻击与实践

    <网络攻防技术与实践>第十一周作业 SQL注入攻击与实践 1.研究缓冲区溢出的原理,至少针对两种数据库进行差异化研究 缓冲区溢出原理   在计算机内部,输入数据通常被存放在一个临时空间内, ...

随机推荐

  1. January 29th, 2018 Week 05th Monday

    Losing all hope was freedom. 彻底绝望就是真正的自由. Losing all the hopes, and we are free to challenge everyth ...

  2. January 09th, 2018 Week 02nd Tuesday

    Use the smile to change the world. Don't let the world change your smile. 用你的笑容去改变这个世界,别让这个世界改变了你的笑容 ...

  3. matplotlib numpy scipy 的安装

    一:windows 端的安装 #cmd指令 python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sym ...

  4. markdownpad破解

    下载在腾讯软件下载比较快. 破解我就不写步骤了,直接给个链接 https://blog.csdn.net/geekqian/article/details/74455409 授权邮箱:Soar360@ ...

  5. firefox event.preventDefault(); 没有效果的解决方案

    $('.sub-list-click a').click(function (event) { event.preventDefault(); var sub = $(this).parent(&qu ...

  6. 控件_CheckBox(多选按钮)

    import android.os.Bundle; import android.app.Activity; import android.widget.CheckBox; import androi ...

  7. Django之views

    一 URL补充 二 Views试图函数 一 URL补充 1 MTV模型 2  django建立流程(用命令版) (1)django-admin startproject projectname (2) ...

  8. SA-题目

    SA的题目 差异:https://lydsy.com/JudgeOnline/problem.php?id=3238 题意概述:给定一个长度为 $n$ 的字符串 $S$,令 $T_ i$ 表示它从第 ...

  9. [NOI2003],[AHOI2006]文本编辑器

    嘟嘟嘟 [NOI2003]的其实就是一个板子--所以我就不说啥了. 唯一需要注意的是读入字符(哎--):题中说"中间可能有空格,请忽略"的意思是要在程序里特判掉,不是不管他-- 输 ...

  10. MongoDB逻辑操作符$or, $and,$not,$nor

    $or是一个逻辑or操作符操作在一个数据或者多个表达式并且需要选择至少一个满足条件的表达式,$or有至少以下表达式: { $or: [ { <expression1> }, { <e ...