-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLAB5.cpp
More file actions
149 lines (137 loc) · 3.66 KB
/
LAB5.cpp
File metadata and controls
149 lines (137 loc) · 3.66 KB
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
//============================================================================
// Name : LAB5.cpp
// Author : QIAN Shiyi & JIN Menghe
// Version :
// Copyright : Your copyright notice
// Description : LAB 5 - Minion's Dirty Dice
//============================================================================
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <unistd.h>
#include <string>
using namespace std;
int randGen(int div){
int random=rand()%div+1;
return random;
}
int Sum(int sum){
cout<<"Dice rolling..."<<endl;
srand(time(NULL));
// Generate the first dice
int dice1=randGen(6);
cout<<"dice1="<<dice1;
// playing
sleep(1);
// show result
// Generate the second dice
int dice2=randGen(6);
cout<<"\tdice2="<<dice2;
sleep(1);
// Generate the third dice
int dice3=randGen(6);
cout<<"\tdice3="<<dice3;
sleep(1);
// Generate the sum of the dices
sum=sum+dice1+dice2+dice3;
cout<<"\nsum="<<sum<<"\n"<<endl;
// to test
//cout<<"dice1="<<dice1<<"\tdice2="<<dice2<<"\tdice3="<<dice3<<"\nsum="<<sum<<"\n"<<endl;
// if any of the dice gives 3
if ((dice1==6)||(dice2==6)||(dice3==6)){
sum=Sum(sum);
}
return sum;
}
void showOfOne(string name){
// information of only one player winning
cout<<"Oh yeeeeesssss! "<<name<<" has won with his luck over the others!"<<endl;
}
void showOfTwo(string name1, string name2){
// information of two players winning
cout<<"What a coincident!!! "<<name1<<" and "<<name2<<" has tossed the same number!"<<endl;
}
void showOfThree(){
// information of tie
cout<<"No way!!! How can you get the same number all by randomly tossing the dices?!"<<endl;
}
int main() {
// description;
cout<<"In this game, three players will be Minion Mel, Carl and Tim!"<<endl;
cout<<"You are going to have a game of tossing dices."<<endl;
cout<<"Each of you will randomly toss 3 dices and add up the three numbers of the dices."<<endl;
cout<<"Specially, if you have tossed one or more than one \"6\", you can dice one more time!"<<endl;
cout<<"The numbers of three dices in the new toss will be added to your sum of last toss."<<endl;
cout<<"If anyone has the largest sum, he will be awarded with one day off and lots of bananas!"<<endl;
// basic function
int a,b,c;
int sum1=0;
int sum2=0;
int sum3=0;
// first player:
cout<<"Player1: Press 1 to roll dice"<<endl;
cin>>a;
if(a==1)
sum1=Sum(0);
// second player:
cout<<"Player2: Press 2 to roll dice"<<endl;
cin>>b;
//cout<<"Dice rolling"<<endl;
if(b==2)
sum2=Sum(0);
// third player:
cout<<"Player3: Press 3 to roll dice"<<endl;
cin>>c;
//cout<<"Dice rolling"<<endl;
if(c==3)
sum3=Sum(0);
cout<<"Player1: "<<sum1<<endl;
cout<<"Player2: "<<sum2<<endl;
cout<<"Player3: "<<sum3<<endl;
if ((sum1!=sum2)&&(sum2!=sum3)&&(sum3!=sum1)){
if (sum1>sum2){
if (sum1>sum3){
showOfOne("Mel");// player1 wins
}
else { // sum1<sum3
showOfOne("Tim");// player3 wins
}
}
else { // sum1<sum2
if (sum2>sum3) {
showOfOne("Carl");// player2 wins
}
else { // sum2<sum3
showOfOne("Tim")// player3 wins
}
}
}
else if ((sum1==sum2)&&(sum1!=sum3)){
if (sum1>sum3){
showOfTwo("Mel", "Carl");// player1 and player2 wins
}
else { // sum1=sum2<sum3
showOfOne("Tim");// player3 wins
}
}
else if ((sum1==sum3)&&(sum1!=sum2)){
if (sum1>sum2){
showOfTwo("Mel", "Tim");// player1 and player3 wins
}
else { // sum1=sum3<sum2
showOfOne("Carl");// player2 wins
}
}
else if ((sum2==sum3)&&(sum2!=sum1)){
if (sum2>sum1){
showOfTwo(Tim", "Carl");// player2 and player3 wins
}
else { // sum2=sum3<sum1
showOfOne("Mel");// player1 wins
}
}
else { // sum1=sum2=sum3
showOfThree();// a tie, no one wins
}
return 0;
}