- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一个角色扮演游戏战斗系统,您可以在其中与地精作战。不幸的是,我用来制作运气系统的随机发生器并没有像我想要的那样工作。出于某种原因,当我希望它每次都是不同的值时,随机变量在每个循环中总是相同的。我尝试了很多东西但没有成功。同样在将其发布在这里之前,我意识到我的 while 循环在满足停止要求时不会停止。
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
int main(){
std::mt19937 generator;
generator.seed(std::time(0));
std::uniform_int_distribution<uint32_t> d100(0, 100);
int random = d100(generator);
std::mt19937 generator2;
generator2.seed(std::time(0));
std::uniform_int_distribution<uint32_t> d20(1, 20);
int random2 = d20(generator2);
string action;
int enmyHP = 100;
int plyrHP = 120;
while(enmyHP >= 1 || plyrHP >= 1){
cout << "You are fighting a goblin. What will you do?"<< endl;
cout << "|| ATTACK || || SPELL || || RUN ||"<< endl;
cin >> action;
if(action == "attack" || action == "Attack" || action == "ATTACK"){
if(random >= 95){
cout << "CRITICAL HIT!" << endl;
cout << "You did 50 damage!" << endl;
enmyHP - 50;
} else if(random < 95 && random > 15){
cout << "You did " << random2 << " damage" << endl;
enmyHP = enmyHP - random2;
cout <<"The goblin has: "<< enmyHP << " HP left" << endl;
} else if(random > 15 && random < 1){
cout << "You miss" << endl;
} else {
cout << "You hit yourself" << endl;
plyrHP = plyrHP - 20;
cout << "You have: " << plyrHP << " HP" << endl;
}
}
}
return 0;
}
最佳答案
在这里,您实际上是在生成随机数,而不是创建两个具有不同分布的骰子,因此每次您使用random
或random2
你会得到相同的数字:
int random = d100(generator);
int random2 = d20(generator2); // only one generator per thread should usually be used
如果您想创建一组可以使用的骰子,您可以将一个分布与对您的生成器的引用绑定(bind)在一起,以创建一个仿函数,一个可调用对象,您可以像函数一样使用它。示例:
#include <iostream>
#include <random>
#include <iomanip> // std::setw
#include <functional> // std::bind
int main() {
std::random_device rd;
std::mt19937 generator(rd());
auto d6 = std::bind(std::uniform_int_distribution<uint16_t>(1, 6), std::ref(generator));
auto d20 = std::bind(std::uniform_int_distribution<uint16_t>(1, 20), std::ref(generator));
auto d100 = std::bind(std::uniform_int_distribution<uint16_t>(1, 100), std::ref(generator));
}
或者,您可以创建 lambda 来实现相同的效果:
std::uniform_int_distribution<uint16_t> dist6(1, 6);
std::uniform_int_distribution<uint16_t> dist20(1, 20);
std::uniform_int_distribution<uint16_t> dist100(1, 100);
// capture the distributions and generator by reference
auto d6 = [&](){ return dist6(generator); };
auto d20 = [&](){ return dist20(generator); };
auto d100 = [&](){ return dist100(generator); };
编辑:按照建议,您还可以创建一个Dice
类:
class Dice {
// static is implied below but added to make it clear: one instance
// is created and shared between instances of Dice.
//
// thread_local makes one instance of the variable per thread
// in case you add mutithreading later.
static thread_local std::random_device rd;
static thread_local std::mt19937 generator;
std::uniform_int_distribution<uint32_t> dist;
public:
Dice(uint32_t low, uint32_t high) : dist(low, high) {}
// make instances of the class callable:
uint32_t operator()() {
return dist(generator);
}
};
thread_local std::random_device Dice::rd;
thread_local std::mt19937 Dice::generator = std::mt19937(Dice::rd());
Dice d6(1, 6);
Dice d20(1, 20);
Dice d100(1, 100);
任何版本都可以像这样使用:
while(true) {
std::cout << d6() << std::setw(3) << d20() << std::setw(4) << d100() << "\n";
}
关于c++ - RPG 格斗系统无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53813483/
作为背景,我一直在根据 Ray Wenderlich 的教程开发一款角色扮演游戏。 (示例)http://www.raywenderlich.com/1163/how-to-make-a-tile-b
有谁知道在哪里可以找到 RP 游戏开发的类图示例?类似here的东西会很有用的。我不是在寻找可以盲目复制的东西,而是在寻找不同的示例,这些示例为我在尝试用铅笔记下自己的类时发现的问题绘制了各种解决方案
我的代码有问题,当调用时,它似乎使战斗持续的时间比应有的时间长。 我认为问题出在 if 语句上,所以我一直在研究它们,但我似乎无法把它弄对。 class Enemy: def __init__
我的 RPG 统计代码有点问题。我希望人们使用 6 个基本属性(力量、敏捷、体质、智力、智慧和魅力),最小值为 10。创建角色时,我希望他们有 15 点可以使用,并且在我的代码,一切正常,除非你在到达
我正在尝试制作一个角色扮演游戏战斗系统,您可以在其中与地精作战。不幸的是,我用来制作运气系统的随机发生器并没有像我想要的那样工作。出于某种原因,当我希望它每次都是不同的值时,随机变量在每个循环中总是相
我正在为我的研究开发一款角色扮演游戏,我们需要使用设计模式,并且我正在尝试编写动态角色技能。例如,我会有一些元素,在装备后,会添加角色以前没有的新技能(治疗...)。 为此,我想使用装饰器模式,但是使
我正在用 js 制作一个网络 Angular 色扮演游戏,使用 melon JS 和 SQL DB 以及 PHP。这个问题是关于如何存储每个非玩家 Angular 色 (NPC) 的已完成任务和当前任
鉴于以下树结构,每个登录的玩家都可以拥有当前和已完成级别的数据、每个级别的任务、每个任务的 NPC 以及每个 NPC 的多个任务......我正在尝试找出最好的方法存储每个玩家的当前和完成数据。 我之
所以我一直在学习 C++ 和 SDL 来制作一些基本的 2d 游戏。我想创建一个类似于魔兽世界的游戏,但它是一个 2D 版本。我希望它在线并使用数据库或其他东西来启动数据,如黄金数量、HP 等。不过我
既然我终于弄清楚了类继承在 ruby 中的工作原理,我正在为文本 rpg 设计一个库存系统。但是,有些怪癖我无法弄清楚。 项目类别: class Item attr_accessor :n
我需要帮助在我的文字 RPG 游戏中设置检查点。我给用户两种选择,其中之一会导致他们被杀。他们死后,我想让他们回到检查点的起点,但我不知道该怎么办。 我尝试过 while (true) {, cont
我正在制作一个角色扮演游戏,但是,我现在遇到了一个错误。玩家的角色可以在所有四个主要方向上移动,但如果您向右、向上或向下移动,玩家就会卡住。 此外,错误似乎有一些逻辑: 如果向下移动,角色会卡在向下移
为了练习,我正在使用 Java 创建一个基于文本的 RPG。我目前正在使用 .properties 文件来处理字符信息。我知道 YAML 可能是一个更好的选择,但我不太确定如何实现它。使用属性,创建库
我正在编写 RPG 程序,我希望角色对象创建控制其“能力”的对象 每个字符都有一个字符串变量 powerOne、PowerTwo 等。 powerOne =“攻击” 我需要一种方法来做到这一点: "
我不确定我的代码有什么问题,我正在尝试创建一个 while 循环,该循环将使用在 SQL 中声明的游标获取字段(代码)中的下一个字符。目标是通过循环获取下一个匹配的字符。然后我想在循环结束时返回结果。
我在 RPG 中编写了一个监控程序,用于检查 fax/400 是否正常运行。 现在我希望这个程序每 15 分钟检查一次。 我没有在作业调度程序中每 15 分钟放置一个作业(这很难管理),而是使用 DL
我需要为我的 RPG 程序中的现有物理文件创建一个新成员。我知道有两种方法,但我必须说,我都不喜欢: 使用QCMDEXC调用ADDPFM 编写一个 CL 程序,调用 ADDPFM 并从 RPG 调用它
我只是在玩一个基于文本的 RPG,我想知道,命令解释器究竟是如何实现的,现在有没有更好的方法来实现类似的东西?编写大量 if 语句很容易,但这似乎很麻烦,特别是考虑到大多数情况下 pick up th
我正在尝试创建一个过程,该过程将采用一个数组并返回已使用元素的计数(为什么这不是 BIF??)。我正在努力寻找一种将未知大小的数组传递给我的程序的方法。 类似... P count B
我正在尝试制作一个简单的基于文本的游戏。 while True: choice = raw_input('>>: ') aa = choice.split() if aa[0]
我是一名优秀的程序员,十分优秀!