简易Fgf游戏

Game.java

package com;

/**
 * 游戏全局接口
 * @author lzq31
 *
 */
public interface Game {
    String PLATFORM_NAME = "青鸟游戏大厅";
    String PLATFORM_VERSION = "1.0.1";

    String FGF_NAME = "炸金花";
}

Player.java

package com;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 玩家基本信息类
 * @author lzq31
 *
 */
public class Player {
    /**
     * 定义一个玩家信息数据库
     */
    public static Map<String, Player> PLAYERS = new HashMap<String, Player>();
    static {
        for (int i = 0; i < 20; i++) {
            int num = i + 1;
            String numCode = num < 10 ? "0" + num : num + "";
            String playerName = "PLAYER" + numCode;
            PLAYERS.put(playerName, new Player(playerName));
        }
    }

    private String playerName; // 玩家的账号
    private int score; // 玩家的积分
    private List<String> gameLogs = new ArrayList<String>(); // 玩家的游戏日志
    public String getPlayerName() {
        return playerName;
    }
    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public List<String> getGameLogs() {
        return gameLogs;
    }
    public void setGameLogs(List<String> gameLogs) {
        this.gameLogs = gameLogs;
    }

    public Player() {
        super();
    }

    /**
     * 根据账号名生成一个玩家对象
     * @param playerName
     */
    public Player(String playerName) {
        super();
        this.playerName = playerName;
    }

    @Override
    public String toString() {
        return this.playerName;
    }

    // shift + alt + s 唤醒Source菜单
    // ctrl + shift + o 全部引入需要导出的包

    public static void main(String[] args) {
        // 获取Map中的KeySet对象
        Set<String> keySet = Player.PLAYERS.keySet();
        // 创建KeySet的迭代器
        Iterator<String> it = keySet.iterator();
        // 通过迭代器循环KeySet
        while (it.hasNext()) {
            // 获取Key值
            String key = it.next();
            // 通过Map.get(key)方法获取对象key的value值
            Player value = Player.PLAYERS.get(key);
            // 打印输出对象,默认会调用对象的toString()方法
            System.out.println(value);
        }
    }
}

Card.java

package com.card;

/**
 * 所有牌的父类
 * @author lzq31
 *
 */
public class Card {
    private String cardName;

    public String getCardName() {
        return cardName;
    }

    public void setCardName(String cardName) {
        this.cardName = cardName;
    }


}

CardGameRuleService.java

package com.card;

/**
 * 牌类游戏的规则接口
 * @author lzq31
 *
 */
public interface CardGameRuleService {
    /**
     * 双方手牌的大小比较方法
     * @return
     */
    int rule(CardHand cardHand1, CardHand cardHand2);
}

CardGameService.java

package com.card;

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

/**
 * 牌类游戏的超级父类
 * @author lzq31
 *
 */
public abstract class CardGameService {

    /**
     * 一副牌
     */
    public List<Card> cards;

    /**
     * 所有玩家的手牌集合
     */
    public List<CardHand> playersCards;

    /**
     * 游戏名称
     */
    private String gameName;
    /**
     * 玩家数量
     */
    private int playerNums;
    /**
     * 房间号
     */
    private String roomCode;
    /**
     * 当前房间的游戏回合
     */
    private int round;

    /**
     * 洗牌方法
     */
    public void shuffle() {
        // 把当前轮次+1
        setRound(getRound() + 1);
        // 初始化牌
        cards = new ArrayList<Card>();
        // 初始化玩家牌
        playersCards = new ArrayList<CardHand>();
    }

    /**
     * 发牌方法
     */
    public abstract void deal();

    /**
     * 关闭牌局方法
     */
    public abstract void close();

    /**************************** Getters and Setters**************************************/

    public List<Card> getCards() {
        return cards;
    }

    public void setCards(List<Card> cards) {
        this.cards = cards;
    }

    public List<CardHand> getPlayersCards() {
        return playersCards;
    }

    public void setPlayersCards(List<CardHand> playersCards) {
        this.playersCards = playersCards;
    }

    public String getGameName() {
        return gameName;
    }

    public void setGameName(String gameName) {
        this.gameName = gameName;
    }

    public int getPlayerNums() {
        return playerNums;
    }

    public void setPlayerNums(int playerNums) {
        this.playerNums = playerNums;
    }

