gpt4 book ai didi

c++ - C++ 如何检查一个数组是否等于任何其他数组?

转载 作者:太空宇宙 更新时间:2023-11-04 04:49:13 24 4
gpt4 key购买 nike

我正在尝试用 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() 检查是否有任何对。

最佳答案

int die[6] = { (rand()%6)+1,(rand()%6)+1, (rand()%6)+1, (rand()%6)+1, (rand()%6)+1, (rand()%6)+1 };

将其替换为:

int die[6] = {1, 2, 3, 4, 5, 6};
std::random_shuffle(die, die+6);
<小时/>

下面的代码对我来说运行得很好:http://ideone.com/F4Eefj

关于c++ - C++ 如何检查一个数组是否等于任何其他数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13462938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com