Dice game help

Hello im a begginer on c++ and i have an exam today :/
Please can you help me on this exam ?

Two player game. Player 1 and Megatron (computer)
In this game, player1 and Megatron have a parin of 8-sided dice. So each have two dice with number 1 to 8.

Setp 1: Each player rolls ONE of tyhe dice. The player that rolls the LOWER number goes first (print out who goes first).
Step 2: Each player takes turn rolling the dice tyring to get a sum of 15 points but no higher. The player enters 1 to roll 1 dice or 2 to roll 2 dice, and 0 to end their turn (pass). If a player goes higher than 15 points they lose. Print the player's score each roll. If neither player goes higher than 15 points, the player closest 15 points wins. Print the winner.
Important: Each player gets only 2 rolls of dice.

Thank you if anyone can help me!
> Please can you help me on this exam ?
Why?

If u dont want, its ok :)
I just asked for help!
And i said in the beginning that im a very beginner in c++
And you think getting others to just hand you a complete answer is going to remedy that situation any time soon?

Great, we pass the exam and you get the credit.

> Hello im a begginer on c++ and i have an exam today :/
http://www.catb.org/esr/faqs/smart-questions.html#urgent
You might want to work on your time management skills as well.

Look, all online forums are pretty much the same.
They all ask that you make a genuine effort at answering your question (you do what you can, you get stuck, then you ask a specific question).

If you're just going to be a copy/paste bot between us and your tutor, then there is no value for anyone involved.

