#include "files.h" using std::cout; using std::cin; using std::endl; using std::string; using std::filesystem::directory_iterator; /* Reads the four hands from a given directory (path) containing PBN files. In addition to the PBN files, options include Straight Suits, Random, or random with N-S always having the most points. The hands are represented by a 52 element UCHAR array, with hands[i] containing the player's index (N=0, E=1, S=2, W=3) for card with the index of i. */ int getHand(const char *path, unsigned char *hands) { const char* presets[] = // preset deals { "Straight suits", "Random", "N-S is best", 0}; const char *pbn = ".PBN"; std::vector names; int npbn = 0; size_t nPresets; for (nPresets=0; presets[nPresets]; nPresets++) { printf("%3d %s\n", nPresets+1, presets[nPresets]); } /*** List all the PBN files in the given directory **/ for (const auto & file : directory_iterator(path)) { const char *pext = file.path().extension().c_str(); int i; for (i=0; i < 4; i++) if (toupper(pext[i]) != pbn[i]) break; if (i != 4) continue; npbn++; printf("%3d %s\n", npbn+nPresets, file.path().stem().c_str()); names.push_back(string(file.path().filename())); } /* Ask for a choice */ cout << "Pick a file: "; size_t n; cin >> n; if (n <= 0 || n > names.size() + nPresets) { cout << "\n bad number.\n"; exit(0); } if (n > nPresets) { // read a PBN file? string pathName = string(path) + "/" + names[n-1-nPresets]; printf("Picked %d, %s\n", n, pathName.c_str()); if (readDeal(pathName.c_str(), hands) == 0) { // read it. printf("error reading %s hand\n", pathName.c_str()); exit(0); } } else { switch (n) { case 1: // straight suits for (int i=0; i < 52; i++) hands[i] = SUIT(i); break; case 2: // random randomDeal(hands); break; case 3: // N-S is best nsIsBetter(hands); break; } } return 1; }