Dice program and how to proceed

I created a dice program and below is my header file. You roll 2 dice and the value of the two dice are calculated. *Face is the number of sides of the dice, *value is the number on each side. My question is regarding the increment and decrement operator. Ive done the prefix and postfix increment by incrementing the *value by 1.
My question is how to proceed with the code for when the value reaches over *face, wrap back to 1? In other words when you increment the value 6 by 1, the total is 7, but as we know a die does not carry the value of 7, so we wrap it back to the value of 1.

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
#ifndef DICE_H
#define DICE_H
#include <string>
#include <ctime>

using namespace std;

class Dice{

private :
	int *face; // no of faces of die
	int *value; //face value that shows

public:
	int getFace()  
	{
		return *face;
	}
	int getVal()   
	{
		return *value;
	}
	int roll(); 
	Dice();
	Dice(int size);  
	Dice(Dice & other); 
	Dice & operator = (Dice & rhs);
	Dice & operator ++();
	Dice operator ++(int);
	~Dice(); 

};
Dice::Dice(Dice & other){
	face = new int(other.getFace());
	value = new int(other.getVal());
}
int Dice::roll(){
	int myNum = rand();
	*value = (myNum % *face+ 1);
	return *value;
}
Dice::Dice(){
	face = new int(6);
	value = new int (rand() % *face + 1);
}
Dice::Dice(int size){
	face = new int (size);
	value = new int (rand() % *face + 1);
}

Dice & Dice::operator = (Dice & rhs){
	if(this != &rhs){
		if (face) delete face;
		if (value) delete value;
		face = new int(rhs.getFace());
		value = new int (rhs.getVal());
	}
	return *this;
}
Dice & Dice::operator ++(){//prefix increment
	++(*value);
	//++(*face); 
	return *this;
}
Dice Dice::operator ++(int){ // postfix increment, by increment *value by 1 //if reaches over *face, wrap back to 1
	Dice tmp(*this); // Temporary one
	++(*value);
	//++(*face);
	return tmp; // Temporary Two
}
Dice::~Dice(){
	if (face) delete face;
	if (value) delete value;
}
#endif 
Last edited on
Not sure if this directly awnsers your question (not sure what your .cpp file looks like.) but this what I think your asking..

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

int increment(int input){
    
    if (input % 6 == 0 ) // if six divdes evenly into input
                         // set input to 6
         input = 6;
    else
        input = input % 6; // else input =  the remainder of input / divided by six
    
    return input;
    
}

int main(){

    // quick test on inputs 1-12
    for (int i = 1; i < 13; i++) {
        std::cout << increment(i)  << std::endl;

    }

    return 0;
}



1
2
3
4
5
6
1
2
3
4
5
6
Program ended with exit code: 0
Last edited on
Topic archived. No new replies allowed.