File: matchid.c

package info (click to toggle)
gnubg 1.08.003-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 30,308 kB
  • sloc: ansic: 104,162; xml: 15,451; sh: 5,292; pascal: 820; yacc: 700; makefile: 586; python: 538; lex: 286; sql: 236; awk: 26
file content (283 lines) | stat: -rw-r--r-- 7,095 bytes parent folder | download
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright (C) 2002-2003 Joern Thyssen <jthyssen@dk.ibm.com>
 * Copyright (C) 2004-2013 the AUTHORS
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * $Id: matchid.c,v 1.30 2021/12/18 22:06:37 plm Exp $
 */

#include "config.h"

#include "backgammon.h"
#include "positionid.h"
#include "matchequity.h"
#include "matchid.h"
#include <string.h>

/*
 * Calculate log2 of Cube value.
 *
 * Input:
 *   n: cube value
 *
 * Returns:
 *   log(n)/log(2)
 *
 */

extern int
LogCube(int n)
{
    int i = 0;

    while (n >>= 1)
        i++;

    return i;
}


static void
SetBit(unsigned char *pc, unsigned int bitPos, int test, unsigned int iBit)
{

    const unsigned int k = bitPos / 8;
    const unsigned char rbit = (unsigned char) (0x1 << (bitPos % 8));

    unsigned char c;

    if (test & (0x1 << iBit))
        c = rbit;
    else
        c = 0;
    pc[k] = (pc[k] & (0xFF ^ rbit)) | c;

}

static void
SetBits(unsigned char *pc, unsigned int bitPos, unsigned int nBits, int iContent)
{

    unsigned int i;

    /* FIXME: rewrite SetBit, SetBits to be faster */


    for (i = 0; i < nBits; i++) {

        SetBit(pc, bitPos + i, iContent, i);

    }

}


static void
GetBits(const unsigned char *pc, const unsigned int bitPos, const unsigned int nBits, int *piContent)
{
    unsigned int i, j;
    unsigned char c[2];

    /* FIXME: rewrite GetBits to be faster */

    c[0] = 0;
    c[1] = 0;

    for (i = 0, j = bitPos; i < nBits; i++, j++) {

        unsigned int k, r;

        k = j / 8;
        r = j % 8;

        SetBit(c, i, pc[k], r);

    }

    *piContent = c[0] | (c[1] << 8);
}


static char *
MatchIDFromKey(unsigned char auchKey[9])
{
    unsigned char *puch = auchKey;
    static char szID[L_MATCHID + 1];
    char *pch = szID;
    static const char aszBase64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    int i;

    for (i = 0; i < 3; i++) {
        *pch++ = aszBase64[puch[0] >> 2];
        *pch++ = aszBase64[((puch[0] & 0x03) << 4) | (puch[1] >> 4)];
        *pch++ = aszBase64[((puch[1] & 0x0F) << 2) | (puch[2] >> 6)];
        *pch++ = aszBase64[puch[2] & 0x3F];

        puch += 3;
    }

    *pch = 0;

    return szID;
}


extern char *
MatchID(const unsigned int anDice[2],
        const int fTurn,
        const int fResigned,
        const int fDoubled,
        const int fMove,
        const int fCubeOwner, const int fCrawford, const int nMatchTo, const int anScore[2], const int nCube,
        const int fJacobyInUse, const gamestate gs)
{

    unsigned char auchKey[9];

    memset(auchKey, 0, 9);

    SetBits(auchKey, 0, 4, LogCube(nCube));
    SetBits(auchKey, 4, 2, fCubeOwner & 0x3);
    SetBits(auchKey, 6, 1, fMove);
    SetBits(auchKey, 7, 1, fCrawford);
    SetBits(auchKey, 8, 3, (int) gs);
    SetBits(auchKey, 11, 1, fTurn);
    SetBits(auchKey, 12, 1, fDoubled);
    SetBits(auchKey, 13, 2, fResigned);
    if (anDice[0] >= anDice[1]) {
        SetBits(auchKey, 15, 3, anDice[0] & 0x7);
        SetBits(auchKey, 18, 3, anDice[1] & 0x7);
    } else {
        SetBits(auchKey, 15, 3, anDice[1] & 0x7);
        SetBits(auchKey, 18, 3, anDice[0] & 0x7);
    }
    SetBits(auchKey, 21, 15, nMatchTo & 0x7FFF);
    SetBits(auchKey, 36, 15, anScore[0] & 0x7FFF);
    SetBits(auchKey, 51, 15, anScore[1] & 0x7FFF);
    SetBits(auchKey, 66, 1, (!fJacobyInUse));

    return MatchIDFromKey(auchKey);

}