    public String getRoomCode() {
        return roomCode;
    }

    public void setRoomCode(String roomCode) {
        this.roomCode = roomCode;
    }

    public int getRound() {
        return round;
    }

    public void setRound(int round) {
        this.round = round;
    }

}

CardHand.java

package com.card;

/**
 * 牌类游戏的手牌超级父接口,只实现需要完成比较的接口定义
 * @author lzq31
 *
 */
public interface CardHand extends Comparable<CardHand>{

}

Poker.java

package com.card;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定义扑克牌类 要实现Comparable接口,让扑克牌具备可比的规则功能
 * 
 * @author lzq31
 *
 */
public class Poker extends Card implements Comparable<Poker> {

    // Ctrl + shift + F
    public static String[] TYPES = { "♦", "♣", "♥", "♠" }; // 所有花色数组
    public static String[] POINTS = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" }; // 所有点数数组

    public static Map<String, Integer> TYPES_VALUES = new HashMap<String, Integer>(); // 花色权值
    public static Map<String, Integer> POINTS_VALUES = new HashMap<String, Integer>(); // 点数权值

    static {
        TYPES_VALUES.put("♦", 1);

        TYPES_VALUES.put("♣", 2);
        TYPES_VALUES.put("♥", 3);
        TYPES_VALUES.put("♠", 4);

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

    }

    /**
     * 花色
     */
    private String type;
    /**
     * 牌点
     */
    private String point;

    /**
     * ♠10
     * 
     * @param tp
     */
    public Poker(String tp) {
        this.type = tp.substring(0, 1); // 给花色赋值
        this.point = tp.substring(1); // 给点数赋值
    }

    /**
     * 获取花色权值
     * 
     * @return
     */
    public int getTypeValue() {
        return TYPES_VALUES.get(this.type);
    }

    /**
     * 获取点数权值
     * 
     * @return
     */
    public int getPointValue() {
        return POINTS_VALUES.get(this.point);
    }

    @Override
    public String toString() {
        String rs = this.getType() + this.getPoint();
        if (rs.length() == 2) rs = rs + " ";
        return rs;
    }

    @Override
    public int compareTo(Poker o) {
        // 先比点数,点数权值大则,小则小,相同点数,比花色权值
        if (o.getPointValue() > this.getPointValue())
            return -1;
        if (o.getPointValue() < this.getPointValue())
            return 1;

        if (o.getTypeValue() > this.getTypeValue())
            return -1;
        if (o.getTypeValue() < this.getTypeValue())
            return 1;

        return 0;
    }

    /****************************
     * * Getters and Setters
     **************************************/
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPoint() {
        return point;
    }

    public void setPoint(String point) {
        this.point = point;
    }

    public static void main(String[] args) {
        List<Poker> ps = new ArrayList<Poker>();

        for (int i = 0; i < 20; i++) {
            int j = (int) (Math.random() * 4);
            int k = (int) (Math.random() * 13);
            String tp = TYPES[j] + POINTS[k];
            ps.add(new Poker(tp));
        }
        Collections.sort(ps);
        for (Poker p : ps) {
            System.out.println(p);
        }

    }

}

Client

package com.card.client;

import com.card.fgf.FgfGameService;

public class Client {
    public static void main(String[] args) {
        FgfGameService gfgService = new FgfGameService(5);

        for (int i = 1; i <= 4; i++) {
            // 洗牌
            gfgService.shuffle();
            // 发牌
            gfgService.deal();
            // 结算
            gfgService.close();
        }

    }
}

FgfCardHand.java

package com.card.fgf;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.Player;
import com.card.CardHand;
import com.card.Poker;

/**
 * 炸金花手牌信息类
 * 
 * @author lzq31
 *
 */
public class FgfCardHand implements CardHand {

    public static Map<Integer, String> HAND_TYPES = new HashMap<Integer, String>();
    /**
     * 单散牌
     */
    public static int HAND_TYPE_VALUE_DSP = 1;
    /**
     * 对子牌
     */
    public static int HAND_TYPE_VALUE_DZP = 2;
    /**
     * 乱顺牌
     */
    public static int HAND_TYPE_VALUE_LSP = 3;
    /**
     * 乱金牌
     */
    public static int HAND_TYPE_VALUE_LJP = 4;
    /**
     * 顺金牌
     */
    public static int HAND_TYPE_VALUE_SJP = 5;
    /**
     * 豹子牌
     */
    public static int HAND_TYPE_VALUE_BZP = 6;

