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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#! /usr/bin/python

"""Copyright 2014 - 2015 Foppe HEMMINGA

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License."""

import os
from ctypes import *

script_path = os.path.abspath(__file__)
# このファイルがあるディレクトリ(つまり 'dds' フォルダ)のパスを取得
script_dir = os.path.dirname(script_path)
# 1つ上の階層(プロジェクトルート)にあるはずのdllへのパスを構築
# これにより、実行場所に関わらず、常に正しい場所のdllを探しに行きます。
dll_path = os.path.join(script_dir, "libdds.so")
# パスを正規化(例: C:\path\dds\..\dds.dll -> C:\path\dds.dll)
dll_path = os.path.normpath(dll_path)
dds = cdll.LoadLibrary(dll_path)
print("Loaded lib {0}".format(dds))

DDS_VERSION = 20700

DDS_HANDS = 4
DDS_SUITS = 4
DDS_STRAINS = 5

MAXNOOFBOARDS = 200

RETURN_NO_FAULT = 1


class futureTricks(Structure):
    _fields_ = [
        ("nodes", c_int),
        ("cards", c_int),
        ("suit", c_int * 13),
        ("rank", c_int * 13),
        ("equals", c_int * 13),
        ("score", c_int * 13),
    ]


class deal(Structure):
    _fields_ = [
        ("trump", c_int),
        ("first", c_int),
        ("currentTrickSuit", c_int * 3),
        ("currentTrickRank", c_int * 3),
        ("remainCards", c_int * DDS_HANDS * DDS_SUITS),
    ]


class dealPBN(Structure):
    _fields_ = [
        ("trump", c_int),
        ("first", c_int),
        ("currentTrickSuit", c_int * 3),
        ("currentTrickRank", c_int * 3),
        ("remainCards", c_char * 80),
    ]


class boards(Structure):
    _fields_ = [
        ("noOfBoards", c_int),
        ("deals", deal * MAXNOOFBOARDS),
        ("target", c_int * MAXNOOFBOARDS),
        ("solutions", c_int * MAXNOOFBOARDS),
        ("mode", c_int * MAXNOOFBOARDS),
    ]


class boardsPBN(Structure):
    _fields_ = [
        ("noOfBoards", c_int),
        ("deals", dealPBN * MAXNOOFBOARDS),
        ("target", c_int * MAXNOOFBOARDS),
        ("solutions", c_int * MAXNOOFBOARDS),
        ("mode", c_int * MAXNOOFBOARDS),
    ]


class solvedBoards(Structure):
    _fields_ = [
        ("noOfBoards", c_int),
        ("solvedBoards", futureTricks * MAXNOOFBOARDS),
    ]


class ddTableDeal(Structure):
    _fields_ = [("cards", c_uint * DDS_HANDS * DDS_SUITS)]


class ddTableDeals(Structure):
    _fields_ = [
        ("noOfTables", c_int),
        ("deals", ddTableDeal * (MAXNOOFBOARDS >> 2)),
    ]


class ddTableDealPBN(Structure):
    _fields_ = [("cards", c_char * 80)]


class ddTableDealsPBN(Structure):
    _fields_ = [
        ("noOfTables", c_int),
        ("deals", ddTableDealPBN * (MAXNOOFBOARDS >> 2)),
    ]


class ddTableResults(Structure):
    #    _fields_ = [("resTable", c_int * DDS_STRAINS * DDS_HANDS)]
    _fields_ = [("resTable", c_int * DDS_HANDS * DDS_STRAINS)]


class ddTablesRes(Structure):
    _fields_ = [
        ("noOfBoards", c_int),
        ("results", ddTableResults * (MAXNOOFBOARDS >> 2)),
    ]


class parResults(Structure):
    """index = 0 is NS view and index = 1
    is EW view. By 'view' is here meant
    which side that starts the bidding."""

    _fields_ = [
        ("parScore", ((c_char * 16) * 2)),
        ("parContractsString", ((c_char * 128) * 2)),
    ]


