随机坐标和禁用下一个随机集的移动

Random coordinates and Disabling the move for the next random set

本文关键字:随机 移动 下一个 坐标      更新时间:2023-10-16

我只想知道我编码的内容是否正确,或者是否走对了。任何指示和帮助都非常感谢。

我需要编写一个可以通过一组坐标随机的代码,然后当已经从第一组随机中选择该坐标时,它将被禁用,或者程序将不得不再次随机一个值。

我已经编写了一些东西,我只是不知道如何检查它是否正确,因为我在这里处理随机数。

#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来执行此操作。