Problems in my code!

my code is a game between 2 players. Both starting at 3 0 1 they need to get to 0. I need to work out:

If player1 thows first how many games out of 1000 will he win
If player 2 thows first how many game out of 1000 will he win

I can't get player 2 to say how many games out of 1000, it's just showing -857575885859...

Could anyone tell me where I'm going wrong in my code!
Last edited on
You didn't initialize some value in the right way, but your code is all in a single file, and it's quite complex to find the mistake. At least, i think it's hard to find the mistake. But you could try to initialize every value in the countsidwins variable.
What do you mean and which value? And yeah sorry about that!
pretty sure srand(time(NULL)) should be srand((unsigned)time(0)) although the only difference there is the unsigned conversion as NULL is defined as 0 :/

also, your code is very hard to read, it should be in this format:

1
2
3
4
5
6
7
8
function prototypes (declarations) (w/description of paramaters and returns)

int main ()
{
    return 0;
}

function Definitions


ie:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int DoStuff (int, int);
//DoStuff - adds the given parameters together and returns the value
//@param int - the first number to be added
//@param int - the second number (Added to the first)
//@return int - the first and second numbers added together

int main ()
{
    int i = DoStuff(3, 4) //i = 7
    return 0;
}

int DoStuff (int a, int b)
{
    return a + b;
}


if you see there its all indented correctly and VERY easy to understand and read

as far as your code here is what i found wrong with it:

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


as far as the -81something that is because the value of countsidwins[1] is not defined/set to a value

ie: int i; cout << i; will display -858992460 or something to that effect

and heres how i would do it:

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
#include <iostream>
#include <ctime>

const int MIN_WINS = 1, //the minimum number of wins for each ammount of throws for it to be displayed
//ie: a game with 20 throws was won 8 times, it is not displayed, 30 throws was won 20 times, it is displayed
	DISPLAY_START = 0,
	DISPLAY_END = 301; //the throwcounts wins to display

int SingleFunction(int);
//SingleFunction - determines what number the dart hits
//@param int - target number
//@return int - actualy number

int main()
{
	int throwcountwins [301],
		p1wins = 0,
		p2wins = 0;

	for (int i = 0; i < 301; i++) throwcountwins[i] = 0;

	for (int i = 0; i < 1000; i++) //player1
	{
		bool gameWon = false;
		int p1throws = 0,
			p2throws = 0,
			p1score = 301,
			p2score = 301;
		while (p1score > 0)
		{
			p1throws++;
			p1score -= SingleFunction(p1score);
		}
		
		while (p2score > 0)
		{
			p2throws++;
			p2score -= SingleFunction(p2score);
		}

		if (p1throws <= p2throws)
		{
			p1wins++;
			throwcountwins[p1throws - 1]++;
		}
		else
		{
			throwcountwins[p2throws - 1]++;
		}
	}

	for (int i = 0; i < 1000; i++) //player2
	{
		bool gameWon = false;
		int p1throws = 0,
			p2throws = 0,
			p1score = 301,
			p2score = 301;
		while (p1score > 0)
		{
			p1throws++;
			p1score -= SingleFunction(p1score);
		}
		
		while (p2score > 0)
		{
			p2throws++;
			p2score -= SingleFunction(p2score);
		}

		if (p1throws < p2throws)
		{
			throwcountwins[p1throws - 1]++;
		}
		else
		{
			p2wins++;
			throwcountwins[p2throws - 1]++;
		}
	}

	for (int i = DISPLAY_START; i < DISPLAY_END; i++)
		if (throwcountwins[i] > MIN_WINS - 1)
			std::cout << "Number of games ending in " << i + 1 << " throws: " << throwcountwins[i] << "\n";

	std::cout << "\n\nJoe throwing first wins out of 1000 games: " << p1wins << std::endl
		<< "\nSid throwing first wins out of 1000 games: " << p2wins << 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)
{
	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};
	target %= 21;

	int hitroll = rand() % 100;
	if (hitroll >= 80) return board[target];
	else if (rand() % 2) return board[target - 1];
	else return board[target + 1];
}



Last edited on

Thanks alot!
Last edited on
Topic archived. No new replies allowed.