手记

纸牌游戏,三个类,运用了List,Set,封装,string方法等

     感觉还有一些缺点,最后的main方法中写的代码太多了,感觉可以精简一些,

main中的一部分代码感觉可以写在某种方法中调用,但是由于需要调用许多之前的参数,在函数中写的话就变得更复杂。。。纠结

    另外就是要判断牌面大小的时候,我用的方法是强行将string类型拆开。。感觉用Cards类应该更好吧,目前没看到用Map做的小伙伴,有的话希望大家分享~

运行结果如下:

Player玩家类

import java.util.ArrayList;
import java.util.List;
/*Player为玩家的类*/
public class Player {
    private String id;
    private String name;
    public  static List<String> cardsInHand;
    public Player(String id,String name){//初始化
        this.id = id;
        this.name = name;
        this.cardsInHand = new ArrayList<String>();
    }
    //封装属性
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

Cards纸牌类


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*Cards为卡片的类*/
public class Cards {
    private String color;
    private String num;
    private String name;
    private String []colorAll = {"方片","梅花","红桃","黑桃"};//创建花色黑红
    private String []numAll = {"2","3","4","5","6","7","8","9",
            "10","J","Q","K","A"};//创建大小
    public static List<String> newCards ;
    //创建用于存放卡片的集合
    public Cards(){
        //初始化
        this.newCards = new ArrayList<String>();    
    }
    //创建新的扑克牌
    public void newCard(){
        System.out.println("-------------创建扑克牌--------------"
                +"\n"+"-------------扑克牌创建成功------------"+"\n--------为:");
                    for(int j =0;j<4;j++){
                        color = colorAll[j];
                        for(int k =0;k<13;k++){
                            num = numAll[k];
                            name = color+num;
                            newCards.add(name);//将整副有顺序的牌传入newCards
                            System.out.print(name+",");
                        }
                    }
    }
    //封装
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getNum() {
        return num;
    }
    public void setNum(String num) {
        this.num = num;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String[] getColorAll() {
        return colorAll;
    }
    public void setColorAll(String[] colorAll) {
        this.colorAll = colorAll;
    }
    public String[] getNumAll() {
        return numAll;
    }
    public void setNumAll(String[] numAll) {
        this.numAll = numAll;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((color == null) ? 0 : color.hashCode());
        result = prime * result + Arrays.hashCode(colorAll);
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((num == null) ? 0 : num.hashCode());
        result = prime * result + Arrays.hashCode(numAll);
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Cards))
            return false;
        Cards other = (Cards) obj;
        if (color == null) {
            if (other.color != null)
                return false;
        } else if (!color.equals(other.color))
            return false;
        if (!Arrays.equals(colorAll, other.colorAll))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (num == null) {
            if (other.num != null)
                return false;
        } else if (!num.equals(other.num))
            return false;
        if (!Arrays.equals(numAll, other.numAll))
            return false;
        return true;
    }

}

hereToPlay运行所用的类


