-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask4.cpp
More file actions
56 lines (55 loc) · 2.01 KB
/
Task4.cpp
File metadata and controls
56 lines (55 loc) · 2.01 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
//CS211 Lab 7
//Task 4
//Max Davy
/*
!!!! important note for professor:
!!!! The code for this task is different than the code I copy-pasted into the submission. It includes work completed up until 8 minutes after the assignment due date.
!!!! The code in the submission is my work as it was at the assignment due date.
!!!! I ran out of time so the code did not have scorekeeping or winner announcement.
!!!! After the assignment due date I went ahead and finished the missing features.
!!!! You will be able to see from the commit history what the code looked like at submission time and what I added afterwards.
!!!! I do not intend to make it look like I finished more than I actually did.
*/
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
/*
Task 4:
Simulate a simple two-player dice battle game. Each player rolls a 6-sided die. They battle for 5
rounds, and the player with the highest total wins.
Requirements:
• Use a for loop to handle 5 rounds.
• Use rand() to simulate dice rolls.
• Keep and display running scores.
• Announce the final winner (or a tie).
*/
int main() {
srand((unsigned int)time(NULL));//seed random number generator with time otherwise the answer will be the same each time the program is run
int number1;
int number2;
int player1wins=0;
int player2wins=0;
int ties=0;
for (int i=1;i<=5;i++){
number1=(rand()%6)+1;
number2=(rand()%6)+1;
cout<<"Round "<<i<<":"<<endl;
cout<<"\tPlayer 1 roll is: "<<number1<<endl;
cout<<"\tPlayer 2 roll is: "<<number2<<endl;
if (number1>number2){
cout<<"\tPlayer 1 wins\n";
player1wins+=1;
} else if (number2>number1){
cout<<"\tPlayer 2 wins\n";
player2wins+=1;
} else {
cout<<"\tTie\n";
}
}
cout<<"Results: \n";
cout<<"\tPlayer 1 won "<<player1wins<<" times.\n";
cout<<"\tPlayer 2 won "<<player2wins<<" times.\n";
cout<<"\tThere were "<<ties<<" ties.\n";
return 0;
}