public class Player implements Comparable<Player>{
int id;
String name;
List<Card> cardList;
Integer maxCard;
public Player(int id, String name){
this.id = id;
this.name = name;
this.cardList = new ArrayList<Card>();
}
public int compareTo(Player o) {
return this.maxCard.compareTo(o.maxCard);
}
}
public class Card implements Comparable<Card>{
Integer id;
String value;
public Card(Integer id, String value) {
this.id = id;
this.value = value;
}
public int compareTo(Card o) {
return this.id.compareTo(o.id);
}
}
public class PlayCards {
Scanner console;
List<Card> cardlist;
Map<Integer, Player> playermap;
List<Card> shufflelist;
public PlayCards(){
console = new Scanner(System.in);
cardlist = new ArrayList<Card>();
playermap = new HashMap<Integer, Player>();
shufflelist = new ArrayList<Card>();
}
//创建扑克牌的方法
public void creatCard() {
System.out.println("------创建扑克牌------");
String[] head = {"方片","梅花","红桃","黑桃"};
String[] number = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
int id = 0;
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
String tempstr = head[j] + number[i];
Card card = new Card(id++, tempstr);
cardlist.add(card);
}
}
System.out.println("------扑克牌创建成功!------");
System.out.println("*********************************************");
for (int i = 0; i < 52; i++) {
if(i % 13 == 0) {
System.out.println();
}
System.out.print(cardlist.get(i).value);
}
System.out.println("
**********************************************");
}
//进行洗牌的方法
public void shuffle() {
Random random = new Random();
System.out.println("------开始洗牌------");
for (int i = 52; i > 0; i--) {
int tempNum = random.nextInt(i);
Card tempcard = cardlist.get(tempNum);
cardlist.remove(tempNum);
shufflelist.add(tempcard);
}
System.out.println("------洗牌结束!------");
}
//创建玩家并录入相关id与姓名的方法
public void creatPlayer() {
System.out.println("------创建玩家------");
int i = 1;
while(i < 3) {
try {
System.out.println("请输入第" + i + "位玩家的ID和姓名");
System.out.println("请输入整数ID:");
Scanner input = new Scanner(System.in);
Integer integer = input.nextInt();
Player player = playermap.get(integer);
if(player == null) {
System.out.println("请输入姓名:");
String name = console.next();
Player newPlayer = new Player(integer, name);
playermap.put(integer, newPlayer);
i++;
}else {
System.out.println("玩家ID被已占用,请重新输入!");
continue;
}
}catch(InputMismatchException e) {
System.out.println("玩家ID输入有误,请输入规定的整数");
continue;
}
}
Set<Entry<Integer, Player>> entry = playermap.entrySet();
for (Entry<Integer, Player> ey: entry) {
System.out.println("----欢迎玩家:" + ey.getValue().name);
}
}
//进行发牌给玩家的方法
public void sendCardToPyer() {
System.out.println("规定两位玩家的手牌数小于27张");
try {
System.out.println("请输入每位玩家的手牌数:");
Scanner input = new Scanner(System.in);
int cardNum = input.nextInt();
if (cardNum > 26 || cardNum < 1) {
sendCardToPyer();
}else {
System.out.println("------开始发牌------");
int k = 0;
Set<Entry<Integer, Player>> entry = playermap.entrySet();
for (int i = 0; i < cardNum; i++) {
for (Entry<Integer, Player> ey: entry) {
ey.getValue().cardList.add(shufflelist.get(k++));
System.out.println("玩家:" + ey.getValue().name + "-拿牌");
}
}
System.out.println("------发牌结束------");
}
}catch(InputMismatchException e) {
sendCardToPyer();
}
}
//两位玩家取出最大的手牌比较并决定胜负以及亮手牌的方法
public void whoIsWinner() {
Set<Entry<Integer, Player>> entry = playermap.entrySet();
List<Player> playerlist = new ArrayList<Player>();
for (Entry<Integer, Player> ey: entry) {
Collections.sort(ey.getValue().cardList);
int listsize = ey.getValue().cardList.size();
ey.getValue().maxCard = ey.getValue().cardList.get(listsize -1).id ;
playerlist.add(ey.getValue());
System.out.println("玩家:" + ey.getValue().name + "最大的手牌为:"
+ ey.getValue().cardList.get(listsize - 1).value);
}
Collections.sort(playerlist);
System.out.println("------玩家:" + playerlist.get(playerlist.size() - 1).name + "获胜!------");
System.out.println("玩家各自的手牌为:");
for (Player player: playerlist){
System.out.print(player.name + ": ");
for (Card card: player.cardList) {
System.out.print(card.value + " ");
}
System.out.println();
}
}
}
public class Initial {
public static void main(String[] args) {
Scanner input = null;
int i = 0;
while(true) {
System.out.println("按 任意数字 开始游戏");
try {
input = new Scanner(System.in);
i = input.nextInt();
break;
}catch(InputMismatchException e) {
System.out.println("请按要求输入!!!");
}
}
PlayCards playcards = new PlayCards();
playcards.creatCard();
playcards.shuffle();
playcards.creatPlayer();
playcards.sendCardToPyer();
playcards.whoIsWinner();
System.out.println("欢迎下次游戏!!!");
System.exit(0);
if (input != null) {
input.close();
input = null;
}
}
}