Cute Chess  0.1
tournament.h
1 /*
2  This file is part of Cute Chess.
3 
4  Cute Chess is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  Cute Chess is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with Cute Chess. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 
19 #ifndef TOURNAMENT_H
20 #define TOURNAMENT_H
21 
22 #include <QObject>
23 #include <QList>
24 #include <QVector>
25 #include <QMap>
26 #include <QFile>
27 #include <QTextStream>
28 #include "board/move.h"
29 #include "timecontrol.h"
30 #include "pgngame.h"
31 #include "gameadjudicator.h"
32 #include "tournamentplayer.h"
33 #include "tournamentpair.h"
34 class GameManager;
35 class PlayerBuilder;
36 class ChessGame;
37 class OpeningBook;
38 class OpeningSuite;
39 class Sprt;
40 
44 class LIB_EXPORT Tournament : public QObject
45 {
46  Q_OBJECT
47 
48  public:
51  {
54  RoundPolicy
55  };
56 
59  {
61  RegularLoss = Chess::Result::ResultError + 1,
69  InsufficientMaterial,
70  MoveRepetiton,
71  MovesRule,
72  CountingRules,
73  OtherDraw,
75  InvalidResultClaim
76  };
77  constexpr static auto c_defaultFormat
78  = "Rank,Name,Elo,Error,Games,Score,DScore";
79 
84  Tournament(GameManager* gameManager, QObject *parent);
92  virtual ~Tournament();
93 
95  virtual QString type() const = 0;
97  GameManager* gameManager() const;
99  bool isFinished() const;
101  QString errorString() const;
108  QString name() const;
110  QString site() const;
112  QString variant() const;
114  int currentRound() const;
124  int gamesPerEncounter() const;
130  int roundMultiplier() const;
132  int finishedGameCount() const;
134  int finalGameCount() const;
136  const TournamentPlayer& playerAt(int index) const;
138  int playerCount() const;
143  int seedCount() const;
150  Sprt* sprt() const;
151 
153  void setName(const QString& name);
155  void setSite(const QString& site);
157  void setVariant(const QString& variant);
163  void setGamesPerEncounter(int count);
168  virtual bool canSetRoundMultiplier() const;
174  void setRoundMultiplier(int factor);
176  void setStartDelay(int delay);
184  void setRecoveryMode(bool recover);
190  void setAdjudicator(const GameAdjudicator& adjudicator);
197  void setOpeningSuite(OpeningSuite* suite);
202  void setOpeningDepth(int plies);
210  void setPgnOutput(const QString& fileName,
212 
219  void setPgnWriteUnfinishedGames(bool enabled);
220 
227  void setPgnCleanupEnabled(bool enabled);
228 
235  void setEpdOutput(const QString& fileName);
236 
243  void setOpeningRepetitions(int count);
256  void setOpeningPolicy(OpeningPolicy policy = DefaultPolicy);
263  void setReverseSides(bool enabled);
270  void setOpeningBookOwnership(bool enabled);
275  void setSeedCount(int seedCount);
282  void setSwapSides(bool enabled);
288  void setResultFormat(const QString &format);
293  QString resultFormat() const;
297  const QMap<QString, QString>& namedResultFormats() const;
301  const QList<QString> resultFieldTokens() const;
306  const QMap<QString, QString>& resultFieldGroups() const;
316  void addPlayer(PlayerBuilder* builder,
317  const TimeControl& timeControl,
318  const OpeningBook* book = nullptr,
319  int bookDepth = 256);
324  virtual QString results() const;
328  virtual QString outcomes() const;
332  bool isStopping() const;
333 
334  public slots:
336  void start();
344  void stop();
345 
346  signals:
355  void gameStarted(ChessGame* game,
356  int number,
357  int whiteIndex,
358  int blackIndex);
372  void gameFinished(ChessGame* game,
373  int number,
374  int whiteIndex,
375  int blackIndex);
382  void gameTerminated(int index,
383  Chess::Result result);
391  void finished();
392 
393  protected:
395  void setCurrentRound(int round);
397  int gamesInProgress() const;
399  TournamentPair* currentPair() const;
406  TournamentPair* pair(int player1, int player2);
413  void startGame(TournamentPair* pair);
420  virtual void onGameAboutToStart(ChessGame* game,
421  const PlayerBuilder* white,
422  const PlayerBuilder* black);
428  int playerIndex(ChessGame* game, Chess::Side side) const;
436  virtual void initializePairing() = 0;
443  virtual int gamesPerCycle() const = 0;
458  virtual TournamentPair* nextPair(int gameNumber) = 0;
465  virtual void onFinished();
474  virtual void addScore(int player, Chess::Side side, int score);
479  virtual void addOutcome(int iWhite,
480  int iBlack,
481  Chess::Result result);
486  virtual bool areAllGamesFinished() const;
494  virtual bool hasGauntletRatingsOrder() const;
495 
496  private slots:
497  void startNextGame();
498  bool writePgn(PgnGame* pgn, int gameNumber);
499  bool writeEpd(ChessGame* game);
500  void onGameStarted(ChessGame* game);
501  void onGameFinished(ChessGame* game);
502  void onGameDestroyed(ChessGame* game);
503  void onGameStartFailed(ChessGame* game);
504 
505  private:
507  enum OutputField
508  {
509  Rank,
510  Name,
511  Games,
512  Wins,
513  Losses,
514  Draws,
515  WhiteWins,
516  WhiteLosses,
517  WhiteDraws,
518  BlackWins,
519  BlackLosses,
520  BlackDraws,
521  Points,
522  Score,
523  DrawScore,
524  EloDiff,
525  ErrorMargin,
526  LOS,
527  WhiteScore,
528  WhiteDrawScore,
529  WhiteEloDiff,
530  WhiteErrorMargin,
531  WhiteLOS,
532  BlackScore,
533  BlackDrawScore,
534  BlackEloDiff,
535  BlackErrorMargin,
536  BlackLOS,
537  TimeForfeits,
538  IllegalMoves,
539  Disconnections,
540  StalledConnections,
541  InvalidResultClaims,
542  RegularLosses,
543  RegularWins,
544  OtherWins,
545  DrawsByStalemate,
546  DrawsByMaterial,
547  DrawsByRepetiton,
548  DrawsByMovesRule,
549  DrawsByCountingRules,
550  DrawsByAdjudication,
551  DrawsByAgreement,
552  OtherDraws,
553  TimeControlString
554  };
555 
556  const QMap<QString, int> m_tokenMap = {
557  {"Rank", Rank},
558  {"Name", Name},
559  {"Games", Games},
560  {"W", Wins},
561  {"L", Losses},
562  {"D", Draws},
563  {"wW", WhiteWins},
564  {"wL", WhiteLosses},
565  {"wD", WhiteDraws},
566  {"bW", BlackWins},
567  {"bL", BlackLosses},
568  {"bD", BlackDraws},
569  {"Points", Points},
570  {"Score", Score},
571  {"DScore", DrawScore},
572  {"Elo", EloDiff},
573  {"Error", ErrorMargin},
574  {"LOS", LOS},
575  {"wScore", WhiteScore},
576  {"wDScore", WhiteDrawScore},
577  {"wElo", WhiteEloDiff},
578  {"wError", WhiteErrorMargin},
579  {"wLOS", WhiteLOS},
580  {"bScore", BlackScore},
581  {"bDScore", BlackDrawScore},
582  {"bElo", BlackEloDiff},
583  {"bError", BlackErrorMargin},
584  {"Time", TimeForfeits},
585  {"Illegal", IllegalMoves},
586  {"Discon", Disconnections},
587  {"Stall", StalledConnections},
588  {"WrClaim", InvalidResultClaims},
589  {"RegulL", RegularLosses},
590  {"RegulW", RegularWins},
591  {"OtherW", OtherWins},
592  {"DStale", DrawsByStalemate},
593  {"DMater", DrawsByMaterial},
594  {"DRepet", DrawsByRepetiton},
595  {"DMoves", DrawsByMovesRule},
596  {"DCount", DrawsByCountingRules},
597  {"DAdj", DrawsByAdjudication},
598  {"DAgree", DrawsByAgreement},
599  {"OtherD", OtherDraws},
600  {"TC", TimeControlString}
601  };
602 
603  const QMap<QString, QString> m_namedGroups =
604  {
605  {"WLDStats", "W,L,D,Points,Score,wW,wL,wD,wPoints,wScore,"
606  "bW,bL,bD,bPoints,bScore"},
607  {"DrawStats", "DScore,DStale,DMater,DRepet,DMoves,DAdj,DAgree,OtherD"},
608  {"Terminations","Time,Illegal,Discon,Stall,WrClaim"}
609  };
610 
611  const QMap<QString, QString> m_namedFormats =
612  {
613  {"minimal", "Rank,Name"},
614  {"small", "Rank,Name,Games,Points"},
615  {"short", "Rank,Name,Elo,Games"},
616  {"concise", "Rank,Name,Elo,Error,Games"},
617  {"default", QString(c_defaultFormat)},
618  {"with-points", "Rank,Name,Elo,Error,Games,Points,Score,DScore"},
619  {"wide", "Rank,Name,Elo,Error,Games,W,L,D,Points,Score,DScore"},
620  {"wide2", "Rank,Name,Elo,Error,Games,W,L,D,Points,"
621  "Score,DScore,wScore,bScore"},
622  {"per-color", "Rank,Name,Elo,Error,Games,W,L,D,Points,"
623  "wW,wL,wD,wPoints,bW,bL,bD,bPoints"},
624  {"ordo", "Rank,Name,Elo,Points,Games,Score"},
625  {"term", "Rank,Name,Elo,Error,Games,Score,"
626  "Time,Illegal,Discon,Stall,WrClaim"},
627  {"draws", "Rank,Name,Elo,Error,Games,W,L,D,Points,Score,DScore,"
628  "DStale,DMater,DRepet,DMoves,DAdj,DAgree,OtherD"},
629  {"wide3", "Rank,Name,TC,Elo,Error,Games,WLDStats,"
630  "DrawStats,Terminations"}
631  };
632 
633  struct GameData
634  {
635  int number;
636  int whiteIndex;
637  int blackIndex;
638  };
639  struct RankingData
640  {
641  QString name;
642  int games;
643  int wins;
644  int losses;
645  int draws;
646  int whiteWins;
647  int whiteLosses;
648  int whiteDraws;
649  int blackWins;
650  int blackLosses;
651  int blackDraws;
652  qreal points;
653  qreal score;
654  qreal drawScore;
655  qreal eloDiff;
656  qreal errorMargin;
657  qreal LOS;
658  qreal whiteScore;
659  qreal whiteDrawScore;
660  qreal whiteEloDiff;
661  qreal whiteErrorMargin;
662  qreal whiteLOS;
663  qreal blackScore;
664  qreal blackDrawScore;
665  qreal blackEloDiff;
666  qreal blackErrorMargin;
667  qreal blackLOS;
668  int timeForfeits;
669  int illegalMoves;
670  int disconnections;
671  int stalledConnections;
672  int invalidResultClaims;
673  int regularLosses;
674  int regularWins;
675  int otherWins;
676  int drawsByStalemate;
677  int drawsByMaterial;
678  int drawsByRepetiton;
679  int drawsByMovesRule;
680  int drawsByCountingRules;
681  int drawsByAdjudication;
682  int drawsByAgreement;
683  int otherDraws;
684  QString timeControl;
685  };
686 
687  QString resultsForSides(int index) const;
688 
689  GameManager* m_gameManager;
690  ChessGame* m_lastGame;
691  QString m_error;
692  QString m_name;
693  QString m_site;
694  QString m_variant;
695  int m_round;
696  int m_oldRound;
697  int m_nextGameNumber;
698  int m_finishedGameCount;
699  int m_savedGameCount;
700  int m_finalGameCount;
701  int m_gamesPerEncounter;
702  int m_roundMultiplier;
703  int m_startDelay;
704  int m_openingDepth;
705  int m_seedCount;
706  bool m_stopping;
707  int m_openingRepetitions;
708  OpeningPolicy m_openingPolicy;
709  bool m_recover;
710  bool m_pgnCleanup;
711  bool m_pgnWriteUnfinishedGames;
712  bool m_finished;
713  bool m_bookOwnership;
714  GameAdjudicator m_adjudicator;
715  OpeningSuite* m_openingSuite;
716  Sprt* m_sprt;
717  QFile m_pgnFile;
718  QTextStream m_pgnOut;
719  QFile m_epdFile;
720  QTextStream m_epdOut;
721  QString m_startFen;
722  int m_repetitionCounter;
723  int m_swapSides;
724  bool m_reverseSides;
725 
726  QString m_resultFormat;
727  PgnGame::PgnMode m_pgnOutMode;
728  TournamentPair* m_pair;
730  QList<TournamentPlayer> m_players;
731  QMap<int, PgnGame> m_pgnGames;
732  QMap<ChessGame*, GameData*> m_gameData;
733  QVector<Chess::Move> m_openingMoves;
734  QMap<int, QString> m_headerMap;
735 };
736 
740 class LIB_EXPORT ResultFormatter : public QObject
741 {
742  Q_OBJECT
743 
744  public:
749  ResultFormatter(const QMap<QString, int>& tokenMap,
750  const QString& format = QString(),
751  QObject* parent = nullptr);
756  void setEntryFormat(const QString& format);
758  QString entryFormat() const;
760  QString entry(const QMap<int, QString>& data) const;
761  private:
762  QString m_entryFormat;
763  QMap<QString, int> m_tokenMap;
764  QStringList m_tokenList;
765 };
766 
767 #endif // TOURNAMENT_H
Use additional data like extra tags and comments.
Definition: pgngame.h:60
A class for constructing new chess players.
Definition: playerbuilder.h:38
Time controls of a chess game.
Definition: timecontrol.h:35
A class for storing a player&#39;s tournament-specific details.
Definition: tournamentplayer.h:31
Definition: tournament.h:66
A game of chess in PGN format.
Definition: pgngame.h:51
Shift on new encounter.
Definition: tournament.h:53
Shift on repetition count and on new encounter.
Definition: tournament.h:52
Definition: chessgame.h:38
Draw Types.
Definition: tournament.h:68
Result error, caused by an invalid result string.
Definition: result.h:63
Regular win (mate, opponent resignation, adjudication)
Definition: tournament.h:63
The side or color of a chess player.
Definition: side.h:35
PgnMode
Definition: pgngame.h:55
The result of a chess game.
Definition: result.h:34
A class for adjudicating chess games.
Definition: gameadjudicator.h:32
A class for managing chess games and players.
Definition: gamemanager.h:40
OpeningPolicy
Definition: tournament.h:50
Base class for chess tournaments.
Definition: tournament.h:44
A collection of opening moves for chess.
Definition: openingbook.h:43
AuxResultType
Definition: tournament.h:58
A single encounter in a tournament.
Definition: tournamentpair.h:31
A Sequential Probability Ratio Test.
Definition: sprt.h:32
A suite of chess openings.
Definition: openingsuite.h:40
Formatter for chess tournament results.
Definition: tournament.h:740