简易Fgf游戏

编程入门 行业动态 更新时间:2024-10-18 16:52:41

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

简易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;}@Overridepublic 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();// 通过迭代器循环KeySetwhile (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() {// 把当前轮次+1setRound(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 + Fpublic 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);}@Overridepublic String toString() {String rs = this.getType() + this.getPoint();if (rs.length() == 2) rs = 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;}/***************************** * 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,删除setCardsTypeprivate 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;}}}}@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);}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;}@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;}// 比较乱金牌:从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());}@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 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);}}@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);}}}

更多推荐

简易Fgf游戏

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

发布评论

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

>www.elefans.com

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