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

java第三季-简易扑克牌游戏

等候稻香
关注TA
已关注
手记 7
粉丝 16
获赞 81

简易扑克牌游戏:

功能:

  1. 创建一副扑克牌,花色为:黑桃,红桃,梅花,方片,且依次变小,
    数字为:”2”,”3”,”4”,”5”,”6”,”7”,”8”,”9”,”10”, “J”,”Q”,”K”,”A”,且依次增大;

  2. 洗牌,即对扑克牌随机排序;

  3. 创建2名玩家,每名玩家手中两张牌,比较输出赢家。
  4. 以下为java代码:

扑克牌类:Poker

**

/** 
* 扑克牌类 
* @author Administrator 
* 
*/ 
public class Poker implements Comparable{ 
//属性 
private String suit;//花色 
private String id;//2-10,JQKA

//方法
public Poker(String suit,String id){
    this.suit=suit;
    this.id=id;
}

//get.set方法
public String getSuit() {
    return suit;
}

public void setSuit(String suit) {
    this.suit = suit;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public int compareTo(Poker arg2) {
    //首先比较数字
    //其次比较花型
    String color="方块 梅花 红桃  黑桃";
    String numbers="2 3 4 5 6 7 8 9 10 J Q K A";
    int result=numbers.indexOf(this.getId())-
            numbers.indexOf(arg2.getId());

    if(result==0){
        //说明数字相同,则比较花型
        int result2=color.indexOf(this.getSuit())-color.indexOf(arg2.getSuit());
        if(result2>0){
            return 1;
        }else if(result2<0){
            return -1;
        }else{
            return 0;
        }
}else{
    return result;
} } } 
`
**

玩家类:Player

** 
import Java.util.ArrayList; 
import java.util.HashSet; 
import java.util.List; 
import java.util.Set;

/** 
* 玩家类 
* @author Administrator 
* 
*/ 
public class Player { 
//玩家属性 
private int id; //玩家id 
private String name;

private List<Poker> pokers;  //玩家手中的牌,用集合存放

//构造方法
public Player(int id,String name){
    this.id=id;
    this.name=name;
    this.pokers=new ArrayList<Poker>();
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

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

public List<Poker> getPokers() {
    return pokers;
}

public void setPokers(List<Poker> pokers) {
    this.pokers = pokers;
}

//get,set方法
} 
`

**

测试类:testCollections

** 
import java.util.Collections; 
import java.util.Iterator; 
import java.util.List; 
import java.util.ArrayList; 
import java.util.Scanner; 
import java.lang.Exception;

public class testCollections {

/**
 * 测试类:
 * 1.首先创建一副扑克牌,扑克牌用List容器
 */
public List<Poker> pokerList=new ArrayList<Poker>();

//添加所有的扑克牌到List中去
public void testAdd(){
    //1.创建所有的扑克牌对应的对象 
    //2.添加对象到List中去
    String[] suitArray={"黑桃","红桃","梅花","方片"};
    String[] idArray={"2","3","4","5","6","7","8","9","10",
                      "J","Q","K","A"};
    int count=0;
    System.out.println("------------创建一副扑克牌-----------------");
    for(int i=0;i<suitArray.length;i++){
        for(int j=0;j<idArray.length;j++){
            Poker newPoker=new Poker(suitArray[i],idArray[j]);
            pokerList.add(newPoker);//添加进去
            count++;
            System.out.println("添加扑克牌:"+newPoker.getSuit()+newPoker.getId());
            if(count%4==0){
                System.out.println();
            }
        }
    }
}

//遍历输出所有扑克牌
public void testForEach(){
    int count=0;//控制输出格式
    System.out.println("--------输出所有扑克牌---------");
    for(Poker poker:pokerList){
        System.out.print(poker.getSuit()+poker.getId()+"\t");
        count++;
        if(count%4==0){
            System.out.println();
        }
    }
}

//对扑克牌打乱顺序
public void testSort(){
    System.out.println("-----------洗牌前--------------");
    testForEach();
 Collections.shuffle(pokerList);
System.out.println("-------------洗牌后--------------");
int count=0;
for(Poker poker:pokerList){
    System.out.print(poker.getSuit()+poker.getId()+"\t");
    count++;
    if(count%4==0){
        System.out.println();
    }
    }
}

/**
 * 创建2个玩家对象,并为每个玩家发2张牌
 * @param args
 */
public void dealPokers(){
    List<Player> playerList=new ArrayList<Player>();
    int id;
    for(int i=1;i<=2;i++){
    while(true){
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入玩家"+i+"的id:");
    try{
     id=sc.nextInt();
    }catch(Exception e){
        System.out.println("输入的不是整数,请重新输入!");
        continue;
    }
    break;
    }
    Scanner console=new Scanner(System.in);
    System.out.println("请输入玩家"+i+"的姓名:");
    String name=console.next();
    Player player=new Player(id,name);
    playerList.add(player);

    System.out.println("欢迎玩家:"+name);
    }

    //为两位玩家发牌

    System.out.println("------开始发牌:");
    System.out.println("玩家"+playerList.get(0).getName()+"拿牌:");
    playerList.get(0).getPokers().add(pokerList.get(0));
    System.out.println("玩家"+playerList.get(1).getName()+"拿牌:");
    playerList.get(1).getPokers().add(pokerList.get(1));
    System.out.println("玩家"+playerList.get(0).getName()+"拿牌:");
    playerList.get(0).getPokers().add(pokerList.get(2));
    System.out.println("玩家"+playerList.get(1).getName()+"拿牌:");
    playerList.get(1).getPokers().add(pokerList.get(3));
        System.out.println("------发牌结束!");

    System.out.println("----------开始游戏-----------");    
        //2.比较两位玩家手中的最大牌    
    Collections.sort(playerList.get(0).getPokers());    
// System.out.println(“玩家”+ 
// playerList.get(0).getName()+”的牌排序为(从小到大):”); 
// printList(playerList.get(0));

    // System.out.println("玩家手中最大牌为:"+playerList.get(0).getPokers().get(1).getSuit()
    //       +playerList.get(0).getPokers().get(1).getId());

     Collections.sort(playerList.get(1).getPokers());   
     //System.out.println("玩家"+
     //playerList.get(1).getName()+"的牌排序为(从小到大):");
     //printList(playerList.get(1));

     //System.out.println("玩家手中最大牌为:"+playerList.get(1).getPokers().get(1).getSuit()
        //   +playerList.get(1).getPokers().get(1).getId());

    //3.得出哪位玩家赢
     Poker oneMax=playerList.get(0).getPokers().get(1);
     Poker twoMax=playerList.get(1).getPokers().get(1);
     List<Poker> newList=new ArrayList<Poker>();
     newList.add(oneMax);
     newList.add(twoMax);
     if(oneMax.compareTo(twoMax)==0){
         System.out.println("平局");}
     else{
         if(oneMax.compareTo(twoMax)>0)
             System.out.println("玩家"+playerList.get(0).getName()+"赢");
         else
             System.out.println("玩家"+playerList.get(1).getName()+"赢");
     }
    //1.输出两位玩家手中的牌
     for(int i=0;i<2;i++){
            printList(playerList.get(i));
            }
}

//输出玩家手中牌Set
public void printList(Player player){
    System.out.println(player.getName()+"的手中牌:");

    for(Poker poker:player.getPokers()){
        System.out.println(
        poker.getSuit()+poker.getId());
    }

}
public static void main(String[] args) {
    testCollections tc=new testCollections();
    tc.testAdd();

    tc.testSort();
    tc.dealPokers();

}

运行结果:
图片描述

图片描述

图片描述

图片描述

图片描述

图片描述

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