软件工程实践作业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中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

Java中Redisson 的原理深度解析

《Java中Redisson的原理深度解析》Redisson是一个高性能的Redis客户端,它通过将Redis数据结构映射为Java对象和分布式对象,实现了在Java应用中方便地使用Redis,本文... 目录前言一、核心设计理念二、核心架构与通信层1. 基于 Netty 的异步非阻塞通信2. 编解码器三、

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

一篇文章彻底搞懂macOS如何决定java环境

《一篇文章彻底搞懂macOS如何决定java环境》MacOS作为一个功能强大的操作系统,为开发者提供了丰富的开发工具和框架,下面:本文主要介绍macOS如何决定java环境的相关资料,文中通过代码... 目录方法一:使用 which命令方法二:使用 Java_home工具(Apple 官方推荐)那问题来了,

JDK21对虚拟线程的几种用法实践指南

《JDK21对虚拟线程的几种用法实践指南》虚拟线程是Java中的一种轻量级线程,由JVM管理,特别适合于I/O密集型任务,:本文主要介绍JDK21对虚拟线程的几种用法,文中通过代码介绍的非常详细,... 目录一、参考官方文档二、什么是虚拟线程三、几种用法1、Thread.ofVirtual().start(

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

从基础到高级详解Go语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础