package com.imooc;
import java.util.Comparator;
public class Card implements Comparator<Card> {
public String suit;//花色
public String point;//点数
public Card(){
}
public Card(String suit,String point){
this.suit = suit;
this.point = point;
}
@Override
public String toString() {
return suit + point;
}
@Override
public int compare(Card o1, Card o2) {
String suit = "方片 梅花 红桃 黑桃";
String point = "2 3 4 5 6 7 8 9 10 J Q K A";
int result = point.indexOf(o1.point) - point.indexOf(o2.point);
if(result < 0){
return -1;
}else if(result > 0){
return 1;
}else{
int result2 = suit.indexOf(o1.suit) - suit.indexOf(o2.suit);
if(result2 < 0 ){
return -1;
}else if(result2 > 0){
return 1;
}else{
return 0;
}
}
}
}
package com.imooc;
import java.util.ArrayList;
import java.util.List;
/**
- 玩家
- @author Administrator
-
*/
public class Player {public int id;//玩家id
public String name;//玩家姓名
public List<Card> handCards;//玩家手牌public Player(int id,String name){
this.id = id;
this.name = name;
handCards = new ArrayList<Card>();
}
}
package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
/**
- 扑克牌游戏测试类
- @author Administrator
-
*/
public class CardsTest {public static void main(String[] args) {
System.out.println("**创建扑克牌****");
//创建存放扑克牌的list集合
List<Card> cardsList = new ArrayList<Card>();
//创建扑克牌的花色数组
String[] suits = {"方片","梅花","红桃","黑桃"};
//创建扑克牌的点数数组
String[] points = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i = 0;i < suits.length;i++){
for(int j = 0;j < points.length;j++){
Card card = new Card(suits[i],points[j]);
cardsList.add(card);
}
}
System.out.println("**扑克牌创建成功****");
System.out.println("**创建的扑克牌为:" + "\n" + cardsList);System.out.println("******************洗牌开始*************************"); Collections.shuffle(cardsList);//集合中的混排 System.out.println("******************洗牌结束*************************"); System.out.println("******************创建玩家*************************"); //创建map用于存放玩家的id和name Map<Integer,String> map = new HashMap<Integer, String>(); //创建list集合用于存储玩家对象 List<Player> playerList = new ArrayList<Player>(); for(int i = 0;i < 2;i++){ //创建Scanner对象 Scanner scanner = new Scanner(System.in); //声明变量id Integer id = 0; System.out.println("请输入第"+(i+1)+"位玩家的id和姓名:"); while(true){ try { System.out.println("输入id:"); id = scanner.nextInt(); break; } catch (InputMismatchException e) { System.out.println("请输入整数id:"); scanner = new Scanner(System.in);//重置Scanner对象,不然会死循环 } } System.out.println("输入姓名:"); String name = scanner.next(); map.put(id, name); playerList.add(new Player(id,name)); } //遍历map集合 for(Entry<Integer, String> entry : map.entrySet()){ System.out.println("*******欢迎玩家:" + entry.getValue()); } System.out.println("******************开始发牌********************"); //将第一张牌发给玩家1 playerList.get(0).handCards.add(cardsList.get(0)); System.out.println("*****玩家:" + playerList.get(0).name + "-拿牌"); //将第二张牌发给玩家2 playerList.get(1).handCards.add(cardsList.get(1)); System.out.println("*****玩家:" + playerList.get(1).name + "-拿牌"); //将第三张牌发给玩家1 playerList.get(0).handCards.add(cardsList.get(2)); System.out.println("*****玩家:" + playerList.get(0).name + "-拿牌"); //将第四张牌发给玩家2 playerList.get(1).handCards.add(cardsList.get(3)); System.out.println("*****玩家:" + playerList.get(1).name + "-拿牌"); System.out.println("******************发牌结束*********************"); System.out.println("******************开始游戏*********************"); //先比较玩家1手中的牌 Collections.sort(playerList.get(0).handCards, new Card()); //得到玩家1手中的最大牌 Card card1 = playerList.get(0).handCards.get(1); System.out.println("玩家:" + playerList.get(0).name + "最大的牌为:" + card1); //再比较玩家2手中的牌 Collections.sort(playerList.get(1).handCards, new Card()); //得到玩家2手中的最大牌 Card card2 = playerList.get(1).handCards.get(1); System.out.println("玩家:" + playerList.get(1).name + "最大的牌为:" + card2); //最后比较两个玩家手中的最大牌,分出胜负 List<Card> lastCardList = new ArrayList<Card>(); lastCardList.add(card1); lastCardList.add(card2); Collections.sort(lastCardList, new Card()); if(lastCardList.get(1).suit.equals(card1.suit) && lastCardList.get(1).point.equals(card1.point)){ System.out.println("******************玩家:"+playerList.get(0).name+"获胜!*********************"); }else{ System.out.println("******************玩家:"+playerList.get(1).name+"获胜!*********************"); } //玩家各自的手牌 System.out.println("玩家各自的手牌为:" + "\n" + playerList.get(0).name + ":" + playerList.get(0).handCards + "\n" + playerList.get(1).name + ":" +playerList.get(1).handCards);
}
}