public class Game {
public Map<Integer, String> pointMap;
public Map<Integer, String> colorMap;
private List<OneCard> allCards;
private Player player1 ;
private Player player2;
private Scanner console = new Scanner(System.in);
{
pointMap = new HashMap();
pointMap.put(1, "3");
pointMap.put(2, "4");
pointMap.put(3, "5");
pointMap.put(4, "6");
pointMap.put(5, "7");
pointMap.put(6, "8");
pointMap.put(7, "9");
pointMap.put(8, "10");
pointMap.put(9, "J");
pointMap.put(10, "Q");
pointMap.put(11, "K");
pointMap.put(12, "A");
pointMap.put(13, "2");
colorMap = new HashMap();
colorMap.put(4, "黑桃");
colorMap.put(3, "红桃");
colorMap.put(2, "梅花");
colorMap.put(1, "方片");
}
public void createCards() {
allCards = new ArrayList<>();
System.out.println("创建扑克牌");
for (int i = 1; i <= 13; i++) {
for (int j = 1; j <= 4; j++) {
allCards.add(new OneCard(j, i));
}
}
System.out.print("[");
for (int i = 0; i <= 51; i++) {
OneCard oneCard = allCards.get(i);
System.out.print("" + colorMap.get(oneCard.getColor()) + pointMap.get(oneCard.getPoint()));
if (i < 51) {
System.out.print(",");
}
}
System.out.println("]");
}
//将之前创建的"一副扑克牌"打乱顺序
public void shuffle() {
System.out.println("开始洗牌");
Collections.shuffle(allCards);
System.out.println("洗牌结束");
}
public void createPlayer() {
System.out.println("创建玩家");
player1 = new Player();
System.out.println("请输入第一位玩家的ID和姓名");
System.out.print("输入id:");
int id1 = console.nextInt();
player1.setId(id1);
System.out.print("输入姓名:");
String name1 = console.next();
player1.setName(name1);
player2 = new Player();
System.out.println("请输入第二位玩家的ID和姓名");
System.out.print("输入id:");
int id2 = console.nextInt();
player2.setId(id2);
System.out.print("输入姓名:");
String name2 = console.next();
player2.setName(name2);
}
public void license() {
List<OneCard> player1Cards = player1.getCards();
List<OneCard> player2Cards = player2.getCards();
for (int i = 0; i < 4; i++) {
if (i % 2 == 0) {
System.out.println("玩家:" + player1.getName() + "-拿牌");
player1Cards.add(allCards.remove(i));
} else {
System.out.println("玩家:" + player2.getName() + "-拿牌");
player2Cards.add(allCards.remove(i));
}
}
}
public void play() {
List<OneCard> player1Cards = player1.getCards();
List<OneCard> player2Cards = player2.getCards();
OneCard play1MaxCard = player1Cards.get(0).compareTo(player1Cards.get(1)) > 0 ? player1Cards.get(0) : player1Cards.get(1);
OneCard play2MaxCard = player2Cards.get(0).compareTo(player2Cards.get(1)) > 0 ? player2Cards.get(0) : player2Cards.get(1);
System.out.println("玩家" + player1.getName() + "最大的手牌是: " +
colorMap.get(play1MaxCard.getColor()) + pointMap.get(play1MaxCard.getPoint()));
System.out.println("玩家" + player2.getName() + "最大的手牌是: " +
colorMap.get(play2MaxCard.getColor()) + pointMap.get(play2MaxCard.getPoint()));
Player winner = play1MaxCard.compareTo(play2MaxCard) > 0 ? player1 : player2;
System.out.println("玩家" + winner.getName());
System.out.println("玩家各自的手牌是: ");
System.out.println(player1.getName() + ":" +
colorMap.get(player1.getCards().get(0).getColor()) +
pointMap.get(player1.getCards().get(0).getPoint())+" "+
colorMap.get(player1.getCards().get(1).getColor()) +
pointMap.get(player1.getCards().get(1).getPoint()));
System.out.println(player2.getName() + ":" +
colorMap.get(player2.getCards().get(0).getColor()) +
pointMap.get(player2.getCards().get(0).getPoint())+" "+
colorMap.get(player2.getCards().get(0).getColor())+
pointMap.get(player2.getCards().get(1).getPoint()));
}
public List<OneCard> getAllCards() {
return allCards;
}
public void setAllCards(List<OneCard> allCards) {
this.allCards = allCards;
}
public static void main(String[] args) {
Game play = new Game();
play.createCards();
play.createPlayer();
play.shuffle();
play.license();
play.play();
}
}
////////
public class OneCard implements Comparable<OneCard>{
private Integer color;
private Integer point;
public OneCard(Integer color, Integer point) {
this.color = color;
this.point = point;
}
public Integer getColor() {
return color;
}
public void setColor(Integer color) {
this.color = color;
}
public Integer getPoint() {
return point;
}
public void setPoint(Integer point) {
this.point = point;
}
@Override
public int compareTo(OneCard o) {
int sub = this.point - o.point;
int result = sub ==0?this.color-o.color:sub;
return result;
}
}
///////////////
public class Player {
private int id;
private String name;
private List<OneCard> cards = new ArrayList<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<OneCard> getCards() {
return cards;
}
public void setCards(List<OneCard> cards) {
this.cards = cards;
}
}