fewer_xie
2018-08-24 10:20
public class Test { public static List<Card> cardList = new ArrayList<Card>(); public static String[] cardNum = new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; public static String[] cardClass = new String[]{"方块","梅花","红桃","黑桃"}; public static Player player1; public static Player player2; public static void main(String[] args) { initPlayer(); initCard(); showCard(); shuffleCard(); drawCard(); showHandCard(); compareCard(); } public static void initPlayer(){ System.out.println("初始化玩家"); player1 = new Player("赌神"); player2 = new Player("赌圣"); } public static void initCard(){ System.out.println("\n创建一副牌\n"); for (int i = 0; i < 13; i++) { for (int j = 0; j < 4; j++) { cardList.add(new Card(cardNum[i],cardClass[j],i*13+j)); } } } public static void showCard(){ System.out.println("查看牌\n"); int count = 0; for (Card card : cardList){ System.out.print(card.toString() + "\t"); if(++count % 10 == 0 || count == 52){ System.out.println(); } } } public static void shuffleCard(){ System.out.println("\n洗牌\n"); Collections.shuffle(cardList); } public static void drawCard(){ System.out.println("双方抽牌\n"); List<Card> player1CardList = new ArrayList<Card>(); List<Card> player2CardList = new ArrayList<Card>(); for (int i = 52; i > 48; i--) { int j = (int)(Math.random()*i); player1CardList.add(cardList.get(j)); cardList.remove(j); j = (int)(Math.random()*--i); player2CardList.add(cardList.get(j)); cardList.remove(j); } player1.setCardList(player1CardList); player2.setCardList(player2CardList); } public static void showHandCard(){ System.out.println("双方公开手牌\n"); System.out.println("player1" + player1.getName() + "的手牌是"); System.out.println(player1.getCardList() + "\n"); System.out.println("player2" + player2.getName() + "的手牌是"); System.out.println(player2.getCardList() + "\n"); } public static void compareCard(){ System.out.println("双方比较手牌\n"); Card player1MaxCard = new Card(); Card player2MaxCard = new Card(); if (player1.getCardList().get(0).getWeight() > player1.getCardList().get(1).getWeight()){ player1MaxCard = player1.getCardList().get(0); } else { player1MaxCard = player1.getCardList().get(1); } System.out.println("player1最大的牌是" + player1MaxCard); if (player2.getCardList().get(0).getWeight() > player2.getCardList().get(1).getWeight()){ player2MaxCard = player2.getCardList().get(0); } else { player2MaxCard = player2.getCardList().get(1); } System.out.println("\nplayer2最大的牌是" + player2MaxCard); if (player1MaxCard.getWeight() > player2MaxCard.getWeight()){ System.out.println("\n场上最大的牌是" + player1MaxCard); System.out.println("\n胜者是player1" + player1.getName()); } else { System.out.println("\n场上最大的牌是" + player2MaxCard); System.out.println("\n胜者是player2" + player2.getName()); } System.out.println("\n游戏结束"); } } public class Player { private String name; private List<Card> cardList; public Player() { } public Player(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Card> getCardList() { return cardList; } public void setCardList(List<Card> cardList) { this.cardList = cardList; } } public class Card { private String num; private String type; private int weight; public Card() { } public Card(String num, String type, int weight) { this.num = num; this.type = type; this.weight = weight; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getType() { return type; } public void setType(String type) { this.type = type; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } @Override public String toString() { return type + num; } }
写的通俗易懂 好!!
想问一下 这个换成对象数组怎么写
条理清晰,也很简单
原来weight是用来比较的,但是我怎么没看到weight有在哪赋值吗
先自问自答一波
Java入门第三季
409792 学习 · 4340 问题
相似问题