For starters, whatever "certificate" you might hope to get (assuming you don't get kicked off the course first for cheating) will be worthless toilet paper.
Perhaps something like this (now that you're probably had the exam):

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
#include <random>
#include <chrono>
#include <iostream>

auto rng {[]() {
	std::random_device seeder;

	return std::mt19937(static_cast<std::mt19937::result_type>(seeder.entropy() ? seeder() : std::chrono::system_clock::now().time_since_epoch().count()));
} ()};

int main()
{
	std::uniform_int_distribution<int> die(1, 8);

	std::cout << "Welcome to the dice game\n";

	const auto r1 {die(rng)};
	const auto r2 {die(rng)};

	std::cout << "\nPlayer 1 rolls the dice and gets " << r1;
	std::cout << "\nPlayer 2 rolls the dice and gets " << r2 << '\n';

	auto play {r1 > r2};

	std::cout << "Player " << play + 1 << " goes first\n";

	int sums[2] {};
	bool played[2][2] {};
	int roll {};

	while (sums[0] <= 15 && sums[1] <= 15 && (played[play][0] == false || played[play][1] == false)) {
		while ((std::cout << "Player " << play + 1 << ". Which dice to roll (1 or 2 or 0 to pass): ") && (!(std::cin >> roll) || roll < 0 || roll > 2 || (roll > 0 && played[play][roll - 1]))) {
			std::cout << "Invalid roll\n";
			std::cin.clear();
			std::cin.ignore(1000, '\n');
		}

		if (roll > 0) {
			const auto rolled {die(rng)};

			played[play][roll - 1] = true;
			std::cout << "Player " << play + 1 << " rolls a " << rolled << '\n';
			std::cout << "The total is now " << (sums[play] += rolled) << '\n';
			if (sums[play] > 15)
				std::cout << "Player  " << play + 1 << " loses\n";
		}

		play = !play;

		if (played[play][0] == true && played[play][1] == true && (played[!play][0] == false || played[!play][1] == false))
			play = !play;
	}

	if (sums[0] != sums[1]) {
		const auto win0 {!(sums[0] <= 15 && (sums[0] >= sums[1] || sums[1] > 15))};

		std::cout << "The winner is player " << win0 + 1 << '\n';
	} else
		std::cout << "It's a draw\n";
}

@seeplus

It's crazy how different newer implementations of C++ look compared to older versions such as c++98

I for one write exclusively in C++98, I guess it's probably because most of the resources and books I've learned from teach the C++98.

Is there any advantages to actually using newer versions of C++ rather than older versions? I know that newer versions implement things like smart pointers, auto and more and I have no idea what they do, am I limiting myself by sticking to the older standards? I guess I'm somewhat fortunate that C++ is fully backwords compatible unlike Python.
C++ is fully backwords compatible


No its not. There are various things that would compile OK with C++98 that won't now compile with C++20 (latest standard). There were a lot of changes from C++98 to C++11 and then more again to later standards. Depending upon the code base, this could be a big issue.

What compiler are you using? I'd certainly suggest upgrading to the latest version - and taking the potential pain. At some point you will probably have to. There are current versions of MS VS, gcc and clang that support at least C++17 and some of C++20.

There's such a massive change from C++98 to C++20 that I wouldn't know where to start re the changes.

You are missing out!
its backwards compatible for now, and probably for a long time to come, because the compilers can be set to drop the new stuff (don't use the flags that set the standard to 11/17/20) or you can easily find an older version of a compiler to do the job. A small number of things will not work anymore if you use strict language and 17 or 20 compiler settings.

But you are seriously limiting yourself. The stl containers alone save insane amounts of writing your own code to do simple things, and then having to debug it.

I came from where you did 5 or 6 years back... worked at a place stuck in the 98 standard and running MSVS 2008 even into 2014+. So I missed it on both ends, 11 standard we did not adopt and 17 came out after I moved away from c++ for a time. It takes a while to relearn the new way of doing things, but most of it is far superior and easier. As a bonus you will be able to understand new code you see, and compile things you get off the web without trying to mix versions.
Last edited on
Some compilers can be set to use an earlier standard. For MS VS2019, the earliest you can set it to is C++14 (AFAIK). Earlier standards are no longer supported. For earlier standards, you'd need an earlier compiler - which sort of defeats the object as they won't support the current standard.

For compilers like g++, then yes I believe that you can specify a previous standard back to C++98 up to the latest one supported by that compiler version (I think it defaults to C++98?). So you could update that compiler to the latest, then compile as C++98, then C++11 etc all the way up to C++20. This approach won't work with MS VS though.

When we upgraded from C++98 to C++11 standard using MS VS, we had loads of issues with our code base that had to be sorted before the code was compiled cleanly as C++11.

The language itself is not fully backwards compatible from C++98 to C++20. There have been changes that mean that C++98 code that compiled OK with a C++98 compiler won't compile as the latest standard.

@adam2016,
Check out the wikipedia page: https://en.wikipedia.org/wiki/C%2B%2B20, it notes the latest modifications to C++ with the 2020 version.

I usually write in c++11, because that's what I learned (C++ Early Objects, 9th ed. by Gaddis, Walters, and Muganda), but I am learning to use later stuff from c++17 and c++20.

@seeplus,
Just a note: I wrote a dice game program a while back, and I used the <time.h> and <stdlib.h> libraries for my random number seed.

Example:
1
2
3
4
unsigned seed; // this is the data type my book said to use!
seed = time (0); // uses time.h library for random number seed
srand (seed);    // sets random number using seed
roll = rand () % 8 + 1; // sets roll of die with random number 


I've never used <chrono> before, so I don't know if this is simpler or easier.

By the way, when would you use an "auto" data type over, say, an "int" or a "double"? Just curious, I've never used them before.
max
Good points guys, I'm stuck in a bit of a rut in a sense, and am only limiting myself. So yeah I see no issue in learning the new standard(s) is there any resources or books you would recommend for this very purpose?

As you mentioned Jonnin, I fail to decipher a lot of code on here due to the fact that many people are using the new standards.

The language itself is not fully backwards compatible from C++98 to C++20. There have been changes that mean that C++98 code that compiled OK with a C++98 compiler won't compile as the latest standard.


Really? Could you give some examples?

I know that NULL is not accepted in newer versions and this would need to be changed to nullptr, what types of things have become deprecated?

Last edited on
@adam2016,
I am not sure what exactly has been deprecated or changed from c++11 onward, but here is a good resource: "C++ Early Objects, 10th ed. by Gaddis, Walters, and Muganda".

It was published in 2021 by Pearson, so it's got the most recent c++ versions in it. Here's a link to the website where you can buy it: https://media.pearsoncmg.com/bc/abp/cs-resources/products/product.html#product,isbn=0135862396

Note: I have used NULL and nullptr before with no issue, could it be that some compilers just don't support it?

max
What changes introduced in C++14 can potentially break a program written in C++11?
https://stackoverflow.com/questions/23980929/what-changes-introduced-in-c14-can-potentially-break-a-program-written-in-c1

Changes between C++14 and C++17
https://isocpp.org/files/papers/p0636r0.html

C++17/C++20 deprecated and removed features (not a complete list):
https://mariusbancila.ro/blog/2018/07/05/c17-removed-and-deprecated-features/

The differences between C++98 and C++11? Just consider the two to be separate languages and you won't be too far off. C++11 changed C++ in a fundamental way almost as extensive as Stroustrup spinning off C++ from C.
Registered users can post here. Sign in or register to post.