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
|
#include <iostream>
#include <ctime>
int BullFunction(int);
//BullFunction - description
//@param int - what does the int argument do?
//@return int - what does the function return?
int SingleFunction(int);
//SingleFunction - description
//@param int - what does the int argument do?
//@return int - what does the function return?
int main()
{
srand((unsigned)time(0)); //change rand each time
int sidthrows = 0, //start sids throws
wins = 0,
sidwins = 0;
/*
int score = 301, // start score
throws = 0; // start throws
i = 0;
j = 0; commented out because these should not have been in this scope
k = 0;
*/
//Each of the following should be written differently, each should declare an integer (int i = 0),
//have spaces between operators (+, /, -, *, <, >, =), and be written in one line (and be indented correctly
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
//countsidthrows was couts[i] in yours (in the for loop not the declaration)
int countwins [2];
for (int i = 0; i < 2; i++) countwins[i] = 0;
int countsidwins [2];
for (int i = 0; i < 2; i++) countwins[i] = 0;
for (int i = 0; i < 1000; i++)
{
// JOE
int score = 301,
throws = 0;
while (score >= 100)
{
throws++; //should be written as 2 lines, not throws++; score = score - BullFunction(70);
score -= BullFunction(70); //score = score - BullFunction(70); -> score -= BullFunction(70); easier to read and understand
}
while (score <= 99 && score > 50)
{
throws++; //see line 58 comments
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++; //see line 58 comments
score -= BullFunction(70);
if (score < 50 && score > 1)
{
throws++; //see line 58 comments
score = 50;
}
}
//SID
score = 301;
sidthrows = 0;
while (score >= 100)
{
sidthrows++; //see line 58 comments
score -= BullFunction(72);
}
while (score <= 99 && score > 50)
{
sidthrows++; //see line 58 comments
score -= SingleFunction(score - 50);
if (score < 50 && score > 1)
{
sidthrows++; //see line 58 comments
score = 50;
}
}
while (score == 50)
{
sidthrows++; //see line 58 comments
score -= BullFunction(72);
if (score < 50 && score > 1)
{
sidthrows++; //see line 58 comments
score = 50;
}
}
if (throws < sidthrows) wins = 1; //If statements can be written in one line if they only do 1 action
else if (sidthrows < throws) sidwins = 1;
counts [throws]++;
countsidthrows [throws]++;
countwins [wins]++;
countsidwins [sidwins]++;
}
for (int i = 7; i < 20; i++)
{
std::cout << "\nNumber of games ending in " << i << " throws: " << counts[i];
}
std::cout << "\nJoe throwing first wins out of 1000 games: " << countwins[1] << std::endl
<< "\nSid throwing first wins out of 1000 games: " << countsidwins[1] << std::endl;\
//can do both couts in one line
std::cout << std::endl;
system("Pause"); //this should be at the end of a console application so it doesnt insta close
return 0;
}
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
r = rand()%100,
i = 1; //initialize i here instead of the else statment
if(r<80) return target; //see line 116
else while(board[i] != target) i++;//see line 116
//this can be done with a single while loop rather than a do-while
if(rand() % 2 == 0) return board[i - 1];//see line 116
else return board[i + 1]; //see line 116
}
int BullFunction (int percent) // throw dart function
{
int r = rand() % 100;
if(r < percent) return 50; //once a return fires the rest does not, you do not need the else
return 1 + rand() % 20;
}
|