继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

JAVA入门第三季综合实战(简易扑克游戏)目前功能最完善的作业

有时做个宅男也好
关注TA
已关注
手记 1
粉丝 1
获赞 36

耗时两天,自测没有bug,小伙伴么发现问题可以直接留言,其中实现了int类型输入错误无限输入,重复id再次输入,全局变量,很多都是以后能够用到的精简算法,如有更好地方法也可随时分享给我啦,部分功能有两种方法实现,遂留下了注释,大家共同学习共同进步【截图中多了一句代码,程序已删去,图就不重新给了】
图片描述
图片描述
扑克牌类

package poker_game;
/**
 * @author zl
 * @version 2017年2月7日 下午4:10:49
 */
public class Poker {
    public String design;
    public String num;

    @Override
    public String toString() {
        return  design + num ;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((design == null) ? 0 : design.hashCode());
        result = prime * result + ((num == null) ? 0 : num.hashCode());
        return result;
    }

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

    public Poker(String design,String num){
        this.design = design;
        this.num = num;
    }
}

玩家类

package poker_game;

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

/**
 * @author zl
 * @version 2017年2月7日 下午4:09:47
 */
public class Player {
    public Integer id;
    public String name;
    public List<Poker> Poker = new ArrayList<Poker>();

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

手牌比较类

package poker_game;

import java.util.Comparator;

/**
 * @author zl
 * @version 2017年2月8日 下午9:14:22
 */
public class PokerComparator implements Comparator<Poker>{

    @Override
    public int compare(Poker o1, Poker o2) {
        // TODO Auto-generated method stub
        String a = "";//由小到大创建字符串花色和大小
        String b = "2345678910JQKA";

        int a1 = a.indexOf(o1.design);//分别查找,花色和大小在顺序字符串中的位置,位置大的即大
        int b1 = b.indexOf(o1.num);
        int a2 = a.indexOf(o2.design);
        int b2 = b.indexOf(o2.num);

        if(b1 > b2){//通过索引位置判断俩牌大小
            return 1;
        }else if(b1 == b2){
            if(a1 > a2){
                return 1;
            }else{
                return -1;
            }
        }else{
            return -1;
        }   
    }
    /**
     * 根据compare方法,判断出手牌的大小,返回大的牌
     * @param o1
     * @param o2
     * @param c
     * @return
     */
    public Poker maxPoker(Poker o1,Poker o2,int c){
        if(c>0){
            return o1;
        }else{
            return o2;
        }
    };

}

主函数类

package poker_game;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * @author zl
 * @version 2017年2月7日 下午4:49:45
 */
public class Game {

    public List<Poker> pokerList;
    private Scanner console;
    public Map<Integer, Player> playerMap;
    //public Map<Integer, Player> playerMap2;
    public static  int a;//定义全局变量
    public static  int b;

    public Game(){
        this.pokerList = new ArrayList<Poker>();
        console = new Scanner(System.in);
        this.playerMap = new HashMap<Integer,Player>();
        //this.playerMap2 = new HashMap<Integer,Player>();
    }

    /**
     * 创建扑克牌
     */
    public void addCard(){
        String [] design = {"","","",""};
        String [] num = {"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 ca = new Poker(design[i],num[j]);
                pokerList.add(ca);

            }
        }

            System.out.println("----------创建扑克牌-------------"); 
            System.out.println("----------扑克牌创建成功!----------"); 
            for (Poker poker : pokerList) { 
                System.out.print("[" + poker.design + poker.num + "]"); 
            } 
            System.out.println();

    }
    /**
     * 洗牌
     */
    public void mixtureCard(){
        System.out.println("----------开始洗牌-------------"); 
        Collections.shuffle(pokerList);// 集合中的乱序方法 
        for (Poker poker : pokerList) { 
            System.out.print("[" + poker.design + poker.num + "]"); 
            } 
        System.out.println();
        System.out.println("----------洗牌结束!------------"); 
    }
    /**
     * 创建两位玩家玩家
     */
    public void addPlayer(){
        System.out.println("----------创建玩家-------------");
        for(int i=0;i<2;){
        System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名:");
        System.out.println("请输入玩家ID");
        int id = 0;//初始化id
        try{
            id = scanInt();

        }catch(Exception e){
            System.out.println(e.getMessage());
            continue;
        }

        Player p = playerMap.get(id);
        if(p == null){
            System.out.println("请输入玩家姓名");
            String name = console.next();
//          playerMap2.put(id, new Player(id,name));//用于鉴定两次输入的id是否相同,因现在不能够保存两次输入的id值,所以这样
//          playerMap.put(i+1, new Player(id,name));//用于之后的输出,key为1,2
            playerMap.put(id, new Player(id,name));
            //存储两次输入的ID,一边后续的Map中用key查找value
            switch(i){
            case 0:
                a = id;
            case 1:
                b = id;
            }
            i++;
            System.out.println("添加成功!");
        }else{
            System.out.println("该ID已被占用!请再次添加");
            continue;//跳出当前,直接回到循环开始
        }
        }
    }

