/**
*Game.java
*/
package program;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class Game {
private List<Card> cardList;
private List<Player> playerList;
private Game() {
this.cardList = new ArrayList<Card>();
this.playerList = new ArrayList<Player>();
}
public void createCard() {
System.out.println("------------创建扑克牌----------------");
for (String suit : Card.CARD_SUIT_ARRAY) {
for (String number : Card.CARD_NUMBER_ARRAY) {
cardList.add(new Card(suit, number));
}
}
System.out.println("------------扑克牌创建成功!!----------------");
System.out.println("为:" + cardList);
}
/**
* 通过Collections工具类实现洗牌
*/
public void suffleCard() {
System.out.println("------------开始洗牌...----------------");
Collections.shuffle(cardList);
System.out.println("------------洗牌结束!----------------");
}
/**
* 创建两个玩家,并判断他们的id是否输入正确和id是否重复
*/
public void createPlayer() {
Scanner console = new Scanner(System.in);
int id;
String name;
Boolean flags = false;// 第一次不用判断是否存在该ID
System.out.println("------------创建玩家...----------------");
for (int i = 1; i <= 2;) {
System.out.println("请输入第" + i + "位玩家ID和姓名:");
try {
System.out.println("输入ID:");
id = console.nextInt();
if (flags == true && playerList.get(0).getId() == id) {
System.out.println("ID已经存在,请重新输入!!!");
} else {
System.out.println("输入姓名:");
name = console.next();
playerList.add(new Player(id, name));
i++;
flags = true;
}
} catch (InputMismatchException e) {
console.next();
System.out.println("请输入正确的ID格式(纯数字)!!!");
}
}
console.close();
console = null;
for (Player player : playerList) {
System.out.println("----欢迎玩家:" + player);
}
}
/**
* 发牌
*/
public void deal() {
System.out.println("------------开始发牌...----------------");
for (int i = 0; i < 2; i++) {
for (Player player : playerList) {
System.out.println("----玩家:" + player + "-拿牌");
player.getPlayerCardList().add(cardList.get(0));
cardList.remove(0);
}
}
System.out.println("------------发牌结束!----------------");
}
public void startGame() {
Player winPlayer = playerList.get(0);// 默认第一个玩家为赢家
Card maxCard = playerList.get(0).getPlayerCardList().get(0);// 默认第一个玩家的手牌为最大牌
Card playerMaxCard = null;// 每个玩家的最大牌
Boolean flags = false;// 判断是否为第一次,第一次不做平局判断
System.out.println("------------开始游戏...----------------");
for (Player player : playerList) {
Collections.sort(player.getPlayerCardList());// 对两玩家的牌进行排序
playerMaxCard = player.getPlayerCardList().get(0);// 当前玩家的最大手牌
System.out.println("玩家:" + player + "最大的手牌为:" + playerMaxCard);
if (maxCard.compareTo(playerMaxCard) > 0) {
winPlayer = player;
maxCard = playerMaxCard;
} else if (flags && maxCard.compareTo(playerMaxCard) == 0) {
System.out.println("平局");
}
flags = true;
}
System.out.println("------------玩家:" + winPlayer + "获胜!----------------");
System.out.println("玩家各自的手牌为:");
for (Player player : playerList) {
System.out.println(player + ":" + player.getPlayerCardList());
}
}
public static void main(String[] args) {
Game game = new Game();
game.createCard();
game.suffleCard();
game.createPlayer();
game.deal();
game.startGame();
}
}
/**
*Card.java
*/
package program;
import java.util.HashMap;
import java.util.Map;
public class Card implements Comparable<Card> {
private String suit;// 花色
private String number;// 点数
public static final String[] CARD_SUIT_ARRAY = { "方片", "梅花", "红桃", "黑桃" };
public static final String[] CARD_NUMBER_ARRAY = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K",
"A" };
public Card(String suit, String number) {
this.suit = suit;
this.number = number;
}
@Override
public String toString() {
return suit + number;
}
/**
* 获取键花色的值
*/
public int getSuitValue() {
Map<String, Integer> suitMap = new HashMap<String, Integer>();
for (int i = 0; i < CARD_SUIT_ARRAY.length; i++) {
suitMap.put(CARD_SUIT_ARRAY[i], i);
}
return suitMap.get(this.suit);
}
/**
* 获取点数的值
*/
public int getNumberValue() {
Map<String, Integer> numberMap = new HashMap<String, Integer>();
for (int i = 0; i < CARD_NUMBER_ARRAY.length; i++) {
numberMap.put(CARD_NUMBER_ARRAY[i], i);
}
return numberMap.get(this.number);
}
/**
* 这里判断大小,当点数不同时判断点数大小,点数相同时比较花色
*/
@Override
public int compareTo(Card card) {
if (!card.number.equals(this.number))
return card.getNumberValue() - this.getNumberValue();
else
return card.getSuitValue() - this.getSuitValue();
}
}
/**
*Player.java
*/
package program;
import java.util.ArrayList;
import java.util.List;
public class Player {
private int id;
private String name;
private List<Card> playerCardList;
public Player(int id, String name) {
this.id = id;
this.name = name;
this.playerCardList = new ArrayList<Card>();
}
@Override
public String toString() {
return name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public List<Card> getPlayerCardList() {
return playerCardList;
}
}