-
Notifications
You must be signed in to change notification settings - Fork 0
/
yahtzee.c
104 lines (89 loc) · 1.84 KB
/
yahtzee.c
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
/*
* yahtzee.c
*
* 02/10/07 - added createPlayers
* -
* - to add
* - players turn
* - score and what not
*/
#include <time.h>
#include <stdlib.h>
#include "yahtzee.h"
#define NUMDICE 5
#define MAXVALUE 6
/*
* checks if all the dice are held
* 1 - all dices are held
* 0 - otherwise
*/
int checkDice(Dice *dice[])
{
int i;
for(i = 0; i < NUMDICE; i++)
if(!dice[i]->hold)
return 0;
return 1;
}
void unholdDice(Dice **dice[])
{
int i;
for(i = 0; i < NUMDICE; i++)
(*dice)[i]->hold = 0;
}
void rollDice(Dice **dice[])
{
int i;
srand(time(NULL));
// rolls dice if hold set to 0
for(i = 0; i < NUMDICE; i++)
if((*dice)[i]->hold == 0)
(*dice)[i]->value = rand() % MAXVALUE + 1;
}
Dice **createDice()
{
int i;
Dice **dice = (Dice **)malloc(NUMDICE * sizeof(Dice *));
for(i = 0; i < NUMDICE; i++)
{
dice[i] = (Dice *)malloc(sizeof(Dice));
dice[i]->value = i % 7 + 1;
dice[i]->hold = 0;
}
return dice;
}
/* creates a player, mallocs everything, assigns default value */
Player **createPlayers(int num, char **names, int *metric)
{
int i;
Player *p, **players = (Player **)malloc(num * sizeof(Player *));
for(i = 0; i < num; i++)
{
p = (Player *)malloc(sizeof(Player));
p->symbol = names[i];
p->metric = metric[i];
p->rolls = 0;
p->value = 0;
p->score = (Score *)malloc(sizeof(Score));
p->score->top = 0;
p->score->bonus = -1;
p->score->bottom = 0;
p->score->total = 0;
p->score->vols = 0;
p->score->one = NULL;
p->score->two = NULL;
p->score->three = NULL;
p->score->four = NULL;
p->score->five = NULL;
p->score->six = NULL;
p->score->threeK = NULL;
p->score->fourK = NULL;
p->score->boat = NULL;
p->score->small = NULL;
p->score->large = NULL;
p->score->yahtzee = NULL;
p->score->chance = NULL;
players[i] = p;
}
return players;
}