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

java 第三季 扑克牌游戏

junius
关注TA
已关注
手记 4
粉丝 0
获赞 31
package com.company.imooc_chapter_3;

import java.util.*;

import static java.util.Collections.shuffle;

/**
 * Created by junius on 2016/12/14.
 */
public class CardGame {
    private static Scanner console= new Scanner(System.in);

    public static void main(String[] args){

        startGame();
    }

    private static void startGame() {
        System.out.println("-------->开始创建扑克牌-------");
        List<Card> cc=createCards();
        System.out.println("---------扑克牌创建成功:------");
        print(cc);
        System.out.println("*********开始洗牌************");
        shuffle(cc);//直接调用Collections洗牌的方法,查单词无意见发现的
//        print(cc);//可以查看洗牌后底牌顺序
        System.out.println("*********洗牌结束************");
        System.out.println("开始创建玩家!");
        List<User> us=creatUser();
        System.out.println("开始发牌...");
        List<User> newUsers=dealCards(us,cc);
        System.out.println("发牌结束!");
        System.out.println("开始游戏");
        comparePoint(newUsers);//寻找玩家自己最大的手牌,并比较玩家手牌
        printPlayerCard(newUsers);

    }

    private static void comparePoint(List<User> newUsers) {
        List<Card> cc = new ArrayList<Card>();
        for(User c:newUsers){
            cc.addAll(c.shouPai);//将所有手牌进行重新添加
        }
        Collections.sort(cc);//将说有手牌排序

        for(User c1:newUsers) {
             for(Card ca:c1.shouPai)
                 if(ca.equals(cc.get(cc.size()-1)))//通过最大手牌来获取玩家信息
                    System.out.println("------------------玩家:"+c1.name+"WIN!---------------");
        }
    }

    private static void printPlayerCard(List<User> newUsers) {
        System.out.println("玩家各自手牌为:");
        for(User us:newUsers){
            System.out.print("玩家:"+us.name+"[");
            for(int i=0;i<us.shouPai.size();i++){
                if(i==(us.shouPai.size()-1))
                    System.out.print(us.shouPai.get(i).color+us.shouPai.get(i).point+"]");
                else
                    System.out.print(us.shouPai.get(i).color + us.shouPai.get(i).point+",");
            }
            System.out.println();
        }
    }

    private static List<User> dealCards(List<User> us, List<Card> cc) {
        System.out.print("请输入每人发牌张数!");
        int num =console.nextInt();
        int count=0;

        for(int i=0;i<us.size();i++) {
            for (int j = 0; j < num; j++) {
                System.out.println("...玩家:"+us.get(j).name+"-拿牌!");
                us.get(j).shouPai.add(cc.get(count));
                count++;
            }
        }
        return us;
    }

    /**
     * 创建玩家list
     * */
    private static List creatUser() {
        List<User> Users=new ArrayList<User>();
        Scanner console = new Scanner(System.in);
        for(int i=0;i<2;i++){
            System.out.println("请输入第"+(i+1)+"位玩家的id和姓名:");
            System.out.println("输入id:");
            String id = console.next();
            System.out.println("输入姓名");
            String name = console.next();
            Users.add(new User(id, name));
        }
        return Users;
    }

    /**
     * 创建52张扑克牌list
     * */
    private static List<Card> createCards() {
        List<Card> Cards=new ArrayList<Card>();
        String[] huase = {"", "", "", ""};//电脑输入法自带扑克牌图标,有点意思

        String[] facePoint = {"2", "3", "4", "5", "6",
                            "7", "8", "9", "10", "J",
                            "Q", "K", "A"};
        for(int i=0;i<huase.length;i++){
            for(int j=0;j<facePoint.length;j++){
                Cards.add(new Card(huase[i],facePoint[j]));//生成52张牌
            }
        }
        return Cards;
    }
    /**
     * 打印扑克牌
     * */
    private static void print(List<Card> Cards) {

        int count=1;
        for (int i = 0; i < Cards.size(); i++) {

            System.out.print(Cards.get(i).color + " " + Cards.get(i).point + ",");
            if (count%13==0) {
                System.out.println();
            }
            count++;
        }
    }

}
package com.company.imooc_chapter_3;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Created by junius on 2016/12/14.
*玩家类
 */
public class User{
//    @Override

    String id;
    String name;
    List<Card> shouPai;

    public User() {
    }

    public User(String id, String name) {
        this.id = id;
        this.name = name;
        shouPai = new ArrayList<Card>();
    }
}
package com.company.imooc_chapter_3;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by junius on 2016/12/14.
* 扑克类
 */
public class Card implements Comparable{
    String color;
    String point;
    Map<String, Integer> huaSe;
    Map<String, Integer> dianShu;

    @Override
    public boolean equals(Object obj) {
        if(obj ==null)
            return false;
        if(obj instanceof Card){
            if(((Card) obj).point==this.point&&((Card) obj).color==this.color)
                return true;
        }
        return false;
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Card){
            //如果点数不同,就比较点数
            if (dianShu.get(this.point) != dianShu.get(((Card) o).point))
                return dianShu.get(this.point) - dianShu.get(((Card) o).point);
        //如果点数相同,就比较花色
        return huaSe.get(this.color) - huaSe.get(((Card) o).color);
        }
        return 0;
    }

    public Card(){}

    public Card(String color,String point){
        this.point = point;
        this.color = color;
        huaSe = new HashMap<String, Integer>();
        dianShu = new HashMap<String, Integer>();
        huaSe.put("", 4);//通过map value来比较扑克花色和点数大小
        huaSe.put("", 3);
        huaSe.put("", 2);
        huaSe.put("", 1);

        dianShu.put("2",2);
        dianShu.put("3",3);
        dianShu.put("4",4);
        dianShu.put("5",5);
        dianShu.put("6",6);
        dianShu.put("7",7);
        dianShu.put("8",8);
        dianShu.put("9",9);
        dianShu.put("10",10);
        dianShu.put("J",11);
        dianShu.put("Q",12);
        dianShu.put("K",13);
        dianShu.put("A",14);
    }
}

2周前就写完了,就差比较的覆盖想了很久 最后的想法也就时2个小时给琢磨出来的,还有一些异常没有处理。

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