我正在尝试用 C++ 制作一个骰子程序。在那个程序中,我有一个名为“bool_if”的函数(不要问我为什么以我的方式命名一些东西,我为我制作了这个程序并且我不想更改变量/函数名称)。我希望 bool_if() 检查数组rolled_die[] 中的任何元素是否相同。抱歉,如果这没有意义,这里是源代码(我在部分源代码中评论了我想要的):
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <unistd.h>
#include <fstream>
using namespace std;
void clear()
{
cout<<"\033[2J\033[1;1H";
}
bool rolled_die[5];
void dice() {
if (die[0] == die[1] || die[0] == die[2] || die[0] == die[3] || die[0] == die[4] || die[0] == die[5]) {
rolled_die[0] = true
}
else if (die[1] == die[2] || die[1] == die[3] || die[1] == die[4] || die[1] == die[5]) {
rolled_die[1] = true
}
else if (die[2] == die[3] || die[2] == die[4] || die[2] == die[5]) {
rolled_die[2] = true
}
else if (die[3] == die[4] || die[3] == die[5]) {
rolled_die[3] = true
}
else if (die[4] == die[5]) {
rolled_die[4] = true
}
}
int finished_score = 0
void bool_if {
if (rolled_die[0] == true || rolled_die[1] == true || rolled_die[2] == true || rolled_die[3] == true || rolled_die[4] == true) {
finished_score++;
}
/* In this part, I need to check if rolled_die[0] is equal to rolled_die[1], or rolled_die[2], etc all the way up to rolled_die[5]. */
int main()
{
srand(time(0));
int die[6] = { (rand()%6)+1,(rand()%6)+1, (rand()%6)+1, (rand()%6)+1, (rand()%6)+1, (rand()%6)+1 };
int bet;
ifstream a_file ("Score.txt");
if ( !a_file.is_open() ) {
clear();
cout<<"Score file could not be loaded. Maybe file doesn't exist?\n";
return 0;
}
int score;
a_file>> score;
clear();
cout<<"How much would you like to bet?\nRemember, you only have $"<< score <<"!\nEnter your bet: ";
cin>> bet;
if ( bet > score ) {
clear();
cout<<"You bet more money then you have!\n";
return 0;
}
clear();
cout<<"Rolling...\n";
usleep(0.1 * 1000000);
cout<<"First die: " << die[0] <<"\n";
usleep(1.3 * 1000000);
cout<<"Second die: " << die[1] <<"\n";
usleep(0.3 * 1000000);
cout<<"Third die: " << die[2] <<"\n";
usleep(2 * 1000000);
cout<<"Fourth die: " << die[3] <<"\n";
usleep(0.3 *1000000);
cout<<"Fifth die: " << die[4] <<"\n";
usleep(1.2 * 1000000);
cout<<"Sixth die: " << die[5] <<"\n";
cout<<"\n\nPress enter to see how you fared.";
cin.get();
clear();
return 0;
}
另外请告诉函数 dice() 不起作用。我希望 dice() 检查任何对。