将条形码转化为游戏对象(怪物、物品等)的算法
我即将编写一个将条形码转换为游戏物品(如怪物、物品、技能等)的游戏。很像旧的“Barcode Battler”游戏。
不幸的是我在数学方面不太有天赋。我需要的是一些提示,如何开发一种算法来“解析”条形码中的一些统计数据(如攻击点、生命值……)而不使其可预测。
假设我有这个 8 位条形码: 12345678
我的一个朋友建议用 MD5 和盐对条形码进行哈希处理,但我遇到的问题是我猜这会产生很多冲突。此外,从游戏设计者的角度来看,预测获得某些特殊属性的机会也很困难。
那么有人知道我如何从这里开始吗?
I am about to write a game that turns barcodes into game items (like monsters, items, skills a.s.o.). Much like the old "Barcode Battler" game.
Unfortunately I am not very talented in mathematics. What I need is some hints how I could develop an algorithm to "parse" some stats out of barcodes (like Attack points, hit points, ...) without making it predictable.
So let's say I have this 8-digit barcode:
12345678
A friend of mine suggested to hash the barcode with MD5 and a salt but the problem I have with this is that I guess that will produce a lot of collisions. Also then it is difficult in a game designers view to predict chances to get some special attributes.
So does anyone have an idea how I could start here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MD5 哈希摘要为 128 位,因此有 2^128 种可能的哈希值,或 340,282,366,920,938,463,463,374,607,431,768,211,456 (3.402823669e+38) 种可能的哈希值。
条形码有 12 位十进制数字,但其中一位是校验和数字,因此我们可以忽略它。有 10^11 种可能的条形码,或 100,000,000,000(1000 亿)种可能性。
根据此页面: http://www.iusmentis.com/technology /encryption/pgp/pgpattackfaq/hash/#bruteforcemd5,您需要尝试至少 2^64 次哈希,然后才会最终发生冲突MD5,这意味着仅使用一组条形码最终发生冲突的可能性非常小。
与简单地使用条形码中的位相比,使用 MD5 的优点是,对于给定的公司,条形码的第一部分始终是相同的,您必须考虑到这一点,否则您最终会得到给定的公司生产类似的物品。
缺点是哈希值将分布在所有可能的值空间中,因此您最终可能会得到各种各样的项目和巨大的差距。
唯一了解的方法就是进行实验。
An MD5 hash digest is 128 bits, so there are 2^128 possible hashes, or 340,282,366,920,938,463,463,374,607,431,768,211,456 (3.402823669e+38) possible hashes.
A barcodes has 12 decimal digits, but one of them is a checksum digit, so we can ignore it. There are 10^11 possible barcodes, or 100,000,000,000 (100 billion) possibilities.
According to this page: http://www.iusmentis.com/technology/encryption/pgp/pgpattackfaq/hash/#bruteforcemd5, you would need to try at least 2^64 hashes before you would end up with a collision on MD5, which means chances are incredibly small that you'll end up with a collision using just the set of barcodes.
The advantage to using MD5 over simply using the bits in the barcode is that for a given company the first part of the barcode will always be the same, and you'll have to account for that or you'll end up with a given company producing similar items.
The disadvantage is that the hashes will be spread all over the possible space of values, so you could end up with a large variety of items and huge gaps.
The only way to know is to experiment.