簡易的FGF遊戲代碼

Game.java

package com;
/**
 * 遊戲的全局接口
 * @author nt
 */
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 nt
 */
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 playeName;
    // 玩家的積分
    private int score;
    // 玩家的遊戲日誌
    private List<String> gameLogs = new ArrayList<String>();

    public String getPlayeName() {
        return playeName;
    }
    public void setPlayeName(String playeName) {
        this.playeName = playeName;
    }
    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 playeName
     */
    public Player(String playeName) {
        super();
        this.playeName = playeName;
    }

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

Card.java

package com.card;
/**
 * 所有牌的父類
 * @author nt
 */
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 nt
 */
public interface CardGameRuleService {
    /**
     * 雙方手牌的大小比較方法
     * @param cardHand1
     * @param cardHand2
     * @return
     */
    int rule(CardHand cardHand1, CardHand cardHand2);
}

CardGameService.java

package com.card;

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

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() {
        setRound(getRound() + 1);
        cards = new ArrayList<Card>();
        playersCards = new ArrayList<CardHand>();
    }

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

    /**
     * 關閉牌局
     */
    public abstract void close();

    /**
     * Getter & Setter
     * @return
     */
    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 nt
 */
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 nt
 */
public class Poker extends Card implements Comparable<Poker> {
    // 所有花色數組
    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 {
        for (int i = 0; i < TYPES.length; i++) {
            TYPES_VALUES.put(TYPES[i], i + 1);
        }
        for (int i = 0; i < POINTS.length; i++) {
            POINTS_VALUES.put(POINTS[i], i + 2);
        }
    }
    // 花色
    private String type;
    // 牌點
    private String point;

    public Poker(String tp) {
        // 給花色賦值
        this.type = tp.substring(0, 1);
        // 給點數賦值
        this.point = tp.substring(1);
    }

    @Override
    public String toString() {
        String rs = this.type + this.point;
        if (rs.length() == 2)
            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;
    }

    /**
     * Getter & Setter
     * @return
     */
    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 Integer getTypeValue() {
        return TYPES_VALUES.get(this.type);
    }
    // 獲取點數權值
    public Integer getPointValue() {
        return POINTS_VALUES.get(this.point);
    }
}

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;

public class FgfCardHand implements CardHand {
    public static Map<Integer, String> HAND_TYPES = new HashMap<Integer, String>();
    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, "豹子牌");
    }

    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;

    private String playerName;
    private Poker poker1;
    private Poker poker2;
    private Poker poker3;
    private int cardsType;

    public FgfCardHand() {
        super();
    }

    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 = getCardsType();
    }

    @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);
    }

    /**
     * Getter & Setter
     * @return
     */
    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() {
        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;
            }
        }       
        if ((this.poker3.getPointValue() - this.poker2.getPointValue()) == 1
                && (this.poker2.getPointValue() - this.poker1.getPointValue()) == 1) {
            return HAND_TYPE_VALUE_LSP;
        }       
        if (this.poker1.getPointValue() == this.poker2.getPointValue()
                || this.poker2.getPointValue() == this.poker3.getPointValue()) {
            return HAND_TYPE_VALUE_DZP;
        }       
        return HAND_TYPE_VALUE_DSP;
    }   
}

FgfGameRuleService

package com.card.fgf;

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

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;
        }

        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;
        }

        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 com.Game;
import com.Player;
import com.card.CardGameService;
import com.card.CardHand;
import com.card.Poker;
import java.util.Collections;

public class FgfGameService extends CardGameService implements Game {
    public FgfGameService(int playerNums) {
        String room = "FGF" + System.currentTimeMillis();
        this.setRoomCode(room);
        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 playerName = "PLAYER" + (i < 10 ? ("0" + i) : (i + ""));

            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);
        }
    }
}

Client.java

package com.card.client;

import com.card.fgf.FgfGameService;

public class Client {
    public static void main(String[] args) {
        FgfGameService fgfService = new FgfGameService(5);
        for (int i = 0; i < 10; i++) {
            fgfService.shuffle();
            fgfService.deal();
            fgfService.close();
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章