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);
}
}
}
package com;
/**
* 遊戲全域介面
* @author lzq31
*
*/
public interface Game {
String PLATFORM_NAME = "青鳥遊戲大廳";
String PLATFORM_VERSION = "1.0.1";
String FGF_NAME = "炸金花";
}
package com.card;
/**
* 所有牌的父類
* @author lzq31
*
*/
public class Card {
private String cardName;
public String getCardName() {
return cardName;
}
public void setCardName(String cardName) {
this.cardName = cardName;
}
}
package com.card;
/**
* 牌類遊戲的規則介面
* @author lzq31
*
*/
public interface CardGameRuleService {
/**
* 雙方手牌的大小比較方法
* @return
*/
int rule(CardHand cardHand1, CardHand cardHand2);
}
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;
}
}
package com.card;
/**
* 牌類遊戲的手牌超級父介面,只實現需要完成比較的介面定義
* @author lzq31
*
*/
public interface CardHand extends Comparable<CardHand>{
}
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 = { "", "", "", "" }; // 所有花se數組
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>(); // 花se權值
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); // 給花se賦值
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) {
// 先比點數,點數權值大則,小則小,相同點數,比花se權值
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);
}
}
}
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