凌_惜
2016-11-08 16:03:36浏览 7258
/**
* 扑克牌类
* @author:凌_惜
* @date:2016年11月7日 下午1:56:12
*/
public class Card {
private String color; //花色
private String number; //牌数
public Card() {
}
public Card(String color, String number) {
this.color = color;
this.number = number;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
/**
* 玩家
* @author:凌_惜
* @date:2016年11月7日 下午2:06:37
*/
public class Player {
private String id; //ID
private String name; //姓名
private List<Card> cards; //手牌
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Card> getCards() {
return cards;
}
public void setCards(List<Card> cards) {
this.cards = cards;
}
}
/**
* 比较规则类
* @author:凌_惜
* @date:2016年11月7日 下午2:26:59
*/
public class Compara implements Comparator<Card> {
@Override
public int compare(Card o1, Card o2) {
String colors = "方块 梅花 红桃 黑桃";
String numbers = "2 3 4 5 6 7 8 9 10 J Q K A";
//比较数字大小,选出最大的手牌
int result = numbers.indexOf(o1.getNumber()) - numbers.indexOf(o2.getNumber());
if (result < 0) {
return -1;
} else if (result > 0) {
return 1;
} else {
//比较花色大小,选出最大的手牌
int result2 = colors.indexOf(o1.getColor()) - colors.indexOf(o2.getColor());
if (result2 < 0) {
return -1;
} else if (result2 > 0) {
return 1;
} else {
return 0;
}
}
}
}
/**
* 测试类
* @author:凌_惜
* @date:2016年11月7日 下午2:38:59
*/
public class CardGameTest {
public static void main(String[] args) {
System.out.println("==========开始创建一副扑克牌(不含大小王)==========");
String[] colors = {"方块","梅花","红桃","黑桃"};
String[] numbers = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
List<Card> cardList = new ArrayList<Card>();
for (String c : colors) {
for (int i = 0; i < numbers.length; i++) {
Card card = new Card(c, numbers[i]);
cardList.add(card);
}
}
StringBuilder builder = new StringBuilder();
for (Card card : cardList) {
builder.append(card.getColor() + card.getNumber() + ",");
}
System.out.println("==========扑克牌创建成功==========");
System.out.println("扑克牌为:[" + builder.substring(0,builder.lastIndexOf(",")) + "]");
System.out.println("==========开始洗牌==========");
Collections.shuffle(cardList);
System.out.println("==========洗牌结束==========");
StringBuilder builder2 = new StringBuilder();
for (Card card : cardList) {
builder2.append(card.getColor() + card.getNumber() + ",");
}
System.out.println("洗牌后为:[" + builder2.substring(0, builder2.lastIndexOf(",")) + "]");
System.out.println("==========创建玩家==========");
Scanner scan = new Scanner(System.in);
List<Player> list = new ArrayList<Player>();
for (int i = 1; i < 3; i++) {
Player player = new Player();
System.out.println("**********请输入第" + i + "位玩家的ID和姓名**********");
System.out.println("请输入ID:");
String id = scan.next();
do{
if (!id.matches("\\d*")) {
System.out.println("错误的ID,请输入正整数字!");
id = scan.next();
} else {
break;
}
}while(true);
player.setId(id);
System.out.println("请输入玩家姓名:");
String name = scan.next();
player.setName(name);
list.add(player);
}
for (Player p : list) {
System.out.println("---欢迎玩家:" + p.getName());
}
System.out.println("==========开始发牌==========");
List<Card> p1 = new ArrayList<Card>();
List<Card> p2 = new ArrayList<Card>();
for (int i = 0; i < 4; i += 2) {
System.out.println("***玩家:" + list.get(0).getName() + "--拿牌");
p1.add(cardList.get(i));
System.out.println("***玩家:" + list.get(1).getName() + "--拿牌");
p2.add(cardList.get(i + 1));
}
list.get(0).setCards(p1);
list.get(1).setCards(p2);
System.out.println("==========发牌结束==========");
System.out.println("==========开始游戏==========");
//选出玩家1最大的牌
Collections.sort(p1, new Compara());
//选出玩家2最大的牌
Collections.sort(p2, new Compara());
List<Card> BigCards = new ArrayList<Card>();
BigCards.add(p1.get(1));
BigCards.add(p2.get(1));
//拿两个玩家最大的牌进行比较
Collections.sort(BigCards,new Compara());
System.out.println("***玩家:" + list.get(0).getName() + "最大的手牌为:" + p1.get(1).getColor() + p1.get(1).getNumber());
System.out.println("***玩家:" + list.get(1).getName() + "最大的手牌为:" + p2.get(1).getColor() + p2.get(1).getNumber());
//比较谁获胜
if (BigCards.get(1).equals(p1.get(1))) {
System.out.println("==========玩家:" + list.get(0).getName() + "获胜==========");
} else {
System.out.println("==========玩家:" + list.get(1).getName() + "获胜==========");
}
//显示玩家获取的手牌
String card1 = p1.get(0).getColor() + p1.get(0).getNumber();
String card2 = p1.get(1).getColor() + p1.get(1).getNumber();
String card3 = p2.get(0).getColor() + p2.get(0).getNumber();
String card4 = p2.get(1).getColor() + p2.get(1).getNumber();
System.out.println("---玩家" + list.get(0).getName() + "的手牌为:[" + card1 + ","+ card2 + "]");
System.out.println("---玩家" + list.get(1).getName() + "的手牌为:[" + card3 + ","+ card4 + "]");
}
}
热门评论
看了 好多份 觉得这份是最切合老师视频所讲 可见楼主已 习得精髓 赞!!
代码很清晰,写的很好,我也学到很多,不过有点小意见,就是ID输入错误那里 可以设置循环 如果一直错误就可以一直提示
//实例化player
Player player = new Player();
System.out.println("**********请输入第" + i + "位玩家的ID和姓名**********");
System.out.println("请输入ID:");
String id = scan.next();
do{
Pattern pattern = Pattern.compile("[0-9]*");
Matcher match = pattern.matcher(id);
if (!match.matches()) {
System.out.println("错误的ID,请输入正整数字!");
id = scan.next();
}
else{
break;
}
}while(true);
。。那个card的无参构造器中调用super() 是什么意思啊。。 小白求解释