shoumananxun
2015-06-04 11:39
游戏开始:O(∩_∩)O哈哈~
package imooc.pocker; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; /* * 可改变玩家数量和玩家手牌数 * 根据手牌中最大牌进行简单大小比较 */ public class Test { public static void main(String[] args) { System.out.println("--------产生一副扑克牌--------"); List<Pocker> pockers = Pocker.createPocker(); System.out.println("--------扑克牌创建成功--------"); System.out.println(pockers.toString()); System.out.println("--------开始洗牌--------"); Collections.shuffle(pockers); System.out.println("--------洗牌结束--------"); System.out.println(pockers.toString()); System.out.println("--------创建玩家--------"); Scanner scanner = new Scanner(System.in); List<Player> players = new ArrayList<Player>(); int handCardNum = 3; int playerNum = 2; while (true) { try { System.out.println("请输入玩家数量:"); playerNum = scanner.nextInt(); int id; String name; for (int i = 0; i < playerNum; i++) { System.out.println("创建第" + (i + 1) + "位玩家的ID和姓名:"); System.out.println("输入玩家id(int): "); id = scanner.nextInt(); System.out.println("输入玩家姓名:"); name = scanner.next(); players.add(new Player(id, name)); } for (int i = 0; i < playerNum; i++) { System.out.println("欢迎玩家:" + players.get(i).getName()); } System.out.println("--------开始发牌--------"); int cardNums = handCardNum * playerNum; Pocker pocker; for (int i = 0; i < cardNums; i++) { pocker = pockers.get(i); players.get(i % playerNum).getHandPockers().add(pocker); } System.out.println("--------发牌结束--------"); System.out.println("--------开始游戏--------"); Pocker maxPocker = new Pocker("方块", "2"); int winPlayer = 0; for (int i = 0; i < playerNum; i++) { Collections.sort(players.get(i).getHandPockers()); // 对手牌进行排序 Collections.reverse(players.get(i).getHandPockers()); // 排序后的手牌进行翻转,第一位为最大值 System.out .println("玩家" + players.get(i).getName() + "最大手牌为:" + players.get(i).getHandPockers().get(0) .toString()); if (maxPocker.compareTo(players.get(i).getHandPockers() .get(0)) < 0) { maxPocker = players.get(i).getHandPockers().get(0); winPlayer = i; } } System.out.println("玩家" + players.get(winPlayer).getName() + "获胜"); System.out.println("玩家各自的手牌为:"); for (int i = 0; i < playerNum; i++) { System.out.println(players.get(i).getName() + ": " + players.get(i).getHandPockers().toString()); } break; } catch (Exception e) { System.out.println("输入值错误,请重新输入"); scanner = new Scanner(System.in); } } } }
玩家类:Player
package imooc.pocker; import java.util.ArrayList; import java.util.List; public class Player { /* * 玩家 * 属性:ID,姓名,手牌 * 手牌为扑克牌的集合 */ private int id; private String name; private List<Pocker> handPockers; public Player(int id, String name) { this.id = id; this.name = name; this.handPockers = new ArrayList<Pocker>(); } public int getId() { return id; } public String getName() { return name; } public List<Pocker> getHandPockers() { return handPockers; } }
扑克牌类:Pocker
package imooc.pocker; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; /** * * @author sun * 扑克牌 * 包括四种花色:黑桃、红桃、梅花、方块、 * 十三中点数:2~10,J、Q、K、A,不包括大小王 */ public class Pocker implements Comparable<Pocker>{ private String color; //扑克牌花色 private String value; //扑克牌面值 private static final int CARDNUM = 52; //扑克牌总量 private static final int SINGLECOLORNUM = 13; //每种颜色扑克牌数量 public Pocker(){ } public Pocker(String color, String value) { this.color = color; this.value = value; } public String getColor() { return color; } public String getValue() { return value; } /* * 产生52张扑克牌 * return List<Pocker> */ public static List<Pocker> createPocker(){ List<Pocker> pockers = new ArrayList<Pocker>(); for(int i = 0; i < CARDNUM; i++){ String newValue = null; int temp = i % SINGLECOLORNUM; switch (temp) { case 11: newValue = "j"; break; case 12: newValue = "Q"; break; case 0: newValue = "K"; break; case 1: newValue = "A"; break; default: newValue = String.valueOf(temp); } String newColor = null; switch (i / SINGLECOLORNUM) { case 0: newColor = "黑桃"; break; case 1: newColor = "红桃"; break; case 2: newColor = "梅花"; break; case 3: newColor = "方块"; } pockers.add(new Pocker(newColor,newValue)); } return pockers; } /* * Collections.shuffle进行洗牌 */ public static List<Pocker> shuffle(List<Pocker> pockers){ Collections.shuffle(pockers); return pockers; } /* * (non-Javadoc) * @see java.lang.Comparable#compareTo(java.lang.Object) * 重写了copmareTo方法,实现了Comparable接口 * 根据牌值和花色在列表中的顺序进行比较 */ @Override public int compareTo(Pocker o) { List<String> valueBase = Arrays.asList("2","3","4","5","6","7","8","9","10","J","Q","K","A"); List<String> colorBase = Arrays.asList("方块","梅花","红桃","黑桃"); if(valueBase.indexOf(this.value) == valueBase.indexOf(o.value)) return Integer.valueOf(colorBase.indexOf(this.color)).compareTo(Integer.valueOf(colorBase.indexOf(o.getColor()))); else return Integer.valueOf(valueBase.indexOf(this.value)).compareTo(Integer.valueOf(valueBase.indexOf(o.getValue()))); } @Override public String toString() { return color+value; } }
虽然我暂时写不好,但是这个软件问题太多了,比如玩家数可以大于17,在输完id和姓名后才报错,然后我试了下17,然后创建玩家就会一直进行下去。
好东西~
pockers.add(new Pocker(newColor,newValue));这里为什么出错啊
if( if(valueBase.indexOf(this.value) == valueBase.indexOf(o.value)) return Integer.valueOf(colorBase.indexOf(this.color)).compareTo(Integer.valueOf(colorBase.indexOf(o.getColor()))); else return Integer.valueOf(valueBase.indexOf(this.value)).compareTo(Integer.valueOf(valueBase.indexOf(o.getValue())));) == valueBase.indexOf(o.value)) return Integer.valueOf(colorBase.indexOf(this.color)).compareTo(Integer.valueOf(colorBase.indexOf(o.getColor()))); else return Integer.valueOf(valueBase.indexOf(this.value)).compareTo(Integer.valueOf(valueBase.indexOf(o.getValue())));
这段代码比较的是什么和什么啊,这段代码看不懂了,大神们能解释一下么?valueBase.indexOf(this.value)是谁的值的位置,valueBase.indexOf(o.value)又是谁的值在valueBase里的位置?
666
public int compareTo(Pocker o) {
List<String> valueBase = Arrays.asList("2","3","4","5","6","7","8","9","10","J","Q","K","A");
List<String> colorBase = Arrays.asList("方块","梅花","红桃","黑桃");
if(valueBase.indexOf(this.value) == valueBase.indexOf(o.value))
return Integer.valueOf(colorBase.indexOf(this.color)).compareTo(Integer.valueOf(colorBase.indexOf(o.getColor())));
else
return Integer.valueOf(valueBase.indexOf(this.value)).compareTo(Integer.valueOf(valueBase.indexOf(o.getValue())));
}
请教下各位大牛,这个方法内部为啥可以调用compareTo本身呢?
return Integer.valueOf(colorBase.indexOf(this.color)).compareTo(Integer.valueOf(colorBase.indexOf(o.getColor())));
这一句怎么理解?直接返回
colorBase.indexOf(this.color).compareTo(colorBase.indexOf(o.getColor()))
不行吗?
大赞!
写的太棒了~~~~
就是,传作品里去吧。
为啥不传到作品里,好东西呀
Java入门第三季
409792 学习 · 4340 问题
相似问题