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

java第三季:扑克牌游戏

慕田峪9497165
关注TA
已关注
手记 1
粉丝 1
获赞 22

扑克牌类实现

package com.demo;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * Created by star on 16/7/19.
 */
public class Poker implements Comparable<Poker> {

    private String huase;
    private String name;

    public Poker(String huase, String name){
        this.huase = huase;
        this.name = name;
    }

    public String getHuase() {
        return huase;
    }

    public String getName() {
        return this.name;
    }

    @Override
    public int compareTo(Poker o){
        Random random=new Random();
        if(random.nextInt()%2==0) {
            return -1;
        } else{
            return 1;
        }
    }
}

玩家类实现

package com.demo;

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

/**
 * Created by star on 16/7/19.
 */
public class Player implements Comparable<Player> {

    private Integer id;
    private String name;
    public List<Poker> coures;

    public Player(Integer id, String name){
        this.id = id;
        this.name = name;
        this.coures = new ArrayList<>();
    }

    public Integer getId() {
        return id;
    }

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

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

    public String getName() {
        return this.name;
    }

    @Override
    public int compareTo(Player o) {
        return 0;
    }
}

排序规则类实现

package com.demo;

import java.util.Comparator;
/**
 * Created by star on 16/7/19.
 *数字、花色比对(越靠后的牌越大)
 */
public class Compara implements Comparator<Poker>{
    @Override
    public int compare(Poker arg0, Poker arg1) {
        String color = "黑桃 红桃 梅花 方片";
        String numbers = "2 3 4 5 6 7 8 9 10 J Q K A";
        int result = numbers.indexOf(arg0.getName()) - numbers.indexOf(arg1.getName());
        if(result < 0){
            return -1;
        }else if(result > 0){
            return 1;
        }else {
            int result2 = color.indexOf(arg0.getHuase()) - color.indexOf(arg1.getHuase());
            if(result2 < 0){
                return -1;
            }else if(result > 0){
                return 1;
            }else{
                return 0;
            }
        }
    }
}

游戏实现

package com.demo;
import java.util.*;
/**
 * Created by star on 16/7/19.
 */
public class PokerCollection {

    private List<Poker> list = new ArrayList<>();
    private Scanner console = new Scanner(System.in);
    private List<Player> player = new ArrayList<>();

    public PokerCollection(){

    }

    public List<Poker> createPoker(){
        String[] huase = {"黑桃", "红桃", "梅花", "方片"};
        String[] name = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
        for(int i = 0; i < huase.length; i++){
            for (int s = 0; s < name.length; s++ ){
                list.add(new Poker(huase[i], name[s]));
            }
        }
        return list;
    }

    public void shuffle(){
        Collections.sort(list);
    }

    public void createPlayer(int index){
        System.out.println("---------生成玩家---------");
        int i = 0;
        while(true){
            try {
                System.out.println("请输入ID(数字):");
                String iD = console.next();
                int ID = Integer.parseInt(iD);
                System.out.println("请输入姓名:");
                String name = console.next();
                player.add(new Player(ID, name));
                i++;
                if(i >= index){
                    break;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
                System.out.println("输入的不是数字 请重新输入");
                continue;
            }
        }
        System.out.println("---------玩家列表---------");
        for (Player play : player){
            Player cr = (Player) play;
            System.out.println("玩家"+cr.getId() + ":" + cr.getName());
        }
    }

    public void dealPoker(int index){
        System.out.println("---------开始发牌---------");
        int size = player.size();
        Iterator<Poker> iter = list.iterator();
        boolean flag = true;//设置跳出外层循环标志位
        try {
            for (int i = 0; i < index && flag; i++) {
                for (int k = 0; k < size; k++) {
                    if(iter.hasNext()){
                        System.out.println("玩家" + player.get(k).getName() + "开始拿牌");
                        player.get(k).coures.add(iter.next());
                    }else{
                        flag =false;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        System.out.println("------------发牌结束-----------");
        System.out.println("---------每位玩家的手牌---------");
        Iterator<Player> playerIterator = player.iterator();
        while(playerIterator.hasNext()){
            Player play = playerIterator.next();
            System.out.println("玩家"+play.getId()+":"+play.getName()+"的手牌: 总共有"+play.coures.size()+"张");
            for (Poker p : play.coures) {
                System.out.println(p.getHuase()+p.getName());
            }
        }
    }

    public void win(){
        for (Player lt : player) {
            Collections.sort(lt.coures, new Compara());
        }
        int size = player.size();//玩家数量
        int index = player.get(0).coures.size();//玩家手牌数量
        String name = "";//赢家姓名
        List<Poker> lastCards = new ArrayList<Poker>();
        for (int i = 0; i < size; i++) {
            lastCards.add(new Poker(player.get(i).coures.get(index-1).getHuase(), player.get(i).coures.get(index-1).getName()));
        }
        Collections.sort(lastCards,new Compara());
        for (int i = 0; i < size; i++) {
            if(lastCards.get(size-1).getHuase().equals(player.get(i).coures.get(index-1).getHuase()) && lastCards.get(size-1).getName().equals(player.get(i).coures.get(index-1).getName())){
                name = player.get(i).getName();
            }
        }
        if(name != ""){
            System.out.println("玩家:" + name + " 是赢家,牌面为 "+lastCards.get(size-1).getHuase()+lastCards.get(size-1).getName());
        }
    }

    public static void main(String[] args){
        PokerCollection pokers = new PokerCollection();
        System.out.println("---------创建扑克牌---------");
        pokers.createPoker();
        System.out.println("----------开始洗牌----------");
        pokers.shuffle();
        pokers.createPlayer(2);//玩家人数
        pokers.dealPoker(3);//手牌数量
        pokers.win();
    }
}
打开App,阅读手记
4人推荐
发表评论
随时随地看视频慕课网APP

热门评论

最终赢家这里的实现感觉还是臃肿,没有想到更好的解决办法。望大家给予斧正

查看全部评论