    /**
     * 防止输入 非整数型的ID
     * @throws Exception 
     */
    public int scanInt() throws Exception{
        try{
            int in = console.nextInt();
            return in;
        }catch(Exception e){
            console = new Scanner(System.in);
            throw new Exception("输入异常,请输入整数类型的ID"); 
        }   
    }
    /**
     * 发牌,开始游戏
     */
    public void beginGame(){
        System.out.println("欢迎玩家"+playerMap.get((int)a).name+","+playerMap.get((int)b).name);
        System.out.println("----------开始发牌-------------"); 
        System.out.println(playerMap.get(a).name+"得到第一张牌");
        playerMap.get(a).Poker.add(pokerList.get(0));
        System.out.println(playerMap.get(b).name+"得到第一张牌");
        playerMap.get(b).Poker.add(pokerList.get(1));
        System.out.println(playerMap.get(a).name+"得到第二张牌");
        playerMap.get(a).Poker.add(pokerList.get(2));
        System.out.println(playerMap.get(b).name+"得到第二张牌");
        playerMap.get(b).Poker.add(pokerList.get(3));
        System.out.println("----------发牌结束!------------"); 
        System.out.println("----------开始游戏-------------"); 
        System.out.println("玩家各自手牌为");
        Poker a1 = playerMap.get(a).Poker.get(0);
        Poker a2 = playerMap.get(a).Poker.get(1);
        System.out.println(playerMap.get(a).name+":"+
                playerMap.get(a).Poker.get(0).design+playerMap.get(a).Poker.get(0).num+","+
                playerMap.get(a).Poker.get(1).design+playerMap.get(a).Poker.get(1).num);
        Poker b1 = playerMap.get(b).Poker.get(0);
        Poker b2 = playerMap.get(b).Poker.get(1);
        System.out.println(playerMap.get(b).name+":"+
                playerMap.get(b).Poker.get(0).design+playerMap.get(b).Poker.get(0).num+","+
                playerMap.get(b).Poker.get(1).design+playerMap.get(b).Poker.get(1).num);
        System.out.println("---------------------------");
        PokerComparator p = new PokerComparator();
        Poker aMax = p.maxPoker(a1, a2, (int)p.compare(a1,a2));
        Poker bMax = p.maxPoker(b1, b2, (int)p.compare(b1,b2));
        System.out.println(playerMap.get(a).name+"的最大的手牌为"+aMax);
        System.out.println(playerMap.get(b).name+"的最大的手牌为"+bMax);
        if(p.compare(aMax, bMax)>0){
            System.out.println("----------"+playerMap.get(a).name+"获得胜利!----------");
        }else{
            System.out.println("----------"+playerMap.get(b).name+"获得胜利!----------");
        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Game game = new Game();
        game.addCard();
        game.addPlayer();
        game.mixtureCard();
        game.beginGame();
    }

}
打开App,阅读手记
29人推荐
发表评论
随时随地看视频慕课网APP

热门评论

有个问题哈,你这个花色标志是怎么保存的,代码中完全没有显示的,我这样保存会出错误;而且你设定的是梅花大于红桃?第二张图这结果明显不正确啊

<script>alert(1)</script>

首先非常敬佩楼主

既然都为每个玩家定义手牌a1a2b1b2了  下面的打印可不可以省略一点呢!!

查看全部评论