File: dump.cpp

package info (click to toggle)
dds 2.9.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 56,572 kB
  • sloc: cpp: 17,621; ansic: 385; makefile: 27; xml: 11; sh: 7
file content (362 lines) | stat: -rw-r--r-- 8,319 bytes parent folder | download | duplicates (3)
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
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
/*
   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 <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>

#include "dump.h"


string PrintSuit(const unsigned short suitCode);
string PrintSuit(
  const unsigned short suitCode,
  const char leastWin);

string PrintDeal(
  const unsigned short ranks[][DDS_SUITS],
  const int spacing);

string RankToDiagrams(
  const unsigned short ranks[DDS_HANDS][DDS_SUITS],
  const nodeCardsType& node);

string WinnersToText(const unsigned short winRanks[]);

string NodeToText(const nodeCardsType& node);

string FullNodeToText(const nodeCardsType& node);

string PosToText(
  const pos& tpos,
  const int target,
  const int depth);

string TopMove(
  const bool val,
  const moveType& bestMove);

string DumpTopHeader(
  const ThreadData& thrd,
  const int tricks,
  const int lower,
  const int upper,
  const int printMode);


string PrintSuit(const unsigned short suitCode)
{
  if (! suitCode)
    return "--";

  string st;
  for (int r = 14; r >= 2; r--)
    if ((suitCode & bitMapRank[r]))
      st += static_cast<char>(cardRank[r]);
  return st;
}


string PrintSuit(
  const unsigned short suitCode,
  const char leastWin)
{
  if (! suitCode)
    return "--";

  string st;
  for (int r = 14; r >= 2; r--)
  {
    if ((suitCode & bitMapRank[r]))
    {
      if (r >= 15 - leastWin)
        st += static_cast<char>(cardRank[r]);
      else
        st += "x";
    }
  }
  return st;
}


string PrintDeal(
  const unsigned short ranks[][DDS_SUITS],
  const int spacing)
{
  stringstream ss;
  for (int s = 0; s < DDS_SUITS; s++)
  {
    ss << setw(spacing) << "" << 
      cardSuit[s] << " " <<
      PrintSuit(ranks[0][s]) << "\n";
  }

  for (int s = 0; s < DDS_SUITS; s++)
  {
    ss << cardSuit[s] << " " <<
      setw(2*spacing - 2) << left << PrintSuit(ranks[3][s]) <<
      cardSuit[s] << " " <<
      PrintSuit(ranks[1][s]) << "\n";
  }

  for (int s = 0; s < DDS_SUITS; s++)
  {
    ss << setw(spacing) << "" << 
      cardSuit[s] << " " <<
      PrintSuit(ranks[2][s]) << "\n";
  }

  return ss.str() + "\n";
}


string RankToDiagrams(
  const unsigned short ranks[DDS_HANDS][DDS_SUITS],
  const nodeCardsType& node)
{
  stringstream ss;
  for (int s = 0; s < DDS_SUITS; s++)
  {
    ss << setw(12) << left << 
      (s == 0 ? "Sought" : "") << 
      cardSuit[s] << " " << setw(20) << PrintSuit(ranks[0][s]) << "|    " <<
      setw(12) << (s == 0 ? "Found" : "") << 
      cardSuit[s] << " " << 
        PrintSuit(ranks[0][s], node.leastWin[s]) << "\n";
  }

  for (int s = 0; s < DDS_SUITS; s++)
  {
    ss << 
      cardSuit[s] << " " << setw(22) << left << PrintSuit(ranks[3][s]) <<
      cardSuit[s] << " " << setw(8) << PrintSuit(ranks[1][s]) << "|    " << 
      cardSuit[s] << " " << 
        setw(22) << PrintSuit(ranks[3][s], node.leastWin[s]) << 
      cardSuit[s] << " " << 
        PrintSuit(ranks[1][s], node.leastWin[s]) << "\n";
  }

  for (int s = 0; s < DDS_SUITS; s++)
  {
    ss << setw(12) << left << "" << 
      cardSuit[s] << " " << setw(20) << PrintSuit(ranks[0][s]) << "|    " <<
      setw(12) << "" << cardSuit[s] << " " <<
      PrintSuit(ranks[0][s], node.leastWin[s]) << "\n";
  }
  return ss.str();
}


string WinnersToText(const unsigned short ourWinRanks[])
{
  stringstream ss;
  for (int s = 0; s < DDS_SUITS; s++)
    ss << cardSuit[s] << " " << PrintSuit(ourWinRanks[s]) << "\n";

  return ss.str();
}


string NodeToText(const nodeCardsType& node)
{
  stringstream ss;
  ss << setw(16) << left << "Address" << 
    static_cast<void const *>(&node) << "\n";

  ss << setw(16) << left << "Bounds" << 
    static_cast<int>(node.lbound) << " to " <<
    static_cast<int>(node.ubound) << " tricks\n";

  ss << setw(16) << left << "Best move" << 
    cardSuit[ static_cast<int>(node.bestMoveSuit) ] <<
    cardRank[ static_cast<int>(node.bestMoveRank) ] << "\n";

  return ss.str();
}


string FullNodeToText(const nodeCardsType& node)

{
  stringstream ss;
  vector<int> v(DDS_SUITS);
  for (unsigned i = 0; i < DDS_SUITS; i++)
    v[i] = 15 - static_cast<int>(node.leastWin[i]);

  ss << setw(16) << left << "Lowest used" << 
    cardSuit[0] << cardRank[v[0]] << ", " <<
    cardSuit[1] << cardRank[v[1]] << ", " <<
    cardSuit[2] << cardRank[v[2]] << ", " <<
    cardSuit[3] << cardRank[v[3]] << "\n";

  return NodeToText(node) + ss.str();
}


string PosToText(
  const pos& tpos,
  const int target,
  const int depth)
{
  stringstream ss;
  ss << setw(16) << left << "Target" << target << "\n";
  ss << setw(16) << "Depth" << depth << "\n";
  ss << setw(16) << "tricksMAX" << tpos.tricksMAX << "\n";
  ss << setw(16) << "First hand" << cardHand[tpos.first[depth]] << "\n";
  ss << setw(16) << "Next first" << cardHand[tpos.first[depth - 1]] << "\n";
  return ss.str();
}


string DumpTopHeader(
  const ThreadData& thrd,
  const int tricks,
  const int lower,
  const int upper,
  const int printMode)
{
  string stext;
  if (printMode == 0)
  {
    // Trying just one target.
    stext = "Single target " + to_string(tricks) + ", " + "achieved";
  }
  else if (printMode == 1)
  {
    // Looking for best score.
    stext = "Loop target " + to_string(tricks) + ", " +
      "bounds " + to_string(lower) + " .. " + to_string(upper) + ", " +
      TopMove(thrd.val, thrd.bestMove[thrd.iniDepth]) + "";
  }
  else if (printMode == 2)
  {
    // Looking for other moves with best score.
    stext = "Loop for cards with score " + to_string(tricks) + ", " +
      TopMove(thrd.val, thrd.bestMove[thrd.iniDepth]);
  }
  return stext + "\n" + string(stext.size(), '-') + "\n";
}


string TopMove(
  const bool val,
  const moveType& bestMove)
{
  if (val)
  {
    stringstream ss;
    ss << "achieved with move " <<
      cardSuit[ bestMove.suit ] <<
      cardRank[ bestMove.rank ];
    return ss.str();
  }
  else
    return "failed";
}


int DumpInput(
  const int errCode, 
  const deal& dl, 
  const int target,
  const int solutions, 
  const int mode)
{
  ofstream fout;
  fout.open("dump.txt");

  fout << "Error code=" << errCode << "\n\n";
  fout << "Deal data:\n";
  fout << "trump=";

  if (dl.trump == DDS_NOTRUMP)
    fout << "N\n";
  else
    fout << cardSuit[dl.trump] << "\n";
  fout << "first=" << cardHand[dl.first] << "\n";

  unsigned short ranks[4][4];

  for (int k = 0; k <= 2; k++)
    if (dl.currentTrickRank[k] != 0)
    {
      fout << "index=" << k << 
        " currentTrickSuit=" << cardSuit[dl.currentTrickSuit[k]] <<
        " currentTrickRank= " << cardRank[dl.currentTrickRank[k]] << "\n";
    }

  for (int h = 0; h < DDS_HANDS; h++)
    for (int s = 0; s < DDS_SUITS; s++)
    {
      fout << "index1=" << h << " index2=" << s <<
        " remainCards=" << dl.remainCards[h][s] << "\n";
      ranks[h][s] = static_cast<unsigned short>
                    (dl.remainCards[h][s] >> 2);
    }

  fout << "\ntarget=" << target << "\n";
  fout << "solutions=" << solutions << "\n";
  fout << "mode=" << mode << "\n\n\n";
  fout << PrintDeal(ranks, 8);

  fout.close();
  return 0;
}


void DumpRetrieved(
  ofstream& fout,
  const pos& tpos,
  const nodeCardsType& node,
  const int target,
  const int depth)
{
  fout << "Retrieved entry\n";
  fout << string(15, '-') << "\n";
  fout << PosToText(tpos, target, depth) << "\n";
  fout << FullNodeToText(node) << "\n";
  fout << RankToDiagrams(tpos.rankInSuit, node) << "\n";
}


void DumpStored(
  ofstream& fout,
  const pos& tpos,
  const Moves& moves,
  const nodeCardsType& node,
  const int target,
  const int depth)
{
  fout << "Stored entry\n";
  fout << string(12, '-') << "\n";
  fout << PosToText(tpos, target, depth) << "\n";
  fout << NodeToText(node);
  fout << moves.TrickToText((depth >> 2) + 1) << "\n";
  fout << PrintDeal(tpos.rankInSuit, 16);
}


void DumpTopLevel(
  ofstream& fout,
  const ThreadData& thrd,
  const int tricks,
  const int lower,
  const int upper,
  const int printMode)
{
  const pos& tpos = thrd.lookAheadPos;

  fout << DumpTopHeader(thrd, tricks, lower, upper, printMode) << "\n";
  fout << PrintDeal(tpos.rankInSuit, 16);
  fout << WinnersToText(tpos.winRanks[thrd.iniDepth]) << "\n";
  fout << thrd.nodes << " AB nodes, " <<
    thrd.trickNodes << " trick nodes\n\n";
}