-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoring.c
172 lines (150 loc) · 4.11 KB
/
scoring.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* scoring.c: Scoring functions.
*
* Copyright (C) 2010 Brian Raiter.
* This program is free software. See README for details.
*/
#include "yahtzee.h"
#include "scoring.h"
/* The score-evaluation functions for each slot.
*/
static int (*slotevalfunctions[ctl_count])(int[6]);
/*
* The individual scoring functions for each slot. Each function
* accepts an array of 6 ints, each int indicating the number of dice
* currently showing that value. The return value is the number of
* points that the dice values would currently earn for that slot.
*/
static int ones(int values[6]) { return 1 * values[0]; }
static int twos(int values[6]) { return 2 * values[1]; }
static int threes(int values[6]) { return 3 * values[2]; }
static int fours(int values[6]) { return 4 * values[3]; }
static int fives(int values[6]) { return 5 * values[4]; }
static int sixes(int values[6]) { return 6 * values[5]; }
static int chance(int values[6])
{
int total, i;
total = 0;
for (i = 0 ; i < 6 ; ++i)
total += (i + 1) * values[i];
return total;
}
static int threeofakind(int values[6])
{
int i;
for (i = 0 ; i < 6 ; ++i)
if (values[i] >= 3)
return chance(values);
return 0;
}
static int fourofakind(int values[6])
{
int i;
for (i = 0 ; i < 6 ; ++i)
if (values[i] >= 4)
return chance(values);
return 0;
}
static int yahtzee(int values[6])
{
int i;
for (i = 0 ; i < 6 ; ++i)
if (values[i] == 5)
return 50;
return 0;
}
static int fullhouse(int values[6])
{
int count, i;
count = 0;
for (i = 0 ; i < 6 ; ++i) {
switch (values[i]) {
case 0: continue;
case 1: return 0;
case 4: return 0;
case 5: return 25;
default: count += values[i];
}
}
return count == 5 ? 25 : 0;
}
static int smallstraight(int values[6])
{
if (values[2] && values[3])
if ((values[0] && values[1]) || (values[1] && values[4])
|| (values[4] && values[5]))
return 30;
return 0;
}
static int largestraight(int values[6])
{
if (values[1] == 1 && values[2] == 1 && values[3] == 1 && values[4] == 1)
return 40;
return 0;
}
/*
* Exported functions.
*/
/* Initialize the slotevalfunctions array.
*/
void initscoring(void)
{
slotevalfunctions[ctl_slot_ones] = ones;
slotevalfunctions[ctl_slot_twos] = twos;
slotevalfunctions[ctl_slot_threes] = threes;
slotevalfunctions[ctl_slot_fours] = fours;
slotevalfunctions[ctl_slot_fives] = fives;
slotevalfunctions[ctl_slot_sixes] = sixes;
slotevalfunctions[ctl_slot_threeofakind] = threeofakind;
slotevalfunctions[ctl_slot_fourofakind] = fourofakind;
slotevalfunctions[ctl_slot_fullhouse] = fullhouse;
slotevalfunctions[ctl_slot_smallstraight] = smallstraight;
slotevalfunctions[ctl_slot_largestraight] = largestraight;
slotevalfunctions[ctl_slot_yahtzee] = yahtzee;
slotevalfunctions[ctl_slot_chance] = chance;
}
/* Compute the score for each open slot using the current dice.
*/
void updateopenslots(void)
{
int values[6] = { 0, 0, 0, 0, 0, 0 };
int i;
for (i = ctl_dice ; i < ctl_dice_end ; ++i)
++values[controls[i].value];
for (i = ctl_slots ; i < ctl_slots_end ; ++i)
if (!isdisabled(controls[i]))
controls[i].value = slotevalfunctions[i](values);
}
/* Update the values for the output-only scoring slots (subtotal,
* total, and bonus).
*/
void updatescores(void)
{
int total, setcount, i;
total = 0;
setcount = 0;
for (i = ctl_slot_ones ; i <= ctl_slot_sixes ; ++i) {
if (isdisabled(controls[i]) || isselected(controls[i])) {
++setcount;
total += controls[i].value;
}
}
if (setcount) {
controls[ctl_slot_subtotal].value = total;
if (total >= 63) {
controls[ctl_slot_bonus].value = 35;
total += 35;
} else {
controls[ctl_slot_bonus].value = setcount == 6 ? 0 : -1;
}
} else {
controls[ctl_slot_subtotal].value = -1;
controls[ctl_slot_bonus].value = -1;
}
for (i = ctl_slot_threeofakind ; i <= ctl_slot_chance ; ++i) {
if (isdisabled(controls[i]) || isselected(controls[i])) {
++setcount;
total += controls[i].value;
}
}
controls[ctl_slot_total].value = setcount ? total : -1;
}