import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class hereToPlay implements Comparator<String>{

    Scanner input = new Scanner(System.in);
    public static List<String> shuffledCards;//洗过的牌集合
    public static Set<String> shuffle = new HashSet<String>();//用来洗牌的Set集合
    public hereToPlay(){
        //一定一定一定要记得初始化!!!!!!!!
        this.shuffledCards=new ArrayList<String>();
        this.shuffle = new HashSet<String>();
    }
    @Override
    public int compare(String c1, String c2) {
        String color1 = c1.substring(0, 2);
        String num1 = c1.substring(2);
        System.out.println("--------"+"将c1拆分"+c1+":"+color1+":"+num1+"--------");
        String color2 = c2.substring(0, 2);
        String num2 = c2.substring(2);
        System.out.println("--------"+"将c2拆分"+c2+":"+color2+":"+num2+"--------");

        if(color1.equals(color2)){
            //花色相同,num1>num2时,返回正数,一号胜利
            System.out.println("--------"+"c1与c2花色相同"+"--------");
            System.out.println("--------"+"num1.compareTo(num2)的值为"+num1.compareTo(num2)+"--------");
            return num1.compareTo(num2);
        }
        else{
            //花色不同,比较花色,索引大的胜利
            int color1WZ=0;
            int color2WZ=0; 
                String []colorAll = {"方片","梅花","红桃","黑桃"};
                //寻找color1的索引位置
                for(int i = 0;i<4;i++){
                    if(color1.equals(colorAll[i])){
                        System.out.println("--------"+"color1的索引位置是:"+i+"--------");
                        color1WZ=i;
                    }
                }
                //寻找color2的索引位置
                for(int i = 0;i<4;i++){
                    if(color2.equals(colorAll[i])){
                        System.out.println("--------"+"color2的索引位置是:"+i+"--------");
                        color2WZ=i;
                    }
                }
                System.out.println("--------"+"(color1WZ).compareTo(color2WZ)的值为"+(color1WZ+"").compareTo((color2WZ+"")+"--------"));
                return (color1WZ+"").compareTo((color2WZ+""));

        }
    }

    //创建玩家
    public Player createPlayer1(){
        System.out.println("-----------开始洗牌----------\n"
                + "--------------洗牌结束!-------------\n"
                + "--------"+"请输入第一位玩家的ID:");
        String p1Id = input.next();
        System.out.println("--------"+"请输入第一位玩家的姓名:");
        String p1Name = input.next();
        Player p1 = new Player(p1Id,p1Name);
        return p1;
    }
    public Player createPlayer2(){
        System.out.println("--------"+"请输入第二位玩家的ID:");
        String p2Id = input.next();
        System.out.println("--------"+"请输入第二位玩家的姓名:");
        String p2Name = input.next();
        Player p2 = new Player(p2Id,p2Name);        
        return p2;
    }
    //洗牌
    public List shuffleCards(Player p1,Player p2,List<String> list1,List<String> list2){
        //需要传入两个玩家,cardsInHand和newCard集合,所以传入四个参数
        /* 核心思想:
         * 首先进行洗牌操作,可以创建一个中间Set集合shuffle,利用Set的无序性进行洗牌,
         * 将newCards中的元素遍历的同时添加到shuffle中,再遍历此Set集合的同时将元素导出到
         * cardsInHand集合中*/
        //list1为newCard,list2为shuffledCards

        for (String string : list1) {
            shuffle.add(string);
        }
        for (String string : shuffle) {
            list2.add(string);
        }
        return list2;//将洗好的牌传入cardsInHand中
    }
    //-----------------main函数------------------------
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        hereToPlay htp = new hereToPlay();
        Cards c = new Cards();//用来调用构造函数产生扑克牌
        c.newCard();
        Player p1=htp.createPlayer1();
        Player p2=htp.createPlayer2();
        System.out.println("--------"+"欢迎玩家:"+p1.getName()+"--------");
        System.out.println("--------"+"欢迎玩家:"+p2.getName()+"--------");
        htp.shuffleCards(p1, p2, Cards.newCards, shuffledCards);
        System.out.println("---------------开始发牌----------------");

        /*这段代码是分牌的代码,本来想写在一个方法中的,但是老师的要求是还要比较大小,
         * 在一个函数中不好完成,我就写在main中了,方便使用之前的参数*/
        //----------------------------------------------------------
                Random r = new Random();
                //先给c1发牌
                String c1 = shuffledCards.get(r.nextInt(52));
                String c2;
                String c3;
                String c4;
                //玩家一先拿牌
                p1.cardsInHand.add(c1);
                System.out.println("--------"+"玩家"+p1.getName()+"-拿牌"+"--------");
                //玩家二拿牌,需要判断不能拿到相同的牌
                do{
                     c2 = shuffledCards.get(r.nextInt(52));
                    }
                while(c1.equals(c2));//c1与c2名字相同
                p2.cardsInHand.add(c2); 
                System.out.println("--------"+"玩家"+p2.getName()+"-拿牌"+"--------");
                //玩家一拿牌,不能跟前两张拿的一样
                do{
                     c3 = shuffledCards.get(r.nextInt(52));
                    }
                while(c3.equals(c1)||c3.equals(c2));//c3与c1或者c3与c2名字相同
                p1.cardsInHand.add(c3);
                System.out.println("--------"+"玩家"+p1.getName()+"-拿牌"+"--------");
                //玩家二拿牌,不能跟前三张拿的一样
                do{
                     c4 = shuffledCards.get(r.nextInt(52));
                    }
                while(c4.equals(c1)||c4.equals(c2)||c4.equals(c3));
                p2.cardsInHand.add(c3);
                System.out.println("--------"+"玩家"+p2.getName()+"-拿牌"+"--------");

                System.out.println("--------"+"玩家:"+p1.getName()+"的手牌为:"
                        + "["+c1+","+c3+"]"+"--------");
                System.out.println("--------"+"玩家:"+p2.getName()+"的手牌为:"
                        + "["+c2+","+c4+"]"+"--------");
        //----------------------------------------------------------

        //------------------------判断机制-------------------------------
        int maxP1 = htp.compare(c1, c3);
        int maxP2 = htp.compare(c2, c4);
        String p1Bigger;//玩家一的较大手牌
        String p2Bigger;//玩家二的较大手牌
        //函数输出为正数,选择的是第一张牌,否则第二张牌
        if(maxP1>0){
            System.out.println("--------玩家:"+p1.getName()+"选择了第一张牌:--------"+c1);
            p1Bigger=c1;
        }else{
            System.out.println("--------玩家:"+p1.getName()+"选择了第二张牌:--------"+c3);
            p1Bigger =c3;
        }
        if(maxP2>0){
            System.out.println("--------玩家:"+p2.getName()+"选择了第一张牌:--------"+c2);
            p2Bigger=c2;
        }else{
            System.out.println("--------玩家:"+p1.getName()+"选择了第一张牌:--------"+c4);
            p2Bigger =c4;
        }
        int win = htp.compare(p1Bigger, p2Bigger);
        if(win>0){
            System.out.println("--------"+p1.getName()+"胜利!"+p1.getName()+"最牛逼!"+"最大的手牌是"+"["+p1Bigger+"]"+"--------");
            System.out.println("--------"+p2.getName()+"最大的手牌是"+"["+p2Bigger+"]"+"--------");
        }
        if(win<0){
            System.out.println("--------"+p1.getName()+"最大的手牌是"+"["+p1Bigger+"]"+"--------");
            System.out.println("--------"+p2.getName()+"胜利!"+p2.getName()+"最牛逼!"+"最大的手牌是"+"["+p2Bigger+"]"+"--------");
        }
        if(win==0){
            System.out.println("-----------------平局!!------------------");
        }
    }

}
8人推荐
随时随地看视频
慕课网APP