手记

来自零碎七八的写法,可做反面教材【java第三季 纸牌游戏】

package zhipai;

public class Pai {

private int num;//序号
private int dian;//点数(用于比较)
private String name;//牌名

public int getNum() {
    return num;
}

public void setNum(int num) {
    this.num = num;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Pai(int num , int dian, String name){
    this.num = num;
    this.dian = dian;
    this.name = name;
}

public Pai() {
    // TODO 自动生成的构造函数存根
}

public int getDian() {
    return dian;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.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;
    Pai other = (Pai) obj;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}

public void setDian(int dian) {
    this.dian = dian;
}

}


package zhipai;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Test {

private List<Pai> listZp;

public Test(){
    this.listZp = new ArrayList<Pai>();
}

public void test(){//说是做测试,其实也差不多
    Pai[] paiArray = {new Pai(1,1,"梅花A"),new Pai(2,1,"方块A"),new Pai(3,1,"黑桃A"),new Pai(4,1,"红桃A"),
            new Pai(5,2,"梅花2"),new Pai(6,2,"方块2"),new Pai(7,2,"黑桃2"),new Pai(8,2,"红桃2"),
            new Pai(9,3,"梅花3"),new Pai(10,3,"方块3"),new Pai(11,3,"黑桃3"),new Pai(12,3,"红桃3"),
            new Pai(13,4,"梅花4"),new Pai(14,4,"方块4"),new Pai(15,4,"黑桃4"),new Pai(16,4,"红桃4"),};
   listZp.addAll(Arrays.asList(paiArray));

   for (Pai pai : listZp) {//把纸牌打印出来(抱歉我买的扑克牌打了几折,老板只给了我16张,,)

       System.out.print(pai.getNum()+":"+pai.getName()+"\t\t");

       if(pai.getNum()%4==0)//为了美观,我进行每输出四张牌换行一次。因为牌型只有四种,所以,以4代替的红桃刚刚用在了这里。
           System.out.println();
}

}

public static void main(String[] args){

//这里嘛,乱七八糟的,大家引以为戒
Random random = new Random();

    Test t1 = new Test();
    t1.test();
    System.out.println("----有请两位选手入场------");//请打开赌神的经典配乐
    System.out.println("万众瞩目的小明");
    System.out.println("以及花见花开的小华");

    Pai[] paiArray = new Pai[4]; //此数组用来接收发下来的四张牌

    Pai p;//用来接收Pai对象
    int pd;//存储list.size()
    System.out.println("洗牌:");
    Collections.shuffle(t1.listZp);//打乱顺序(这个函数的原理我还没弄懂,或者说我根本没看。)
    int ii=0;//英语水平,起的名字也很有特色
    for (Pai pai : t1.listZp) {//打印洗牌后的排序,当然,玩家看不见。

        System.out.print(pai.getName()+"\t");
        ii++;  
        if(ii%4==0)
               System.out.println();
    }
    System.out.println("开始发牌————————");
    for(int i=0; i<4;i++){//发牌

            pd = random.nextInt(t1.listZp.size());//这里其实用顺序发牌的,我这种是出老千试发法引以为戒
            p = t1.listZp.get(pd);
            t1.listZp.remove(pd);//删除免得,出现重复的牌

        paiArray[i]=p;//放入pai类型数组中
    }
    System.out.println("小明:"+paiArray[0].getName()+":"+paiArray[1].getName());
    System.out.println("小华:"+paiArray[2].getName()+":"+paiArray[3].getName());
    System.out.println("最后结果:");
    int[] pArray = new int[4];

    for(int j=0 ; j < paiArray.length;j++){
        pArray[j] = paiArray[j].getNum();
    }
     int max=pArray[0];//把数组的第一个数赋值给max,此时对应的下标为0 
     int t=0;
        for(int k=0;k<4;k++)//从数组的第二个数开始判断,max是否是最大值 
            if(max<pArray[k]){//不是最大值,就把该值赋值给max,并修改下标 
                max=pArray[k];
                t=k;
            } 
        t=t+1;
        System.out.println(t);

        if(t<=2)
            System.out.print("小明获胜!");
        else
            System.out.println("小华获胜");

}

}

-------第二段代码简单修改后

package zhipai;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Test {

private List<Pai> listZp;

public Test(){
    this.listZp = new ArrayList<Pai>();
}

public void test(){
    Pai[] paiArray = {new Pai(1,1,"梅花A"),new Pai(2,1,"方块A"),new Pai(3,1,"黑桃A"),new Pai(4,1,"红桃A"),
            new Pai(5,2,"梅花2"),new Pai(6,2,"方块2"),new Pai(7,2,"黑桃2"),new Pai(8,2,"红桃2"),
            new Pai(9,3,"梅花3"),new Pai(10,3,"方块3"),new Pai(11,3,"黑桃3"),new Pai(12,3,"红桃3"),
            new Pai(13,4,"梅花4"),new Pai(14,4,"方块4"),new Pai(15,4,"黑桃4"),new Pai(16,4,"红桃4"),};
   listZp.addAll(Arrays.asList(paiArray));

   for (Pai pai : listZp) {

       System.out.print(pai.getNum()+":"+pai.getName()+"\t\t");

       if(pai.getNum()%4==0)
           System.out.println();
}

}

public static void main(String[] args){
    Random random = new Random();
    List<Pai> listZp2 = new ArrayList<Pai>();
    Test t1 = new Test();
    t1.test();
    System.out.println("----有请两位选手入场------");
    System.out.println("万众瞩目的小明");//我知道我很堕落
    System.out.println("以及花见花开的小华");//我堕落到懒惰

    Pai p;//用来接收Pai对象

    System.out.println("洗牌:");
    Collections.shuffle(t1.listZp);//打乱顺序(这个函数的原理我还没弄懂,或者说我根本没看。)
    int ii=0;//英语水平,起的名字也很有特色
    for (Pai pai : t1.listZp) {//打印洗牌后的排序,当然,玩家看不见。

        System.out.print(pai.getName()+"\t");
        ii++;  
        if(ii%4==0)
               System.out.println();
    }
    System.out.println("开始发牌————————");
    for(int i=0; i<4;i++){//发牌(严格来说,这里只是把要发的四张去取来)

            //pd = random.nextInt(t1.listZp.size());//这里其实用顺序发牌的,我这种是出老千试发法引以为戒
            p = t1.listZp.get(0);
            t1.listZp.remove(0);//删除,免得,出现重复的牌
        listZp2.add(p);//放入新的集合中
    }

    boolean pd = true;
    int iii=0;//英语水平低,起的名字倒很有特色

    int[] numx = new int[4];//这里用来接收四张牌的序号
    for (Pai pai : listZp2) {//循环发牌,你一张我一张,最后小明的牌的下标是0,1

        if(pd){//你一张我一张,ii+2与ii-1,刚好能够达到想要的目的
            numx[iii] = pai.getNum();
            iii=iii+2;
            pd=false;
        }else{
            numx[iii] = pai.getNum();
            iii=iii-1;
            pd=true;
        }
    }
    /*
     * 室友约游戏,这里就潦草的写了下,千万别学我这么堕落
     */
    System.out.println("小明:");//在集合中,两者的牌是你一样我一张,所以要错开,当然,我这里直接用数字不好(反面 反面,注意)
    System.out.println(listZp2.get(0).getName()+"\t"+listZp2.get(2).getName());
    System.out.println("小华:");
    System.out.println(listZp2.get(1).getName()+"\t"+listZp2.get(3).getName());

    Collections.sort(listZp2);//用Comparable 比较
    System.out.println("排序后:");
    for (Pai pai : listZp2) {//打印排序后的牌
        System.out.println(pai.getDian()+":"+pai.getNum()+":"+pai.getName());

    }
    for(int i = 0 ; i<numx.length;i++){//循环找最大牌
        if(numx[i]==listZp2.get(3).getNum()){//因为排序了,最大的牌在最后所以...(数组里存储两人原来的牌名,只要找到最大牌和谁对应就能知道谁赢了)
            if(i<2){//下标前两张是小明的numx[0],numx[1]
                System.out.println("小明获胜");
            }else{//numx[2],numx[3]
                System.out.println("小华获胜");
            }
            }
        }
}

}

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