【发布时间】:2014-07-06 16:35:14
【问题描述】:
我正在使用 C++ 开发井字游戏,我想知道是否可以随机化一个数字,但它只能选择 2 或 5。
我在用户和计算机之间进行了随机启动,以便使用这些代码块进行公平启动:
srand(time(NULL));
cout << "The First Person to go is randomly chosen!" << endl;
FirsttoGo = (rand()%2) + 1;
cout << "The first move goes to Player: " << FirsttoGo << endl;
turn = FirsttoGo;
我想添加的功能是,无论谁开始游戏,都可以选择 X 或 O,但如果计算机先启动,计算机应该能够在 X 或 O 之间进行选择。我还为X和O
static const int O = 5;
static const int X = 2;
有没有办法做到这一点?
【问题讨论】:
-
不要为此使用
rand()。 -
或许
((int [2]){ 5, 2 })[rand() % 2]? -
2和5的选择似乎相当随意。为什么不是更简单、更容易随机生成的东西,比如0和1?
标签: c++ algorithm tic-tac-toe