我正在使用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() > RAND_MAX/2) ? 2 : 5;