package com_poker_game;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.smartcardio.Card;
public class Cards implements Comparable<Cards>{
public String type;
public String num;
List<String> huaSe=Arrays.asList("黑桃","方片","草花","红桃");
List<String> daXiao=Arrays.asList("2","3","4","5","6","7","8","9",
"10","J","Q","K","A");
public Cards(String type,String num){
this.type= type;
this.num=num;
}
/**
* @param args
*/
public Cards(){
}
@Override
public int compareTo(Cards o) {
// TODO Auto-generated method stub
if (daXiao.indexOf(this.num) == daXiao.indexOf(o.num))
return Integer.valueOf(huaSe.indexOf(this.type)).compareTo
(Integer.valueOf(huaSe.indexOf(o.type)));
else
return Integer.valueOf(daXiao.indexOf(this.num)).compareTo
(Integer.valueOf(daXiao.indexOf(o.num)));
}
}
package com_poker_game;
import java.util.*;
public class player {
public int id;
public String name;
List<Cards> playerOneCards=new ArrayList<Cards>();
List<Cards> playerSecondCards=new ArrayList<Cards>();
public player(int id,String name){
this.id=id;
this.name=name;
}
public player() {
// TODO Auto-generated constructor stub
}
}
package com_poker_game;
import java.util.*;
public class playingCcards {
player player=new player();
List<Cards> cardsList=new ArrayList<Cards>();
List<player> playerList=new ArrayList<player>();
String[] huaSe={"黑桃","方片","草花","红桃"};
String[] daXiao={"2","3","4","5","6","7","8","9",
"10","J","Q","K","A"};
// public void createCards(){
// for(int i=0;i<huaSe.length;i++){
// for(int j=0;i<daXiao.length;j++){
// cardsList.add(new Cards(huaSe[i],daXiao[j]));
// //cardsSet输出所有扑克牌
// System.out.print(huaSe[i]+daXiao[j]+" ");
// }
//
// }
//
// }
//建立扑克
public void createCards() {
System.out.println("----------------创建扑克牌----------------");
for(int i = 0;i < huaSe.length;i++){
for(int j = 0;j < daXiao.length;j++){
cardsList.add(new Cards(huaSe[i],daXiao[j]));
System.out.print(huaSe[i]+daXiao[j]+" ");
}
}
System.out.println("\n"+"-------------扑克牌创建成功!--------------");
/***
* 另外一种输出方法 就不用了
*/
// for (Cards card : cardsList) {
// System.out.print(card.getType() + card.getNum() + ",");
// }
System.out.println("总共有" + cardsList.size() + "张牌。");
//------------------------------洗牌
Collections.shuffle(cardsList);
System.out.println("--------------洗牌完成--------------");
}
//建立玩家
public void creatPlayer(){
int playerID=0;
String playerName;
Scanner sc=new Scanner(System.in);
for(int k=1;k<=2;k++){
System.out.println("请输入第"+k+"位玩家ID:");
//while(true) 想要在catch后面继续写代码 while(true)才好使
while(true){
try{
sc=new Scanner(System.in);
playerID=sc.nextInt();
break;
}catch(Exception e){
System.out.println("必须为整数");
continue;
}
}
System.out.println("请输入第"+k+"位玩家姓名:");
playerName=sc.next();
playerList.add(new player(playerID,playerName));
}
//遍历playerlist查看此时系统中有哪些玩家
for(int p=0;p<playerList.size();p++){
System.out.println("目前存在的玩家有:"+playerList.get(p).id+
" "+playerList.get(p).name);
}
}
//发牌
public void sendPokers(){
int n=0;
for(int u=0;u<4;u++){
//哈哈很喜欢这个方法 u==0+(2*n),再多人也不怕!!!!!
if(u==0+(2*n)){
player.playerOneCards.add(cardsList.get(u));
}
if(u==1+(2*n)){
player.playerSecondCards.add(cardsList.get(u));
//n++的位置很重要的啊!!!!!!!!!!!!!!!!
n++;
}
}
// for(int p=0;p<player.playerCards.size();p++){
// System.out.println("目前已经发出的牌有:"+player.playerCards.get(p));
// }
for(Cards pcr1:player.playerOneCards){
System.out.println("玩家"+playerList.get(0).name+"的牌有:"
+pcr1.type+pcr1.num);
}
for(Cards pcr2:player.playerSecondCards){
System.out.println("玩家"+playerList.get(1).name+"的牌有:"
+pcr2.type+pcr2.num);
}
}
//比较两人手牌大小
public void compare(){
List<Cards> everyoneMaxPokeList=new ArrayList<Cards>();
Collections.sort(player.playerOneCards);
Collections.sort(player.playerSecondCards);
//crd.compareTo(per1.playerOneCards,per1.playerSecondCards);
//获取手牌list的size
int size=player.playerOneCards.size();
//System.out.println("lalal"+size);
//获得每个人最大的手牌
System.out.println("玩家"+playerList.get(0).name+"的最大牌:"+player.playerOneCards.get(size-1).type+
player.playerOneCards.get(size-1).num);
System.out.println("玩家"+playerList.get(1).name+"的最大牌:"+player.playerSecondCards.get(size-1).type+
player.playerSecondCards.get(size-1).num);
//把每个人最大手牌放入everyoneMaxPokeList中再做比较
everyoneMaxPokeList.add(player.playerOneCards.get(size-1));
everyoneMaxPokeList.add(player.playerSecondCards.get(size-1));
Collections.sort(everyoneMaxPokeList);
int sze=everyoneMaxPokeList.size();
//看看everyoneMaxPokeList中最大的数据到底属于谁!
//下面这种写法equal方法有问题 待研究
// if(everyoneMaxPokeList.get(sze-1).equals(player.playerOneCards)){
// System.out.println("Winner is:"+playerList.get(0).name);
// }else{
// System.out.println("Winner is:"+playerList.get(1).name);
// }
if(player.playerOneCards.contains(everyoneMaxPokeList.get(sze-1))){
System.out.println("Winner is:"+playerList.get(0).name);
}else{
System.out.println("Winner is:"+playerList.get(1).name);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
playingCcards pc=new playingCcards();
pc.createCards();
pc.creatPlayer();
pc.sendPokers();
pc.compare();
}
}