软件工程实践作业2 --梭哈游戏(java) 实践报告

2023-10-23 07:50

本文主要是介绍软件工程实践作业2 --梭哈游戏(java) 实践报告,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一,题目简介:

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

 

转载于:https://www.cnblogs.com/GY-Bky/p/4467648.html

这篇关于软件工程实践作业2 --梭哈游戏(java) 实践报告的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/266541

相关文章

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis