java 扑克牌比较五张-姚记扑克背面认牌图
发布时间:2023-03-24 09:15 浏览次数:次 作者:佚名
功能描述
1、创建扑克牌。包括四种花色(黑桃,红心,梅花,方块),十三种点数(2-10,J,Q,K),不考虑大小王。
2、创建两个玩家。包括玩家id、姓名、手牌等集合,手牌为扑克牌的集合。
3、洗牌。将创建好的扑克牌打乱。
4、发牌。将洗牌之后的扑克牌集合,从第一张开始java 扑克牌比较五张java 扑克牌比较五张,发给两个玩家,安装一人 一张的方式,每人发两张。
5、游戏。比较两名玩家手中的扑克牌。规则为:去两名玩家手上点数最大的牌进行比较,点数大的赢,若点数一样大,就按照色花进行比较。
难点分析:
1、洗牌,如何将扑克牌进行打乱。(主要知识点Random,list.contains())
2、扑克牌如何进行大小比较。(主要知识点collections.sort() , comparable)
实现代码
Card.java(扑克牌类)
package com.betty.dome2; public class Card implements Comparable{ // 牌面 private String num; // 花色 private String name; public Card(){ } public Card(String num,String name){ this.num = num; this.name = name; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((num == null) ? 0 : num.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Card other = (Card) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (num == null) { if (other.num != null) return false; } else if (!num.equals(other.num)) return false; return true; } // 获取牌面大小 private Integer getNumValue(String num){ switch(num){ case "A": return 12; case "2": return 13; case "3": return 1; case "4": return 2; case "5": return 3; case "6": return 4; case "7": return 5; case "8": return 6; case "9": return 7; case "10": return 8; case "J": return 9; case "Q": return 10; case "K": return 11; } return -1; } // 获取花色大小 private Integer getNameValue(String name){ switch(name){ case "黑桃": return 4; case "红心": return 3; case "梅花": return 2; case "方块": return 1; } return -1; } @Override public int compareTo(Card card) { // 如果牌面相同就看花色 int numCompare = getNumValue(this.num).compareTo(getNumValue(card.num)); if(numCompare == 0){ return getNameValue(this.name).compareTo(getNameValue(card.name)); } return numCompare; } }
Player.java(角色类)
package com.betty.dome2; import java.util.ArrayList; import java.util.List; public class Player { // 角色id private Integer ID; // 角色姓名 private String name; // 角色获得的牌 ListcardList; public Player(){ } public Player(Integer ID,String name){ this.ID = ID; this.name = name; this.cardList = new ArrayList (); } public Integer getID() { return ID; } public void setID(Integer iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Welcome.java(主程序)
package com.betty.dome2; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; import java.util.Scanner; public class Welcome { String[] nums = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; String[] names = {"黑桃","红心","梅花","方块"}; ListcardStartList; // 开始创建扑克牌时的序列 List cardEndList; // 洗牌后的序列 List playerList; // 玩家列表 public Welcome(){ this.cardStartList= new ArrayList (); this.cardEndList = new ArrayList (); this.playerList = new ArrayList (); } // 创建扑克牌 void createCard(){ System.out.println("-----------创建扑克牌---------------"); for (String name : names) { for (String num : nums) { cardStartList.add(new Card(num,name)); } } System.out.println("---------扑克牌创建成功--------------"); System.out.print("["); for(int i=0;i cardList = player.cardList; Collections.sort(cardList); Card card = cardList.get(cardList.size()-1); if(maxCard.compareTo(card)<0){ maxCard = card; winner = player; } System.out.println("----玩家:"+ player.getName() + "最大的牌为:" + card.getName()+ card.getNum()); } System.out.println("-----------玩家:"+winner.getName()+"获胜----------"); System.out.println("玩家各自的手牌为:"); for(int j=0;j 实现的效果图:
总结
到此这篇关于Java实现简易扑克牌游戏的文章就介绍到这了,更多相关Java扑克牌游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!