创建Card类
package com.imooc.mika.cardsplay; public class Card implements Comparable{ private int color; private int number; public Card() { } public Card(int color, int number) { super(); this.color = color; this.number = number; } public int getColor() { return color; } public void setColor(int color) { this.color = color; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } @Override public String toString() { String colorString = ""; String numberString = ""; switch (color) { case 0: colorString = "♠"; break; case 13: colorString = "♥"; break; case 26: colorString = "♣"; break; case 39: colorString = "♦"; break; } switch (number) { case 11: numberString = "J"; break; case 12: numberString = "Q"; break; case 13: numberString = "K"; break; case 14: numberString = "A"; break; default: numberString = number + ""; break; } return colorString + numberString; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + color; result = prime * result + number; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Card other = (Card) obj; if (color != other.color) return false; if (number != other.number) return false; return true; } @Override public int compareTo(Card o) { if (this.number > o.number) return 1; else if (this.number < o.number) return -1; else if (this.color > o.color) return 1; else if (this.color < o.color) return -1; else return 0; } }
创建player类
package com.imooc.mika.cardsplay;
import java.util.ArrayList;
public class Player {
private int id;
private String name;
public ArrayList handCards = new ArrayList();
public Player(int id, String name) {
this.id = id;
this.name = name;
}
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;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Player other = (Player) obj;
if (id != other.id)
return false;
return true;
}
@Override
public String toString() {
return "ID:" + id + ", Name:" + name;
}
}
主程序
package com.imooc.mika.cardsplay;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class CardsPlayEasy {
private static Scanner sn = new Scanner(System.in);
private static ArrayList players = new ArrayList();
private static ArrayList cards = new ArrayList();
public static void main(String[] args) {
// 创建扑克牌,一副牌52张,可创建多副
cardsInitial();
// 洗牌
shuffleCards();
// 创建玩家,可创建多人
createPlayers();
// 发牌
getHandCards();
// 游戏
playGame();
}
private static void cardsInitial() {
System.out.println("---------------创建扑克牌--------------");
int fu = 0;
while (true) {
System.out.println("输入创建几副牌(1副52张):");
fu = inputCommand();
if (fu < 1)
System.out.println("至少1副牌");
else
break;
}
for (int k = 0; k < fu; k++) {
for (int i = 0; i < 52; i++)
cards.add(new Card(i / 13 * 13, i % 13 + 2));
}
System.out.println("---------------扑克牌创建成功!-----------------");
System.out.println(cards);
}
private static void shuffleCards() {
System.out.println("---------------开始洗牌-----------------");
Collections.shuffle(cards);
System.out.println("---------------洗牌完成!-----------------");
}
private static void createPlayers() {
System.out.println("---------------创建玩家:-----------------");
int count = 0;
while (true) {
System.out.println("输入创建几个玩家:");
count = inputCommand();
if (count < 1)
System.out.println("至少1个玩家");
else
break;
}
for (int i = 0; i < count; i++) {
System.out.println("请输入第" + (i + 1) + "玩家的ID和姓名:");
int playerId = 0;
while (true) {
System.out.println("输入ID(1~" + count + "):");
playerId = inputCommand();
if (playerId < 1 || playerId > count)
System.out.println("不在ID范围内");
else if (players.contains(new Player(playerId, "")))
System.out.println("该ID玩家已存在");
else
break;
}
System.out.println("输入姓名:");
String playerName = sn.next();
players.add(new Player(playerId, playerName));
}
for (Player player : players)
System.out.println("-----欢迎玩家:" + player);
}
private static void playGame() {
ArrayList maxHandCards = new ArrayList();
for (Player player : players) {
System.out.println("玩家:" + player.getName() + "手牌为:" + player.handCards);
Collections.sort(player.handCards, Collections.reverseOrder());
if (!player.handCards.isEmpty()) {
maxHandCards.add(player.handCards.get(0));
System.out.println("玩家:" + player.getName() + "最大手牌为:" + player.handCards.get(0));
}
}
Collections.sort(maxHandCards, Collections.reverseOrder());
if (!maxHandCards.isEmpty()) {
for (Player player : players) {
if (maxHandCards.get(0).equals(player.handCards.get(0))) {
System.out.println("-------玩家" + player.getName() + "获胜!-----");
}
}
} else
System.out.println("-------没人有牌!-----");
}
private static void getHandCards() {
System.out.println("---------------开始发牌-----------------");
int everyMaxCards = 0;
while (true) {
System.out.println("输入每个玩家最多抓取几张牌:");
everyMaxCards = inputCommand();
if (everyMaxCards < 1)
System.out.println("至少1张牌");
else
break;
}
if (players.isEmpty())
return;
for (int i = 0; i < cards.size(); i++) {
Player player = players.get((i % players.size()));
if (player.handCards.size() >= everyMaxCards)
break;
player.handCards.add(cards.get(i));
System.out.println("玩家:" + player.getName() + "-拿牌");
System.out.println("----------------发牌结束!-------------------");
}
}
private static int inputCommand() {
try {
return sn.nextInt();
} catch (Exception e) {
sn = new Scanner(System.in);
return -1;
}
}
}



