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

时间:2012-11-19 21:53:38

标签: c++ linux arrays kubuntu

我正在尝试用C ++编写一个骰子程序。在那个程序中,我有一个名为“bool_if”的函数(不要问我为什么用我的方式命名一些东西,我为我制作了这个程序,我不想改变变量/函数名)。我希望bool_if()检查数组rolling_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()不起作用。我希望骰子()检查任何对。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您可以使用for循环显着简化此代码。例如,这会将数组的每个索引与该数组之后的每个索引进行比较(这似乎是您手动执行的操作):

#include <iostream>

int main(int argc, char** argv) {
    int die[] = { 4, 5, 6, 4, 5, 3 };
    const size_t LENGTH = 6;

    for(size_t i = 0; i < LENGTH; ++i) {
       for(size_t j = i + 1; j < LENGTH; ++j) {
           if(die[i] == die[j]) {
               std::cout << "Found a pair at (" << i << ", " << j << ")" << std::endl;
           }
       }
    }

    return 0;
}

你的dice()函数似乎是这样做的,但你不是在任何地方调用它。