2.0版本

此次代码添加加了抛出异常在这里插入代码片的方法

package Java.demoTest01;public class Poker {private String point;private String color;public void setPoint(String point) {this.point=point;}public String getPoint() {return point;}public void setColor(String color) {this.color=color;}public String getColor() {return color;}public Poker(String color,String point) {// TODO 自动生成的构造函数存根this.point=point;this.color=color;}@Overridepublic String toString() {return color +" "+ point;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((color == null) ? 0 : color.hashCode());result = prime * result + ((point == null) ? 0 : point.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (!(obj instanceof Poker))return false;Poker other = (Poker) obj;if (color == null) {if (other.color != null)return false;} else if (!color.equals(other.color))return false;if (point == null) {if (other.point != null)return false;} else if (!point.equals(other.point))return false;return true;}}
package Java.demoTest01;import java.util.ArrayList;
import java.util.List;public class GamePlayer {private int id;private String name;private List<Poker> pokers;public GamePlayer(int id, String name) {// TODO 自动生成的构造函数存根this.id = id;this.name = name;this.pokers = new ArrayList<Poker>();}public void setId(int id) {this.id = id;}public int getId() {return id;}public void setName(String name) {this.name = name;}public String getName() {return name;}public List<Poker> getPokers() {return pokers;}public void setPokers(List<Poker> pokers) {this.pokers = pokers;}
}
package Java.demoTest01;import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;public class ControlCenter implements Comparator<Poker> {private List<Poker> pokerList;private List<GamePlayer> playerList;private List<Poker> pokerListAfterShuffle;private int playerId1;private String playerName1;private int playerId2;private String playerName2;private Scanner sc;private Poker getPokers;public ControlCenter() {// TODO 自动生成的构造函数存根this.pokerList = new ArrayList<>();this.playerList = new ArrayList<>();this.pokerListAfterShuffle = new ArrayList<>();this.sc = new Scanner(System.in);}/** 第一步:创建扑克牌*/public void poker() {String arrayPoint[] = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };String arrayColor[] = { "方块", "梅花", "红桃", "黑桃" };for (String colors : arrayColor) {for (String points : arrayPoint) {Poker poker = new Poker(colors, points);pokerList.add(poker);}}System.out.print("扑克牌:[");for (Poker pokerlist : pokerList) {System.out.print(pokerlist + " ");}System.out.println("]");}/** 第二步:洗牌*/public void shufflePoker() {System.out.println("-----洗牌开始-----");Collections.shuffle(pokerList);for (Poker shuffle : pokerList) {pokerListAfterShuffle.add(shuffle);}System.out.println("-----洗牌完毕-----");}/** 第三步:创建玩家*/public void player() {System.out.println("有请第1位玩家输入Id和Name:");while(true) {try {System.out.println("请输入Id:(注意:必须输入数字!!!)");playerId1 = sc.nextInt();break;}catch(InputMismatchException e) {System.out.println("请重新输入,因为您输入的不是数字");sc.nextLine();}}System.out.println("请输入Name:");playerName1 = sc.next();GamePlayer gp1 = new GamePlayer(playerId1, playerName1);playerList.add(gp1);System.out.println("有请第2位玩家输入Id和Name:");while(true) {try {System.out.println("请输入Id:(注意:必须输入数字!!!)");playerId2 = sc.nextInt();break;}catch(InputMismatchException e) {System.out.println("请重新输入,因为您输入的不是数字");sc.nextLine();}}System.out.println("请输入Name:");playerName2 = sc.next();GamePlayer gp2 = new GamePlayer(playerId2, playerName2);playerList.add(gp2);System.out.println("玩家:" + playerList.get(0).getName() + "和玩家" + playerList.get(1).getName() + "登录成功");}/** 第四步:发牌*/public void Licensing() {Random r = new Random();for (int i = 0; i < 2; i++) {for (int j = 0; j < 2; j++) {System.out.println(playerList.get(i).getName() + "拿牌");int k = r.nextInt(52);getPokers = pokerListAfterShuffle.get(k);playerList.get(i).getPokers().add(getPokers);}}}/** 第五步:重写comparator方法*/public int compare(Poker o1, Poker o2) {if (o2.getColor().charAt(0) - o1.getColor().charAt(0) > 1) {return 1;} else if (o2.getColor().charAt(0) - o1.getColor().charAt(0) < 1) {return -1;} else {if (o2.getPoint().charAt(0) - o1.getPoint().charAt(0) > 1) {return 1;} else if (o2.getPoint().charAt(0) - o1.getPoint().charAt(0) < 1) {return -1;} else {return 0;}}}/** 第六步:扑克牌比大小*/public void gameStart() {Collections.sort(playerList.get(0).getPokers(), new ControlCenter());Collections.sort(playerList.get(1).getPokers(), new ControlCenter());for (int i = 0; i < 2; i++) {for (int j = 0; j < 2; j++) {System.out.println("玩家:" + playerList.get(i).getName() + "手中的牌:" + playerList.get(i).getPokers().get(j).getColor()+ playerList.get(i).getPokers().get(j).getPoint());}}System.out.println("玩家" + playerList.get(0).getName() + "的牌:" + playerList.get(0).getPokers().get(0));System.out.println("玩家" + playerList.get(1).getName() + "的牌:" + playerList.get(1).getPokers().get(0));List<Poker> lastList = new ArrayList<>();lastList.add(playerList.get(0).getPokers().get(0));lastList.add(playerList.get(1).getPokers().get(0));Collections.sort(lastList, new ControlCenter());if (lastList.get(0).equals(playerList.get(0).getPokers().get(0))) {System.out.println("第一位玩家:" + playerList.get(0).getName()+"获胜");} else {System.out.println("第二位玩家:" + playerList.get(1).getName()+"获胜");}}public static void main(String[] args) {// TODO 自动生成的方法存根ControlCenter cc = new ControlCenter();cc.poker();cc.shufflePoker();cc.player();cc.Licensing();cc.gameStart();}}

/

/
代码实现效果
本人Java小白,此代码如果有BUG请私信我反馈

Java语言编写扑克牌小游戏相关推荐

  1. c语言编写数据存储的游戏,c语言经典小程序和c语言编写的小游戏带注释(自动保存的).doc...

    c语言经典小程序和c语言编写的小游戏带注释(自动保存的) 1.写一个定时?关机的小程?序,可以立即关?闭计算机,也可以一段?时间后关闭?计算机. #inclu?de #inclu?de #inclu? ...

  2. linux 剪刀石头布c语言,利用C语言编写“剪刀石头布”小游戏

    前言 大家好~ 我是一名C语言初学者,学了C语言基础后,我制作了一个小游戏:剪刀石头布. 希望大家能对我的思路和代码提出小Tips(eg.更简便的方法与程序) 我也会虚心接受大家的建议~ 一.游戏原理 ...

  3. java 2048游戏_JAVA2048游戏 本课程设计是基于java语言的2048小游戏设计 联合开发网 - pudn.com...

    JAVA2048游戏 所属分类:游戏 开发工具:Java 文件大小:789KB 下载次数:4 上传日期:2020-11-23 10:57:11 上 传 者:滴滴滴大萌 说明:  本课程设计是基于jav ...

  4. Java代码编写猜拳小游戏

    Java代码编写猜拳小游戏 import java.util.Random; import java.util.Scanner;public class Guess {public static vo ...

  5. 自己写了一个JAVA的简单扑克牌小游戏

    记录一下,自学两个星期JAVA写了个简单的程序. 但是对List和Set掌握还是不是很好,希望大神能指导一下程序如何简化. //Game 主体 package com.Garry; import ja ...

  6. 用C语言编写一个小游戏

    这是一个比较通用的方法来编写一个小游戏用 C 语言: 首先,确定游戏的目标和规则.这可能包括游戏的玩法.胜负条件.游戏元素和游戏流程等. 其次,确定游戏的用户界面.这可能包括游戏的菜单.按钮.文本框. ...

  7. C语言编写2048小游戏

    该博文为原创文章,未经博主同意不得转载,如同意转载请注明博文出处 本文章博客地址:https://cplusplus.blog.csdn.net/article/details/104992424 2 ...

  8. C语言 编写“剪刀石头布”小游戏

    目录 前言 一.游戏原理 二.C语言代码 1.引入函数 2.初始页面显示 3.游戏过程及结果 总结 前言 大家好~ 我是一名C语言初学者,学了C语言基础后,我制作了一个小游戏:剪刀石头布. 希望大家能 ...

  9. 用c语言编写打猎小游戏,使用c语言编写简单小游戏.docx

    PAGE / NUMPAGES 纯真童趣的<泡泡堂>,还有武林情仇,笑傲江湖的<剑侠情缘on line>.它是e时代常谈的话题,是交互式娱乐的主力军,是一种 高层次的综合艺术, ...

最新文章

  1. linux笔记本上安装了双显卡驱动(intel+nvidia)
  2. 【Unity】11.5 物理材质 (Physics Material)
  3. java final类 能被继承吗_Java中的类被final关键字修饰后,该类将不可以被继承()...
  4. 《×××颂》贵在突破了中国花鸟画难以反映社会主题的尴尬
  5. 12款优秀的 JavaScript 日历和时间选择控件
  6. 【MFC系列-第11天】CWinApp类成员分析
  7. QT 编译通过但是运行出现程序异常结束或者crashed的问题 在QT creator中最佳解决办法
  8. 【leetcode 简单】 第三十五题 环形链表
  9. 如何在rul中添加图片
  10. 动态规划之01背包问题(含代码C)
  11. 15分钟搭建自己的博客
  12. Linux平台(Ubuntu或者树莓派)上下载磁力链接;使用Deluge下载
  13. 安卓h5 ajax上传图片,移动端通过ajax上传图片(文件)并在前台展示——通过H5的FormData对象...
  14. 携手做大做强中国集成电路产业链
  15. 世界上最会“算计”的公司争相布局区块链,普华永道等四大会计师事务所的变革之路...
  16. Evaluate之迷思
  17. Scratch滚动的天空(2)
  18. 权限管理系统(包括审批流程)数据库设计图
  19. 绝对让你怀疑人生的游戏榜,游戏建模跟《人类一败涂地》很相似
  20. 产品用户手册难写在哪里?

热门文章

  1. 洛谷P6503[COCI2010-2011#3] DIFERENCIJA
  2. laravel 发送邮箱验证码
  3. 程序员为何在相亲界炙手可热
  4. 南通高二计算机会考题型,考试题型
  5. php最大输入时间,php 根据输入的参数,获取上季度最后一个月的时间
  6. Advanced Installer打包
  7. 个性化智能推荐系统分析与调研
  8. 【读书笔记】《代码不朽》
  9. 手动输入计算机在线,2001计算机英文录入反复练习
  10. 高性能MySQL(第3版)(MySQL旗舰名著 惊献全面升级)