Repository navigation
Expand file tree
/
Copy pathPBN.cpp
More file actions
179 lines (153 loc) · 3.3 KB
/
Copy pathPBN.cpp
File metadata and controls
179 lines (153 loc) · 3.3 KB
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
/*
DDS, a bridge double dummy solver.
Copyright (C) 2006-2014 by Bo Haglund /
2014-2018 by Bo Haglund & Soren Hein.
See LICENSE and README.
*/
#include "dds.h"
#include "PBN.h"
int IsCard(const char cardChar);
int ConvertFromPBN(
char const * dealBuff,
unsigned int remainCards[DDS_HANDS][DDS_SUITS])
{
for (int h = 0; h < DDS_HANDS; h++)
for (int s = 0; s < DDS_SUITS; s++)
remainCards[h][s] = 0;
int bp = 0;
while (((dealBuff[bp] != 'W') && (dealBuff[bp] != 'N') &&
(dealBuff[bp] != 'E') && (dealBuff[bp] != 'S') &&
(dealBuff[bp] != 'w') && (dealBuff[bp] != 'n') &&
(dealBuff[bp] != 'e') && (dealBuff[bp] != 's')) && (bp < 3))
bp++;
if (bp >= 3)
return 0;
int first;
if ((dealBuff[bp] == 'N') || (dealBuff[bp] == 'n'))
first = 0;
else if ((dealBuff[bp] == 'E') || (dealBuff[bp] == 'e'))
first = 1;
else if ((dealBuff[bp] == 'S') || (dealBuff[bp] == 's'))
first = 2;
else
first = 3;
bp++;
bp++;
int handRelFirst = 0;
int suitInHand = 0;
int card, hand;
while ((bp < 80) && (dealBuff[bp] != '\0'))
{
card = IsCard(dealBuff[bp]);
if (card)
{
switch (first)
{
case 0:
hand = handRelFirst;
break;
case 1:
if (handRelFirst == 0)
hand = 1;
else if (handRelFirst == 3)
hand = 0;
else
hand = handRelFirst + 1;
break;
case 2:
if (handRelFirst == 0)
hand = 2;
else if (handRelFirst == 1)
hand = 3;
else
hand = handRelFirst - 2;
break;
default:
if (handRelFirst == 0)
hand = 3;
else
hand = handRelFirst - 1;
}
remainCards[hand][suitInHand] |=
static_cast<unsigned>((bitMapRank[card] << 2));
}
else if (dealBuff[bp] == '.')
suitInHand++;
else if (dealBuff[bp] == ' ')
{
handRelFirst++;
suitInHand = 0;
}
bp++;
}
return RETURN_NO_FAULT;
}
int IsCard(const char cardChar)
{
switch (cardChar)
{
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'T':
case 't':
return 10;
case 'J':
case 'j':
return 11;
case 'Q':
case 'q':
return 12;
case 'K':
case 'k':
return 13;
case 'A':
case 'a':
return 14;
default:
return 0;
}
}
int ConvertPlayFromPBN(
const playTracePBN& playPBN,
playTraceBin& playBin)
{
const int n = playPBN.number;
if (n < 0 || n > 52)
return RETURN_PLAY_FAULT;
playBin.number = n;
for (int i = 0; i < 2 * n; i += 2)
{
char suit = playPBN.cards[i];
int s;
if (suit == 's' || suit == 'S')
s = 0;
else if (suit == 'h' || suit == 'H')
s = 1;
else if (suit == 'd' || suit == 'D')
s = 2;
else if (suit == 'c' || suit == 'C')
s = 3;
else
return RETURN_PLAY_FAULT;
playBin.suit[i >> 1] = s;
int rank = IsCard(playPBN.cards[i+1]);
if (rank == 0)
return RETURN_PLAY_FAULT;
playBin.rank[i >> 1] = rank;
}
return RETURN_NO_FAULT;
}