简体   繁体   中英

C++ 2 Player Dice Game

Player A rolls m dice, while Player B rolls m + 1 dice. If Player A rolls an 's and Player B rolls b n's, then Player A wins if a > b . Otherwise, Player A rolls up to k of the m dice (the ones not showing n) to roll again. If a' of these are n's then Player A wins if a + a' > b . If a + a' <= b, then Player B wins.

So I am wondering if I did what I wrote is correct. Along with the following:

I want to run the program where for each value of m,k and q 10000 times and calculate the number of times Player B wins. Along with this I want to be able to loop the values through different values of m,k and q to 10, 10, 15, respectively.

#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;
}

Start with first creating a function called cheddar() with the requisite parameters.

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
}

Once you are comfortable with calling a function, then go on to writing a loop that calls this function using the various values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM