需要从我的硬代码中创建一个循环
Need to make a loop out of my hard-code
我正在编写一个迷宫算法代码,现在我已经硬编码了其中的一部分,但我需要将其更改为循环以实现可扩展性。有人可以帮我如何将它变成一个循环吗?
movement(x_3_vector.at(1),y_3_vector.at(1),x_4_vector.at(1),y_4_vector.at(1),"3");
movement(x_3_vector.at(2),y_3_vector.at(2),x_4_vector.at(2),y_4_vector.at(2),"3");
movement(x_3_vector.at(3),y_3_vector.at(3),x_4_vector.at(3),y_4_vector.at(3),"3");
movement(x_3_vector.at(4),y_3_vector.at(4),x_4_vector.at(4),y_4_vector.at(4),"4");
movement(x_3_vector.at(5),y_3_vector.at(5),x_4_vector.at(5),y_4_vector.at(5),"4");
movement(x_3_vector.at(6),y_3_vector.at(6),x_4_vector.at(6),y_4_vector.at(6),"4");
movement(x_3_vector.at(7),y_3_vector.at(7),x_4_vector.at(7),y_4_vector.at(7),"4");
movement(x_3_vector.at(8),y_3_vector.at(8),x_4_vector.at(8),y_4_vector.at(8),"4");
movement(x_3_vector.at(9),y_3_vector.at(9),x_4_vector.at(9),y_4_vector.at(9),"4");
movement(x_3_vector.at(10),y_3_vector.at(10),x_4_vector.at(10),y_4_vector.at(10),"5");
movement(x_3_vector.at(11),y_3_vector.at(11),x_4_vector.at(11),y_4_vector.at(11),"5");
movement(x_3_vector.at(12),y_3_vector.at(12),x_4_vector.at(12),y_4_vector.at(12),"5");
movement(x_3_vector.at(13),y_3_vector.at(13),x_4_vector.at(13),y_4_vector.at(13),"5");
movement(x_3_vector.at(14),y_3_vector.at(14),x_4_vector.at(14),y_4_vector.at(14),"5");
movement(x_3_vector.at(15),y_3_vector.at(15),x_4_vector.at(15),y_4_vector.at(15),"5");
movement(x_3_vector.at(16),y_3_vector.at(16),x_4_vector.at(16),y_4_vector.at(16),"6");
movement(x_3_vector.at(17),y_3_vector.at(17),x_4_vector.at(17),y_4_vector.at(17),"6");
movement(x_3_vector.at(18),y_3_vector.at(18),x_4_vector.at(18),y_4_vector.at(18),"6");
movement(x_3_vector.at(19),y_3_vector.at(19),x_4_vector.at(19),y_4_vector.at(19),"6");
movement(x_3_vector.at(20),y_3_vector.at(20),x_4_vector.at(20),y_4_vector.at(20),"6");
问题是我需要根据我占据迷宫中的单元格数量来调用"移动"函数,所以有时我需要用字符串"3"调用运动函数 3 次,有时我需要用字符串"4"调用它 6 次......等等...
谢谢!
您当前带有循环的代码:
const char* s[20] = {
"3", "3", "3",
"4", "4", "4", "4", "4", "4",
"5", "5", "5", "5", "5", "5",
"6", "6", "6", "6", "6"
};
for (int i = 0; i != 20; ++i) {
movement(x_3_vector.at(1 + i),
y_3_vector.at(1 + i),
x_4_vector.at(1 + i),
y_4_vector.at(1 + i),
s[i]);
}
for (int i=1; i<=20; i++) {
movement(x_3_vector.at(i),y_3_vector.at(i),x_4_vector.at(i),y_4_vector.at(i), getParam(i));
}
// TODO: improve this function in order to reflect what the string value must be at any time
string getParam(int index) {
// TODO: make sure it's between 1-20 or whatever your constraints are
if (index >= 16) return "6";
else if (index >= 10) return "5";
else if (index >= 4) return "4";
else return "3";
}
相关文章:
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- C++-字符串是否包含一个带有简单循环的单词
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 我可以创建一个包含两个变量的 for 循环,但时间复杂度仍然为 O(n) 吗?
- 使用一个循环与两个循环
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- 使用一个 for 循环如何迭代和测试迭代器值?
- 我正在尝试创建一个菜单,但我的代码一直在循环
- 转到基于范围的 for 循环中的下一个迭代器
- C++:返回一个基于范围 for 循环迭代器,其中包含继承对象
- 如何循环 n 次,在 xml 文件中每个循环一个级别
- 初始化时 num 的哪个值会给这个嵌套循环一个"true"输出?
- 两次相同的 for 循环:一个编译,另一个不编译
- 如何在C 中循环一个语句
- 循环一个级数
- 如何在c++中循环一个函数
- 循环一个精灵以显示不止一次
- C++循环一个菜单,直到选择了所有选项
- QT:如何每秒循环一个方法?C++
- 循环一个mpl::map