卡牌是这样写的吗
import java.util.ArrayList; import java.util.List; public class Player { private String id; private String name; List<Poker> handCard = new ArrayList<Poker>();//手牌 public Player(){}; public Player(String id,String name){ this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
public class Poker {
/**扑克牌的类
* @param args
*/
private String colour;
private String point;
public Poker(){}
public Poker(String colour, String point){
this.colour = colour;
this.point = point;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
}
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
/**
* 比较规则
* @author wujianpei
*
*/
public class Compare implements Comparator<Poker>{
@Override
public int compare(Poker o1, Poker o2) {
List<String> colours = Arrays.asList("方块","梅花","红桃","黑桃");
List<String> points = Arrays.asList("2","3","4","5","6","7","8","9","10","J","Q","K","A");
if(points.indexOf(o1.getPoint())==points.indexOf(o2.getPoint()))
return((Integer)(colours.indexOf(o1.getColour())))
.compareTo
((Integer)(colours.indexOf(o2.getColour())));
else{
return((Integer)(points.indexOf(o1.getPoint())))
.compareTo
((Integer)(points.indexOf(o2.getPoint())));
}
}
}
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class MainTest { /**两张牌斗大的测试类 * @param args * 先把poker 和 Player的list创建出来 */ List<Poker> poker = new ArrayList<Poker>(); List<Player> player = new ArrayList<Player>(); /** * 1.创建一副扑克 * @param args */ public void buildPoker(){ String colours[] = {"方块","梅花","红桃","黑桃"}; String points[] = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; for (int i = 0 ; i<4 ;i++){ for(int j = 0; j<13;j++){ //创建一张新扑克牌 Poker cards = new Poker(); cards.setColour(colours[i]); cards.setPoint(points[j]); poker.add(cards);//加入到poker的list中 } } System.out.println("------创建扑克牌----------共有"+poker.size()+"张"); } /** * 2.洗牌 * @param args */ public void washCards(){ Collections.shuffle(poker); System.out.println("------洗牌------------"); for (Poker p : poker) { System.out.print(p.getColour()+p.getPoint()); }System.out.println(); } /** * 3.创建2个玩家 * @param args */ public void addPlayers(){ Scanner console = new Scanner(System.in); for(int i = 1; i<3;i++){ Player p = new Player(); System.out.println("-----请输入玩家"+i+"的id------"); p.setId(console.next()); System.out.println("-----请输入玩家"+i+"的姓名------"); p.setName(console.next()); player.add(p); } // for (Player p : player) { // System.out.println("成功创建玩家id:"+p.getId()+", 姓名:"+p.getName()); // } console.close(); } /** * 4.发牌 * @param args */ public void getcards(){ //拿一张牌出来 for(int i = 0; i<2;i++){ for(int j = 0;j<2;j++){ //poker中拿第一张牌 Poker card = poker.get(0); //放入Playerj的list里面 Player play = player.get(j); play.handCard.add(card); //把第一张牌从pokerlist中移除 poker.remove(card); // System.out.println("玩家"+(j+1)+"获得第"+(i+1)+"张卡片"); } } //打印双方的牌,等下看结果清晰 for(int i =0 ;i<2;i++){ for(int j = 0 ; j<2; j++){ System.out.println("玩家"+(i+1)+"的第"+(j+1)+"张牌是"+player.get(i).handCard.get(j).getColour() +player.get(i).handCard.get(j).getPoint()); } } } /** * 5比较大小 * @param args */ public void testCompare(){ //先写一个比较规则类 Compare cp = new Compare(); //比较自己手牌大小 for(int i = 0 ; i<2; i++){ Collections.sort(player.get(i).handCard, cp); System.out.println("玩家"+player.get(i).getName()+"的最大手牌为:" +player.get(i).handCard.get(1).getColour()+player.get(i).handCard.get(1).getPoint()); } //比较双方最大的牌 if( cp.compare(player.get(0).handCard.get(0) , player.get(1).handCard.get(1))>0){ System.out.println("玩家"+player.get(0).getName()+"赢了"); } else{ System.out.println("玩家"+player.get(1).getName()+"赢了"); } } public static void main(String[] args) { MainTest mt = new MainTest(); mt.buildPoker(); mt.washCards(); mt.addPlayers(); mt.getcards(); mt.testCompare(); } }