为了账号安全,请及时绑定邮箱和手机立即绑定

JAVA简单扑克牌小游戏

标签:
Java

简单的扑克牌小游戏,主要实现,创建扑克牌库,洗牌,发牌,对玩家手牌进行排序,并比较玩家手牌大小。
创建Card类

public class Card implements Comparable<Card>{
    private String huase;
    private String point;
    public static String points[]={"2","3","4","5","6","7","8","9","10","j","Q","K","A"};
    public static String huases[]={"方片","梅花","红桃","黑桃"};
    public Card(String _huase,String _point){
        this.huase = _huase;
        this.point = _point;
    }
    public String getHuase() {
        return huase;
    }
    public void setHuase(String huase) {
        this.huase = huase;
    }
    public String getPoint() {
        return point;
    }
    public void setPoint(String point) {
        this.point = point;
    }
    @Override
    public int compareTo(Card o) {
        // TODO Auto-generated method stub
        int a=0  ,b = 0, c=0 ;
        for(int i = 0;i<points.length;i++){
            if(this.point.equals(points[i])){
                 a = i;
                break;
            }
        }
        for(int i = 0;i<points.length;i++){
            if(o.point.equals(points[i])){
                 b =i;
                break;
            }
        }
        c = a-b;
        if(c!=0){
            return c;
        }else{
            int d=0,e=0,f=0;
            for (int i =0;i<huases.length;i++){
                if (this.getHuase().equals(huases[i])){
                    d = i;
                    break;
                }
            }
            for (int i =0;i<huases.length;i++){
                if (o.getHuase().equals(huases[i])){
                    e = i;
                    break;
                }

           }
            return d-e;

        }
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Card))
            return false;
        Card other = (Card) obj;
        if (huase == null) {
            if (other.huase != null)
                return false;
        } else if (!huase.equals(other.huase))
            return false;
        if (point == null) {
            if (other.point != null)
                return false;
        } else if (!point.equals(other.point))
            return false;
        return true;
    }

}

创建玩家People类

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class People {
    private int ID;
    private String name;
    private List<Card> HandCard;
    public People(int _ID , String _name){
        this.ID = _ID;
        this.name = _name;
        HandCard = new ArrayList<Card>();
    }
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Card> getHandCard() {
        return HandCard;
    }
    public void setHandCard(List<Card> handCard) {
        HandCard = handCard;
    }

}

异常类

public class WrongCommand extends Exception {
    public WrongCommand(){

    }
    public WrongCommand(String message){
        super(message);
    }

}

扑克牌创建与洗牌

public class SetCard {
    public static String huase[]={"方片","梅花","红桃","黑桃"};
    public static String point[]={"2","3","4","5","6","7","8","9","10","j","Q","K","A"};
    List<Card> CardList = new ArrayList();
    public  void buildCard(){
        for(int i=0;i<huase.length;i++){
            for(int j = 0;j<point.length;j++){
                Card card = new Card(huase[i],point[j]);
                CardList.add(card);
            }
        }

    }
    public void randomCard(){
        for (int i=0;i<1000;i++){
            Random random = new Random();
            int a = random.nextInt(CardList.size());
            int b = random.nextInt(CardList.size());
            CardList.get(a);
            Card temp = new Card(CardList.get(a).getHuase(),CardList.get(a).getPoint());
            CardList.get(a).setHuase(CardList.get(b).getHuase());
            CardList.get(a).setPoint(CardList.get(b).getPoint());
            CardList.get(b).setHuase(temp.getHuase());
            CardList.get(b).setPoint(temp.getPoint());
        }
    }

}

主函数

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

public class CardGame {

    Scanner scan = new Scanner(System.in);
    public People creatPlayer() throws WrongCommand{
        try{
            System.out.println("请输入玩家姓名");
             String name = scan.next();
            System.out.println("请输入玩家ID");
             int ID = scan.nextInt();
             People player= new People(ID,name);
             return player;
        }catch(Exception e){
            throw new WrongCommand("请输入正确的ID类型");
        }

    }
    public void Game(SetCard e,People p1,People p2 ){
//游戏内容主体
        e.buildCard();
        System.out.println("扑克牌已生成");
        for(Card card:e.CardList){
            System.out.println(card.getHuase()+card.getPoint());
        }
        e.randomCard();
        System.out.println("扑克牌已洗牌");
        for(Card card:e.CardList){
            System.out.println(card.getHuase()+card.getPoint());
        }
        for(int i=0;i<e.CardList.size();i=i+4){
            for (int j = 0;j<2;j++){
                Card card = new Card(e.CardList.get(i+j).getHuase(),e.CardList.get(i+j).getPoint());
                p1.getHandCard().add(card);
            }
            for (int j = 2;j<4;j++){
                Card card = new Card(e.CardList.get(i+j).getHuase(),e.CardList.get(i+j).getPoint());
                p2.getHandCard().add(card);
            }
            Collections.sort(p1.getHandCard());
            Collections.sort(p2.getHandCard());
            Card card1 = new Card(p1.getHandCard().get(p1.getHandCard().size()-1).getHuase(),p1.getHandCard().get(p1.getHandCard().size()-1).getPoint());
            Card card2 = new Card(p2.getHandCard().get(p2.getHandCard().size()-1).getHuase(),p2.getHandCard().get(p2.getHandCard().size()-1).getPoint());
            List<Card> GameList  = new ArrayList<Card>();
            GameList.add(card1);
            GameList.add(card2);
            Collections.sort(GameList);
            if (p1.getHandCard().contains(GameList.get(1))){
                System.out.println("本轮"+p1.getName()+"获胜");
            }else if(p2.getHandCard().contains(GameList.get(1))){
                System.out.println("本轮"+p2.getName()+"获胜");
            }
            System.out.println(p1.getName()+"手牌:");
            for(Card card:p1.getHandCard()){
                System.out.println(card.getHuase()+card.getPoint());
            }
            System.out.println(p2.getName()+"手牌");
            for(Card card:p2.getHandCard()){
                System.out.println(card.getHuase()+card.getPoint());
            }
            p1.getHandCard().clear();
            p2.getHandCard().clear();
            System.out.println("请输入是否进继续进行游戏,请输入yes或者no");
            String choose = scan.next();
            if(scan.equals("no")){break;}
        }
        System.out.println("游戏结束");
    }

    public static void main(String[] args) {
        CardGame CG = new CardGame();
        People[] player=new People[2];
        for (int i =0;i<2;i++){
            boolean flag = true;
            while(flag){
                try{
                player[i] = CG.creatPlayer();

                }catch(WrongCommand e){
                    System.out.println(e.getMessage());
                    continue;
                }
                if (player[i].getName()!=null){
                    flag = false;
                }

            }
        }
        SetCard SC = new SetCard();
        CG.Game(SC, player[0], player[1]);
    }

}

功能基本实现

点击查看更多内容
11人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消