Coursework Help

Hi, for my second semester coursework we've been given a piece of code which is a darts game between two players Joe and Sid. Here is the code we were given :-

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
  //   Darts part 1
// rules brushup, 2 lads called Joe and Sid are playing. Joe goes first and they can only throw one dart per turn. The game
//can only be won/end by throwing a
//bullseye on 50, anything else gets discounted.
#include <iostream>
#include <ctime>
#include <stdio.h>
#include <stdlib.h> // rand/srand
#include <string>
 
using namespace std;
int r = rand()%100;
int rnd_Joe = rand()%70;
int rnd_Sid = rand()%72;
int r_Single_Joe = rand()%100;
int r_Single_Sid = rand()%100;
int counts[51], i;              // loop counter for J
int Count[51], l;       // loop counter for Sid
int bull_throw_J(int);  // Bull throw for Jay
int bull_throw_S(int); // Bull throw for Sid
 
 
int turn = 0;   // will decide the turn of the player, if turn = 0 then its Joe, if it = 1 then its Sids turn :)
int score [2] = {301,301};
 
 
 
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};
int single = board[0];
int X = -50 + score[2]; // logic to dictate what to aim for once score is less than 70 but more than 50
 
int throw_single_Joe(int single)    //Throwing for single function (board) for Joe
{
       
        if(rand()%100 < 80) // hit     
{
        return single;
}
else                            // miss
{                      
                     //to find neighbours search for single in board
        i = 0;
        do {
                i++;
        } while(board[i] != single);
               
                      // now return left or right neighbour, each equally likely
               
if(rand()%2==0)
        return board[i-1];
else
        return board[i+1];
        }
}
 
 
int throw_single_Sid(int single)    //Throwing for single function (board) for Sid
{
       
        if(rand()%100<80) // hit       
{
        return single;
}
else                            // miss
{                      
                     //to find neighbours search for single in board
        l = 0;
        do {
                l++;
        } while(board[l] != single);
               
                      // now return left or right neighbour, each equally likely
               
if(rand()%2==0)
        return board[l-1];
else
        return board[l+1];
        }
}
 
int bull_throw_J() //Bull Function (return 50 or 1 to 20) For Joe
{    
       
if (r<rand()%70)// successful bull hit  
 
        {        
return 50;
}
else
{                       // miss
return 1 + rand()%20;
}
}
 
int bull_throw_S() //Bull Function (return 50 or 1 to 20) For Sid
{    
       
if (r<rand()%72)// successful bull hit  
 
        {        
return 50;
}
else
{                       // miss
return 1 + rand()%20;
}
}
 
 
 
int main()
{
        srand(time(NULL));
    //GAMEPLAY
        while ((score[0] >0) && (score [1] > 0))
       
        //for( int h = 0; h<10000 ; h++)
{
 
        if (((turn%2) == 0 ) && (score[0] > 70)) // 1st turn Joe
        {
       
                 single = throw_single_Joe(20);
                // turn++;
                 score[0] = score[0] - single;
        }
 
        if (((turn%2) == 1 ) && (score[1]>70)) // Sid
        {
                single = throw_single_Sid(20);
                //turn++;
                score[1] = score[1] - single;
        }
 
 
        if (((turn%2) == 0 ) && (score[0] < 70) && (score[0] > 50)) //if Joe's turn & his score is 51-69
        {
                 
                single = throw_single_Joe(score[0]-50);
                // turn++;
                 if ((single == score[0] - 50) || (score[0] - single > 50))
                 {
                 score[0] = score[0] - single;
                 }
 
 
        }
if (((turn%2) == 1 ) && (score[1] < 70) && (score[1] > 50)) //if Sid's turn & his score is 51-69
        {
                 
                 single = throw_single_Sid(score[1]-50);
                // turn++;
                 if ((single == score[1] - 50) || (score[1] - single > 50))
                 {
                 score[1] = score[1] - single;
                 }
 
        }
 
if (((turn%2) == 0 ) && (score[0] == 50)) // Joe's Bull
{
 
        single = bull_throw_J();
        if (single == 50)
        {
                score[0] = 0;
 
        }      
}
 
if (((turn%2) == 1 ) && (score[1] == 50)) // Sid's Bull
{
 
        single = bull_throw_S();
        if (single == 50)
        {
                score[1] = 0;
 
        }      
 
 
 
}
 
 
 
if ((turn%2) == 0) {
        cout << "Joe hit" << single << "\n";
        cout << "His score is " << score[0] << "\n";
}
 
if ((turn%2) == 1) {
        cout << "Sid hit" << single << "\n";
        cout << "His score is " << score[1] << "\n";
}
 
turn++;
 
}
 
if (score[0] == 0)
        cout << "Joe won";
else
        cout << "Sid won";
 
cin >> turn;
 
        return 0;
}




Our task is to make it so:-

- each player now has 3 darts and can throw 3 each turn instead of 1.
- the players can hit the double and treble areas.
- players must finish exactly on either a double or the bull.

Our lecturer gave us some functions to use for a double throw and a treble throw (which I will paste below) but I unsure how to implement them. If anyone could help on how I should implement these now features it would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int throw_treble(int d, int p) {

	//  return result of throwing for treble d with accuracy p%  (o<90)
	
	// Board neighbours ignoring slot zero
	int bd[2][21]={{0,20,15,17,18,12,13,19,16,14,6,8,9,4,11,10,7,2,1,3,5},
		       {0,18,17,19,13,20,10,16,11,12,15,14,5,6,9,2,8,3,4,7,1}};
	
	int r = rand()%100;
	
	if(r<p)
		return 3*d;
	else if(r<90)
		return d;
	else if(r<93)
		return 3*bd[0][d];
	else if (r<96)
		return 3*bd[1][d];
	else if(r<98)
		return bd[0][d];
	else 
		return bd[1][d];
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int throw_double(int d) {
	
	//  return result of throwing for double d with accuracy 80%
	
	// Board neighbours ignoring slot zero
	int bd[2][21]={{0,20,15,17,18,12,13,19,16,14,6,8,9,4,11,10,7,2,1,3,5},
		       {0,18,17,19,13,20,10,16,11,12,15,14,5,6,9,2,8,3,4,7,1}};
	int r = rand()%100;
	
	if(r<80)
		return 2*d;
	else if(r<85)
		return 0;
	else if(r<90)
		return d;
	else if (r<93)
		return 2*bd[0][d];
	else if(r<96)
		return 2*bd[1][d];
	else if (r<98)
		return bd[0][d];
	else 
		return bd[1][d];
}
Topic archived. No new replies allowed.