Running Total and Proper Function Use

This game is an assignment for a class. The point of the game is to reach 100 rolling a dice. Each time you roll the dice if you get 2-6 you add that to a running total which starts at 0. If you pass before you get a 1, you keep that running total. If you get a 1 before you pass you lose all your progress for that turn.

There are two major issues I'm running into.

The first is how, using as much of my existing code as possible, to keep a running total. I need to have this display in a couple of places.

Lets say I rolled a 6, then passed. I want my playerTotalScore = 6; Then lets say I rolled a 5, 6, then 1. I need playerTotalScore to go back to 6. And lets say the next turn I get a 5,5 then pass. I need PlayerTotalScore to equal 16.

The second issue is how I use my devilAmountToRoll() correctly. I don't believe it is working correctly. I want the function to analyze the devilTotalScore and based on how high that is, decrease the amount of rolls the devil takes. I think this means inputting values into my function, not sure how to do that.

Thanks for the help!

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

int devilTotalScore = 0;

using namespace std;

int diceRoll(){
	int value;

	value = (rand() % 6) + 1;
	return value;
}

int devilAmountToRoll(){ // The point of this function is to reduce the amount the devil rolls
	int value;			 // as the score increases.I don't believe it is getting used at all. 

	if (devilTotalScore == 0){	

		value = (rand() % 6) + 1; //random number 1-6
		if (value <= 3){          // if random number <= 3 add two to it.
			value += 2;
		}
	}
	else if (devilTotalScore <= 30 && devilTotalScore > 0){
		value = (rand() % 5) + 1; //random number 1-5
		if (value <= 2){          // if random number <= 2 add 1 to it.
			value += 1;
		}
	}
	else if (devilTotalScore <= 60 && devilTotalScore > 30){
		value = (rand() % 4) + 1; //random number 1-4
	}
	else if (devilTotalScore <= 90 && devilTotalScore > 60){
		value = (rand() % 3) + 1; //random number 1-3
	}
	else if (devilTotalScore <= 99 && devilTotalScore > 90){
		value = (rand() % 2) + 1; //random number 1-2
	}

	return value;
}


int main(){
	srand(time(0));
	
	int playerTotalScore = 0;
	int diceValue;
	
	int devilTimesToRoll = devilAmountToRoll();

	bool playerContinue;
	playerContinue = true;
	
	bool devilContinue;
	devilContinue = true;
	
	char choice;

	while (playerTotalScore != 100 || devilTotalScore != 100){
		playerContinue = true;
		devilContinue = true;
	
		while (playerContinue){
			cout << "Pass[p] or roll [r]: ";
			cin >> choice;

			switch (choice)
			{
			case 'P':
			case 'p':
				cout << "Okay, you have " << playerTotalScore << " locked in.\n\n";
				playerContinue = false;
				continue;
				break;
			case 'R':
			case 'r':
				diceValue = 0;  // remove the previous diceRoll()
				diceValue = diceRoll();
				cout << "You rolled a ..." << diceValue << ".\n";
				if (diceValue == 1){
					//If player gets one. Post Message and set playerTotalScore back value before anything was rolled.
					playerContinue = false;
					cout << "Sorry, bub, you are busted back to " << playerTotalScore << ".\n\n";
				}
				else{
					playerTotalScore += diceValue;
					cout << "Thats a total of " << playerTotalScore << " if you pass now.\n";
				}
				break;
			default:
				cout << "Invalid input, please enter (p) or an (r).\n";
			}
		}

		while (devilContinue){
			diceValue = 0;
			cout << "The numer of times the devil should roll is " << devilTimesToRoll << ".\n";
			for (int i = 1; i <= devilTimesToRoll; i++)
			{
				diceValue = diceRoll();
				if (diceValue == 1)
				{
					cout << "The devil elects to roll!\n";
					cout << "The devil rolled a..." << diceValue << ".\n";
					i = devilTimesToRoll; //Exits the for loop
					devilContinue = false; //Exits the while loop
					if (devilTotalScore == 0)
					{
						cout << "Ouch, the devil busted back to " << devilTotalScore << ".\n\n";
					}
					else{
						cout << "Devil pending values: " << devilTotalScore << endl;
						cout << "Ouch, the devil busted back to " << devilTotalScore << ".\n\n";
					}
				}
				else {
					cout << "The devil elects to roll!\n";
					cout << "The devil rolled a..." << diceValue << ".\n";
					devilTotalScore += diceValue;
				
					cout << "Thats a total of " << devilTotalScore << ".\n";

					if (i == devilTimesToRoll) //if the for loop is sucessfully run all (devilTimesToRoll) times then do this.
					{
						cout << "The devil passes.\n\n";
						
					
					}
				}
			}

			devilContinue = false;  //end the devils turn after passing, exits the while loop
			 
		}

	}

	if (playerTotalScore == 100)
	{
		cout << "\nYou win!\n";
	}
	else if (devilTotalScore == 100)
	{
		cout << "\nThe devil wins!\n";
	}

	return 0;
}
Last edited on
closed account (o3hC5Di1)
Hi there,


JRimmer wrote:
The first is how, using as much of my existing code as possible, to keep a running total. I need to have this display in a couple of places.


At the start of each turn, you can create a variable that keeps the old state:

int turn_start_score = playerTotalScore;

That way, if the user roles a 1, you can just do playerTotalScore = turn_start_score;.


JRimmer wrote:
The second issue is how I use my devilAmountToRoll() correctly.


You are only calling this function once, before your while loop:

int devilTimesToRoll = devilAmountToRoll(); //line 51

What you want to do means that every turn the opponent takes, you need to re-evaluate the amount of times he wants to roll. so you will need to redo devilTimesToRoll = devilAmountToRoll(); at that point in your code.


Just as a pointer, it's considered good practice not to create global variable, such as your "devilTotalScore". It's very hard to keep track of whih functions actually change this variable. Consider for your functions to take the scores they need as arguments, for example:

int devilAmountToRoll(int devilTotalScore){}

Then you just create them in main():

1
2
3
4
5
6
int main()
{
    int devil_score=0, devil_amount=0, player_score=0;
    //...
    devil_amount = devilAmountToRoll(devil_score);
}



Hope that helps.

All the best,
NwN


Topic archived. No new replies allowed.