C ++ 2玩家骰子游戏

时间:2014-03-26 21:53:40

标签: c++ dice

玩家A滚动 米 骰子,而玩家B滚动 米 + 1个骰子。如果玩家A滚动 一个 &和# 球员B滚动 b n' s,然后玩家A赢了 a> b 。否则,玩家A卷起来 ķ 的 米 骰子(没有显示的那些 n)再次滚动。如果 一个' 这些是 然后玩家A获胜,如果 一个 + 一个' > b 。如果 a + a' < = b,然后玩家B获胜。

所以我想知道我写的是否正确。以下是:

我想运行程序,其中m,k和q的每个值为10000次,并计算玩家B获胜的次数。与此同时,我希望能够将值分别通过m,k和q的不同值循环到10,10,15。

#include <iostream>
#include <stdio.h> // NULL
#include <stdlib.h> // srand, rand
#include <time.h> // time
#include <fstream>
using namespace std;

void cheddar();

int main(){
  cheddar();
}

void cheddar(){
  int m = 3;
  int n = 6;
  int k = 1;
  int q = 6; 
  int dicerolledA = m;
  int dicerolledB = m+1;
  int dicererolled = k;
  int diceA[20];
  int diceB[20];
  int countnA = 0;
  int countnB = 0;
  int awins = 0;
  int bwins = 0;
  int totalcount = 1;
  srand(time(0));
  //while (totalcount <= 10000){
    for(int i=dicerolledA-1;i>=0;i--){
      diceA[i]=rand()%q+1;
      //cout << diceA[i] << ' ';
      if (diceA[i] == n){
        countnA++;
      }
    }
    //cout << endl;
    for(int i=dicerolledB-1;i>=0;i--){
      diceB[i]=rand()%q+1;
      //cout << diceB[i] << ' ';
      if (diceB[i] == 6){
        countnB++;
      }
    }
    //cout << endl;
    if(countnB >= countnA){
      for(int i=dicererolled-1;i>=0;i--){
        diceA[i] = rand()%q+1;
        //cout << diceA[i];
        if(diceA[i] == n){
          countnA++;
        }
      }
      //cout << endl;
      if(countnB >= countnA){
        bwins++;
      }
      else{
        awins++;
      }
    }
    else if (countnA > countnB){
      awins++;
    }
    totalcount++;
    dicerolledA = m;
    dicerolledB = m+1;
    dicererolled = k;
    countnA = 0;
    countnB = 0;
  //}
  cout << bwins << ' ';
  awins = 0;
  bwins = 0;
  totalcount = 1;
}

1 个答案:

答案 0 :(得分:2)

首先创建一个名为cheddar()的函数,其中包含必需的参数。

void cheddar(int m, int k, int q);
int main()
{
   int mValue, kValue, qValue;
   //...
   cheddar(mValue, kValue, qValue);
}

void cheddar(int m, int k, int q)
{
   // function
}

一旦熟悉调用函数,然后继续编写一个使用各种值调用此函数的循环。

相关问题