二十一点游戏模拟器(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/blackjack-game-simulator-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2614 个词 阅读量 0二十一点游戏模拟器(译文)
原文地址:https://www.codeproject.com/Articles/20176/Blackjack-Game-Simulator
原文作者:Mladen Janković
译文由本站 robot-v1.0 翻译
前言
Framework for blackjack game simulation
二十一点游戏模拟框架
- 下载演示应用程序-102.2 KB(Download demo application - 102.2 KB)
- 下载框架项目-121.0 KB(Download framework project - 121.0 KB)
- 下载完整项目-142.3 KB(Download full project - 142.3 KB)
介绍(Introduction)
该项目为二十一点游戏模拟器提供了一个框架.我不知道这对某人是否有用…嗯,对赌徒测试他们听说过的战术可能有用.我必须做一个演示,所以我一起修改了一些东西,最后,我明白了,所以留了很多改进的地方.有关21点游戏和规则的更多信息,(This project provides a framework for a blackjack game simulator. I don’t know if this will be useful to someone… Well, it could be useful to gamblers for testing tactics they have heard of. I had to do a presentation, so I hacked something together and in the end, I got this, so it leaves many places for improvement. For more information about the blackjack game and rules,) 维基百科的文章(Wikipedia’s article) 是一个不错的起点.(is a nice place to start.)
使用代码(Using the Code)
该解决方案包含五个组件:(The solution contains five components:)
BlackJack
-GUI应用(- GUI application)Common
-框架使用的通用类,枚举和接口(- Common classes, enums and interfaces used by the framework)Game
-负责二十一点游戏逻辑的组件(- Component that takes care of blackjack game logic)Player
-非常简单的电脑播放器(- Very simple computer player)HumanPlayer
-人类玩家的简单界面(- Simple interface for human players)
共同组成(Common Component)
通用组件包含:(The Common component contains:)
Deck
类-管理卡组(class - managing card deck)Hand
等级-管理牌手(class - managing player’s hand)IPlayer
接口-二十一点玩家的接口(interface - interface for blackjack players)PlayerOptions
枚举-玩家回合上的可能选项(enum - possible options on player’s turn)PlayerInGameStatus
枚举-当前玩家状态(比赛或站立状态)(enum - current player status (playing or standing))PlayerEndGameStatus
枚举-游戏结束时的玩家状态(获胜者,失败者…)(enum - player status at the end of the game (winner, loser…))IPlayer
的方法由Game组件调用,以将事件通知玩家.(’s methods are called by the Game component to notify the player about events.)
游戏组件(Game Component)
游戏组件包含:(The Game component contains:)
-
PlayerStatus
class-播放器的包装器类;诸如当前玩家的手和当前状态之类的数据存储在此类中,以防止作弊:)(class - wrapper class for player; data such as current player’s hand and current status are stored in this class to prevent cheating :)) -
Game
类-二十一点游戏逻辑与控制(class - blackjack game logic and control) -
PlayerDirectory
类-动态加载播放器的组件(class - dynamically loads the player’s components) 游戏的简化算法:(Simplified algorithm of the game:) -
开始新游戏(Start a new game)
-
洗牌(Shuffle the deck)
-
开始新一轮(Begin a new round)
-
向所有玩家发两张牌(Deal two cards to all players)
-
询问所有状态为"正在玩"的玩家是否想要另一张牌:如果有人愿意,给他们另一张牌;如果有人不将其状态更改为"站立"(Ask all players with the status “playing” if they want another card: if someone does, give them another card; if someone doesn’t, change his status to “standing”)
-
有玩家处于"正在播放"状态时,请重复步骤5(Repeat step 5 while there are players with “playing” status)
-
根据游戏规则比较玩家的手以宣布赢家和输家,更新玩家统计信息和结束游戏状态(Compare players' hands to declare winner and losers according to the rules of the game, updating player statistics and end game status)
-
洗牌,发牌并将其退回套牌末端(Shuffle, deal cards and return them to the end of the deck)
-
如果这不是最后一轮,请跳至步骤3.(If this is not the last round, jump to step 3.) 所描述的算法是在(The described algorithm is implemented in the)
Round
,(,)BeginRound
,(,)PlayerTurn
,(,)GiveCardToPlayer
和(and)EndRound
的方法(methods of the)Game
类.游戏可以通过事件进行观察((class. Game can be observed with events (members of the)Game
类):(class):) -
OnRoundEnd
-
OnGameEnd
-
OnPlayerAdded
-
OnPlayerRemoved
-
OnPlayerListCleared
-
OnDealerChange
游戏在其自己的线程中运行,并具有与GUI动画同步的方法,这些方法在回合结束时播放.在回合结束时,游戏线程调用(Game runs in its own thread and has methods for synchronization with GUI animations, which are played at the end of a round. At round end, the game thread calls)WaitForAnimation
并等待,直到GUI线程通过调用发出动画已结束的信号(and waits until the GUI thread signals that the animation is finished by calling)AnimationFinished
.方法(. Methods)StartGame
,(,)StopGame
,(,)PauseGame
和(and)ContinueGame
用于控制游戏.(are used to control the game.)
播放器的加载和实例化(Loading and Instantiation of Players)
的(The) PlayerDirectory
类的任务是加载玩家的组件并实例化玩家.在类构造函数中,来自(class has the tasks of loading players' components and instantiation of players. In the class constructor, all assemblies from the)**.\播放器(.\Players)**如果包含玩家的职业,则检查并加载该文件夹.玩家类别的名称必须为(folder are checked and loaded if containing a player’s class. The name of a player class must be) MyPlayer
并且必须在(and must be in the) Player
命名空间.(namespace.)
可以通过调用其他程序集来添加(Other assemblies can be added by calling the) LoadPlayer
方法并指定组装路径.要创建播放器实例,只需调用(method and specifying the path to assembly. To create an instance of a player, just call) CreateInstanceOfPlayer
,根据需要指定程序集名称和构造函数参数.(, specifying the assembly name and constructor parameters as needed.)
playerDirectory.CreateInstanceOfPlayer("PlayerWithNameAndAge",
new object[]{ "Someone", 30 } );
简单播放器(Simple Player)
具有简单逻辑的简单玩家:(A simple player with simple logic:)
- “如果我有二十一点,我会站起来的”(“If I have blackjack, I’ll stand”)
- “如果我有21岁,我会站起来”(“If I have 21, I’ll stand”)
- “如果大于或等于17,我会坚持下去”(“If have more than or equal to 17, I’ll stand”)
- “如果我少于17岁,我会再选一个”(“If I have less than 17, I’ll take another”) …可以用几行代码实现.(…can be implemented in a few lines of code.)
using Common;
namespace Player
{
public class MyPlayer : IPlayer
{
private Hand _myHand = new Hand();
private string _name;
public string Name { get { return _name; } }
public void NewRound(bool newGame,
bool isDealer, int dealersFirstCard, int numberOfDecks)
{ _myHand.Restart(); }
public PlayerOptions Turn(int left)
{
if (_myHand.IsBlackJack || _myHand.Values.Contains(21))
return PlayerOptions.Stand;
return _myHand.Values[0] <=
17 ? PlayerOptions.Hit : PlayerOptions.Stand;
}
public void GiveCard(int card) { _myHand.Add(card); }
public void EndRound(PlayerEndGameStatus status,
List<int> dealedCards) { }
public MyPlayer(string name) { _name = name; }
public MyPlayer() { _name = "strange name " +
GetHashCode().ToString(); }
}
}
框架项目(Framework Project)
框架项目提供了一种通过完整设置环境和项目来测试玩家逻辑的简便方法.所有不需要的源代码已被剥离.(The framework project provides an easy way of testing a player’s logic with complete setup of environment and project. All unneeded source code has been stripped.)
附加信息(Additional Information)
就像我说的那样,还有很多改进的余地(即卡片应该按类而不是整数显示,可以改进动画代码).另外,一些二十一点规则也没有实现,例如玩家的split选项.(Like I said, there’s room for many improvements (i.e. cards should be presented by class not integer number, animation code can be improved). Also, some rules from blackjack are not implemented, like the split option for players.)
兴趣点(Points of Interest)
好吧,我的大学在培养更聪明的球员方面获得了一些乐趣,并且参加了一些比赛.希望您也喜欢.(Well, my college has had some fun making smarter players and had a little competition. I hope you will enjoy it, too.)
历史(History)
- 2007年8月24日-发布原始版本(24 August, 2007 – Original version posted)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# C#2.0 Windows .NET .NET2.0 Visual-Studio Design Dev 新闻 翻译