class allParResults(Structure):
    _fields_ = [("presults", parResults * MAXNOOFBOARDS)]


class parResultsDealer(Structure):
    _fields_ = [
        ("number", c_int),
        ("score", c_int),
        ("contracts", c_char * 10 * 10),
    ]


class contractType(Structure):
    """undertricks: 0 = make; 1-13 = sacrifice
    overTricks: 0-3; e.g. 1 for 4S + 1
    level: 1-7
    denom: 0 = No Trumps, 1 = trump Spades, 2 = trump Hearts
        3 = trump Diamonds, 4 = trump Clubs
    seats: One of the cases N, E, S, W, NS, EW;
        0 = N, 1 = E, 2 = S, 3 = W, 4 = NS, 5 = EW"""

    _fields_ = [
        ("underTricks", c_int),
        ("overTricks", c_int),
        ("level", c_int),
        ("denom", c_int),
        ("seats", c_int),
    ]


class parResultsMaster(Structure):
    """score: Sign acccording to NS iew
    number: Number of contracts giving the par score"""

    _fields_ = [
        ("score", c_int),
        ("number", c_int),
        ("contracts", contractType * 10),
    ]


class parTextResults(Structure):
    """parText: Short text for par information, e.g.
        Par -110: EW 2S  EW 2D+1
    equal: TRUE in the normal case when it does not matter who
        starts the bidding. Otherwise, FALSE."""

    _fields_ = [("parTextResults", c_char * 2 * 128), ("equal", c_int)]


class playTraceBin(Structure):
    _fields_ = [("number", c_int), ("suit", c_int * 52), ("rank", c_int * 52)]


class playTracePBN(Structure):
    _fields_ = [("number", c_int), ("cards", c_char * 106)]


class solvedPlay(Structure):
    _fields_ = [("number", c_int), ("tricks", c_int * 53)]