    static {
        HAND_TYPES.put(1, "单散牌");
        HAND_TYPES.put(2, "对子牌");
        HAND_TYPES.put(3, "乱顺牌");
        HAND_TYPES.put(4, "乱金牌");
        HAND_TYPES.put(5, "顺金牌");
        HAND_TYPES.put(6, "豹子牌");
    }

    private String playerName; // 玩家账号
    private Poker poker1;
    private Poker poker2;
    private Poker poker3;
    // 通过在构造器中调用getCardsTypeFun赋值,暴露getCardsType,删除setCardsType
    private int cardsType; // 牌型 比如豹子、金花等等

    // 构造器:将三种随机发的牌进行排序后赋给当前类的三个牌实例变量
    public FgfCardHand(String playerName, Poker poker1, Poker poker2, Poker poker3) {
        super();
        this.playerName = playerName;

        List<Poker> pokers = new ArrayList<Poker>();
        pokers.add(poker1);
        pokers.add(poker2);
        pokers.add(poker3);
        Collections.sort(pokers);

        this.poker1 = pokers.get(0);
        this.poker2 = pokers.get(1);
        this.poker3 = pokers.get(2);

        this.cardsType = getCardsTypeFun();
    }

    /**
     * 获取当前对象牌型的方法
     * 
     * @return
     */
    public int getCardsTypeFun() {
        // 1:单张牌 2:对子牌3:乱顺牌4:乱金牌5:顺金牌6:豹子牌
        // 判断是否是豹子牌型
        if (this.poker1.getPointValue() == this.poker2.getPointValue()
                && this.poker2.getPointValue() == this.poker3.getPointValue()) {

            return HAND_TYPE_VALUE_BZP;
        }
        // 判断是否是金牌
        if (this.poker1.getTypeValue() == this.poker2.getTypeValue()
                && this.poker2.getTypeValue() == this.poker3.getTypeValue()) {
            // 是否是顺金
            if (this.poker3.getPointValue() - this.poker2.getPointValue() == 1
                    && this.poker2.getPointValue() - this.poker1.getPointValue() == 1) {
                return HAND_TYPE_VALUE_SJP;
            }
            // 乱金牌
            else {
                return HAND_TYPE_VALUE_LJP;
            }
        } else {
            // 是否是乱顺牌
            if (this.poker3.getPointValue() - this.poker2.getPointValue() == 1
                    && this.poker2.getPointValue() - this.poker1.getPointValue() == 1) {
                return HAND_TYPE_VALUE_LSP;
            } else {
                // 是否是对子牌
                if (this.poker1.getPointValue() == this.poker2.getPointValue()
                        || this.poker2.getPointValue() == this.poker3.getPointValue()
                        || this.poker1.getPointValue() == this.poker3.getPointValue()) {
                    return HAND_TYPE_VALUE_DZP;
                }
                // 其他的都是单张牌
                else {
                    return HAND_TYPE_VALUE_DSP;
                }
            }
        }
    }

    @Override
    public String toString() {

        int score = Player.PLAYERS.get(this.playerName).getScore();

        return this.playerName + " " + HAND_TYPES.get(this.cardsType) + "[" + this.poker1 + ", " + this.poker2 + ", "
                + this.poker3 + "]\t当前积分:" + score ;
    }

    @Override
    public int compareTo(CardHand o) {
        FgfCardHand cardHand = (FgfCardHand) o;
        return FgfGameRuleService.getInstance().rule(cardHand, this);
    }

