// Plsyer类 import java.util.ArrayList; import java.util.List; /** * Created by iShu on 16/5/17. */ public class Player { private int ID; private String name; private List<Card> handCards; public Player(int ID, String name) { this.ID = ID; this.name = name; handCards = new ArrayList<>(); } public Player() { handCards = new ArrayList<>(); } public int getID() { return ID; } public String getName() { return name; } public List<Card> getHandCards() { return handCards; } }
// Card类 import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * 扑克牌类 * 包含四中花色: 黑桃, 红桃, 草花, 方块 * 包含十三中点色: 2-10, J, Q, K, A * 不包含大小王 * Created by iShu on 16/5/17. */ public class Card implements Comparable<Card>{ private String color; private String value; private static final int CARDNUM = 52; private static final int SINGLECARDNUM = 13; private static final int COLORNUM = 4; private static final List<String> Colors = Arrays.asList("方块", "草花", "红桃","黑桃"); private static final List<String> Values = Arrays.asList("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"); public String getColor() { return color; } public String getValue() { return value; } public Card() { } public Card(String color, String value) { this.color = color; this.value = value; } /** *产生52张扑克牌 * @return */ public static List<Card> generateCard() { List<Card> pocker = new ArrayList<>(); for (int i = 0; i < CARDNUM; ++i) { String newValue = null; String newColor = null; int tmp = i % SINGLECARDNUM; newValue = Values.get(tmp); tmp = i % COLORNUM; newColor = Colors.get(tmp); pocker.add(new Card(newColor, newValue)); // System.out.println(newColor + ", " + newValue); } return pocker; } @Override public String toString() { return "[" + color + " " + value + "]"; } /** * 根据牌的至和花色进行排序 * 排序规则为: * 如果牌的值相同,则根据花色进行比较 黑桃 > 红桃 > 草花 > 方片 * 否则比较牌的牌的值 * @param o: 待比较的牌 * @return */ @Override public int compareTo(Card o) { if (Values.indexOf(this.value) == Values.indexOf(o.value)) { return Integer.valueOf(Colors.indexOf(this.color)).compareTo(Integer.valueOf(Colors.indexOf(o.color))); } else { return Integer.valueOf(Values.indexOf(this.value)).compareTo(Integer.valueOf(Values.indexOf(o.value))); } } }
//PlayMain类 import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; /** * Created by iShu on 16/5/17. */ public class PlayMain { private List<Player> playerList; private List<Card> cardList; Scanner scanner; private static final int HandCardNum = 3; private static final int PlayerNum = 2; public PlayMain() { playerList = new ArrayList<>(); cardList = Card.generateCard(); scanner = new Scanner(System.in); } /** * 产生玩家 */ public void createPlayers() { int id; String name; int playerNum = PlayerNum; int i = 1; while(playerNum > 0) { try{ System.out.println("创建第" + i + "位玩家"); System.out.println("输入玩家编号:"); id = scanner.nextInt(); } catch (Exception e) { System.out.println("请输入正整数: "); scanner.next(); // 吸收缓冲区 continue; } System.out.println("输入玩家姓名:"); name = scanner.next(); playerList.add(new Player(id, name)); playerNum--; i++; } } /** * 显示玩家信息 */ public void showPlayer() { for (int i = 0; i < PlayerNum; ++i) { System.out.println("欢迎玩家" + playerList.get(i).getName()); } } /** * 显示产生的扑克牌信息 */ public void showCardList() { System.out.println(cardList.toString()); } /** * 对扑克牌进行排序 */ public void sortCardList() { Collections.sort(cardList); } /** * 对扑克牌进行洗牌 */ public void shuffleCardList() { Collections.shuffle(cardList); } /** * 玩家进行玩游戏 */ public void play() { System.out.println("------------开始发牌-----------"); int cardNums = PlayerNum * HandCardNum; for (int i = 0; i < cardNums; ++i) { Card card = cardList.get(i); playerList.get(i % PlayerNum).getHandCards().add(card); } System.out.println("-----------发牌结束-------------"); System.out.println("------------开始游戏------------"); int winner = 0; Card maxPocker = playerList.get(0).getHandCards().get(0); for (int i = 0; i < PlayerNum; ++i) { // 对手拍进行排序 Collections.sort(playerList.get(i).getHandCards()); // 对排序后的手牌进行逆序 Collections.reverse(playerList.get(i).getHandCards()); if (maxPocker.compareTo(playerList.get(i).getHandCards().get(0)) < 0) { maxPocker = playerList.get(i).getHandCards().get(0); winner = i; } } System.out.println("恭喜玩家" + playerList.get(winner).getName() + "获胜"); // 显示玩家手牌 System.out.println("玩家各自的手牌为:"); for (int i = 0; i < PlayerNum; ++i) { System.out.println(playerList.get(i).getName() + "手牌为: " + playerList.get(i).getHandCards().toString()); } } public static void main(String[] args) { System.out.println("----------产生一副扑克牌------------"); PlayMain playMain = new PlayMain(); playMain.showCardList(); System.out.println("----------创建扑克牌成功------------"); playMain.sortCardList(); playMain.showCardList(); System.out.println("------------开始洗牌---------------"); playMain.shuffleCardList(); System.out.println("------------洗牌结束---------------"); playMain.showCardList(); System.out.println("------------创建玩家---------------"); playMain.createPlayers(); playMain.showPlayer(); playMain.play(); } }
添加对象的时候用嵌套循环不行吗,为什么要用 i % SINGLECARDNUM 这种方式
好厉害,学习了
为什么我参考你的这句代码的时候一直空指针异常报错呢。
playerList.get(i % PlayerNum).getHandCards().add(card);
还有个疑问,为什么要逆序呀。
好巧妙,但是这里是不是每个玩家三张牌
参考你的代码成功实现了
cool,新手参考下。
赞!!!