简单的扑克牌游戏,难点在于异常处理和比较牌的大小

//玩家(Player)类
package eg2;

import java.util.ArrayList;
import java.util.List;

public class Player {
String id;
String name;
public List<Poker> pokers;
public Player(String id,String name){
this.id=id;
this.name=name;
this.pokers=new ArrayList<Poker>();
}

}
//扑克类
package eg2;

public class Poker {
String color;
String point;
public Poker(){

}
public Poker(String color,String point){
this.color=color;
this.point=point;
}
}
//游戏类
package eg2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class Game {
public List<Poker> pokerCard;
public Game(){
this.pokerCard=new ArrayList<Poker>();
}
//添加扑克到pokerCard
public void AddCard(){
String[] colors=new String[]{“黑桃”,”红桃”,”梅花”,”方块”};
String[] points=new String[]{“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 poker=new Poker(colors[i],points[j]);
pokerCard.add(poker); }
}
}
//遍历输出扑克牌
public void Gameiterator(){
System.out.print(“为:”);
Iterator<Poker> iter=pokerCard.iterator();
while(iter.hasNext()){
Poker te=iter.next();
System.out.print(te.color+te.point);
}
}
//洗牌
public void shuffle(){
Collections.shuffle(pokerCard);
}

//取出牌库里的第i张牌
public Poker qupai(int i){
Poker them=pokerCard.get(i);
return them;
}
//创建玩家并进行异常处理
public Player Establish(int i){

  class comm{
      public int shuru(int i){
        int x=0;

//采用while循环如果一直抛出异常就一直继续,如果未发现异常通过break跳出循环
while(true){
try{

          Scanner scan=new Scanner(System.in);
          System.out.println("请输入第"+i+"位玩家id:");
         x=scan.nextInt();
         break;
             }
             catch(InputMismatchException e){
                 System.out.println("请输入正整数id");
             }

          }
          return x;
          }
  }
  comm it=new comm();
    String id1=it.shuru(i)+"";
    Scanner scan=new Scanner(System.in);
    System.out.println("请输入第"+i+"位玩家姓名:");
    String name1=scan.next();
    Player play=new Player(id1,name1);
    System.out.println("----欢迎玩家:"+play.name);
    return play;

}

//发牌
public void fapai(Player play,int j){
//创建一个内部类,用于存放取牌方法
class qu{
//取出牌库里第i张牌
public Poker qupai(int i){
Poker them=pokerCard.get(i);
return them;
}
}
qu it=new qu();
play.pokers.add(it.qupai(j));
System.out.println(“玩家”+play.name+”拿牌”);
}
public static void main(String[] args) {
Game it=new Game();
System.out.println(“————–创建扑克牌—————“);
it.AddCard();
System.out.println(“————–扑克牌成功创建————-“);
it.Gameiterator();
System.out.println(“————–开始洗牌—————–“);
it.shuffle();
System.out.println(“————–洗牌结束—————–“);
System.out.println(“————–创建玩家—————–“);
Player play1=it.Establish(1);
Player play2=it.Establish(2);
System.out.println(“————–开始发牌—————–“);
it.fapai(play1,0);
it.fapai(play2, 1);
it.fapai(play1, 2);
it.fapai(play2, 3);
System.out.println(“————–发牌结束—————–“);
System.out.println(“————–开始游戏—————–“);
Poker p11=play1.pokers .get(0);
Poker p12=play1.pokers.get(1);
Poker p1d=it.CompareSize(p11, p12);//比较玩家1的两张牌大小,大的牌给p1d
System.out.println(“玩家”+play1.name+”最大手牌为:”+p1d.color+p1d.point);
Poker p21=play2.pokers .get(0);
Poker p22=play2.pokers.get(1);
Poker p2d=it.CompareSize(p21, p22);//比较玩家2的两张牌大小 ,大的牌给p2d
System.out.println(“玩家”+play2.name+”最大手牌为:”+p2d.color+p2d.point);
Poker pd=it.CompareSize(p1d, p2d);
if(pd.equals(p11)pd.equals(p12)){
System.out.println(“————–玩家”+play1.name+”获胜———–“);
}else{
System.out.println(“————–玩家”+play2.name+”获胜———–“);
}

    System.out.println("玩家"+play1.name+"的手牌:"+p11.color+p11.point+","+p12.color+p12.point);
    System.out.println("玩家"+play2.name+"的手牌:"+p21.color+p21.point+","+p22.color+p22.point);
}
//CompareSize方法用来比较p1和p2两张牌的大小
public Poker  CompareSize(Poker p1,Poker p2){
//创建一个方法内部类,用来传递牌的大小
class com{
    public com(){

    }
    //比较牌的点数
    public int comparePoints(Poker p){

        int x=0;
        String[] points=new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
          for(int i=0;i<points.length;i++){
                if(p.point.equals(points[i])){
                    x=i;
                    }

             }
          return x;

    }
    //比较牌的花色
     public int compareColor(Poker p){
        int y=0;
         String[] colors=new String[]{"方块","梅花","红桃","黑桃"};
         for(int i=0;i<colors.length;i++){
                if(p.color.equals(colors[i])){
                    y=i;
                    }
         }
         return y;
    }
}
//创建方法内部了对象,用来调用方法内部类的方法
com it=new com();
if(it.comparePoints(p1)>it.comparePoints(p2)){//比较两张牌的点数
    return p1;
}else{
    if(it.comparePoints(p1)<it.comparePoints(p2)){
    return p2;
    }
    else{
        if(it.compareColor(p1)>it.compareColor(p2)){//比较两张牌的花色
            return p1;
        }
        else{
            if(it.compareColor(p1)<it.compareColor(p2)){
                return p2;
            }
            else{
                System.out.print("两张牌一样大");
                return p1;
            }
        }
    }
}

}

}

点赞