结构布局怎样修改优化,还有俩人比较的地方感觉写的好繁琐,自己都快绕进去了 哎 ********************************************************************************************* Card 类 package gamecard; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Card implements Comparable<Card>{ /** * 无参和有参构造器 */ public Card() { } public Card(String color, String value) { this.color = color; this.value = value; } //static vocabulary public final int cardSum = 13; public final int colorSum = 4; public final String[] colors = { "红桃", "黑桃", "方块", "梅花", "大" }; public final String[] values = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "王" }; public String VVV = "2345678910JQKA王 "; public String CCC = "梅花方块黑桃红桃小大 "; List<Card> cardList = new ArrayList<Card> (); /** * 创建一副扑克牌 * 1、建立一个list容器来存放扑克牌 * 2、循环遍历产生扑克牌 * 3、把扑克牌加入list容器 * 4、添加大小王 * 5、返回list容易值 * @return */ public void createCard() { for (int i = 0; i < cardSum; ++i) { for (int j = 0; j < colorSum; ++j) { color = colors[j]; value = values[i]; cardList.add(new Card(color,value)); } } cardList.addAll(Arrays.asList(new Card("大", "王"), new Card("小", "王"))); printCards(); } //打印牌 public void printCards() { int m = 0; for (Card card : cardList) { m++; System.out.print(card.toString()); if (0 == m%4) { System.out.print("\n"); } } System.out.println(); } /** * 洗牌 */ public void shuffleCards() { Collections.shuffle(cardList); // printCards(); } /** * 牌的花色和大小 * @return */ public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } private String color; private String value; /** * toString()方法重写 */ public String toString() { return " " + this.getColor() + this.getValue() + " \t"; } @Override public int compareTo(Card o) { // TODO Auto-generated method stub if (VVV.indexOf(this.value) != VVV.indexOf(o.value)) { return Integer.valueOf(VVV.indexOf(this.value)).compareTo(Integer.valueOf(VVV.indexOf(o.value))); } else { return Integer.valueOf(CCC.indexOf(this.color)).compareTo(Integer.valueOf(CCC.indexOf(o.color))); } } } ******************************************************************** 玩家类 package gamecard; import java.util.ArrayList; import java.util.List; public class Player { public List<Card> pCards = new ArrayList<Card>(); //构造器 public Player() { } public Player(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } private int id; private String name; //手牌增加 public void addpCards(Card card) { pCards.add(card); } @Override public String toString() { StringBuilder str = new StringBuilder(""); for (Card card : pCards) { str.append(card.getColor() + card.getValue() + " "); } return "玩家" + this.getName() + "\t手牌 : [ " + str + " ]"; } }
为啥异常捕获的 catch需要 in.next() 不加就无限循环 求大神告知
************************************************************************** 测试类 package gamecard; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class GameTest { public static Scanner in = new Scanner(System.in); //******************************************************** 可随意设计玩家人数和发牌数目 public static final int playerNum = 5; public static final int personalCardNum = 10; //************************************************************ //玩家群体对象 public static List<Player> players = new ArrayList<Player>(); //扑克牌对象 public static Card card = new Card(); public String VVV = "2345678910JQKA王 "; public String CCC = "梅花方块黑桃红桃小大 "; /** * 游戏流程 * @author yyq * 1、取牌,洗牌 * 2、创建玩家 * 3、输入玩家信息、给玩家发牌 * 4、显示两人最大牌 * 5、比较牌的大小,宣布获胜者 * 6、打印两玩家的手牌 */ public static void main(String[] args) { GameTest it = new GameTest(); //步骤一 System.out.println("--------------------游戏开始--------------------"); System.out.println("--------------------取出新牌--------------------"); card.createCard(); System.out.println("--------------------开始洗牌--------------------"); card.shuffleCards(); System.out.println("--------------------洗牌结束--------------------"); //步骤二 System.out.println("--------------------创建玩家--------------------"); it.createPlayer(); //步骤三 System.out.println("--------------------开始发牌--------------------"); it.getCards(); System.out.println("--------------------发牌结束--------------------"); //步骤四 System.out.println("--------------------结果显示--------------------"); it.showMaxAndWonner(); //步骤六 System.out.println("--------------------手牌显示--------------------"); it.showCard(); } /** * 创建玩家 * 1、新建玩家 * 2、提示输入ID,如果ID不符合规范重输 * 3、提示输入姓名,并给对象当赋值 */ public void createPlayer() { for (int i = 0; i < playerNum; ++i) { System.out.println("c创建第" + (i+1) + "位玩家!"); System.out.println("请输入玩家ID:"); int id; while (true) { try { id = in.nextInt(); break; } catch(Exception e) { System.out.println("请输入正整数:"); in.next(); // 吸收缓冲区 这是什么意思,这句话啥作用,我看别人加这句话就对了,但我不理解 } } System.out.println("请输入玩家姓名:"); String name = in.next(); players.add(new Player(id, name)); } } /** * 给玩家发牌 */ public void getCards() { int n = playerNum*personalCardNum; for (int i = 0; i < n; ++i) { System.out.println("玩家" + players.get(i%playerNum).getName() + "\t: 拿牌"); players.get(i%playerNum).pCards.add(card.cardList.get(i)); } } /** * 比较获胜者 */ public void showMaxAndWonner() { int n = players.get(0).pCards.size()-1; for (int i = 0; i < playerNum; ++i) { Player p = players.get(i%playerNum); //手牌排序 Collections.sort(players.get(i).pCards); System.out.println("玩家\t" + p.getName() + "最大手牌是:\t" + p.pCards.get(n).toString()); } Card card = players.get(0).pCards.get(n); String nameString = players.get(0).getName(); for (int i = 1; i < playerNum; ++i) { if (CCC.indexOf(card.getColor()) < CCC.indexOf(players.get(i).pCards.get(n).getColor())) { card = players.get(i).pCards.get(n); nameString = players.get(i).getName(); } else if (VVV.indexOf(card.getValue()) == VVV.indexOf(players.get(i).pCards.get(n).getValue()) && VVV.indexOf(card.getValue()) < VVV.indexOf(players.get(i).pCards.get(n).getValue())) { card = players.get(i).pCards.get(n); nameString = players.get(i).getName(); } } System.out.println(nameString + "获胜!"); } public void showCard() { for (int i = 0; i < playerNum; ++i) { Collections.sort(players.get(i).pCards); System.out.println(players.get(i).toString()); } } }