Basically a game of 301 darts and both players need to get to 0.
Code:
#include <iostream>#include <ctime>
int BullFunction(int percent) // throw dart function
{
int r = rand()%100;
if(r<percent) // hit
return 50; // return 50 for bullseye hit to my while loop
else // miss
return 1 + rand()%20; // return random number between 1 and 20
}
int SingleFunction(int target)
{
// return single or a neighbour
if (target>20)
{
target = 20;
}
int board[22] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5, 20, 1}; // darts board
int r = rand()%100;
int i;
if(r<80) // hit
return target;
else { // miss
//to find neighbours search for single in board
i = 0;
do {
i++;
} while(board[i] != target);
// now return left or right neighbour, each equally likely
if(rand()%2==0)
return board[i-1];
else
return board[i+1];
}
}
int main()
{
srand((unsigned)time(0)); //change rand each time
int score = 301; // start score
int throws = 0; // start throws
int sidthrows = 0; //start sids throws
int i = 0;
int coin;
int wins = 0;
int sidwins = 0;
int flipjoewins = 0;
int flipsidwins = 0;
//COUNTERS
int counts[200]; // loop counter
for (int i = 0; i < 200; i++) counts[i] = 0; // initialise
int countsidthrows[200]; // loop counter
for (int i=0; i < 200; i++) countsidthrows[i] = 0; // initialise
int countwins [2];
for (int i = 0; i < 2; i++) countwins[i] = 0;
int countsidwins [2];
for (int i = 0; i < 2; i++) countsidwins[i] = 0;
int countflipjoewins [2];
for (int i = 0; i < 2; i++) countflipjoewins[i] = 0;
int countflipsidwins [2];
for (int i = 0; i < 2; i++) countflipsidwins[i] = 0;
for (i = 0; i < 1000; i++)
{
// JOE
score = 301;
throws = 0;
while (score >= 100) // while loop will continue until 100 is greater than my score
{
throws++; score = score - BullFunction(70); // add 1 to throws; score = score - ThrowDart(); takes off whatever Throwdart(); function returns
}
while (score <= 99 && score > 50) // while loop will continue until my score is between 100 and 50
{
throws++; score = score - SingleFunction(score - 50);
if (score < 50 && score > 1)
{
throws++;
score = 50;
}
}
while (score == 50) // while loop will continue until 0 is greater than my score
{
throws++; score = score - BullFunction(70);
if (score < 50 && score > 1)
{
throws++;
score = 50;
}
}
//SID
score = 301;
sidthrows = 0;
while (score >= 100) // while loop will continue until 100 is greater than my score
{
sidthrows++; score = score - BullFunction(72); // add 1 to throws; score = score - ThrowDart(); takes off whatever Throwdart(); function returns
}
while (score <= 99 && score > 50) // while loop will continue until my score is between 100 and 50
{
sidthrows++; score = score - SingleFunction(score - 50);
if (score < 50 && score > 1)
{
sidthrows++;
score = 50;
}
}
while (score == 50) // while loop will continue until 0 is greater than my score
{
sidthrows++; score = score - BullFunction(72);
if (score < 50 && score > 1)
{
sidthrows++;
score = 50;
}
}
if (throws < sidthrows)
wins = 1 ;
else
wins = 0 ;
if (sidthrows < throws)
sidwins = 1 ;
else
sidwins = 0 ;
coin = 1 + rand()%2;
switch(coin) {
if (coin == 1)
{
case 1:
if (throws < sidthrows)
flipjoewins = 1 ;
if (throws > sidthrows)
flipjoewins = 0 ;
}
if (coin == 2)
{
case 2:
if (sidthrows < throws)
flipsidwins = 1 ;
if (sidthrows > throws)
flipsidwins = 0 ;
}
}
counts [throws]++;
countsidthrows [sidthrows]++;
countwins [wins]++;
countsidwins [sidwins]++;
countflipjoewins [flipjoewins]++;
countflipsidwins [flipsidwins]++;
}
for (i=7; i<16; i++)
{
std::cout<<"\nNumber of games ending in "<< i << " throws: "<<(counts[i]*100)/1000 << "%";
}
std::cout<<"\nAverage number of throws to win is:" << (countsidthrows[i] + counts[i])*0+9<<std::endl;
//joe first
std::cout<<"\n\nJoe throwing first wins out of 1000 games: "<<(countwins[1]*100)/1000 << "%" <<std::endl;
//sid first
std::cout<<"\nSid throwing first wins out of 1000 games: "<<(countsidwins[1]*100)/1000 << "%" <<std::endl;
//flipping
std::cout<<"\n\nFlipping to see who goes first with Joe throwing first wins out of 1000: "<<(countflipjoewins[1]*100)/1000 << "%" <<std::endl;
//sid first
std::cout<<"\nFlipping to see who goes first with Sid throwing first wins out of 1000: "<<(countflipsidwins[1]*100)/1000 << "% \n" <<std::endl;
return 0;
}