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

Java简易扑克牌游戏(控制台)

标签:
Java

一、创建扑克牌的Class

public class Pok {

    public String point;
    public String color;

    public Pok(String point,String color){
        this.color=color;
        this.point=point;
    }
}

二、创建玩家类

public class Player {

    public int id;
    public String name;
    List<Pok> handCards =new ArrayList<Pok>();

    public Player(int id,String name){
        this.id =id;
        this.name =name;
    }

三、比较方法,重写compare方法,自定义扑克牌之间比较大小规则
具体是通过比较点数的大小,四个花色也通过数字来比较大小

import java.util.Comparator;

public class Compare implements Comparator<Pok>{

    @Override
    public int compare(Pok o1, Pok o2) {
        String[] color = {"方块","梅花","红桃","黑桃"};
        String[] point=new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        int temp1 =0;
        int temp2 =0;
        for(int i=0;i<point.length;i++){
            if(o1.point.equals(point[i])) temp1 +=i*10;
            if(o2.point.equals(point[i])) temp2 +=i*10;
        }
        for(int i=0;i<color.length;i++){
            if(o1.color.equals(color[i])) temp1 +=i;
            if(o2.color.equals(color[i])) temp2 +=i;
        }
        if(temp1>temp2) return 1;
        if(temp1==temp2)
        return 0;
        else
            return -1;
    }
    //写个主方法测试下
    public static void main(String[] args){
        Compare c= new Compare();
        Pok p1= new Pok("3","梅花");
        Pok p2= new Pok("3","方块");
        int a= c.compare(p1, p2); 
        System.out.println(a);
    }}

四、游戏按步骤(方法)开始:
①:创建牌组
②:洗牌:用Collections.shuffle();打乱序列的排序
③:创建两名玩家(从控制台输入时,注意可能出现的异常)
④:给玩家各发两张牌
⑤:对玩家的手牌进行排序,比较最大值即可判断出获胜者!!!

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

public class GameStart {

    String[] color = {"方块","梅花","红桃","黑桃"};
    String[] point=new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    Scanner input= new Scanner(System.in);
    List<Pok> poklist= new ArrayList<Pok>();
    List<Player> playerlist= new ArrayList<Player>();

    public void pokSet(){
        System.out.println("————————————————组建牌组:");
        for(int i=0;i<color.length;i++){
            for(int j=0;j<point.length;j++){
                poklist.add(new Pok(point[j],color[i]));
            }
        }
        int i=0;
        for (Pok p : poklist) {
            System.out.print(p.color+p.point+" ");
            i++;
            if(i%13==0)
                System.out.println("");
        }
    }

    public void shuffle(){
        System.out.println("——————————————————开始洗牌喽:");
        Collections.shuffle(poklist);
        int i=0;
        for (Pok p : poklist) {
            System.out.print(p.color+p.point+" ");
            i++;
            if(i%13==0)
                System.out.println("");
        }
    }

    public void playerSet(){
        System.out.println("————————————————创建两名玩家:");
        for(int i=0;i<2;i++){
            System.out.println("请输入玩家ID:");
            int id= 0;
            try{
                id= input.nextInt();
            }catch(Exception e){
                System.out.println("输错了,欧尼酱!");
                input= new Scanner(System.in);
                i--;
                continue;
            }
            System.out.println("请输入玩家姓名:");
            String name= input.next();
            playerlist.add(new Player(id,name));

        }input.close(); 
    }
//给俩玩家发牌,即把无序的牌组的前四张 按顺序添加到他们手牌中
    public void deal(){
        playerlist.get(0).handCards.add(poklist.get(0));
        playerlist.get(1).handCards.add(poklist.get(1));
        playerlist.get(0).handCards.add(poklist.get(2));
        playerlist.get(1).handCards.add(poklist.get(3));
        System.out.println("——————————————发牌结束,来看看他俩的手牌吧:");
        for(Player p:playerlist){
            System.out.println(p.name+":");
            for(Pok pk:p.handCards){
                System.out.print(pk.color+pk.point+" ");
            }
            System.out.println("");
        }
    }
//对玩家的手牌排序,然后比较他们手牌中最大的一张即可 
    public void sort(){
        Collections.sort(playerlist.get(0).handCards,new Compare());
        Collections.sort(playerlist.get(1).handCards,new Compare());

        //创建一个max序列,存放俩玩家手牌中最大的一张,然后给max排序,判断其第一张是谁的牌即可
        List<Pok> max= new ArrayList<Pok>();
        max.add(playerlist.get(0).handCards.get(0));
        max.add(playerlist.get(1).handCards.get(0));
        Collections.sort(max,new Compare());
        System.out.println("———————————————对两位玩家手牌进行比较");
        if(max.get(0)==playerlist.get(0).handCards.get(0)){
            System.out.println("则获胜方为:"+playerlist.get(0).name);
        }else{
            System.out.println("则获胜方为:"+playerlist.get(1).name);
        }
    }

    public static void main(String[] args){
        GameStart game= new GameStart();
        game.pokSet();
        game.shuffle();
        game.playerSet();
        game.deal();
        game.sort();
    }}
点击查看更多内容
TA 点赞

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

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
5
获赞与收藏
1

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消