简易的FGF游戏代码

编程入门 行业动态 更新时间:2024-10-18 10:21:28

<a href=https://www.elefans.com/category/jswz/34/1769136.html style=简易的FGF游戏代码"/>

简易的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;}@Overridepublic 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);}@Overridepublic String toString() {String rs = this.type + this.point;if (rs.length() == 2)rs += " ";return rs;}@Overridepublic 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();}@Overridepublic 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);}@Overridepublic 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;}@Overridepublic 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());}@Overridepublic 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);}@Overridepublic 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);}}@Overridepublic 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();}}
}

更多推荐

简易的FGF游戏代码

本文发布于:2024-03-05 16:03:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1712746.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:简易   代码   游戏   FGF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!