qq_侯杰_0
2016-04-11 13:50
请问:如果玩家数也由键盘获取,那么手牌这块该怎么处理呢?
package gamecard; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class GameTest { // 定义玩家数目和发牌数 public static final int playerNum = 5; //玩家五人 public static final int personalCardNum = 10; //每次发牌10张 public static Scanner in = new Scanner(System.in); 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()); } } } 有疑问@我
不知道
Java入门第三季
409792 学习 · 4340 问题
相似问题