- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在编写一个游戏内货币系统的小型模型,它或多或少类似于龙与地下城货币系统或 EverquestII 货币系统。基本上我有四种不同类型的货币:铜、银、金和白金。该系统以 100 的汇率完成,因此:
1 Platinum = 100 Gold.
1 Gold = 100 Silver.
1 Silver = 100 Copper.
但问题是,我不太清楚如何将这些钱相互转换,我的意思是,我无法思考如何将 12 个铂金币转换为铜币,这给出了巨大的转化率将产生一个相当大的天文数字。
这是我用来存放钱的类:
public class CurrencyData
{
public enum CurrencyType
{
Copper ,
Silver ,
Gold ,
Platinum
}
public uint Copper { get; set; }
public uint Silver { get; set; }
public uint Gold { get; set; }
public uint Platinum { get; set; }
public CurrencyData( uint platinum , uint gold , uint silver , uint copper )
{
Copper = copper;
Silver = silver;
Gold = gold;
Platinum = platinum;
}
}
我面临的其中一个问题是反向交换。我的意思是假设我想“尝试”将 123 铜转换为银。好吧,这将产生 1 个银币,但我们还有一些无法转换的剩余部分,因为它需要 100 个铜币才能制造 1 个银币。转换后我会剩下额外的 23 个铜币,需要将其添加回铜币堆。我怎么做?一般来说,双向转换资金的最佳方式是什么?
编辑:
使用下面的优秀示例,这是我不可避免地结束的类(class)。
using System;
using System.Collections.Generic;
using System.Drawing;
namespace NovaEngine4Framework.Framework.Game.Currency
{
/// <summary>
///
/// CoinBag.cs
/// v1.1.0
/// Gordon Kyle Wallace, "Krythic".
/// LordKrythic@gmail.com
///
/// This class was created with the goal of replicating a "Dungeons and
/// Dragons" or original "Everquest" style currency system. In which there
/// are 4 different levels of currency: Copper, Silver, Gold, and Platinum.
/// Each coin has a different monetary worth, such that Platinum is the most
/// expensive coin, and Copper is the least expensive. The bag holds a desired
/// amount of money, and can be dynamically exchanged with the other denominations
/// at user whim. Each denomination is worth 100 of the lesser, so 1 Platinum is
/// equal to 100 Gold and so on.
///
/// </summary>
public class CoinBag
{
public enum Coins
{
/// <summary>
/// Copper is the lowest denominator of currency.
/// It requires 100 Copper to make 1 Silver.
/// </summary>
Copper = 1 ,
/// <summary>
/// Silver is the second most common form of currency.
/// It requires 100 Silver to Make 1 Gold.
/// </summary>
Silver = 2 ,
/// <summary>
/// Gold is the most common form of currency. It takes
/// part in most expensive transactions.
/// It requires 100 Gold to make 1 Platinum.
/// </summary>
Gold = 3 ,
/// <summary>
/// Platinum is a coin which most people never see. A single
/// Platinum coin can purchase almost anything.
/// 1 Platinum Coin = 100 Gold.
/// 1 Platinum Coin = 10,000 Silver.
/// 1 Platinum Coin = 1,000,000 Copper.
/// </summary>
Platinum = 4
}
private readonly Dictionary<Coins , long> _internalWallet;
public const int CurrencyMinimum = 0;
public const int CurrencyMaximum = 99999;
public const String CopperName = "Copper";
public const String SilverName = "Silver";
public const String GoldName = "Gold";
public const String PlatinumName = "Platinum";
public const char CopperAbbreviation = 'c';
public const char SilverAbbreviation = 's';
public const char GoldAbbreviation = 'g';
public const char PlatinumAbbreviation = 'p';
public static readonly Color CopperTextColor = Color.SaddleBrown;
public static readonly Color SilverTextColor = Color.Silver;
public static readonly Color GoldTextColor = Color.Gold;
public static readonly Color PlatinumTextColor = Color.SlateBlue;
public long Copper { get { return _internalWallet[ Coins.Copper ]; } }
public long Silver { get { return _internalWallet[ Coins.Silver ]; } }
public long Gold { get { return _internalWallet[ Coins.Gold ]; } }
public long Platinum { get { return _internalWallet[ Coins.Platinum ]; } }
public CoinBag( uint platinum , uint gold , uint silver , uint copper )
{
_internalWallet = new Dictionary<Coins , long>
{
{Coins.Platinum, platinum},
{Coins.Gold, gold},
{Coins.Silver, silver},
{Coins.Copper, copper}
};
}
/// <summary>
/// Increases the chosen currency field by a desired amount.
/// </summary>
/// <param name="amount">The amount to be added.</param>
/// <param name="type">The type of currency that will be increased.</param>
public void Add( uint amount , Coins type )
{
_internalWallet[ type ] += amount;
}
/// <summary>
/// Parses the given coin type and returns the in-game
/// text color, which will be used when drawing the name
/// within the game world.
/// </summary>
/// <param name="type"></param>
/// <returns>The color associated with the coin enum to be used for rendering.</returns>
public static Color ParseCoinTextColor( Coins type )
{
switch( type )
{
case Coins.Copper:
return CopperTextColor;
case Coins.Silver:
return SilverTextColor;
case Coins.Gold:
return GoldTextColor;
case Coins.Platinum:
return PlatinumTextColor;
default:
throw new Exception( "Could not parse Coin Color: " + type );
}
}
/// <summary>
/// Retrieves the current balance of a specified coin
/// within the bag, then returns that value with the
/// appended abbreviation attached to the end of it.
/// So, Coins.Copper would return "32c" if the current
/// balance were 32 Copper at the time of invocation.
/// </summary>
/// <param name="coin"></param>
/// <returns>The abbreviated balance of the specified coin.</returns>
public String CheckBalance( Coins coin )
{
switch( coin )
{
case Coins.Copper:
return "" + _internalWallet[ coin ] + CopperAbbreviation;
case Coins.Silver:
return "" + _internalWallet[ coin ] + SilverAbbreviation;
case Coins.Gold:
return "" + _internalWallet[ coin ] + GoldAbbreviation;
case Coins.Platinum:
return "" + _internalWallet[ coin ] + PlatinumAbbreviation;
default:
throw new Exception( "Could not parse Abbreviated Render text: " + coin );
}
}
/// <summary>
/// Parses the given coin type and returns the in-game
/// string abbreviation. Coins.Copper returns 'c'; etc.
/// </summary>
/// <param name="type"></param>
/// <returns>The char abbreviation associated with the coin enum.</returns>
public static char ParseAbbreviation( Coins type )
{
switch( type )
{
case Coins.Copper:
return CopperAbbreviation;
case Coins.Silver:
return SilverAbbreviation;
case Coins.Gold:
return GoldAbbreviation;
case Coins.Platinum:
return PlatinumAbbreviation;
default:
throw new Exception( "Could not parse Coin Abbreviation: " + type );
}
}
/// <summary>
/// Parses the given coin type and returns the in-game
/// string name. Coins.Copper returns "Copper"; etc.
/// </summary>
/// <param name="type"></param>
/// <returns>The String name associated with the coin enum.</returns>
public static String ParseName( Coins type )
{
switch( type )
{
case Coins.Copper:
return CopperName;
case Coins.Silver:
return SilverName;
case Coins.Gold:
return GoldName;
case Coins.Platinum:
return PlatinumName;
default:
throw new Exception( "Could not parse Coin Name: " + type );
}
}
/// <summary>
/// Increases the current balance of this bag with the
/// desired currency.
/// </summary>
/// <param name="platinum">The amount of platinum to be added.</param>
/// <param name="gold">The amount of gold to be added.</param>
/// <param name="silver">The amount of silver to be added.</param>
/// <param name="copper">The amount of copper to be added.</param>
public void Add( uint platinum , uint gold , uint silver , uint copper )
{
_internalWallet[ Coins.Copper ] += copper;
_internalWallet[ Coins.Silver ] += silver;
_internalWallet[ Coins.Gold ] += gold;
_internalWallet[ Coins.Platinum ] += platinum;
}
/// <summary>
/// Increases the current balance of this bag with the
/// current balance of another.
/// </summary>
/// <param name="bag">The other bag.</param>
public void Add( CoinBag bag )
{
_internalWallet[ Coins.Copper ] += bag.Copper;
_internalWallet[ Coins.Silver ] += bag.Silver;
_internalWallet[ Coins.Gold ] += bag.Gold;
_internalWallet[ Coins.Platinum ] += bag.Platinum;
}
/// <summary>
/// Subtracts the chosen currency by a desired amount.
/// </summary>
/// <param name="amount">The amount to subtract.</param>
/// <param name="type">The type of money that will be subtracted.</param>
public void Subtract( uint amount , Coins type )
{
_internalWallet[ type ] -= amount;
}
/// <summary>
/// Subtracts the current balance of the Coinbag with
/// the desired fields.
/// </summary>
/// <param name="platinum">The amount of Platinum to subtract.</param>
/// <param name="gold">The amount of Gold to subtract.</param>
/// <param name="silver">The amount of silver to subtract.</param>
/// <param name="copper">The amount of copper to subtract.</param>
public void Subtract( uint platinum , uint gold , uint silver , uint copper )
{
_internalWallet[ Coins.Copper ] -= copper;
_internalWallet[ Coins.Silver ] -= silver;
_internalWallet[ Coins.Gold ] -= gold;
_internalWallet[ Coins.Platinum ] -= platinum;
}
/// <summary>
/// Subtracts the current balance of the Coinbag with
/// the balance of another.
/// </summary>
/// <param name="bag">The second bag.</param>
public void Subtract( CoinBag bag )
{
_internalWallet[ Coins.Copper ] -= bag.Copper;
_internalWallet[ Coins.Silver ] -= bag.Silver;
_internalWallet[ Coins.Gold ] -= bag.Gold;
_internalWallet[ Coins.Platinum ] -= bag.Platinum;
}
/// <summary>
/// Completley Balances the current CoinBag by shifting
/// over Copper->Silver->Gold->Platinum.
/// </summary>
public void Balance()
{
Exchange( Coins.Copper , Coins.Silver , Copper );
Exchange( Coins.Silver , Coins.Gold , Silver );
Exchange( Coins.Gold , Coins.Platinum , Gold );
}
/// <summary>
/// Completely Empties the wallet of all money.
/// </summary>
public void Empty()
{
_internalWallet[ Coins.Copper ] -= 0;
_internalWallet[ Coins.Silver ] -= 0;
_internalWallet[ Coins.Gold ] -= 0;
_internalWallet[ Coins.Platinum ] -= 0;
}
/// <summary>
/// Exchanges one field of currency to another based
/// upon its monetary worth. The exchange rate for
/// all currency is 100 of the lesser.
/// 100 Gold = 1 Platinum.
/// 100 Silver = 1 Gold.
/// 100 Copper = 1 Silver.
/// </summary>
/// <param name="fromType">The Type that will be exchanged.</param>
/// <param name="toType">What the fromType will be exchanged to.</param>
/// <param name="amountOfFromType">The amount to be exchanged.</param>
public void Exchange( Coins fromType , Coins toType , long amountOfFromType )
{
if( fromType == toType )
return;
long fromTypeAmount = _internalWallet[ fromType ];
if( amountOfFromType > fromTypeAmount )
return; // Not enough money.
if( fromType > toType )
{
_internalWallet[ toType ] += amountOfFromType * ( long )Math.Pow( 100 , ( int )fromType - ( int )toType );
}
else
{
long overflow = amountOfFromType % 100;
amountOfFromType -= overflow;
_internalWallet[ toType ] += amountOfFromType / ( long )Math.Pow( 100 , ( int )toType - ( int )fromType );
}
_internalWallet[ fromType ] -= amountOfFromType;
}
/// <summary>
/// Creates a String representation for the current
/// monetary state of the coinbag. The format is
/// as follows:
/// [ Platinum->Gold->Silver->Copper ]
/// Or:
/// [ 100p,23g,17s,780c ]
/// </summary>
/// <returns>A String reprsentation of the Coinbag.</returns>
public String ToCurrencyString()
{
return
"" + Platinum + PlatinumAbbreviation + "," +
Gold + GoldAbbreviation + "," +
Silver + SilverAbbreviation + "," +
Copper + CopperAbbreviation;
}
}
}
最佳答案
下面是我的做法。首先,为 CurrencyType
赋值以了解哪个值更高并将其用于转换。
public enum CurrencyType
{
Copper = 1,
Silver = 2,
Gold = 3,
Platinum = 4
}
然后让 CurrencyData
类如下所示。如果您担心天文数字,我会使用 long。
private readonly Dictionary<CurrencyType, long> data;
public long Platinum { get { return data[CurrencyType.Platinum]; } }
... properties for other currencies
public CurrencyData(long platinum, long gold, long silver, long copper)
{
data = new Dictionary<CurrencyType, long>
{
{ CurrencyType.Platinum, platinum },
{ CurrencyType.Gold, gold },
{ CurrencyType.Silver, silver },
{ CurrencyType.Copper, copper }
};
}
最后是转换的方法
public void Exchange(CurrencyType fromType, CurrencyType toType, long amountOfFromType)
{
if (fromType == toType)
return;
var fromTypeAmount = data[fromType];
if (amountOfFromType > fromTypeAmount)
throw new InvalidOperationException("Not enough money");
if (fromType > toType)
{
data[toType] += amountOfFromType * (long) Math.Pow(100, (int)fromType - (int)toType);
}
else
{
var overflow = amountOfFromType % 100;
amountOfFromType -= overflow;
data[toType] += amountOfFromType / (long) Math.Pow(100, (int)toType - (int)fromType);
}
data[fromType] -= amountOfFromType;
}
你可以试试
wallet.Exchange(CurrencyType.Platinum, CurrencyType.Gold, 3);
wallet.Exchange(CurrencyType.Copper, CurrencyType.Silver, 125);
关于c# - 游戏内货币转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28007855/
我正在尝试将一个字符串逐个字符地复制到另一个字符串中。目的不是复制整个字符串,而是复制其中的一部分(我稍后会为此做一些条件......) 但我不知道如何使用迭代器。 你能帮帮我吗? std::stri
我想将 void 指针转换为结构引用。 结构的最小示例: #include "Interface.h" class Foo { public: Foo() : mAddress((uint
这有点烦人:我有一个 div,它从窗口的左上角开始过渡,即使它位于文档的其他任何位置。我试过 usign -webkit-transform-origin 但没有成功,也许我用错了。有人可以帮助我吗?
假设,如果将 CSS3 转换/转换/动画分配给 DOM 元素,我是否可以检测到该过程的状态? 我想这样做的原因是因为我正在寻找类似过渡链的东西,例如,在前一个过渡之后运行一个过渡。 最佳答案 我在 h
最近我遇到了“不稳定”屏幕,这很可能是由 CSS 转换引起的。事实上,它只发生在 Chrome 浏览器 上(可能还有 Safari,因为一些人也报告了它)。知道如何让它看起来光滑吗?此外,您可能会注意
我正在开发一个简单的 slider ,它使用 CSS 过渡来为幻灯片设置动画。我用一些基本样式和一些 javascript 创建了一支笔 here .注意:由于 Codepen 使用 Prefixfr
我正在使用以下代码返回 IList: public IList FindCodesByCountry(string country) { var query =
如何设计像这样的操作: 计算 转化 翻译 例如:从“EUR”转换为“CNY”金额“100”。 这是 /convert?from=EUR&to=CNY&amount=100 RESTful 吗? 最佳答
我使用 jquery 组合了一个图像滚动器,如下所示 function rotateImages(whichHolder, start) { var images = $('#' +which
如何使用 CSS (-moz-transform) 更改一个如下所示的 div: 最佳答案 你可以看看Mozilla Developer Center .甚至还有例子。 但是,在我看来,您的具体示例不
我需要帮助我正在尝试在选中和未选中的汉堡菜单上实现动画。我能够为菜单设置动画,但我不知道如何在转换为 0 时为左菜单动画设置动画 &__menu { transform: translateX(
我正在为字典格式之间的转换而苦苦挣扎:我正在尝试将下面的项目数组转换为下面的结果数组。本质上是通过在项目第一个元素中查找重复项,然后仅在第一个参数不同时才将文件添加到结果集中。 var items:[
如果我有两个定义相同的结构,那么在它们之间进行转换的最佳方式是什么? struct A { int i; float f; }; struct B { int i; float f; }; void
我编写了一个 javascript 代码,可以将视口(viewport)从一个链接滑动到另一个链接。基本上一切正常,你怎么能在那里看到http://jsfiddle.net/DruwJ/8/ 我现在的
我需要将文件上传到 meteor ,对其进行一些图像处理(必要时进行图像转换,从图像生成缩略图),然后将其存储在外部图像存储服务器(s3)中。这应该尽可能快。 您对 nodejs 图像处理库有什么建议
刚开始接触KDB+,有一些问题很难从Q for Mortals中得到。 说,这里 http://code.kx.com/wiki/JB:QforMortals2/casting_and_enumera
我在这里的一个项目中使用 JSF 1.2 和 IceFaces 1.8。 我有一个页面,它基本上是一大堆浮点数字段的大编辑网格。这是通过 inputText 实现的页面上的字段指向具有原始值的值对象
ScnMatrix4 是一个 4x4 矩阵。我的问题是什么矩阵行对应于位置(ScnVector3),旋转(ScnVector4),比例(ScnVector3)。第 4 行是空的吗? 编辑: 我玩弄了
恐怕我是 Scala 新手: 我正在尝试根据一些简单的逻辑将 Map 转换为新 Map: val postVals = Map("test" -> "testing1", "test2" -> "te
输入: This is sample 1 This is sample 2 输出: ~COLOR~[Green]This is sample 1~COLOR~[Red]This is sam
我是一名优秀的程序员,十分优秀!