static int
MatchFromKey(int anDice[2],
             int *pfTurn,
             int *pfResigned,
             int *pfDoubled,
             int *pfMove, int *pfCubeOwner, int *pfCrawford, int *pnMatchTo, int anScore[2], int *pnCube,
             int *pfJacoby, gamestate * pgs, const unsigned char *auchKey)
{
    int temp;
    GetBits(auchKey, 0, 4, pnCube);
    *pnCube = 0x1 << *pnCube;

    GetBits(auchKey, 4, 2, pfCubeOwner);
    if (*pfCubeOwner && *pfCubeOwner != 1)
        *pfCubeOwner = -1;

    GetBits(auchKey, 6, 1, pfMove);
    GetBits(auchKey, 7, 1, pfCrawford);
    GetBits(auchKey, 8, 3, &temp);
    *pgs = (gamestate) temp;
    GetBits(auchKey, 11, 1, pfTurn);
    GetBits(auchKey, 12, 1, pfDoubled);
    GetBits(auchKey, 13, 2, pfResigned);
    GetBits(auchKey, 15, 3, &anDice[0]);
    GetBits(auchKey, 18, 3, &anDice[1]);
    GetBits(auchKey, 21, 15, pnMatchTo);
    GetBits(auchKey, 36, 15, &anScore[0]);
    GetBits(auchKey, 51, 15, &anScore[1]);
    GetBits(auchKey, 66, 1, pfJacoby);
    *pfJacoby = !(*pfJacoby);

    /* FIXME: implement a consistency check */

    if (anDice[0] < 0 || anDice[0] > 6)
        return -1;
    if (anDice[1] < 0 || anDice[1] > 6)
        return -1;
    if (*pnMatchTo < 0 || *pnMatchTo > MAXSCORE)
        return -1;

    if (*pnMatchTo) {
        if (anScore[0] < 0 || anScore[0] > *pnMatchTo)
            return -1;
        if (anScore[1] < 0 || anScore[1] > *pnMatchTo)
            return -1;
    } else {
        /* money game */
        if (*pfCrawford)
            /* no Crawford game in money play */
            return -1;
    }

    return 0;

}


extern int
MatchFromID(unsigned int anDice[2],
            int *pfTurn,
            int *pfResigned,
            int *pfDoubled, int *pfMove, int *pfCubeOwner, int *pfCrawford, int *pnMatchTo, int anScore[2], int *pnCube,
            int *pfJacoby, gamestate * pgs, const char *szMatchID)
{

    unsigned char auchKey[9];
    unsigned char *puch = auchKey;
    unsigned char ach[L_MATCHID + 1];
    unsigned char *pch = ach;
    int i;

    memset(ach, 0, sizeof(ach));
    /* decode base64 into key */
    for (i = 0; i < L_MATCHID && szMatchID[i]; i++)
        pch[i] = Base64((unsigned char) szMatchID[i]);

    for (i = 0; i < 3; i++) {
        *puch++ = (unsigned char) (pch[0] << 2) | (pch[1] >> 4);
        *puch++ = (unsigned char) (pch[1] << 4) | (pch[2] >> 2);
        *puch++ = (unsigned char) (pch[2] << 6) | pch[3];

        pch += 4;
    }

    /* get matchstate info from the key */

    return MatchFromKey((int *) anDice, pfTurn, pfResigned, pfDoubled, pfMove, pfCubeOwner, pfCrawford, pnMatchTo,
                        (int *) anScore, pnCube, pfJacoby, pgs, auchKey);
}

/*
 * Generate match ID from matchstate
 *
 * Input:
 *   pms: match state
 *
 */

extern char *
MatchIDFromMatchState(const matchstate * pms)
{

    return MatchID(pms->anDice,
                   pms->fTurn,
                   pms->fResigned,
                   pms->fDoubled, pms->fMove, pms->fCubeOwner, pms->fCrawford, pms->nMatchTo, pms->anScore, pms->nCube,
                   pms->fJacoby, pms->gs);

}