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

简易扑克牌游戏-Java第三季结束

zyj9527
关注TA
已关注
手记 2
粉丝 0
获赞 20

扑克牌类:

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

public class Poker  implements Comparable<Poker>{
        //静态变量
        final public static String poker_number [] = {"2","3","4","5","6","7","8","9","10","J","Q","K","A",};
        final public static String poker_pattarn [] = {"方块", "梅花", "红桃", "黑桃"};

        String number;      //数字
        String pattarn;       //花色

        public Poker (String number, String pattarn) {
                this.number = number;
                this.pattarn = pattarn;
        }
        public String getNumber() {
                return number;
        }
        public String getPattarn() {
                return pattarn;
        }
        //获取下标
        int getIndex (String array[], String string) {
                List <String> list = new ArrayList<String> ();
                for (String str : array) {
                        list.add(str);
                }
                return list.indexOf(string);
        }
        @Override
        //比较大小,先比数字,再比花色
        public int compareTo(Poker o) {
                if (getIndex(poker_number, this.number) > getIndex(poker_number, o.getNumber()))
                        return 1;
                else if (getIndex(poker_number, this.number) < getIndex(poker_number, o.getNumber()))
                        return -1;
                else {
                        if (getIndex(poker_number, this.pattarn) > getIndex(poker_number, o.getPattarn()))
                                return 1;
                        else if (getIndex(poker_number, this.pattarn) < getIndex(poker_number, o.getPattarn()))
                                return -1;
                        else
                                return 0;
                }
        }
}

玩家类:

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

public class Player {
        int id;                           //玩家ID
        String name;                //玩家姓名
        List <Poker> pokers;    //玩家手中的扑克牌
        public Player (int id, String name) {
                this.id = id;
                this.name = name;
                pokers = new ArrayList <Poker> ();
        }
        public List<Poker> getPokers() {
                return pokers;
        }
        public void addPokers(Poker poker) {
                this.pokers.add(new Poker (poker.getNumber(), poker.getPattarn()));
        }
        public int getId() {
                return id;
        }
        public String getName() {
                return name;
        }
}

扑克牌游戏类:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

public class PokerGame {
        List <Poker> pokers;                  //牌堆
        Map<String, Player> players;    // 玩家
        Scanner input;

        //扑克牌初始化
        public PokerGame () {
                pokers = new ArrayList <Poker> ();
                players = new HashMap <String, Player> ();
                System.out.println ("----------创建扑克牌----------");
                for (int i = 0; i < Poker.poker_pattarn.length; i++) {
                        for (int j = 0; j < Poker.poker_number.length; j ++) {
                                pokers.add(new Poker (Poker.poker_number[j], Poker.poker_pattarn[i]));
                        }
                }
                System.out.println ("----------扑克牌创建成功----------");
                showPokers();
        }
        //扑克牌显示
        public void showPokers () {
                String current_pokers [] = new String [Poker.poker_number.length * Poker.poker_pattarn.length ];
                for (int i = 0; i < pokers.size(); i ++) {
                        String poker = pokers.get(i).getPattarn() + pokers.get(i).getNumber();
                        current_pokers[i] = poker;
                }
                System.out.println ("当前扑克牌为:" + Arrays.toString(current_pokers));
        }
        //洗牌
        public void shufflePokers () {
                Collections.shuffle(pokers);
                System.out.println ("----------扑克牌洗牌成功----------");
                showPokers();
        }
        //玩家初始化
        public void createPlayers () {
                int id;
                String name;
                System.out.println ("----------创建玩家----------");
                System.out.println ("请输入第"+(players.size()+1)+"位玩家信息:");      
                while (true) {
                        input = new Scanner(System.in);
                        System.out.println ("请输入ID:");
                        try {
                                id = input.nextInt();
                        } catch (Exception e) {
                                System.out.println ("请输入整形类型的ID:");
                                continue;
                        }
                        if (players.containsKey(Integer.toString(id))){
                                System.out.println ("玩家ID重复!");
                                continue;
                        }
                        break;
                }
                System.out.println ("请输入姓名:");
                name = input.next();
                players.put(Integer.toString(id), new Player (id ,name));
        }
        //显示玩家信息
        public void showPlayers () {
                System.out.println ("----------当前玩家信息----------");
                for (Map.Entry<String, Player> entry : players.entrySet()) {
                        System.out.println ("ID:"+entry.getKey() + " 姓名:" + entry.getValue().getName());
                }
        }
        //发牌
        public void dealPokers () {
                System.out.println ("----------开始发牌----------");
                for (int i = 0; i < 2; i++) {
                        for (Map.Entry<String, Player> entry : players.entrySet()) {
                                entry.getValue().addPokers(pokers.get(0));
                                pokers.remove(0);
                                System.out.println ("玩家 " + entry.getValue().getName() + " 拿牌");
                        }
                }
                System.out.println ("----------发牌结束----------");
        }
        //开始游戏
         public void startGame () {
                 System.out.println ("----------开始游戏----------");
                 Player Max = null;
                 for (Map.Entry<String, Player> player : players.entrySet()) {
                        Collections.sort(player.getValue().getPokers());
                        int size = player.getValue().getPokers().size();
                        Poker maxPoker = player.getValue().getPokers().get(size - 1);
                        System.out.println (player.getValue().getName()+"最大的手牌为" + maxPoker.getPattarn() + maxPoker.getNumber());
                        if (Max == null)
                                Max = player.getValue();
                        else {
                                if (maxPoker.compareTo(Max.getPokers().get(size - 1)) > 0)
                                        Max = player.getValue();
                        }
                 }
                 System.out.println ("----------玩家"+ Max.getName() +"获胜----------");

         }

         //遍历手牌
         public void showPokersOfPlayers () {
                 System.out.println ("玩家各自的手牌为:");
                 for (Map.Entry<String, Player> player : players.entrySet()) {
                         int size = player.getValue().getPokers().size();
                        String arrayPoker[] = new String [size];
                        for (int i = 0; i < size; i++) {
                                arrayPoker[i] = player.getValue().getPokers().get(i).getPattarn() + player.getValue().getPokers().get(i).getNumber();                                
                        }
                        System.out.println (player.getValue().getName() + ":" + Arrays.toString(arrayPoker));
                 }
         }
}

主函数:

public class Initial {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                PokerGame pokergame = new PokerGame();
                pokergame.shufflePokers();      //洗牌
                pokergame.createPlayers();      //创建一个玩家
                pokergame.createPlayers();      //创建一个玩家
                pokergame.showPlayers();        //显示玩家信息
                pokergame.dealPokers();          //发牌
                pokergame.startGame();           //开始游戏
                pokergame.showPokersOfPlayers(); //显示玩家手牌
        }

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

热门评论

这个比较是方法确实好!

查看全部评论