class playTracesBin(Structure):
    _fields_ = [
        ("noOfBoards", c_int),
        ("plays", playTraceBin * (MAXNOOFBOARDS // 10)),
    ]


class playTracesPBN(Structure):
    _fields_ = [
        ("noOfBoards", c_int),
        ("plays", playTracePBN * (MAXNOOFBOARDS // 10)),
    ]


class solvedPlays(Structure):
    _fields_ = [
        ("noOfBoards", c_int),
        ("solved", solvedPlay * (MAXNOOFBOARDS // 10)),
    ]


SetMaxThreads = dds.SetMaxThreads
"""int userThreads"""
SetMaxThreads.argtypes = [c_int]
SetMaxThreads.restype = None

FreeMemory = dds.FreeMemory
FreeMemory.argtypes = None
FreeMemory.restype = None

SolveBoard = dds.SolveBoard
"""deal dl
int target
int solutions
int mode,
pointer to struct futureTricks * futp
int threadIndex"""
SolveBoard.argtypes = [deal, c_int, c_int, c_int, POINTER(futureTricks), c_int]
SolveBoard.restype = c_int

SolveBoardPBN = dds.SolveBoardPBN
"""dealPBN dlpbn
int target
int solutions
int mode
pointer to struct futureTricks * futp
int thrId"""
SolveBoardPBN.argtypes = [
    dealPBN,
    c_int,
    c_int,
    c_int,
    POINTER(futureTricks),
    c_int,
]
SolveBoardPBN.restype = c_int

CalcDDtable = dds.CalcDDtable
"""struct ddTableDeal tableDeal
pointer to struct ddTableResults * tablep"""
CalcDDtable.argtypes = [ddTableDeal, POINTER(ddTableResults)]
CalcDDtable.restype = c_int

CalcDDtablePBN = dds.CalcDDtablePBN
"""srtuct ddTableDealPBN tableDealPBN
pointer to struct ddTableResults * tablep"""
CalcDDtablePBN.argtypes = [ddTableDealPBN, POINTER(ddTableResults)]
CalcDDtablePBN.restype = c_int

CalcAllTables = dds.CalcAllTables
"""pointer to struct dd TableDeals * dealsp
int mode
int trumpFilter[DDS_STRAINS]
poiter to struct ddTablesRes * resp
pointer to struct allParResults'* presp"""
CalcAllTables.argtypes = [
    POINTER(ddTableDeals),
    c_int,
    c_int * DDS_STRAINS,
    POINTER(ddTablesRes),
    POINTER(allParResults),
]
CalcAllTables.restype = c_int

CalcAllTablesPBN = dds.CalcAllTablesPBN
"""pointer to struct ddTableDealsPBN * dealsp
int mode
int trumpFilter[DDS_STRINS]
pointer to struct ddTablesRes *resp
pointer to struct allParResults * presp"""
CalcAllTablesPBN.argtypes = [
    POINTER(ddTableDealsPBN),
    c_int,
    c_int * DDS_STRAINS,
    POINTER(ddTablesRes),
    POINTER(allParResults),
]
CalcAllTablesPBN.restype = c_int

SolveAllBoards = dds.SolveAllBoards
"""pointer to struct boardsPBN * bop
pointer to struct solvedBoards * solvedp"""
SolveAllBoards.argtypes = [POINTER(boardsPBN), POINTER(solvedBoards)]
SolveAllBoards.restype = c_int

SolveAllChunks = dds.SolveAllChunks
"""pointer to struct boardsPBN * bop
pointer to struct solvedBoards * solvedP
int chunkSize"""
SolveAllChunks.argtypes = [POINTER(boardsPBN), POINTER(solvedBoards), c_int]
SolveAllChunks.restype = c_int

solveAllChunksBin = dds.SolveAllChunksBin
"""pointer to struct boards * bop
pointer to struct solvedBoards * solvedp
int chunkSize"""
solveAllChunksBin.argtypes = [POINTER(boards), POINTER(solvedBoards), c_int]
solveAllChunksBin.restype = c_int

solveAllChunksPBN = dds.SolveAllChunksPBN
"""pointer to struct boardsPBN * bop
pointer to struct solvedBoards * solvedp
int chunkSize"""
solveAllChunksPBN.argtypes = [POINTER(boardsPBN), POINTER(solvedBoards), c_int]
solveAllChunksPBN.restype = c_int

SolveAllChunksPBN = dds.SolveAllChunksPBN
"""pointer to struct boardsPBN * bop
pointer to struct solvedBoards * solvedp
int chunkSize"""
SolveAllChunksPBN.argtypes = [POINTER(boardsPBN), POINTER(solvedBoards), c_int]
SolveAllChunksPBN.restype = c_int

Par = dds.Par
"""pointer to struct ddTableResults * tablep
pointer to struct parResults * presp
int vulnerable"""
Par.argtypes = [POINTER(ddTableResults), POINTER(parResults), c_int]
Par.restype = c_int

CalcPar = dds.CalcPar
"""struct ddTableDeal
int ulnerable
pointer to struct ddTablesRes * tablep
pointer to parResults * presp"""
CalcPar.argtypes = [
    ddTableDeal,
    c_int,
    POINTER(ddTableResults),
    POINTER(parResults),
]
CalcPar.restype = c_int

CalcPar = dds.CalcPar
"""struct ddTableDeal tableDeal
int vulnerable
pointer to struct ddTableResults * tablep
pointer to parResults * presp"""
CalcPar.argtypes = [
    ddTableDeal,
    c_int,
    POINTER(ddTableResults),
    POINTER(parResults),
]
CalcPar.restype = c_int

CalcParPBN = dds.CalcParPBN
"""struct ddTableDealPBN tableDealPBN
pointer tostruct ddTableResults * tablep
int vulnerable
pointer to struct parResults * presp"""
CalcParPBN.argtypes = [
    ddTableDealPBN,
    POINTER(ddTableResults),
    c_int,
    POINTER(parResults),
]
CalcParPBN.restype = c_int

SidesPar = dds.SidesPar
"""pointer to struct ddTableResults * tablep,
array struct parResultsDealer sidesRes[2],
int vulnerable"""
SidesPar.argtypes = [POINTER(ddTableResults), parResultsDealer * 2, c_int]
SidesPar.restypes = c_int

DealerPar = dds.DealerPar
"""pointer to struct ddTableResults * tablep
pointer to struct parResultsDealer * presp
int dealer
int vulnerable"""
DealerPar.argtypes = [
    POINTER(ddTableResults),
    POINTER(parResultsDealer),
    c_int,
    c_int,
]
DealerPar.restype = c_int

DealerParBin = dds.DealerParBin
"""pointer to struct ddTableResults * tablep
pointer to struct parResultsMaster * presp
int dealer
int vulnerable"""
DealerParBin.argtypes = [
    POINTER(ddTableResults),
    POINTER(parResultsMaster),
    c_int,
    c_int,
]
DealerParBin.restype = c_int

SidesParBin = dds.SidesParBin
"""pointer to struct ddTableResults * tablep
array struct parResultsMaster sidesRes[2]
int vulnerable"""
SidesParBin.argtypes = [POINTER(ddTableResults), parResultsMaster * 2, c_int]
SidesParBin.restype = c_int

ConvertToDealerTextFormat = dds.ConvertToDealerTextFormat
"""pointer to struct parResultsMaster *pres
pointer to char *resp"""
ConvertToDealerTextFormat.argtypes = [POINTER(parResultsMaster), c_char_p]
ConvertToDealerTextFormat.restype = c_int

ConvertToSidesTextFormat = dds.ConvertToSidesTextFormat
"""pointer to struct parResultsMaster * pres, 
pointer to struct parTextResults * resp"""
ConvertToSidesTextFormat.argtypes = [
    POINTER(parResultsMaster),
    POINTER(parTextResults),
]
ConvertToSidesTextFormat.restype = c_int

AnalysePlayBin = dds.AnalysePlayBin
"""struct deal dl
struct playTraceBin play
pointer to struct solvedPlay * solved
int thrId"""
AnalysePlayBin.argtypes = [deal, playTraceBin, POINTER(solvedPlay), c_int]
AnalysePlayBin.restype = c_int

AnalysePlayPBN = dds.AnalysePlayPBN
"""struct dealPBN dlPBN
struct playTracePBN playPBN                                 
pointer to struct solvedPlay * solvedp
int thrId"""
AnalysePlayPBN.argtypes = [dealPBN, playTracePBN, POINTER(solvedPlay), c_int]
AnalysePlayPBN.restype = c_int

AnalyseAllPlaysBin = dds.AnalyseAllPlaysBin
"""pointer to struct boards * bop
pointer to struct playTracesBin * plp
pointer to struct solvedPlays * solvedp
int chunkSize"""
AnalyseAllPlaysBin.argtypes = [
    POINTER(boards),
    POINTER(playTracesBin),
    POINTER(solvedPlays),
    c_int,
]
AnalyseAllPlaysBin.restype = c_int

AnalyseAllPlaysPBN = dds.AnalyseAllPlaysPBN
"""pointer to struct boardsPBN * bopPBN
pointer to struct playTracesPBN * plpPBN
pointer to struct solvedPlays * solvedp
int chunkSize"""
AnalyseAllPlaysPBN.argtypes = [
    POINTER(boardsPBN),
    POINTER(playTracesPBN),
    POINTER(solvedPlays),
    c_int,
]
AnalyseAllPlaysPBN.restype = c_int

ErrorMessage = dds.ErrorMessage
"""int code
char * 80"""
ErrorMessage.argtypes = [c_int, POINTER(c_char)]
ErrorMessage.restype = c_int