Hello,
I have a database with chessproblems in pgn. Ordered by topics.
Do,you know a tool - to randomize the order of of the game in the pgn?
Peter
Randomize a pgn file
Moderators: hgm, Dann Corbit, Harvey Williamson
-
phhnguyen
- Posts: 1431
- Joined: Wed Apr 21, 2010 4:58 am
- Location: Australia
- Full name: Nguyen Hong Pham
Re: Randomize a pgn file
it is an easy task. Just compile and run a C++ code as below:
Usage:
Code: Select all
//
// ShufflePgn
//
// Created by Nguyen Pham on 24/7/2022.
//
#include <iostream>
#include <fstream>
#include <vector>
#include <random>
#include <algorithm>
#include <iterator>
int main(int argc, const char * argv[])
{
std::cout << "shuffle games of a pgn file" << std::endl;
std::string inpath, outpath;
// arguments
{
if (argc < 2) {
std::cerr << "Usage: shufflepgn INFILE OUTFILE" << std::endl;
return -1;
}
inpath = std::string(argv[1]);
outpath = std::string(argv[2]);
}
std::vector<std::string> pgnVec;
// read in
{
std::ifstream file(inpath);
if (!file.is_open()) {
return -1;
}
std::string pgn, line;
auto cnt = 0;
while (std::getline(file, line)) {
auto isEvenLine = line.find("[Event") != std::string::npos;
if (isEvenLine) {
if (cnt) {
pgnVec.push_back(pgn);
}
pgn.clear();
cnt = 0;
}
pgn += line + "\n";
if (!line.empty()) {
cnt++;
}
}
file.close();
if (cnt) {
pgnVec.push_back(pgn);
}
}
if (pgnVec.size() <= 1) {
std::cerr << "Error: cannot read input file or it has only or under a game" << std::endl;
return -1;
}
// shuffle games
{
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(pgnVec.begin(), pgnVec.end(), g);
}
// write out
{
std::ofstream out(outpath);
for(auto && pgn : pgnVec) {
out << pgn << "\n";
}
out.close();
}
return 0;
}Code: Select all
shufflepgn INPUTPATH OUTPUTPATH
https://banksiagui.com
The most features chess GUI, based on opensource Banksia - the chess tournament manager
The most features chess GUI, based on opensource Banksia - the chess tournament manager
-
pohl4711
- Posts: 2388
- Joined: Sat Sep 03, 2011 7:25 am
- Location: Berlin, Germany
- Full name: Stefan Pohl
Re: Randomize a pgn file
Scid
Sort Database, choose random.
-
PeterO
- Posts: 211
- Joined: Sun Jul 31, 2016 6:35 pm
Re: Randomize a pgn file
Thank you VERY MUCH!
Peter
Peter