当前位置:网站首页>慕课网-简易扑克牌游戏 思路清晰 简易版
慕课网-简易扑克牌游戏 思路清晰 简易版
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等接口。
如有建议,欢迎评论区交流~
边栏推荐
- 6.Map接口与实现类
- class object property method class member
- 5.Set interface and implementation class
- .equals ==
- Ontology development diary 04 - to try to understand some aspects of protege
- 记录一次被入侵5900端口经历
- RPC服务远程漏洞
- 搭建Tigase进行二次开发
- 3.练习Thread
- Source GBase database, oracle quote "ORA - 01000: beyond the shop open the cursor"
猜你喜欢
随机推荐
mysql 修改密码和忘记密码操作
QT sets the icon of the exe executable
常用功能测试的检查点与用例设计思路
功能自动化测试实施的原则以及方法有哪些?
单元测试是什么?怎么写?主要测试什么?
goproxy.io 证书过期
"The camera can't be used" + win8.1 + DELL + external camera + USB drive-free solution
What does the test plan include?What is the purpose and meaning?
Go-控制语句那些事
A Practical Guide to Building OWL Ontologies using Protege4 and CO-ODE Tools - Version 1.3 (7.4 Annotation Properties - Annotation Properties)
1. The concept of flow
用户设备IP三者绑定自动上号
6.Map interface and implementation class
BigDecimal用法常用操作记录
本体开发日记04-努力理解protege的某个方面
7.FileFilter interface
.ts 音频文件转换成 .mp3 文件
电脑硬件基础知识科普
web测试之功能测试常用的方法有哪几种?有什么要点要注意?
记录一次被入侵5900端口经历








