-1

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

4

2 回答 2

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

于 2012-11-19T21:59:09.670 回答
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()函数似乎可以做到这一点,但您没有在任何地方调用它。

于 2012-11-19T21:59:15.073 回答