public class Card implements Comparable<Card>{
private String cType;//牌的花色
private String cNum;//牌的序号
private static final int cardNum = 52;//牌的数量
private static final int typeNum = 13;//每种扑克牌数量
public Card(String cType,String cNum){
this.cType=cType;
this.cNum=cNum;
}
public Card(){}
public String getcType() {
return cType;
}
public void setcType(String cType) {
this.cType = cType;
}
public String getcNum() {
return cNum;
}
public void setcNum(String cNumber) {
this.cNum = cNum;
}
//开始创建扑克牌
public static List<Card> createdCard(){
System.out.println("开始创建扑克牌。。。。。。。");
List<Card> cards = new ArrayList<Card>();
for (int i=0;i<cardNum;i++){
String newValue = "";
int temp = i%typeNum;
switch (temp){
case 1:newValue="A";break;
case 11:newValue="J";break;
case 12:newValue="Q";break;
case 0:newValue="K";break;
default:newValue=String.valueOf(temp);
}
String colorValue = "";
switch (temp){
case 0:colorValue="黑桃";break;
case 1:colorValue="红桃";break;
case 2:colorValue="梅花";break;
case 3:colorValue="方块";break;
}
cards.add(new Card(colorValue,newValue));
}
System.out.println("创建扑克牌成功");
return cards;
}
//洗牌
public static List<Card> shuffle(List<Card> cards){
Collections.shuffle(cards);
return cards;
}
//根据牌值和花色比较牌的大小
public int compareTo(Card c){
List<String> baseValue = Arrays.asList("2","3","4","5","6","7","8","9","10","J","Q","K","A");
List<String> baseColor = Arrays.asList("方块","梅花","红桃","黑桃");
if (baseValue.indexOf(this.cNum)==baseValue.indexOf(c.cNum))
return Integer.valueOf(baseColor.indexOf(this.cType)).compareTo(Integer.valueOf(baseColor.indexOf(c.cType)));
else
return Integer.valueOf(baseValue.indexOf(this.cNum)).compareTo(Integer.valueOf(baseValue.indexOf(c.cNum)));
// 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.cNum) == valueBase.indexOf(c.cNum))
// return Integer.valueOf(colorBase.indexOf(this.cType)).compareTo(Integer.valueOf(colorBase.indexOf(c.getcType())));
// else
// return Integer.valueOf(valueBase.indexOf(this.cNum)).compareTo(Integer.valueOf(valueBase.indexOf(c.getcNum())));
}
public String toString(){
return cType+cNum;
}
}
class Player{
private int id;
private String name;
private List<Card> cards;
public Player(){}
public Player(int id,String name){
this.id = id;
this.name=name;
cards = new ArrayList<Card>();
}
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<Card> getCards() {
return cards;
}
public void setCards(List<Card> cards) {
this.cards = cards;
}
}
public class Test {
List<Player> players = new ArrayList<Player>();
//创建玩家
public List<Player> createdPlayer() {
Scanner input = new Scanner(System.in);
System.out.println("开始创建玩家.....");
int playerNum=2;
int id ;
String name;
while (true) {
try{
for (int i = 0; i < playerNum; i++) {
System.out.println("请输入第" + (i + 1) + "名玩家的ID:");
id = input.nextInt();
System.out.println("请输入第" + (i + 1) + "名玩家的姓名:");
name = input.next();
players.add(new Player(id,name));
}
System.out.println("创建玩家成功!");
for (int i = 0; i < playerNum; i++) {
System.out.println("欢迎玩家" + players.get(i).getName() + "加入游戏!");
}
break;
} catch(Exception e){
System.out.println("输入错误,请重新输入玩家id");
input = new Scanner(System.in);
}
}
return players;
}
public void playingGame(){
List<Card> cards = Card.createdCard();
List<Card> sCards = Card.shuffle(cards);
createdPlayer();
System.out.println("开始发牌。。。。。。");
int handCardNum=3;
int cardNum = handCardNum*players.size();
Card card;
for (int i=0;i<cardNum;i++){
card = sCards.get(i);
players.get(i%players.size()).getCards().add(card);
}
System.out.println("————发牌结束");
System.out.println("开始游戏。。。。。。");
Card maxCard = new Card("方块","2");
int winPlayer = 0;
for (int j=0;j<players.size();j++){
for (int i=0;i<handCardNum;i++){
Collections.sort(players.get(j).getCards());
Collections.reverse(players.get(j).getCards());
if(maxCard.compareTo(players.get(j).getCards().get(i))<0) {
maxCard=players.get(j).getCards().get(i);
winPlayer = j;
}
}
System.out.println("玩家"+players.get(j).getName()+"最大的牌为:"+players.get(j).getCards().get(0).toString());
}
System.out.println("玩家"+players.get(winPlayer).getName()+"获胜!");
for (int j=0;j<players.size();j++) {
System.out.println("玩家" + players.get(j).getName() + "的手牌为:");
for (int i=0;i<handCardNum;i++)
System.out.println(players.get(j).getCards().get(i).getcType()+players.get(j).getCards().get(i).getcNum());
}
}
public static void main(String[] args) {
Test test = new Test();
test.playingGame();
}
}