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

java第三季作业-纸牌游戏(非原创,重在自己理解哈哈)~

没有梦想的咸魚
关注TA
已关注
手记 4
粉丝 0
获赞 83

玩家类Player

package com.practise2;

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

public class Player {
    //创建玩家id
    public Integer id;
    //创建玩家姓名
    public String name;
    //初始化手牌集合
    public List<Poker> Poker=new ArrayList<Poker>();
    //含参构造器,传入参数
    public Player(Integer id,String name){
        this.id=id;
        this.name=name;
    }
}

扑克类Poker

package com.practise2;

public class Poker {
    //定义扑克牌的花色
    public String color;
    //定义扑克牌的大小
    public String num;
    //重写toString方法,返回值为“花色+点数”
    public String toString(){
        return color+num;
    }
    //含参构造器,传递进去两个参数
    public Poker(String color,String num){
        this.color=color;
        this.num=num;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((color == null) ? 0 : color.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 (color == null) {
            if (other.color != null)
                return false;
        } else if (!color.equals(other.color))
            return false;
        if (num == null) {
            if (other.num != null)
                return false;
        } else if (!num.equals(other.num))
            return false;
        return true;
    }
}

手牌比较类PokerComparator

package com.practise2;

import java.util.Comparator;

public class PokerComparator implements Comparator<Poker> {

    public int compare(Poker o1, Poker o2) {
        // TODO Auto-generated method stub
        //创建字符串花色和大小
        String a="方块梅花红桃黑桃";
        String b="2345678910JQKA";
        //分别查找,花色和大小在顺序字符串中的位置,位置大的即大
        int a1=a.indexOf(o1.color);
        int b1=b.indexOf(o1.num);
        int a2=a.indexOf(o2.color);
        int b2=b.indexOf(o2.num);
        //通过索引位置判断牌的大小
        if(a1>a2){
            return 1;
        }else if(a1==a2){
            if(b1>b2){
                return 1;
            }else{
                return -1;
            }
        }else{
            return -1;
        }
    }
    //compare方法,返回大的牌
    public Poker maxPoker(Poker o1,Poker o2,int c){
        if(c>0){
            return o1;
        }else{
            return o2;
        }
    }
}

测试类Game

package com.practise2;

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

public class Game {
    //以List为接口,创建集合pokerList
    public List<Poker> pokerList;
    //导入输入函数
    private Scanner input;
    //Map为接口,创建玩家集合players
    public Map<Integer,Player> players;
//  public Map<Integer,Player> players2;
    //定义全局静态变量a,b
    public static int a;
    public static int b;
    /**
     * 在构造器中初始化扑克牌序列以及玩家信息
     */
    public Game(){
        this.pokerList=new ArrayList<Poker>();
        input=new Scanner(System.in);
        this.players=new HashMap<Integer,Player>();
//      this.players2=new HashMap<Integer,Player>();
    }
    /**
     * 创建扑克牌
     * @param args
     */
    public void creatCard(){
        //创建花色数组
        String [] color={"黑桃","红桃","梅花","方块"};
        //创建大小数组
        String [] num={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        //将数组中的值传递给对象pk,并将其添加到集合中形成一副牌
        for(int i=0;i<color.length;i++){
            for(int j=0;j<num.length;j++){
                Poker pk=new Poker(color[i],num[j]);
                pokerList.add(pk);
            }
        }
        System.out.println("----------创建扑克牌----------");
        System.out.println("---------扑克牌创建成功--------");
        for (Poker pk : pokerList) {
            System.out.print("["+pk+"]");//已经重写了toString方法,所以可以直接输出pk
        }
        System.out.println();//回车
    }
    /**
     * 洗牌
     * @param args
     */
    public void washCard(){
        System.out.println("开始洗牌");
        //调用Collections工具类中的乱序方法
        Collections.shuffle(pokerList);
        for (Poker pk : pokerList) {
            System.out.print("["+pk.color+pk.num+"]");
        }
        System.out.println("----------洗牌结束!----------");
    }
    /**
     * 创建两位玩家
     * @param args
     */
    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;
            try{
                id=scanInt();
            }catch(Exception e){
                System.out.println(e.getMessage());
                continue;
            }
            Player p=players.get(id);
            if(p==null){
                System.out.println("请输入玩家姓名:");
                String name =input.next();
                //用于鉴定两次输入的id是否相同,因现在不能保存两次输入的id值,所以这样
//              plays2.put(id,new Player(id,name));
//              players.put(i+1,new Player(id,name));
                players.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
     * @param args
     */
    public int scanInt() throws Exception{
        try{
            int in=input.nextInt();
            return in;
        }catch(Exception e){
            input=new Scanner(System.in);
            throw new Exception("输出异常,请输入整数类型的ID");
        }
    }
    /**
     * 游戏开始
     * @param args
     */
    public void beginGame(){
        System.out.println("欢迎玩家:"+players.get((int)a).name+players.get((int)b).name);
        System.out.println("----------开始发牌----------");
        System.out.println(players.get(a).name+"得到第一张牌");
        players.get(a).Poker.add(pokerList.get(0));
        System.out.println(players.get(b).name+"得到第一张牌");
        players.get(b).Poker.add(pokerList.get(1));
        System.out.println(players.get(a).name+"得到第二张牌");
        players.get(a).Poker.add(pokerList.get(2));
        System.out.println(players.get(b).name+"得到第二张牌");
        players.get(b).Poker.add(pokerList.get(3));
        System.out.println("----------发牌结束!--------");
        System.out.println("----------游戏开始!---------");
        System.out.println("玩家各自手牌为:");
        Poker a1=players.get(a).Poker.get(0);
        Poker a2=players.get(a).Poker.get(1);
        Poker b1=players.get(b).Poker.get(0);
        Poker b2=players.get(b).Poker.get(1);
        System.out.println("玩家:"+players.get(a).name+"的手牌为:"+
        a1.color+a1.num+","+a2.color+a2.num);
        System.out.println("玩家:"+players.get(b).name+"的手牌为:"+
        b1.color+b1.num+","+b2.color+b2.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(players.get(a).name+"的最大手牌为:"+aMax);
        System.out.println(players.get(b).name+"的最大手牌为:"+bMax);
        if(p.compare(aMax,bMax)>0){
            System.out.println("----------"+players.get(a).name+"获得胜利!----------");
        }else{
            System.out.println("----------"+players.get(b).name+"获得胜利!----------");
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Game zy=new Game();
        zy.creatCard();
        zy.addPlayer();
        zy.washCard();
        zy.beginGame();

    }

}

很大程度上借鉴了一位慕友的代码,不过后来发现有问题,主要集中在比较类中的参数比较设置错误,于是修改了一下就ok了,我的心得是:别人写的代码可以抄,但是一定要理解,不然毫无意义哈哈!

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