Need help on my assignment

Create the logic for the dice game Twig, in which a player can compete with the computer. The
object of the game is to be the first to score 10 points. The user and computer take turns “rolling” a
pair of dice following these rules:
 On a turn, each player rolls two dice. If no 1 appears, the dice values are added to a running total
for the turn, and the player can choose whether to roll again or pass the turn to the other player.
When a player passes, the accumulated turn total is added to the player’s game total.
 If a 1 appears on one of the dice, the player’s turn total becomes 0; in other words, nothing more
is added to the player’s game total for that turn, and it becomes the other player’s turn.
 If a 1 appears on both of the dice, not only is the player’s turn over, but the player’s entire
accumulated total is reset to 0.
 When the computer does not roll a 1 and can choose whether to roll again, generate a random
value from 1 to 2. The computer will then decide to continue when the value is 1 and decide to
quit and pass the turn to the player when the value is not 1.


My Lecturer told me to use If while but idk how
Last edited on
We can help, but can't do it for you.

If you need help here, you should try. If you're stuck, explain what you're stuck with and post your code.

Good luck.
#include <iostream>
#include<stdlib.h>
#include<time.h>

using namespace std;
int main()
{

int repeat = 0;
int score1=0;
int score2=0;
int sum =0;
int sum1;
int sum2;
int round1=0;
int round2=2;
int round3=0;
int number1 = 1 || 2 || 3 || 4 || 5 || 6;
int number2 = 1 || 2 || 3 || 4 || 5 || 6;
int number3 = 1 || 2 || 3 || 4 || 5 || 6;
int number4 = 1 || 2 || 3 || 4 || 5 || 6;
int value1 = number1 + number2;
int value2 = number3 + number4;
{
srand(time(0));

for (int round = 1; round<4;round++){

cout << "Press enter to roll your 1st dice:" << endl;
cout << "Your 1st: " << rand() % 6 + 1 << endl;
cin >> number1;

cout << "Press enter to roll your 2nd dice:" << endl;
cout << "Your 2nd: " << rand() % 6 + 1 << endl;
cin >> number2;
cout << "Your Total: " << number1 + number2 << endl;
sum1 = number1 + number2;

cout << "Press enter to roll computer's 1st dice:" << endl;
cout << "Computer's 1st: " << rand() % 6 + 1 << endl;
cin >> number3;

cout << "Press enter to roll computer's 2nd dice:" << endl;
cout << "Computer's 2nd: " << rand() % 6 + 1 << endl;
cin >> number4;
cout << "Computer's Total: " << number3 + number4 << endl;
sum2 = number3 + number4;

cout << "Your score: " << sum1 << " Computer's score: " << sum2 << endl;
cin.get() >>round1;

if (sum1>=10)
{
cout << "Game over You win!" << endl;
round =4;

if (sum2>=10)

cout << "Game over Opponent win!" << endl;
round =4;
}

}
}
}


is it possible to loop a random result and accummulate at the same time??
Without the game logic and just obtaining the thrown dice values, consider as an alternative program structure:

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

int main()
{
	constexpr unsigned Require {10};
	constexpr unsigned noDice {2};
	constexpr const char* const nums[] {"1st", "2nd"};
	constexpr const char* const names1[] {"your", "computer's"};
	constexpr const char* const names2[] {"You", "Computer"};
	enum Players {YOU, COMP, NOPLAY};
	std::string l;	// Dummy
	unsigned totals[NOPLAY] {};

	srand(static_cast<unsigned>(time(nullptr)));

	for (unsigned r = 1; totals[YOU] < Require && totals[COMP] < Require; ++r) {
		unsigned rolled[NOPLAY][noDice] {};

		std::cout << "\nRound " << r << '\n';

		for (unsigned t = 0; t < NOPLAY; ++t) {
			for (unsigned d = 0; d < noDice; ++d) {
				std::cout << "Press enter to roll " << names1[t] << " " << nums[d] << " dice: ";
				std::getline(std::cin, l);
				rolled[t][d] = rand() % 6 + 1;
				totals[t] += rolled[t][d];
				std::cout << names2[t] << " rolled a " << rolled[t][d] << '\n';
			}

			std::cout << names1[t] << " total: " << totals[t] << '\n';
		}

		std::cout << "Your score: " << totals[YOU] << " Computer's score: " << totals[COMP] << '\n';
	}

	if (totals[YOU] >= Require)
		std::cout << "Game over. You win!\n";
	else
		std::cout << "Game over. Computer wins!\n";
}

is it possible to loop a random result and accummulate at the same time?

We can loop a random number of times as follows:
1
2
3
4
5
6
7
    srand(time(0));

    int max_rounds = rand() % 4; // choose a random number of rounds, max 4
    cout << "Playing with " << max_rounds << " rounds" << endl;

    for (int round = 1; round < max_rounds;round++)
    {


You are accumulating sum1 and sum2 in your code, so I'm not sure what the accumulate question is about.
Topic archived. No new replies allowed.