-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program04.cpp
155 lines (141 loc) · 3.58 KB
/
Program04.cpp
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
150
151
152
153
154
155
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
/* Easy dice game
|
| The game consists of 7 rounds.
| In each round, the computer throws a die,
| then the human throws a die.
| The winner of the round is the player who has the highest throw.
| In case of a tie, neither player wins.
| The winner of the game is the player who has won the most rounds.
|
*/
char input[132]; /* user input buffer */
int throwDie()
{
static int initialized = 0;
int num;
if ( !initialized )
{
printf("Initializing Die!\n\n");
srand( time(NULL) );
initialized = 1;
}
num = (int)(6.0*rand()/(RAND_MAX)) + 1 ; /* See #if 0 comments for explanation of method. */
return num;
}
#if 0
/*
In Numerical Recipes in C: The Art of Scientific Computing
(William H. Press, Brian P. Flannery, Saul A. Teukolsky,
William T. Vetterling; New York: Cambridge University
Press, 1990 (1st ed, p. 207)), the following comments are
made:
"If you want to generate a random integer between 1
and 10, you should always do it by
j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
and never by anything resembling
j=1+((int) (1000000.0*rand()) % 10);
(which uses lower-order bits)."
*/
#endif
/* Player turn */
int humanTurn()
{
int toss;
toss = throwDie();
printf("\nPlayer rolls a %d\n", toss );
return toss;
}
/* Robot turn */
int computerTurn()
{
int toss;
toss = throwDie();
printf("\nRobot rolls a %d\n", toss );
return toss;
}
/* Workaround to avoid the extra includes to get Sleep() for Windows, or sleep() for Unix. */
/* clock_t is the data type returned by clock(); really just a typefef'ed long. */
void sleep(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
int main (int argc, char *argv[])
{
int round, humanWins=0, computerWins=0;
int humanToss;
const int numberOfRounds = 7;
int yes, ch, numberOfRolls;
int humanSum, robotSum, j;
/* Start game loop. */
for ( round = 1; round<=numberOfRounds; round++ )
{
/* Resets */
humanSum = robotSum = numberOfRolls = ch = 0;
yes = 1;
printf("\nRound %d\n\n", round );
printf("Player's Turn: (hit enter)");
gets( input ); /* pause for dramatic effect */
while ( yes )
{
humanToss = humanTurn();
humanSum += humanToss;
numberOfRolls++;
printf("\nPlayer total: %d\n", humanSum);
printf("\nDo you wish to throw again? [Y or N] :");
scanf ("%s", &ch );
ch = toupper(ch);
if ( ch != 89 )
{
yes = 0;
}
}
printf("\nRobot's Turn:");
sleep (1500);
for (j = 0; j < numberOfRolls; j++)
{
printf ("\n");
robotSum += computerTurn();
sleep (1500); /* More pause drama. Adjust as personally necessary. */
}
/* Determine Winner of the Round */
if ( humanSum > robotSum )
{
humanWins++;
printf("\nPlayer wins the round. human: %3d. robot: %3d\n",
humanWins, computerWins );
}
else if ( robotSum > humanSum )
{
computerWins++;
printf("\nRobot wins the round. human:%3d. robot: %3d\n",
humanWins, computerWins );
}
else if ( robotSum == humanSum)
{
computerWins++;
printf("\nTie goes to the robot. human:%3d. robot: %3d\n",
humanWins, computerWins );
}
/* Round over. */
}
/* Determine Winner to the Game */
if ( humanWins > computerWins )
{
printf("\n\nWINNER!! You win the game!\n");
}
else if ( computerWins < humanWins )
{
printf("\n\nThe robot wins the game!\n");
}
else
{
printf("\n\nTie Game!\n");
}
system ("pause");
return 0;
}