Java实现简易扑克牌游戏的完整实例
作者:waShaXiu
这篇文章主要介绍了Java实现简易扑克牌游戏的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
功能描述
1、创建扑克牌。包括四种花色(黑桃,红心,梅花,方块),十三种点数(2-10,J,Q,K),不考虑大小王。
2、创建两个玩家。包括玩家id、姓名、手牌等集合,手牌为扑克牌的集合。
3、洗牌。将创建好的扑克牌打乱。
4、发牌。将洗牌之后的扑克牌集合,从第一张开始,发给两个玩家,安装一人 一张的方式,每人发两张。
5、游戏。比较两名玩家手中的扑克牌。规则为:去两名玩家手上点数最大的牌进行比较,点数大的赢,若点数一样大,就按照色花进行比较。
难点分析:
1、洗牌,如何将扑克牌进行打乱。(主要知识点Random,list.contains())
2、扑克牌如何进行大小比较。(主要知识点collections.sort() , comparable)
实现代码
Card.java(扑克牌类)
package com.betty.dome2; public class Card implements Comparable<Card>{ // 牌面 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; // 角色获得的牌 List<Card> cardList; public Player(){ } public Player(Integer ID,String name){ this.ID = ID; this.name = name; this.cardList = new ArrayList<Card>(); } 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 = {"黑桃","红心","梅花","方块"}; List<Card> cardStartList; // 开始创建扑克牌时的序列 List<Card> cardEndList; // 洗牌后的序列 List<Player> playerList; // 玩家列表 public Welcome(){ this.cardStartList= new ArrayList<Card>(); this.cardEndList = new ArrayList<Card>(); this.playerList = new ArrayList<Player>(); } // 创建扑克牌 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<cardStartList.size();i++){ Card card = cardStartList.get(i); if(i%13!=0) System.out.print(","); if((i+1)%13==1 && i!=0){ System.out.println("]"); System.out.print("["); } System.out.print(card.getName()+card.getNum()); } System.out.println("]"); } // 洗牌 void shuffleCard(){ System.out.println("-----------开始洗牌---------------"); Random random = new Random(); Integer listSize = cardStartList.size(); for(int i=0;i<listSize;i++){ Card cd = new Card(); do{ cd = cardStartList.get(random.nextInt(listSize)); }while(cardEndList.contains(cd)); cardEndList.add(cd); } System.out.println("-----------洗牌结束---------------"); } // 输入角色姓名 @SuppressWarnings("resource") private String getName() { System.out.print("输入姓名:"); Scanner input = new Scanner(System.in); return input.next(); } // 输入角色id && 输入整数判断 @SuppressWarnings("resource") private Integer getId() { Scanner input = new Scanner(System.in); Integer id; while(true){ try { System.out.print("输入ID:"); id = input.nextInt(); break; } catch (Exception e) { System.out.println("请输入整数"); input = new Scanner(System.in); } } return id; } // 创建角色 private void createPlayers() { Integer playerNum = 2; for(int i=0;i<playerNum;i++){ playerList.add(new Player(getId(), getName())); } for (Player player : playerList) { System.out.println("----欢迎玩家 :" + player.getName()); } } // 给角色发牌 private void sendCard() { System.out.println("-----------开始发牌---------------"); int cardSendNum = 2,index = 0; for(int i=0;i<cardSendNum;i++){ for(int j=0;j<playerList.size();j++){ Player player = playerList.get(j); player.cardList.add(cardEndList.get(index++)); System.out.println("----玩家 :" + player.getName()+" 拿牌"); } } System.out.println("-----------发牌结束---------------"); } // 游戏开始 private void playGame() { System.out.println("-----------游戏开始---------------"); Card maxCard = new Card("0","方块"); Player winner = new Player(); for(int i=0;i<playerList.size();i++){ Player player = playerList.get(i); List<Card> 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<playerList.size();j++){ Player player = playerList.get(j); System.out.print("玩家 :" + player.getName()+"["); for(int i=0;i<player.cardList.size();i++){ if(i!=0) System.out.print(","); System.out.print(player.cardList.get(i).getName()+ player.cardList.get(i).getNum()); } System.out.print("]\n"); } } public static void main(String[] args) { Welcome we = new Welcome(); we.createCard(); // 创建扑克牌 we.shuffleCard(); // 洗牌 we.createPlayers(); // 创建角色 we.sendCard(); // 给角色发牌 we.playGame(); // 游戏开始 } }
实现的效果图:
总结
到此这篇关于Java实现简易扑克牌游戏的文章就介绍到这了,更多相关Java扑克牌游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!