当前位置:网站首页>慕课网-简易扑克牌游戏 思路清晰 简易版
慕课网-简易扑克牌游戏 思路清晰 简易版
2022-08-09 09:29:00 【农场主er】
游戏流程
- 创建一副全新的扑克牌;
- 加入两名玩家;
- 两位玩家各自随机抽取一张扑克牌;
- 比较点数大小,分出胜负。
对象
- 卡牌类
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Cards {
private final String[] suit = {
"黑桃", "红桃", "方块", "梅花"};
private final String[] points = {
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
private String[][] cards;
//便于后续排序
private Map<String,Integer> compare;
//构造函数
public Cards() {
cards = new String[suit.length][points.length];
compare=new HashMap<>();
for(int i=0;i<points.length;i++){
compare.put(points[i],i);
}
}
//创建扑克牌
public void CreatCards() {
for (int i = 0; i < suit.length; i++) {
for (int j = 0; j < points.length; j++) {
cards[i][j] = suit[i] + points[j];
}
}
}
//得到扑克牌
public String[][] getCards(){
return this.cards;
}
public Map<String,Integer> getMap(){
return this.compare;
}
//打印扑克牌
public void printCards() {
for (String[] each : cards) {
System.out.println(Arrays.toString(each));
}
}
}
2.玩家类
public class Players {
private final int cardsNumber=1;
private String name;
private String card;
//构造函数
public Players(String name){
this.name=name;
}
//得到名字
public String getName(){
return this.name;
}
得到卡牌
public String getCard(){
return this.card;
}
}
游戏
import java.util.Map;
import java.util.Scanner;
public class TestCards {
public static void main(String[] args) {
//新的扑克牌
System.out.println("---创建扑克牌---");
Cards aCards = new Cards();
aCards.CreatCards();
aCards.printCards();
String[][] cards = aCards.getCards();
//客户1
Scanner in = new Scanner(System.in);
System.out.print("请输入第一位玩家姓名:");
String name1 = in.nextLine();
Players player1 = new Players(name1);
//客户2
System.out.print("请输入第二位玩家姓名:");
String name2 = in.nextLine();
Players player2 = new Players(name2);
//发牌
System.out.println("---随机抽牌---");
//玩家1的牌
System.out.print(player1.getName() + "的牌:");
String card1 = cards[(int) (Math.random() * (cards.length - 1))][(int) (Math.random() * (cards[0].length - 1))];
System.out.println(card1);
//玩家2的牌
System.out.print(player1.getName() + "的牌:");
String card2 = cards[(int) (Math.random() * (cards.length - 1))][(int) (Math.random() * (cards[0].length - 1))];
while(card2.equals(card1))
card2 = cards[(int) (Math.random() * (cards.length - 1))][(int) (Math.random() * (cards[0].length - 1))];
System.out.println(card2);
//比较大小
System.out.println("---一决胜负---");
Map<String,Integer> map=aCards.getMap();
int condition=map.get(card1.substring(2,3)).compareTo(map.get(card2.substring(2,3)));
switch(condition){
case 1:
System.out.println(player1.getName()+"胜利!");
break;
case 0:
System.out.println(player1.getName()+"和"+player2.getName()+"打平");
break;
case -1:
System.out.println(player2.getName()+"胜利!");
break;
}
}
}
精简(不足)之处
- 用随机抽牌代替洗牌过程;
- 每人只抽了一张牌;
- 输入姓名缺少了异常捕捉;
- 比较大小没有利用Comparable等接口。
如有建议,欢迎评论区交流~
边栏推荐
- Rights management model, ACL, RBAC and ABAC (steps)
- 接口开发规范及测试工具的使用
- What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
- .equals==
- 性能测试的基本概念是什么?做好性能测试需要掌握哪些知识?
- Ontology development diary 04 - to try to understand some aspects of protege
- 7.FileFilter接口
- mysql 修改密码和忘记密码操作
- 性能测试报告包括哪些内容?模板范文哪里找?看这里
- 归并排序
猜你喜欢
随机推荐
Ontology Development Diary 01-Jena Configuration Environment Variables
Max Flow P
通过程序发送 Gmail 邮件
Ontology Development Diary 05-Strive to Understand SWRL (Part 2)
选择黑盒测试用例设计方法的综合策略方案总结
全网最全的软件测试基础知识整理(新手入门必学)
A Practical Guide to Building OWL Ontologies using Protege4 and CO-ODE Tools - Version 1.3 (7.4 Annotation Properties - Annotation Properties)
本体开发日记02-sparql简单查询
1.流的概念
你一定要看的安装及卸载测试用例的步骤及方法总结
Do you know the principles of test cases and how to write defect reports?
字典
MySQL Leak Detection and Filling (2) Sorting and Retrieval, Filtering Data, Fuzzy Query, Regular Expression
类 对象 属性 方法 类的成员
.equals==
【分布式事务】
unix环境编程学习-多线程
A little experience sharing about passing the CISSP exam at one time
3.List interface and implementation class
mac 上安装Redis和配置