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 
57  constexpr static auto c_defaultFormat
58  = "Rank,Name,Elo,Error,Games,Score,DScore";
59 
64  Tournament(GameManager* gameManager, QObject *parent);
72  virtual ~Tournament();
73 
75  virtual QString type() const = 0;
77  GameManager* gameManager() const;
79  bool isFinished() const;
81  QString errorString() const;
88  QString name() const;
90  QString site() const;
92  QString variant() const;
94  int currentRound() const;
104  int gamesPerEncounter() const;
110  int roundMultiplier() const;
112  int finishedGameCount() const;
114  int finalGameCount() const;
116  const TournamentPlayer& playerAt(int index) const;
118  int playerCount() const;
123  int seedCount() const;
130  Sprt* sprt() const;
131 
133  void setName(const QString& name);
135  void setSite(const QString& site);
137  void setVariant(const QString& variant);
143  void setGamesPerEncounter(int count);
148  virtual bool canSetRoundMultiplier() const;
154  void setRoundMultiplier(int factor);
156  void setStartDelay(int delay);
164  void setRecoveryMode(bool recover);
170  void setAdjudicator(const GameAdjudicator& adjudicator);
177  void setOpeningSuite(OpeningSuite* suite);
182  void setOpeningDepth(int plies);
190  void setPgnOutput(const QString& fileName,
192 
199  void setPgnWriteUnfinishedGames(bool enabled);
200 
207  void setPgnCleanupEnabled(bool enabled);
208 
215  void setEpdOutput(const QString& fileName);
216 
223  void setOpeningRepetitions(int count);
236  void setOpeningPolicy(OpeningPolicy policy = DefaultPolicy);
243  void setReverseSides(bool enabled);
250  void setOpeningBookOwnership(bool enabled);
255  void setSeedCount(int seedCount);
262  void setSwapSides(bool enabled);
268  void setResultFormat(const QString &format);
273  QString resultFormat() const;
277  const QMap<QString, QString>& namedResultFormats() const;
281  const QList<QString> resultFieldTokens() const;
291  void addPlayer(PlayerBuilder* builder,
292  const TimeControl& timeControl,
293  const OpeningBook* book = nullptr,
294  int bookDepth = 256);
299  virtual QString results() const;
300 
301  public slots:
303  void start();
311  void stop();
312 
313  signals:
322  void gameStarted(ChessGame* game,
323  int number,
324  int whiteIndex,
325  int blackIndex);
339  void gameFinished(ChessGame* game,
340  int number,
341  int whiteIndex,
342  int blackIndex);
350  void finished();
351 
352  protected:
354  void setCurrentRound(int round);
356  int gamesInProgress() const;
358  TournamentPair* currentPair() const;
365  TournamentPair* pair(int player1, int player2);
372  void startGame(TournamentPair* pair);
379  virtual void onGameAboutToStart(ChessGame* game,
380  const PlayerBuilder* white,
381  const PlayerBuilder* black);
387  int playerIndex(ChessGame* game, Chess::Side side) const;
395  virtual void initializePairing() = 0;
402  virtual int gamesPerCycle() const = 0;
417  virtual TournamentPair* nextPair(int gameNumber) = 0;
424  virtual void onFinished();
432  virtual void addScore(int player, Chess::Side side, int score);
437  virtual bool areAllGamesFinished() const;
445  virtual bool hasGauntletRatingsOrder() const;
446 
447  private slots:
448  void startNextGame();
449  bool writePgn(PgnGame* pgn, int gameNumber);
450  bool writeEpd(ChessGame* game);
451  void onGameStarted(ChessGame* game);
452  void onGameFinished(ChessGame* game);
453  void onGameDestroyed(ChessGame* game);
454  void onGameStartFailed(ChessGame* game);
455 
456  private:
458  enum OutputField
459  {
460  Rank,
461  Name,
462  Games,
463  Wins,
464  Losses,
465  Draws,
466  WhiteWins,
467  WhiteLosses,
468  WhiteDraws,
469  BlackWins,
470  BlackLosses,
471  BlackDraws,
472  Points,
473  Score,
474  DrawScore,
475  EloDiff,
476  ErrorMargin,
477  LOS,
478  WhiteScore,
479  WhiteDrawScore,
480  WhiteEloDiff,
481  WhiteErrorMargin,
482  WhiteLOS,
483  BlackScore,
484  BlackDrawScore,
485  BlackEloDiff,
486  BlackErrorMargin,
487  BlackLOS
488  };
489 
490  const QMap<QString, int> m_tokenMap = {
491  {"Rank", Rank},
492  {"Name", Name},
493  {"Games", Games},
494  {"W", Wins},
495  {"L", Losses},
496  {"D", Draws},
497  {"wW", WhiteWins},
498  {"wL", WhiteLosses},
499  {"wD", WhiteDraws},
500  {"bW", BlackWins},
501  {"bL", BlackLosses},
502  {"bD", BlackDraws},
503  {"Points", Points},
504  {"Score", Score},
505  {"DScore", DrawScore},
506  {"Elo", EloDiff},
507  {"Error", ErrorMargin},
508  {"LOS", LOS},
509  {"wScore", WhiteScore},
510  {"wDScore", WhiteDrawScore},
511  {"wElo", WhiteEloDiff},
512  {"wError", WhiteErrorMargin},
513  {"wLOS", WhiteLOS},
514  {"bScore", BlackScore},
515  {"bDScore", BlackDrawScore},
516  {"bElo", BlackEloDiff},
517  {"bError", BlackErrorMargin},
518  {"bLOS", BlackLOS}
519  };
520 
521  const QMap<QString, QString> m_namedFormats =
522  {
523  {"minimal", "Rank,Name"},
524  {"small", "Rank,Name,Games,Points"},
525  {"short", "Rank,Name,Elo,Games"},
526  {"concise", "Rank,Name,Elo,Error,Games"},
527  {"default", QString(c_defaultFormat)},
528  {"with-points", "Rank,Name,Elo,Error,Games,Points,Score,DScore"},
529  {"wide", "Rank,Name,Elo,Error,Games,W,L,D,Points,Score,DScore"},
530  {"wide2", "Rank,Name,Elo,Error,Games,W,L,D,Points,"
531  "Score,DScore,wScore,bScore"},
532  {"per-color", "Rank,Name,Elo,Error,Games,W,L,D,Points,"
533  "wW,wL,wD,wPoints,bW,bL,bD,bPoints"},
534  {"ordo", "Rank,Name,Elo,Points,Games,Score"}
535  };
536 
537  struct GameData
538  {
539  int number;
540  int whiteIndex;
541  int blackIndex;
542  };
543  struct RankingData
544  {
545  QString name;
546  int games;
547  int wins;
548  int losses;
549  int draws;
550  int whiteWins;
551  int whiteLosses;
552  int whiteDraws;
553  int blackWins;
554  int blackLosses;
555  int blackDraws;
556  qreal points;
557  qreal score;
558  qreal drawScore;
559  qreal eloDiff;
560  qreal errorMargin;
561  qreal LOS;
562  qreal whiteScore;
563  qreal whiteDrawScore;
564  qreal whiteEloDiff;
565  qreal whiteErrorMargin;
566  qreal whiteLOS;
567  qreal blackScore;
568  qreal blackDrawScore;
569  qreal blackEloDiff;
570  qreal blackErrorMargin;
571  qreal blackLOS;
572  };
573 
574  QString resultsForSides(int index) const;
575 
576  GameManager* m_gameManager;
577  ChessGame* m_lastGame;
578  QString m_error;
579  QString m_name;
580  QString m_site;
581  QString m_variant;
582  int m_round;
583  int m_oldRound;
584  int m_nextGameNumber;
585  int m_finishedGameCount;
586  int m_savedGameCount;
587  int m_finalGameCount;
588  int m_gamesPerEncounter;
589  int m_roundMultiplier;
590  int m_startDelay;
591  int m_openingDepth;
592  int m_seedCount;
593  bool m_stopping;
594  int m_openingRepetitions;
595  OpeningPolicy m_openingPolicy;
596  bool m_recover;
597  bool m_pgnCleanup;
598  bool m_pgnWriteUnfinishedGames;
599  bool m_finished;
600  bool m_bookOwnership;
601  GameAdjudicator m_adjudicator;
602  OpeningSuite* m_openingSuite;
603  Sprt* m_sprt;
604  QFile m_pgnFile;
605  QTextStream m_pgnOut;
606  QFile m_epdFile;
607  QTextStream m_epdOut;
608  QString m_startFen;
609  int m_repetitionCounter;
610  int m_swapSides;
611  bool m_reverseSides;
612 
613  QString m_resultFormat;
614  PgnGame::PgnMode m_pgnOutMode;
615  TournamentPair* m_pair;
617  QList<TournamentPlayer> m_players;
618  QMap<int, PgnGame> m_pgnGames;
619  QMap<ChessGame*, GameData*> m_gameData;
620  QVector<Chess::Move> m_openingMoves;
621 };
622 
626 class LIB_EXPORT ResultFormatter : public QObject
627 {
628  Q_OBJECT
629 
630  public:
635  ResultFormatter(const QMap<QString, int>& tokenMap,
636  const QString& format = QString(),
637  QObject* parent = nullptr);
642  void setEntryFormat(const QString& format);
644  QString entryFormat() const;
646  QString entry(const QMap<int, QString>& data) const;
647  private:
648  QString m_entryFormat;
649  QMap<QString, int> m_tokenMap;
650  QStringList m_tokenList;
651 };
652 
653 #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:29
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
The side or color of a chess player.
Definition: side.h:35
PgnMode
Definition: pgngame.h:55
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
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:626