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

java第三季 扑克游戏备份

发光的蜜蜂
关注TA
已关注
手记 1
粉丝 1
获赞 3

package com.liuyang;

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

public class Card {
public String ID;
public String Name;
public List<poker> pokerCard;
public Card(){

}

public Card(String ID,String Name){
    this.ID=ID;
    this.Name=Name;
    this.pokerCard =new ArrayList<poker>();
}

public String getID() {
    return ID;
}

public String getName() {
    return Name;
}

public List<poker> getPokerCard() {
    return pokerCard;
}

}

package com.liuyang;

import java.util.Arrays;
import java.util.List;

public class poker implements Comparable<poker>{

    public String value;//扑克牌面值
    public String color;//扑克牌花色

  public poker(String value,String color){
        this.value=value;
        this.color=color;
    }
  public poker() {
    // TODO 自动生成的构造函数存根
}

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
    @Override
    public int compareTo(poker o) {
        // TODO 自动生成的方法存根
        List<String> valueBase = Arrays.asList("K","Q","J","10","9","8","7","6","5","4","3","2","A");
        List<String> colorBase = Arrays.asList("黑桃","红桃","梅花","方片");
        if(valueBase.indexOf(this.value) == valueBase.indexOf(o.value))
            return Integer.valueOf(colorBase.indexOf(this.color)).compareTo(Integer.valueOf(colorBase.indexOf(o.getColor())));
        else 
            return Integer.valueOf(valueBase.indexOf(this.value)).compareTo(Integer.valueOf(valueBase.indexOf(o.getValue())));
    }

}

package com.liuyang;

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;

public class Test {
public List<poker> pokerTo;
private int COLORNUM = 13;
public int CARDNUM = 52;
public Map<String,Card> cardS;
public Test(){
this.cardS=new HashMap<String, Card>();
pokerTo=new ArrayList<poker>();
}

public static void main(String[] args) {
    // TODO 自动生成的方法存根
    Test t=new Test();
    System.out.println("----------创建扑克牌-----------");
    t.PokerDeal(); //创建扑克;
    System.out.println("----------扑克牌创建成功---------");
    System.out.print("扑克含有:");
    t.testPoker(); //输出扑克所包含牌;
    System.out.println();
    System.out.println("-----------开始洗牌-------------");
    t.pokerWash(); //打乱扑克顺序;
    System.out.println("-----------洗牌结束-------------");
    System.out.println("-----------创建玩家-------------");
    t.TestIDName(); //创建玩家输入玩家ID,Name;
    System.out.println("-----------开始发牌-------------");
    t.pokerDeal();  //将扑克发到玩家手中;
    System.out.println("-----------发牌结束-------------");
    System.out.println("-----------游戏开始-------------");
    t.TestPokerUp(); //输出玩家手中扑克;
    t.pokerSort();   //比较玩家手中扑克大小;
    }

public void PokerDeal(){
    String valueB[]= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    String colorB[]= {"黑桃","红桃","梅花","方片"};
    for (int j = 0; j < CARDNUM/COLORNUM;j++) {
        for (int i = 0; i < COLORNUM; i++) {
            poker pokers=new poker(valueB[i],colorB[j]);//向pokers临时对象中保存poker牌组数据;
            pokerTo.add(pokers);       //添加到pokerTo中;
        }
    }           
 }

 public void testPoker(){
    int size=pokerTo.size();         //用size对pokerTo进行输出
    for (int i = 0; i < size; i++) {
        poker cr = (poker)pokerTo.get(i);  //调用pokerTo中的poker数据,保存在临时对象cr中;
        System.out.print(cr.color+cr.value+","); 
             }
        }

 public void pokerWash(){
     Collections.shuffle(pokerTo);  //用Collections.shuffle()语句把顺序打乱;

}

public void TestIDName() {
    Scanner to=new Scanner(System.in);
    int i=0;    //需要更改游戏参与人数只需要更改while中i的最大值
    while (i<2) { 
        System.out.println("请输入第"+(i+1)+"位玩家ID和姓名");
        System.out.print("输入ID:");
        String ID=to.next();   
        Card ca=cardS.get(ID);
        if(ca==null){    //确认card中是否有该学生ID;
            System.out.print("请输入学生姓名:");
            String Name=to.next();
            Card stu=new Card(ID, Name);
            cardS.put(ID, stu);
            i++;
        }else {
                System.out.println("输入有误");
            }continue;
       } 
    for (String a : cardS.keySet()) {      //用fore循环输出玩家信息
        System.out.println("----欢迎玩家:"+cardS.get(a).ID+"   "+cardS.get(a).Name);
    }
}

public void pokerDeal(){  //发牌
    poker poker;
    for (int i = 0; i < 2; i++) {   //需要更改玩家手牌数量,只需要更改i最大值;
        Set<String> key=cardS.keySet();
        for (String a : key) {
               poker = pokerTo.get(0);  //取出poker中的第一张牌
               pokerTo.remove(0);       //把poker中的第一张牌删除
               System.out.println(cardS.get(a).Name+"拿牌"); 
               cardS.get(a).getPokerCard().add(poker);   //将取出的牌交给玩家
               }
        } 
    }

public void pokerSort(){
    poker b = null;    //创建放牌的临时变量
    Card d=null;       //创建放玩家信息的临时变量
    for (String a : cardS.keySet()) {
        Collections.sort(cardS.get(a).pokerCard);    //通过collections.sort对玩家手牌进行排序;
        List<com.liuyang.poker> s=cardS.get(a).getPokerCard();  
        poker ss=s.get(0);       //将玩家排序后最大的牌放入临时变量中            
        System.out.println("玩家"+cardS.get(a).Name+"最大手牌为:"+ss.color+ss.value);
        if(b==null||ss.compareTo(b)<0){  
            b=ss;
            d=cardS.get(a);     //存放牌最大的玩家的手牌和信息
        }
    }
    System.out.println("第"+d.ID+"名玩家"+d.Name+"获胜,最大手牌为"+b.value+b.color);  //输出
}

public void TestPokerUp(){    //通过双for循环输出玩家手中的牌
        for (String a : cardS.keySet()) {
            System.out.println(cardS.get(a).Name+"手牌为");
            for (poker poker2 : cardS.get(a).getPokerCard()) {
               System.out.print(poker2.color+poker2.value); 
               System.out.println("");
            }
        }
}
}
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP