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

简易扑克牌游戏Java代码

标签:
Java

玩家类

package imooc_easy_poker_game;

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

public class Player {

    //定义玩家的包含的属性(ID和name)
    public int id;
    public String name;
    public List<Poker> pokers;

    public Player(){

    }

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

    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Player other = (Player) obj;
        if (id != other.id)
            return false;
        return true;
    }

}

扑克类

package imooc_easy_poker_game;

public class Poker{
    //定义扑克牌的属性(花色和点数)
    public String colour;
    public String point;

    public Poker(){

    }

    public Poker(String colour,String point){
        this.colour = colour;
        this.point = point;     
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((colour == null) ? 0 : colour.hashCode());
        result = prime * result + ((point == null) ? 0 : point.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Poker other = (Poker) obj;
        if (colour == null) {
            if (other.colour != null)
                return false;
        } else if (!colour.equals(other.colour))
            return false;
        if (point == null) {
            if (other.point != null)
                return false;
        } else if (!point.equals(other.point))
            return false;
        return true;
    }

}

扑克比较类

package imooc_easy_poker_game;

import java.util.Comparator;

public class pokerComparator implements Comparator<Poker> {

    @Override
    public int compare(Poker poker0, Poker poker1) {
        // 创建花色和点数的数组
        //存储花色的数组,从小到大
        String[] colours = {"方片","梅花","红桃","黑桃"};
        //存储点数的数组
        String[] points = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

        //比较索引位置,判断大小

        //得到花色的索引
        int c0=0;
        int c1=0;
        for(int i=0;i<colours.length;i++){
            if(poker0.colour.equals(colours[i])){
                c0=i;
            }
            if(poker1.colour.equals(colours[i])){
                c1=i;
            }           
        }

        //得到点数的索引
        int p0=0;
        int p1=0;
        for(int j=0;j<points.length;j++){
            if(poker0.point.equals(points[j])){
                p0=j;
            }
            if(poker1.point.equals(points[j])){
                p1=j;
            }           
        }

        //判断面值大小
        if(p0>p1){//先判断点数
            return 1;       
        }else if (p0==p1) {//点数相等,判断花色
            if(c0>c1){
                return 1;
            }else if (c0==c1) {//花色相等
                return 0;
            }else{
                return -1;
            }
        }else{
            return -1;
        }

    }

}

主类

package imooc_easy_poker_game;

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

public class PlayPoker {

    //存放扑克的List
    public List<Poker> pokerList;
    //存放玩家
    public List<Player> players;
    //存放所有的输入
    private Scanner console;
    //构造器中初始化pokerList
    public PlayPoker(){
        this.pokerList =  new ArrayList<Poker>();
        this.players = new ArrayList<Player>();
        console = new Scanner(System.in);
    }

    /**
     * 创建一副扑克牌,不包含大小王
     */
    public void createPoker(){
        //存储花色的数组
        String[] colours = {"黑桃","红桃","梅花","方片"};
        //存储点数的数组
        String[] points = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        //遍历花色和点数,创建扑克牌
        for(String colour:colours){
            for(String point:points){
                Poker pokerAdd = new Poker(colour,point);
                pokerList.add(pokerAdd);
            }           
        }
        System.out.println("成功创建扑克!");
        //打印扑克
        for(Poker poker:pokerList){
            System.out.print("["+poker.colour + ":"+ poker.point+"]");              
        }
        System.out.println();
    }

    /**
     * 随机洗牌
     * @param args
     */
    public void shuffle(){
        System.out.println("开始洗牌!");
        Collections.shuffle(pokerList);
        System.out.println("洗牌完成!洗牌后的牌为:");
        for(Poker poker:pokerList){
            System.out.print("["+poker.colour + ":"+ poker.point+"]");              
        }
        System.out.println();
    }

    /**
     * 创建玩家,count表示玩家个数
     */
    public void createPlayer(int count){
        int m= 0;
        while(m<count){
            System.out.println("输入玩家ID:");
            int inputID = 0;
            Player newPalyer = new Player();
            //判断输入的ID是否为整数,非整数,捕获异常
            while(true){
                try{
                    inputID = console.nextInt();
                    newPalyer.id = inputID;
                    break;
                }catch(Exception e){
                    System.out.println("玩家ID需为整数,请重新输入:");
                    console = new Scanner(System.in);
                }
            }
            /**
             * 判断ID是否被占用
             */
            if(players.contains(newPalyer)){
                System.out.println("输入的玩家ID已经存在,请重新输入!");
                continue;
            }else{
                System.out.println("输入玩家姓名:");
                String inputName = console.next();
                newPalyer.name = inputName;
                players.add(newPalyer);
                System.out.println("成功创建玩家:"+inputName);
                m++;
            }
        }
    }

    /**
     * 顺序发牌,count表示玩家数量,count2表述发牌轮数
     */
    public void play(int count,int count2){ 
        //循环发牌轮数
        for(int j=0;j<count2;j++){
            //循环玩家
            for(int i=0;i<count;i++){
                System.out.println("玩家"+ players.get(i).name +"第"+(j+1)+"轮拿牌为:");
                Poker fPoker =  pokerList.get(i+count*j);
                players.get(i).pokers.add(fPoker);
                System.out.println(fPoker.colour+fPoker.point);
            }           
        }
        System.out.println("发牌完成!");

        //排序玩家手牌
        for(int i=0;i<count;i++){
            Collections.sort(players.get(i).pokers,new pokerComparator());
            //输出玩家最大手牌
            System.out.println("玩家"+players.get(i).name+"的最大手牌为:"+players.get(i).pokers.get(count2-1).colour + players.get(i).pokers.get(count2-1).point);      
        }

        //比较玩家最大手牌的大小
        Player maxPlayer = new Player();
        pokerComparator pc = new pokerComparator();
        //先比较第一和第二个玩家的最大手牌,得出其中较大的一个。然后用较大的逐一比较后面玩家的最大手牌,得出最后获胜玩家
        int result = pc.compare(players.get(0).pokers.get(count2-1), players.get(1).pokers.get(count2-1));
        if(result==1){
            maxPlayer = players.get(0);         
        }else {
            maxPlayer = players.get(1); 
        }

        for(int i=2;i<count;i++){
            int result2  = pc.compare(maxPlayer.pokers.get(count2-1),players.get(i).pokers.get(count2-1));
            System.out.println("result2:"+result2);
            if(result2<0){
                maxPlayer = players.get(i);                             
            }
        System.out.println("玩家:"+maxPlayer.name +"最终获胜!");              

        //输出玩家所有的牌
        for(int i=0;i<count;i++){
            System.out.println("玩家"+players.get(i).name+"的所有手牌为:");
            for(int j=0;j<count2;j++){
                System.out.println("["+players.get(i).pokers.get(j).colour+":"+players.get(i).pokers.get(j).point+"]");
            }
        }       
    }

    public static void main(String[] args) {
        PlayPoker pp = new PlayPoker();
            pp.createPoker();
            pp.shuffle();
            pp.createPlayer(2);
            pp.play(2, 2);
    }
}
点击查看更多内容
6人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消