随机坐标和禁用下一个随机集的移动
Random coordinates and Disabling the move for the next random set
我只想知道我编码的内容是否正确,或者是否走对了。任何指示和帮助都非常感谢。
我需要编写一个可以通过一组坐标随机的代码,然后当已经从第一组随机中选择该坐标时,它将被禁用,或者程序将不得不再次随机一个值。
我已经编写了一些东西,我只是不知道如何检查它是否正确,因为我在这里处理随机数。
#include <cstdlib>
#include <iostream>
using namespace std;
int x;
int y;
void random(), check(), rad();
const int numRows = 10;
const int numCols = 10;
bool moveflag [numRows][numCols];
string move [numRows][numCols] =
{
{"A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1", "I1", "J1"}, //row1
{"A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2", "I2", "J2"}, //row2
{"A3", "B3", "C3", "D3", "E3", "F3", "G3", "H3", "I3", "J3"}, //row3
{"A4", "B4", "C4", "D4", "E4", "F4", "G4", "H4", "I4", "J4"}, //row4
{"A5", "B5", "C5", "D5", "E5", "F5", "G5", "H5", "I5", "J5"}, //row5
{"A6", "B6", "C6", "D6", "E6", "F6", "G6", "H6", "I6", "J6"}, //row6
{"A7", "B7", "C7", "D7", "E7", "F7", "G7", "H7", "I7", "J7"}, //row7
{"A8", "B8", "C8", "D8", "E8", "F8", "G8", "H8", "I8", "J8"}, //row8
{"A9", "B9", "C9", "D9", "E9", "F9", "G9", "H9", "I9", "J9"}, //row9
{"A10", "B10", "C10", "D10", "E10", "F10", "G10", "H10", "I10", "J10"}
};
int main(int argc, char *argv[])
{
for (int row = 0; row < numRows; ++row)
{
for (int col = 0; col < numCols; ++col)
cout << move[row][col] << "t";
}
random();
system("PAUSE");
return EXIT_SUCCESS;
}
void rad(){
cout << endl << move[y][x] <<endl;
moveflag[y][x] = true;
}
void random(){
x = rand() % 10;
y = rand() % 10;
check();
if(!moveflag[y][x])
{rad();}
}
void check(){
if (moveflag[y][x])
{random();}
}
这种方法的问题在于,随着算法的进行,随机数的拒绝率将稳步增加。
更好的方法是打乱输入坐标,然后按顺序读取洗牌集。
使用std::random_shuffle
来执行此操作。
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么 Serial.println(<char[]>);返回随机字符?
- 将对象移动到std::shared_ptr
- 字符串-C++后显示的随机字符
- 何时在引用或唯一指针上使用移动语义
- 如何从具有移动语义的类对象中生成共享指针
- 循环中的随机函数
- 在c++构造函数中使用随机字符串生成器
- SDL/C++: 如何平滑但随机地移动矩形?
- 我试图将光标平滑地移动到随机点
- 矩阵中字符的随机移动
- 为什么我的TicTacToe游戏在生成随机移动时会卡住
- 随机坐标和禁用下一个随机集的移动
- 需要帮助来理解ns3 ->随机游走2移动模型
- OpenGL-在随机方向上移动的对象
- C++:Tic Tac Toe计算机随机移动
- c++的随机代理移动是相同的
- 随机移动物体
- 在cocos2d-x中的游戏场景中随机移动障碍物
- 如何在c++中随机移动球