应该没有问题
package com.imooc;
import java.util.Comparator;
public class Puke{
public String huase;
public String value;
public Puke() {
huase="none";
value="none";
}
public Puke(String huase, String value) {
this.huase=huase;
this.value=value;
}
public String getHuase() {
return this.huase;
}
public Puke myCompare(Puke p) {
if (this.value.compareTo(p.value) > 0) {
return this;
}
if (this.value.compareTo(p.value) < 0) {
return p;
}
if (this.value.compareTo(p.value) == 0) {
if (this.huase.equals("黑桃")) {
return this;
} else if (p.huase.equals("黑桃")) {
return p;
} else if (this.huase.equals("红桃")) {
return this;
} else if (p.huase.equals("红桃")) {
return p;
} else if (this.huase.equals("梅花")) {
return this;
} else {
return p;
}
}
return new Puke(); // 这行代码必须要有,否则报错
}
}
package com.imooc;
import java.util.List;
public class Player {
public String name;
public Integer id;
public List<Puke> list;
public Player() {
name="none";
id=0;
}
public Player(String name, Integer id) {
this.name=name;
this.id=id;
}
}
package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Game {
public List<Puke> puke;
public Player[] player;
public Game() {
System.out.println("欢迎来到扑克牌游戏!");
System.out.println("创建扑克牌中");
puke = new ArrayList<Puke>();
// 创建一副按顺序排列的扑克牌
for (int i = 0; i < 52; i++) {
if (i < 13) {
if (i < 9) {
puke.add(new Puke("方片", String.valueOf(i + 2)));
} else if (i == 9) {
puke.add(new Puke("方片", "J"));
} else if (i == 10) {
puke.add(new Puke("方片", "Q"));
} else if (i == 11) {
puke.add(new Puke("方片", "K"));
} else if (i == 12) {
puke.add(new Puke("方片", "A"));
}
} else if (i < 26) {
if (i < 22) {
puke.add(new Puke("梅花", String.valueOf(i - 11)));
} else if (i == 22) {
puke.add(new Puke("梅花", "J"));
} else if (i == 23) {
puke.add(new Puke("梅花", "Q"));
} else if (i == 24) {
puke.add(new Puke("梅花", "K"));
} else if (i == 25) {
puke.add(new Puke("梅花", "A"));
}
} else if (i < 39) {
if (i < 35) {
puke.add(new Puke("红桃", String.valueOf(i - 24)));
} else if (i == 35) {
puke.add(new Puke("红桃", "J"));
} else if (i == 36) {
puke.add(new Puke("红桃", "Q"));
} else if (i == 37) {
puke.add(new Puke("红桃", "K"));
} else if (i == 38) {
puke.add(new Puke("红桃", "A"));
}
} else if (i < 52) {
if (i < 48) {
puke.add(new Puke("黑桃", String.valueOf(i - 37)));
} else if (i == 48) {
puke.add(new Puke("黑桃", "J"));
} else if (i == 49) {
puke.add(new Puke("黑桃", "Q"));
} else if (i == 50) {
puke.add(new Puke("黑桃", "K"));
} else if (i == 51) {
puke.add(new Puke("黑桃", "A"));
}
}
}
System.out.println();
System.out.println("创建扑克牌成功");
System.out.println("创建的扑克牌为:");
for (Puke i : puke) {
System.out.println(i.huase + i.value);
}
System.out.println("-------------开始洗牌------------------------------");
Collections.shuffle(puke);// 将列表顺序打乱
System.out.println("-------------洗牌完成------------------------------");
System.out.println("-------------创建玩家------------------------------");
player = new Player[2];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 2; i++) {
int tempid;
String tempName;
while (true) {
System.out.println("请您输入第" + (i + 1) + "个玩家的id");
try {
tempid = scanner.nextInt();
break;
} catch (Exception e) {
e.printStackTrace();
System.out.println("请您输入整形值!");
}
}
while (true) {
System.out.println("请您输入第" + (i + 1) + "个玩家的name");
try {
tempName = scanner.next();
break;
} catch (Exception e) {
e.printStackTrace();
System.out.println("请您输入String类型的字符串!");
}
}
player[i] = new Player(tempName, tempid);
}
System.out.println("欢迎玩家:" + player[0].name);
System.out.println("欢迎玩家:" + player[1].name);
// 接下来每人发两张牌
player[0].list = new ArrayList<Puke>();
player[1].list = new ArrayList<Puke>();
System.out.println("--------开始发牌------------------");
System.out.println("玩家:" + player[0].name + "拿牌");
player[0].list.add(puke.get(0));
System.out.println("玩家:" + player[1].name + "拿牌");
player[1].list.add(puke.get(1));
System.out.println("玩家:" + player[0].name + "拿牌");
player[0].list.add(puke.get(2));
System.out.println("玩家:" + player[1].name + "拿牌");
player[1].list.add(puke.get(3));
System.out.println("---------发牌结束----------------");
// 开始pk
Puke p1max = player[0].list.get(0).myCompare(player[0].list.get(1));
Puke p2max = player[1].list.get(0).myCompare(player[1].list.get(1));
if (p1max.myCompare(p2max).equals(p1max)) {
System.out.println("恭喜玩家:" + player[0].name + "获胜");
} else {
System.out.println("恭喜玩家:" + player[1].name + "获胜");
}
System.out.println("玩家:" + player[0].name + "的牌是:"+
player[0].list.get(0).huase+player[0].list.get(0).value+","+player[0].list.get(1).huase+player[0].list.get(1).value);
System.out.println("玩家:" + player[1].name + "的牌是:"+
player[1].list.get(0).huase+player[1].list.get(0).value+","+player[1].list.get(1).huase+player[1].list.get(1).value);
for (Puke i : puke) {
System.out.println(i.huase + i.value);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Game();
}
}