wangvic21
2018-10-31 17:04
将牌初始化时按照由小到大排列,其索引值即可表示牌的大小。
Poker.java中利用Map表示玩家的姓名,ID,持有的牌和牌的大小等。
Player.java是玩家的姓名和ID。
/********************Poker.java*******************/ package com.wangvic.practice.poker; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.InputMismatchException; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; /** * Poker list * * @author mhutc * @param CARDNUMBER 发牌数 * @param PLAYERNUMBER 玩家数量 * @param pokerList 从小到大排列 */ public class Poker { /** 从小到大排列 */ public List<PokerDefinition> pokerList; /** 洗好之后的牌 */ public List<PokerDefinition> pokerShuffleList; static fianl int CARDNUMBER = 3; static final int PLAYERNUMBER = 2; private String[] colorList = { "方片", "梅花", "红心", "黑桃" }; private String[] pointList = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" }; public Map<String, Player> mapPlayer = new HashMap<String, Player>(); private Scanner console = new Scanner(System.in); public Poker() { this.pokerList = new ArrayList<PokerDefinition>(); this.pokerShuffleList = new ArrayList<PokerDefinition>(); } public void initialPoker() { System.out.println("---------创建扑克牌---------"); addPoker(); System.out.println("--------扑克牌创建成功-------"); // 输出List System.out.println("扑克牌为:" + pokerList); } public void addPoker() { for (String point : pointList) { for (String color : colorList) { pokerList.add(new PokerDefinition(color, point)); } } } public void addPlayer() { for (int i = 0; i < PLAYERNUMBER;) { System.out.println("请输入第" + (i + 1) + "位玩家的ID和姓名"); System.out.println("输入ID:"); try { String id = console.nextInt() + ""; if (mapPlayer.containsKey(id)) { System.out.println("该ID已经被占用,请重新输入:"); continue; } else { System.out.println("输入姓名:"); String name = console.next(); mapPlayer.put(id, new Player(id, name)); i++; } } catch (InputMismatchException e) { System.out.println("Warning,请输入整数"); console.next(); // TODO: handle exception } } } public void showPlayer() { Set<String> id = mapPlayer.keySet(); for (String string : id) { System.out.println("----欢迎玩家:" + mapPlayer.get(string).getName()); } } public void initialPlayer() { addPlayer(); showPlayer(); System.out.println("----------玩家已就位----------"); System.out.println("---------游戏即将开始---------"); } /** * Shuffle the poker. */ public void shufflePoker() { pokerShuffleList.addAll(pokerList); Collections.shuffle(pokerShuffleList); } /** * Start poker game. */ public void start() { initialPoker(); initialPlayer(); } /** * Shuffle the poker and deal. */ public void dealPoker() { shufflePoker(); // 输出List System.out.println("洗牌后扑克牌为:" + this.pokerShuffleList); // Everyone got two poker cards. for (int i = 0; i < CARDNUMBER; i++) { int j = 0; Set<String> id = mapPlayer.keySet(); for (String string : id) { System.out.println( "----玩家" + mapPlayer.get(string).getName() + "-拿牌"); PokerDefinition pd = pokerShuffleList.get(i * PLAYERNUMBER + j); mapPlayer.get(string).playerPoker.add(pd); int record = pokerList.indexOf(pd); mapPlayer.get(string).record.add(record); j++; } } } public void showPoker() { Set<String> id = mapPlayer.keySet(); for (String string : id) { System.out.println( "----玩家" + mapPlayer.get(string).getName() + "-拿的牌是:"); System.out.println(mapPlayer.get(string).playerPoker); System.out.println( "----玩家" + mapPlayer.get(string).getName() + "的成绩是:"); System.out.println(mapPlayer.get(string).record); } } public int getMaxRecord(List<Integer> record) { Collections.sort(record); int size = record.size(); return record.get(size - 1); } public int compare(Map<String, Player> mapPlayer) { List<Integer> maxRecord = new ArrayList<Integer>(); Set<String> id = mapPlayer.keySet(); for (String string : id) { int temp = getMaxRecord(mapPlayer.get(string).record); System.out.print( "玩家" + mapPlayer.get(string).getName() + "的最高成绩是:" + temp); System.out.println(",最大牌是:" + pokerList.get(temp)); maxRecord.add(temp); } Collections.sort(maxRecord); return maxRecord.get(maxRecord.size() - 1); } public void result() { int result = compare(mapPlayer); Set<String> id = mapPlayer.keySet(); for (String string : id) { if (mapPlayer.get(string).record.contains(result)) { System.out.println( "恭喜玩家" + mapPlayer.get(string).getName() + "胜利!"); break; } } } public static void main(String[] args) { Poker poker = new Poker(); poker.start();// 游戏开始,初始化玩家和牌 poker.dealPoker();// 洗牌和发牌 poker.showPoker();// 玩家牌展示 poker.result(); } }
/*********************Player.java*****************/ package com.wangvic.practice.poker; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; /** * * @author mhutc * */ public class Player { private String id; private String name; public List<Integer> record; public Set<PokerDefinition> playerPoker; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Player(String id, String name) { this.id = id; this.name = name; this.playerPoker = new HashSet<PokerDefinition>(); this.record = new ArrayList<Integer>(); } }
/*******************PokerDefinition.java*****************/ package com.wangvic.practice.poker; /** * * @author mhutc * */ public class PokerDefinition { /** * (non-Javadoc) * * @see java.lang.Object#hashCode() */ @Override public 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; } /** * (non-Javadoc) * * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof PokerDefinition)) { return false; } PokerDefinition other = (PokerDefinition) 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; } private String color; private String point; public PokerDefinition() { } public PokerDefinition(String color, String point) { this.color = color; this.point = point; } public String getColor() { return color; } /** * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { // TODO Auto-generated method stub return color + point; } public void setColor(String color) { this.color = color; } public String getPoint() { return point; } public void setPoint(String point) { this.point = point; } }
在添加玩家的 AddPlayer()方法中,使用
continue,在输入错误的情况下,会导致玩家变少吧?
Java入门第三季
409792 学习 · 4340 问题
相似问题