    public String getPlayerName() {
        return playerName;
    }

    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }

    public Poker getPoker1() {
        return poker1;
    }

    public void setPoker1(Poker poker1) {
        this.poker1 = poker1;
    }

    public Poker getPoker2() {
        return poker2;
    }

    public void setPoker2(Poker poker2) {
        this.poker2 = poker2;
    }

    public Poker getPoker3() {
        return poker3;
    }

    public void setPoker3(Poker poker3) {
        this.poker3 = poker3;
    }

    public int getCardsType() {
        return cardsType;
    }

    public static void main(String[] args) {

        List<FgfCardHand> list = new ArrayList<FgfCardHand>();

        for (int i = 0; i < 10; i++) {
            String playerName = "PLAYER" + i;

            int j = (int) (Math.random() * 4);
            int k = (int) (Math.random() * 13);
            String tp = Poker.TYPES[j] + Poker.POINTS[k];
            Poker poker1 = new Poker(tp);

            j = (int) (Math.random() * 4);
            k = (int) (Math.random() * 13);
            tp = Poker.TYPES[j] + Poker.POINTS[k];
            Poker poker2 = new Poker(tp);

            j = (int) (Math.random() * 4);
            k = (int) (Math.random() * 13);
            tp = Poker.TYPES[j] + Poker.POINTS[k];
            Poker poker3 = new Poker(tp);

            FgfCardHand cardHand = new FgfCardHand(playerName, poker1, poker2, poker3);
            list.add(cardHand);

        }

        Collections.sort(list);

        for (FgfCardHand ch : list) {
            System.out.println(ch);
        }
    }

}

FgfGameRuleService.java

package com.card.fgf;

import com.card.CardGameRuleService;
import com.card.CardHand;

/**
 * 炸金花规则服务
 * @author lzq31
 *
 */
public class FgfGameRuleService implements CardGameRuleService {

    // 定义一个单态的服务对象
    private static FgfGameRuleService instance = new FgfGameRuleService();

    private FgfGameRuleService(){}

    public static FgfGameRuleService getInstance() {
        return instance;
    }

    @Override
    public int rule(CardHand cardHand1, CardHand cardHand2) {
        FgfCardHand fgf1 = (FgfCardHand) cardHand1;
        FgfCardHand fgf2 = (FgfCardHand) cardHand2;

        // 首先判断牌型
        if (fgf1.getCardsType() > fgf2.getCardsType()) return 1;
        if (fgf1.getCardsType() < fgf2.getCardsType()) return -1;

        // 如果牌型相同

        // 比较豹子牌:只要看任意一张牌的点数就可以
        if (fgf1.getCardsType() == FgfCardHand.HAND_TYPE_VALUE_BZP) {
            if (fgf1.getPoker1().getPointValue() > fgf2.getPoker1().getPointValue()) return 1;
            if (fgf1.getPoker1().getPointValue() < fgf2.getPoker1().getPointValue()) return -1;
        }

        // 比较顺金牌:比较最大的那种牌的点数
        if (fgf1.getCardsType() == FgfCardHand.HAND_TYPE_VALUE_SJP) {
            if (fgf1.getPoker3().getPointValue() > fgf2.getPoker3().getPointValue()) return 1;
            if (fgf1.getPoker3().getPointValue() < fgf2.getPoker3().getPointValue()) return -1;
        }

        // 比较乱金牌:从3-1分别比较
        if (fgf1.getCardsType() == FgfCardHand.HAND_TYPE_VALUE_LJP) {
            if (fgf1.getPoker3().getPointValue() > fgf2.getPoker3().getPointValue()) return 1;
            if (fgf1.getPoker3().getPointValue() < fgf2.getPoker3().getPointValue()) return -1;

            if (fgf1.getPoker2().getPointValue() > fgf2.getPoker2().getPointValue()) return 1;
            if (fgf1.getPoker2().getPointValue() < fgf2.getPoker2().getPointValue()) return -1;

            if (fgf1.getPoker1().getPointValue() > fgf2.getPoker1().getPointValue()) return 1;
            if (fgf1.getPoker1().getPointValue() < fgf2.getPoker1().getPointValue()) return -1;
        }

        // 比较乱顺牌:比较最大的那种牌的点数
        if (fgf1.getCardsType() == FgfCardHand.HAND_TYPE_VALUE_LSP) {
            if (fgf1.getPoker3().getPointValue() > fgf2.getPoker3().getPointValue()) return 1;
            if (fgf1.getPoker3().getPointValue() < fgf2.getPoker3().getPointValue()) return -1;
        }

        // 比较对子牌:获得对子的牌点,然后如果对子牌点一样,要比较单张牌点
        if (fgf1.getCardsType() == FgfCardHand.HAND_TYPE_VALUE_DZP) {
            if (fgf1.getPoker2().getPointValue() > fgf2.getPoker2().getPointValue()) return 1;
            if (fgf1.getPoker2().getPointValue() < fgf2.getPoker2().getPointValue()) return -1;
            int fgf1Dp = 0;
            int fgf2Dp = 0;

            if (fgf1.getPoker1().getPointValue() == fgf1.getPoker2().getPointValue()) {
                fgf1Dp = fgf1.getPoker3().getPointValue();
            } else {
                fgf1Dp = fgf1.getPoker1().getPointValue();
            }

            if (fgf2.getPoker1().getPointValue() == fgf2.getPoker2().getPointValue()) {
                fgf2Dp = fgf2.getPoker3().getPointValue();
            } else {
                fgf2Dp = fgf2.getPoker1().getPointValue();
            }

            if (fgf1Dp > fgf2Dp) return 1;
            if (fgf1Dp < fgf2Dp) return -1;
        }

        // 比较单顺牌:从3-1分别比较
        if (fgf1.getCardsType() == FgfCardHand.HAND_TYPE_VALUE_DSP) {
            if (fgf1.getPoker3().getPointValue() > fgf2.getPoker3().getPointValue()) return 1;
            if (fgf1.getPoker3().getPointValue() < fgf2.getPoker3().getPointValue()) return -1;

            if (fgf1.getPoker2().getPointValue() > fgf2.getPoker2().getPointValue()) return 1;
            if (fgf1.getPoker2().getPointValue() < fgf2.getPoker2().getPointValue()) return -1;

            if (fgf1.getPoker1().getPointValue() > fgf2.getPoker1().getPointValue()) return 1;
            if (fgf1.getPoker1().getPointValue() < fgf2.getPoker1().getPointValue()) return -1;
        }

        return 0;
    }

}

