1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
int main ()
{
srand ( time (0) ); // seed the random number
int score = 301; // set the start score at 301
int throws = 0; // set the number of throws taken to 0
int n[22] = {5, 20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5, 20}; // sets an array with the values arround the dartboard in order, plus the last and first values overlapping so that later on I can add or minus one from the array to get the section of the dartboard that is adjacent, this also lines up the numbers better as the numbers on the board effectively start from n[1] rather than n[0]
do // start game loop
{
throws++; // add one to the number of throws
if (score > 99) // checks to see if score is 100 or more (so that it can throw a 50 and not go below a score of 50)
{
int r = rand() % 100 + 1; // sets r as a random integer less than 100 (i.e a random percentage chance)
if (r < 71) // if r is 70 or less it will hit the bullseye
{
score = score - 50; // takes 50 points off of the score for a bullseye hit
}
else // if r is greater than 70 (30% chance) hits another section of the dartboard
{
int missbull = rand() % 20 +1; // selects a random number between 1 and 20 and sets missbull to that value (this is the value hit instead of the bullseye)
score = score - missbull; // removes the number hit from the current score
}
}
else // score is less than 100 so cannot aim fo the bull
{
if ( score == 50) // if the current score is precisely 50 (therefore a bullseye hit would end the game
{
int random2 = rand() % 100 + 1; // sets a random integer between 1 and 100
if (random2 < 71) // checks if that integer is 70 or less (a successful bullseye hit
{
cout << "Joe threw " << throws << " before he reached zero." << endl; // outputs the number of throws taken to reach 0
}
}
else if ( score > 70 ) // if the score is greater than 70 (it must be below 100 to have got this far) it will attempt to throw a 20 as this will get it as close to 50 as possible whilst not going below it
{
int random3 = rand() % 100 + 1; // sets a random integer between 1 and 100
if (random3 < 81) // if that integer is less than 81 (80% chance) it will hit a twenty
{
score = score - 20; // takes the hit twenty off of the score
}
else if ( (random3 > 80) && (random3 < 91) ) // if the integer is higher than 80 but less than 91 it will hit to the left of the twenty (the five)
{
score = score - 5; // takes the hit five off of the score
}
else if (random3 > 90) // if the integer is greater than 90 it will miss to the right of the twenty and hit the one
{
score = score - 1; // takes the hit one off of the score
}
}
else if ( (score < 71 ) && (score > 50) ) // if the score is 70 or less and higher than 50 it could aim for any of the numbers on the board to reach 50
{
int a = score - 50 ; // sets a value a to be the current score minus 50 as this is the best number to aim at to get to fifty
int random = rand()% 100 + 1; // sets a random variable between 1 and 100
if (random < 81 ) // if the random number is less than 81 (80% chance) then it hit the number it was aiming for
{
score = score - a; // takes the hit number off of the score (this will always leave the score at 50)
}
else if ( ( random > 80) && ( random < 91 ) ) // if the integer is higher than 80 but less than 91 it will hit to the left of the value (a) that it is aiming for
{ // as the values on the dartboard do not go round in numeric order the value a cannot simply have 1 added to or subtracted from it
int b = 0; // sets b to 0 so that it can be used in the switch
switch ( a ) // switches the a to it's corresponding position arround the dartboard
{
case '1': // if a is 1 sets b to 2 as it is the third position within the array n that will be used to move arround the board
b = 2;
break; // exits the switch
case '2': // etc.
b = 9;
break;
case '3':
b = 11;
break;
case '4':
b = 4;
break;
case '5':
b = 20;
break;
case '6':
b = 6;
break;
case '7':
b = 13;
break;
case '8':
b = 15;
break;
case '9':
b = 18;
break;
case '10':
b = 7;
break;
case '11':
b = 16;
break;
case '12':
b = 19;
break;
case '13':
b = 5;
break;
case '14':
b = 17;
break;
case '15':
b = 8;
break;
case '16':
b = 14;
break;
case '17':
b = 10;
break;
case '18':
b = 3;
break;
case '19':
b = 12;
break;
case '20':
b = 1;
break;
}
a = n[ b - 1 ]; // looks in the array for the position represented by b and subtracts one from it (missing to the left/anticlockwise) and changes a to this value
if ( score - a > 0 ) // checks the subtracting the new value would not make the score below 50 ( i.e when the number to the left of it is of a greater value than the original number)
{
score = score - a; // subtracts the new value of a (the number to the left on the board) from the score
}
}
else if ( random > 90 ) // if the random number is greater than 90 then it misses to the right
{
int c = 0; // sets c to 0 so that it can be used in the switch
switch ( a ) // switches the a to it's corresponding position arround the dartboard
{
case '1': // if a is 1 sets c to 2 as it is the third position within the array n that will be used to move arround the board
c = 2;
break; // exits the switch
case '2': // etc.
c = 9;
break;
case '3':
c = 11;
break;
case '4':
c = 4;
break;
case '5':
c = 20;
break;
case '6':
c = 6;
break;
case '7':
c = 13;
break;
case '8':
c = 15;
break;
case '9':
c = 18;
break;
case '10':
c = 7;
break;
case '11':
c = 16;
break;
case '12':
c = 19;
break;
case '13':
c = 5;
break;
case '14':
c = 17;
break;
case '15':
c = 8;
break;
case '16':
c = 14;
break;
case '17':
c = 10;
break;
case '18':
c = 3;
break;
case '19':
c = 12;
break;
case '20':
c = 1;
break;
}
a = n[c + 1]; // looks in the array for the position represented by c and adds one from it (missing to the right/clockwise) and changes a to this value
if ( score - a > 0) // checks the subtracting the new value would not make the score below 50 ( i.e when the number to the left of it is of a greater value than the original number)
{
score = score - a; // subtracts the new value of a (the number to the right on the board) from the score
}
}
}
}
}
while ( score > 0 ); // causes the do-while loop to repeat whilst the score is above 0
return 0;
}
|