FgfGameService.java

package com.card.fgf;

import java.util.Collections;

import com.Game;
import com.Player;
import com.card.CardGameService;
import com.card.CardHand;
import com.card.Poker;

public class FgfGameService extends CardGameService implements Game {

    public FgfGameService(int playerNums) {
        // 定义房间的名称
        String roomCode = "FGF" + System.currentTimeMillis();
        this.setRoomCode(roomCode);
        this.setPlayerNums(playerNums);
        System.out.println("===========" + PLATFORM_NAME + "(" + PLATFORM_VERSION + ")" + "-" + FGF_NAME + "=========");
        System.out.println("房间号:" + this.getRoomCode() + " 玩家数量:" + this.getPlayerNums());
    }

    @Override
    public void shuffle() {
        super.shuffle();
        System.out.println("\n\n" + "============轮次(" + this.getRound() + ") 洗牌中==========");
        // 装牌
        for (String type : Poker.TYPES) {
            for (String point : Poker.POINTS) {
                String tp = type + point;
                Poker poker = new Poker(tp);
                cards.add(poker);
            }
        }
        // 洗牌
        Collections.shuffle(cards);
    }

    @Override
    public void deal() {
        int k = 0;
        for (int i = 1; i <= this.getPlayerNums(); i++) {
            String pn = i < 10 ? "0" + i : i + "";
            String playerName = "PLAYER" + pn;
            Poker poker1 = null;
            Poker poker2 = null;
            Poker poker3 = null;

            for (int j = 0; j < 3; j++) {
                if (j == 0) {
                    poker1 = (Poker) this.cards.get(k);
                }
                if (j == 1) {
                    poker2 = (Poker) this.cards.get(k);
                }
                if (j == 2) {
                    poker3 = (Poker) this.cards.get(k);
                }
                k++;
            }

            FgfCardHand cardHand = new FgfCardHand(playerName, poker1, poker2, poker3);
            playersCards.add(cardHand);
        }

        // 打印玩家手牌信息
        for (CardHand hand : playersCards) {
            FgfCardHand fgfHand = (FgfCardHand) hand;
            System.out.println(fgfHand);
        }
    }

    @Override
    public void close() {
        System.out.println("\n\n" + "============轮次(" + this.getRound() + ") 结算==========");
        Collections.sort(this.playersCards);
        // 打印结算完成后的玩家手牌信息
        int i = 0;
        for (CardHand hand : playersCards) {
            FgfCardHand fgfHand = (FgfCardHand) hand;

            int score = playersCards.size() - i++;

            // 取出当前账号的积分
            int currentScore = Player.PLAYERS.get(fgfHand.getPlayerName()).getScore();
            currentScore += score;
            Player.PLAYERS.get(fgfHand.getPlayerName()).setScore(currentScore);

            System.out.println(fgfHand);
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值