home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / game / think / chaos / src / pairings.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-27  |  32.1 KB  |  1,394 lines

  1. /*  Chaos:            The Chess HAppening Organisation System    V5.2
  2.     Copyright (C)   1993    Jochen Wiedmann
  3.  
  4.     This program 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 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program 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 this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.  
  19.     $RCSfile: Pairings.c,v $
  20.     $Revision: 2.4 $
  21.     $Date: 1993/09/24 16:57:37 $
  22.  
  23.     This file contains the pairing-functions. The algorithm of the
  24.     swiss-pairing is described in the function LoseGroup().
  25.  
  26.     Computer:    Amiga 1200            Compiler:    Dice 2.07.54 (3.0)
  27.  
  28.     Author:    Jochen Wiedmann
  29.         Am Eisteich 9
  30.       72555 Metzingen
  31.         Tel. 07123 / 14881
  32.         Internet: wiedmann@mailserv.zdv.uni-tuebingen.de
  33. */
  34.  
  35.  
  36. #ifndef CHAOS_H
  37. #include "chaos.h"
  38. #endif
  39.  
  40.  
  41.  
  42.  
  43. /*
  44.     This structure is used to build the groups of players with same numbers
  45.     of points.
  46. */
  47. struct Group
  48. { struct Group *Next;
  49.   struct Player *First;
  50.   struct Player *Last;
  51.   int NumMembers;
  52.   int NumAllocMembers;
  53. };
  54.  
  55.  
  56.  
  57. /*
  58.     Some functions to administrate a double-linked-list. They are close
  59.     to the corresponding Exec-functions.
  60. */
  61. /*
  62.     MyRemove() removes player t from group g.
  63. */
  64. void MyRemove(struct Group *g, struct Player *t)
  65.  
  66. {
  67.   if (t->LT_Pred == NULL)
  68.   { g->First = t->LT_Succ;
  69.   }
  70.   else
  71.   { t->LT_Pred->LT_Succ = t->LT_Succ;
  72.   }
  73.   if (t->LT_Succ == NULL)
  74.   { g->Last = t->LT_Pred;
  75.   }
  76.   else
  77.   { t->LT_Succ->LT_Pred = t->LT_Pred;
  78.   }
  79.   g->NumMembers--;
  80. }
  81.  
  82.  
  83.  
  84.  
  85. /*
  86.     MyAddTail() adds player t to the tail of group g.
  87. */
  88. void MyAddTail(struct Group *g, struct Player *t)
  89.  
  90. { if ((t->LT_Pred = g->Last)  ==  NULL)
  91.   { g->First = t;
  92.   }
  93.   else
  94.   { g->Last->LT_Succ = t;
  95.   }
  96.   t->LT_Succ = NULL;
  97.   g->Last = t;
  98.   g->NumMembers++;
  99. }
  100.  
  101.  
  102.  
  103.  
  104. /*
  105.     MyAddHead() adds player t to the head of group g.
  106. */
  107. void MyAddHead(struct Group *g, struct Player *t)
  108.  
  109. { if ((t->LT_Succ = g->First)  ==  NULL)
  110.   { g->Last = t;
  111.   }
  112.   else
  113.   { g->First->LT_Pred = t;
  114.   }
  115.   t->LT_Pred = NULL;
  116.   g->First = t;
  117.   g->NumMembers++;
  118. }
  119.  
  120.  
  121.  
  122.  
  123. /*
  124.     MyInsert() inserts player t into group g after player pred.
  125.     Note, that pred may be null, in which case MyAddHead() is called.
  126. */
  127. void MyInsert(struct Group *g, struct Player *t, struct Player *pred)
  128.  
  129. { if (pred == NULL)
  130.   { MyAddHead(g, t);
  131.   }
  132.   else
  133.   { if (pred->LT_Succ == NULL)
  134.     { MyAddTail(g, t);
  135.     }
  136.     else
  137.     { t->LT_Succ = pred->LT_Succ;
  138.       t->LT_Pred = pred;
  139.       pred->LT_Succ->LT_Pred = t;
  140.       pred->LT_Succ = t;
  141.       g->NumMembers++;
  142.     }
  143.   }
  144. }
  145.  
  146.  
  147.  
  148.  
  149. /*
  150.     MyEnqueue() inserts player t into group g. The group is sorted by the
  151.     player->Nr fields.
  152. */
  153. void MyEnqueue(struct Group *g, struct Player *t)
  154.  
  155. { struct Player *succ;
  156.  
  157.   for (succ = g->First;  succ != NULL;  succ = succ->LT_Succ)
  158.   { if (t->Nr < succ->Nr)
  159.     { break;
  160.     }
  161.   }
  162.   if (succ == NULL)
  163.   { MyAddTail(g, t);
  164.   }
  165.   else
  166.   { MyInsert(g, t, succ->LT_Pred);
  167.   }
  168. }
  169.  
  170.  
  171.  
  172. /*
  173.     GameAddress() returns the address of a game-structure.
  174.  
  175.     Inputs: t - player, whose game list will be searched
  176.         r - number of the round, which's game structure will be
  177.         returned. This may be 0, in which case the address of
  178.         t->First_Game will be returned.
  179.  
  180.     Result: pointer to the game structure corresponding to player t, round r
  181. */
  182. struct Game *GameAddress(struct Player *t, int r)
  183.  
  184. { struct Game *g;
  185.  
  186.   for (g = (struct Game *) &(t->First_Game);  r != 0;
  187.        g = g->Next, r--)
  188.   {
  189.   }
  190.   return(g);
  191. }
  192.  
  193.  
  194.  
  195. /*
  196.     The NewGames() function gets called after each round that has been
  197.     paired. It assumes, that each players Opponent, GFlags and BoardNr fields
  198.     are initialized.
  199.  
  200.     Inputs: memlistptr - pointer to the memory list, where the allocated
  201.              memory will be included.
  202.         fpoints    - number of points, that free players will receive
  203.              (2 for swiss pairing, 0 for round robin)
  204.  
  205.     Result: TRUE, if successful, FALSE otherwise
  206. */
  207. static int NewGames(void **memlistptr, int fpoints)
  208.  
  209. { struct Game *g, *gmem;
  210.   struct Player *t;
  211.   int NumGames = 0;
  212.  
  213.   /*
  214.       Allocate new game structures.
  215.   */
  216.   if ((gmem = GetMem(memlistptr, sizeof(*g)*NumPlayers))  ==  NULL)
  217.   { return(FALSE);
  218.   }
  219.   NumRounds++;
  220.  
  221.   /*
  222.       Initialize the game structures.
  223.   */
  224.   for (t = ((struct Player *) PlayerList.lh_Head), g = gmem;
  225.        t->Tn_Node.ln_Succ != NULL;
  226.        t = (struct Player *) t->Tn_Node.ln_Succ, g++)
  227.   { if (t->Flags & TNFLAGSF_WITHDRAWN)
  228.     { t->GFlags |= GMFLAGSF_NOFIGHT;
  229.     }
  230.     else if (t->Opponent == NULL)
  231.     { t->GFlags |= GMFLAGSF_NOFIGHT|GMFLAGSF_POINTFORFREE;
  232.     }
  233.  
  234.     GameAddress(t, NumRounds-1)->Next = g;
  235.  
  236.     g->Next = NULL;
  237.     g->Opponent = t->Opponent;
  238.     g->Flags = t->GFlags;
  239.     g->BoardNr = t->BoardNr;
  240.  
  241.     if (g->Flags & GMFLAGSF_NOFIGHT)
  242.     { if (t->Flags & TNFLAGSF_WITHDRAWN)
  243.       { g->Result = 0;
  244.     g->Flags = GMFLAGSF_WITHDRAWN;
  245.       }
  246.       else
  247.       { t->Points += fpoints;
  248.     g->Result = fpoints;
  249.     t->Flags |= TNFLAGSF_HADFREE;
  250.       }
  251.     }
  252.     else
  253.     { NumGames++;
  254.       g->Result = -1;
  255.       if (g->Flags & GMFLAGSF_WHITE)
  256.       { t->HowMuchWhite++;
  257.     if (t->HowMuchWhiteLast > 0)
  258.     { t->HowMuchWhiteLast++;
  259.     }
  260.     else
  261.     { t->HowMuchWhiteLast = 1;
  262.     }
  263.       }
  264.       else
  265.       { t->HowMuchWhite--;
  266.     if (t->HowMuchWhiteLast < 0)
  267.     { t->HowMuchWhiteLast--;
  268.     }
  269.     else
  270.     { t->HowMuchWhiteLast = -1;
  271.     }
  272.       }
  273.     }
  274.   }
  275.  
  276.   NumGamesMissing = NumGames/2;
  277.   return(TRUE);;
  278. }
  279.  
  280.  
  281.  
  282.  
  283. /*
  284.     The function CreateRankings() creates the internal ranking list.
  285.     The list is sorted by ELO numbers (if players have one) and by DWZ
  286.     numbers otherwise.
  287.     Players without rating number will appear at the tail of the list
  288. */
  289. void CreateRankings(void)
  290.  
  291. { struct Player *t, **tptr;
  292.   int t1, t2;
  293.  
  294.   RankingFirst = NULL;
  295.   for (t = (struct Player *) PlayerList.lh_Head;
  296.        t->Tn_Node.ln_Succ != NULL;
  297.        t = (struct Player *) t->Tn_Node.ln_Succ)
  298.   { /*
  299.     Get the curremt players (t) rating number into t1.
  300.     */
  301.     if ((t1 = t->ELO)  ==  0)
  302.     { t1 = tdwz(t);
  303.     }
  304.  
  305.     /*
  306.     Insert t into the internal ranking list
  307.     */
  308.     for (tptr = &RankingFirst;  *tptr != NULL;
  309.      tptr = &((*tptr)->RankNext))
  310.     { if (t1 != 0)
  311.       { if ((t2 = (*tptr)->ELO)  ==  0)
  312.     { t2 = tdwz(*tptr);
  313.     }
  314.     if (t1 > t2)
  315.     { break;
  316.     }
  317.       }
  318.     }
  319.  
  320.     t->RankNext = *tptr;
  321.     *tptr = t;
  322.   }
  323. }
  324.  
  325.  
  326.  
  327.  
  328. /*
  329.     DoSwissPairingFirst() makes the pairings of the first round of a
  330.     Swiss pairing tournament.
  331.  
  332.     Inputs: user - True, if the user may set games
  333. */
  334. static int DoSwissPairingFirst(int user)
  335.  
  336. { struct Player *t, *thelp;
  337.   int NumGames, NumFreePlayers;
  338.   int MinRating, AktRating, NumMinRating;
  339.   int i, j;
  340.   int flag;
  341.   int BoardNr;
  342.  
  343.   /*
  344.       Build the internal ranking list
  345.   */
  346.   CreateRankings();
  347.  
  348.  
  349.   /*
  350.       Allow the user to make settings.
  351.   */
  352.   if ((BoardNr = GetSettings(user))  ==  -1)
  353.   { return(FALSE);
  354.   }
  355.  
  356.   /*
  357.       get colour of player 1
  358.   */
  359.   flag = (RangeRand(2) == 0)  ?  GMFLAGSF_WHITE  :  0;
  360.  
  361.  
  362.  
  363.   /*
  364.       Count the number of players without opponent. Get the number of players
  365.       with minimal rating. Additionally setup the colors for set players.
  366.   */
  367.   NumFreePlayers = 0;
  368.   NumMinRating = 0;
  369.   if ((MinRating = RankingFirst->ELO)  ==  0)
  370.   { MinRating = tdwz(RankingFirst);
  371.   }
  372.   for (t = RankingFirst;  t != NULL;  t = t->RankNext)
  373.   { if (t->Opponent == NULL)
  374.     { NumFreePlayers++;
  375.       if ((AktRating = t->ELO)  ==  0)
  376.       { AktRating = tdwz(t);
  377.       }
  378.       if (AktRating < MinRating)
  379.       { NumMinRating = 0;
  380.     MinRating = AktRating;
  381.       }
  382.       if (MinRating == AktRating)
  383.       { NumMinRating++;
  384.       }
  385.     }
  386.     else
  387.     { if ((t->GFlags & GMFLAGSF_WHITE) == 0  &&
  388.       (t->Opponent->GFlags & GMFLAGSF_WHITE) == 0)
  389.       { thelp = (t->Nr < thelp->Nr)  ?  t  :  t->Opponent;
  390.     thelp->GFlags = flag;
  391.     flag = thelp->Opponent->GFlags = GMFLAGSF_WHITE - flag;
  392.       }
  393.     }
  394.   }
  395.  
  396.  
  397.   /*
  398.       Give a point for free, if needed.
  399.   */
  400.   NumGames = NumFreePlayers/2;
  401.   if (NumFreePlayers % 2  !=  0)
  402.   { if (NumMinRating < 5)
  403.     { NumMinRating = (5 < NumGames) ? 5 : NumGames;
  404.     }
  405.     j = RangeRand(NumMinRating)+1;
  406.     t = RankingFirst;
  407.     i = 0;
  408.     for(;;)
  409.     { if (t->Opponent == NULL)
  410.       { if (i == NumFreePlayers - j)
  411.     { break;
  412.     }
  413.     i++;
  414.       }
  415.       t = t->RankNext;
  416.     }
  417.     t->GFlags = GMFLAGSF_POINTFORFREE|GMFLAGSF_NOFIGHT;
  418.   }
  419.  
  420.  
  421.   /*
  422.       Do the other pairings. t points into the upper and thelp into the
  423.       lower half of the players.
  424.   */
  425.   thelp = RankingFirst;   /*  initialize thelp        */
  426.   for (i = 0;  i < NumGames; thelp = thelp->RankNext)
  427.   { if ((thelp->GFlags & GMFLAGSF_POINTFORFREE)  ==  0   &&
  428.     thelp->Opponent == NULL)
  429.     { i++;
  430.     }
  431.   }
  432.   t = RankingFirst;      /*  initialize t        */
  433.  
  434.   for (i = 0;  i < NumGames;  i++)
  435.   { /*
  436.     t and thelp must not point on free or set players!
  437.     */
  438.     while ((t->GFlags & GMFLAGSF_POINTFORFREE)  !=  0   ||
  439.        t->Opponent != NULL)
  440.     { t = t->RankNext;
  441.     }
  442.     while ((thelp->GFlags & GMFLAGSF_POINTFORFREE)  !=  0   ||
  443.        t->Opponent != NULL)
  444.     { thelp = thelp->RankNext;
  445.     }
  446.  
  447.     t->Opponent = thelp;
  448.     thelp->Opponent = t;
  449.     t->BoardNr = thelp->BoardNr = i + BoardNr;
  450.     t->GFlags = flag;
  451.     thelp->GFlags = GMFLAGSF_WHITE - flag;
  452.     flag = thelp->GFlags;
  453.     t = t->RankNext;
  454.     thelp = thelp->RankNext;
  455.   }
  456.   return(TRUE);
  457. }
  458.  
  459.  
  460.  
  461. /*
  462.     GamePossible() checks, if two players may be paired.
  463. */
  464. int GamePossible(struct Player *t1, struct Player *t2)
  465.  
  466. { struct Game *g;
  467.  
  468.   if (t1 == t2  ||
  469.       (t1->HowMuchWhiteLast >= 2  &&  t2->HowMuchWhiteLast >= 2)  ||
  470.       (t1->HowMuchWhiteLast <= -2  &&  t2->HowMuchWhiteLast <= -2))
  471.   { return(FALSE);
  472.   }
  473.  
  474.   for (g = t1->First_Game;  g != NULL;  g = g->Next)
  475.   { if (g->Opponent == t2)
  476.     { return(FALSE);
  477.     }
  478.   }
  479.   return(TRUE);
  480. }
  481.  
  482.  
  483.  
  484.  
  485. /*
  486.     The DoGame() function looks for a game in the actual group.
  487.  
  488.     Inputs: g            - group in which should be looked
  489.         t            - player, who should get an opponent
  490.         BoardNr        - number of the first board that isn't used
  491.         MayChangeColors - number of allowed pairings in the same color
  492.                   group
  493.         MayGoUpperHalf  - number of allowed pairings in the same (upper
  494.                   or lower) half
  495.         MayGoDown        - number players that may be left (1, if the
  496.                   number of players in the group is odd)
  497.  
  498.     Results:  0 = No pairings possible, terminate
  499.           1 = Successfull finish, terminate
  500.          -1 = The lower groups could not be paired and this is a group
  501.           with an even number of players. Put one into the next!
  502. */
  503. static int DoGroup(struct Group *);
  504. static int DoGame(struct Group *g, struct Player *t,
  505.               int MayChangeColors, int MayGoUpperHalf, int MayGoDown)
  506.  
  507. { struct Player *tg, *thelp, *LowerHalf;
  508.   int result;
  509.   int IsLowerHalf, i;
  510.  
  511.   switch (g->NumMembers-g->NumAllocMembers)
  512.   { /*
  513.     Only one player left? Put him into the next group or give him
  514.     a point for free if there are no lower groups.
  515.     */
  516.     case 1:
  517.       /*
  518.       Look for free player
  519.       */
  520.       for (t = g->First;  t->Opponent != NULL;  t = t->LT_Succ)
  521.       {
  522.       }
  523.  
  524.       if (g->Next == NULL)
  525.       { if (t->Flags & TNFLAGSF_HADFREE)
  526.     { return(0);
  527.     }
  528.     t->GFlags |= GMFLAGSF_POINTFORFREE|GMFLAGSF_NOFIGHT;
  529.     return(1);
  530.       }
  531.  
  532.       if (t->Flags & TNFLAGSF_NOTDOWN)
  533.       { return (FALSE);
  534.       }
  535.       thelp = t->LT_Pred;
  536.       MyRemove(g, t);
  537.       MyEnqueue(g->Next, t);
  538.       if ((result = DoGroup(g->Next))  ==  FALSE)
  539.       /*
  540.       Remember, that it doesn't help to put this player into the next
  541.       group.
  542.       */
  543.       { t->Flags |= TNFLAGSF_NOTDOWN;
  544.       }
  545.       MyRemove(g->Next, t);
  546.       MyInsert(g, t, thelp);
  547.       return(result);
  548.  
  549.  
  550.     /*
  551.     Group finished? On to the next! If not successfull now, we need
  552.     to change the group!
  553.     */
  554.     case 0:
  555.       return((DoGroup(g->Next) == 0)  ?  -1  :  1);
  556.  
  557.  
  558.     /*
  559.     We still need to do some pairings in this group. Sigh!
  560.     */
  561.     default:
  562.       /*
  563.       Looking for a player without opponent.
  564.       */
  565.       while (t->Opponent != NULL)
  566.       { t = t->LT_Succ;
  567.       }
  568.  
  569.       /*
  570.       We must not look for opponents in the upper half, if this player
  571.       is from the lower half! So check, if he is.
  572.       */
  573.       IsLowerHalf = TRUE;
  574.       LowerHalf = g->First;
  575.       i = (g->NumMembers/2);
  576.       do
  577.       { if (LowerHalf == t)
  578.     { IsLowerHalf = FALSE;
  579.     }
  580.     LowerHalf = LowerHalf->LT_Succ;
  581.       }
  582.       while(--i);
  583.  
  584.  
  585.       /*
  586.       Try to get an opponent in the lower half with different colors.
  587.       */
  588.       for (tg = IsLowerHalf ? t->LT_Succ : LowerHalf;   tg != NULL;
  589.        tg = tg->LT_Succ)
  590.       { if (tg->Opponent == NULL  &&
  591.         t->HowMuchWhiteLast*tg->HowMuchWhiteLast <= 0  &&
  592.         GamePossible(t, tg))
  593.     { /*
  594.           Try this pairing
  595.       */
  596.       t->Opponent = tg;
  597.       tg->Opponent = t;
  598.       g->NumAllocMembers += 2;
  599.       /*
  600.           Check if the remaining players may get paired.
  601.       */
  602.       if ((result = DoGame(g, t->LT_Succ,
  603.                    MayChangeColors, MayGoUpperHalf, MayGoDown))
  604.               !=  0)
  605.       { return(result);
  606.       }
  607.       /*
  608.           They don't, so undo this pairing.
  609.       */
  610.       g->NumAllocMembers -= 2;
  611.       t->Opponent = tg->Opponent = NULL;
  612.     }
  613.       }
  614.  
  615.       /*
  616.       Looking for an opponent in the lower half without changing the
  617.       colors. (If it is allowed!)
  618.       */
  619.       if (MayChangeColors != 0)
  620.       { for (tg = IsLowerHalf ? t->LT_Succ : LowerHalf;   tg != NULL;
  621.          tg = tg->LT_Succ)
  622.     { if (tg->Opponent == NULL  &&
  623.           t->HowMuchWhiteLast*tg->HowMuchWhiteLast > 0  &&
  624.           GamePossible(t, tg))
  625.       { /*
  626.         Try this pairing
  627.         */
  628.         t->Opponent = tg;
  629.         tg->Opponent = t;
  630.         g->NumAllocMembers += 2;
  631.         /*
  632.         Check if the remaining players may get paired.
  633.         */
  634.         if ((result = DoGame(g, t->LT_Succ, MayChangeColors-1,
  635.                  MayGoUpperHalf, MayGoDown))
  636.             !=  0)
  637.         { return(result);
  638.         }
  639.         /*
  640.         They don't, so undo this pairing.
  641.         */
  642.         g->NumAllocMembers -= 2;
  643.         t->Opponent = tg->Opponent = NULL;
  644.       }
  645.     }
  646.       }
  647.  
  648.       /*
  649.       Looking for an opponent in the upper half with different colors.
  650.       (If it is allowed!)
  651.       */
  652.       if (MayGoUpperHalf != 0)
  653.       { for (tg = IsLowerHalf ? NULL : LowerHalf->LT_Pred;
  654.          tg != NULL  &&  tg != t;  tg = tg->LT_Pred)
  655.     { if(tg->Opponent == NULL  &&
  656.          t->HowMuchWhiteLast*tg->HowMuchWhiteLast <= 0  &&
  657.          GamePossible(t, tg))
  658.       { /*
  659.         Try this pairing
  660.         */
  661.         t->Opponent = tg;
  662.         tg->Opponent = t;
  663.         g->NumAllocMembers += 2;
  664.         /*
  665.         Check if the remaining players may get paired.
  666.         */
  667.         if ((result = DoGame(g, t->LT_Succ, MayChangeColors,
  668.                  MayGoUpperHalf-1, MayGoDown))
  669.             !=  0)
  670.         { return(result);
  671.         }
  672.         /*
  673.         They don't, so undo this pairing.
  674.         */
  675.         g->NumAllocMembers -= 2;
  676.         t->Opponent = tg->Opponent = NULL;
  677.       }
  678.     }
  679.       }
  680.  
  681.       /*
  682.       Finally looking for an opponent in the upper half without
  683.       changing colors. Do we really need this? Arrgl! (It would not be
  684.       allowed otherwise.)
  685.       */
  686.       if (MayChangeColors != 0  &&  MayGoUpperHalf != 0)
  687.       { for (tg = IsLowerHalf ? NULL : LowerHalf->LT_Pred;
  688.          tg != NULL  &&  tg != t;  tg = tg->LT_Pred)
  689.     { if(tg->Opponent == NULL  &&
  690.          t->HowMuchWhiteLast*tg->HowMuchWhiteLast > 0  &&
  691.          GamePossible(t, tg))
  692.       { /*
  693.         Try this pairing
  694.         */
  695.         t->Opponent = tg;
  696.         tg->Opponent = t;
  697.         g->NumAllocMembers += 2;
  698.         /*
  699.         Check if the remaining players may get paired.
  700.         */
  701.         if ((result = DoGame(g, t->LT_Succ, MayChangeColors-1,
  702.                  MayGoUpperHalf-1, MayGoDown))
  703.             !=  0)
  704.         { return(result);
  705.         }
  706.         /*
  707.         They don't, so undo this pairing.
  708.         */
  709.         g->NumAllocMembers -= 2;
  710.         t->Opponent = tg->Opponent = NULL;
  711.       }
  712.     }
  713.       }
  714.  
  715.       /*
  716.       The LAST possibility is to drop one player, who will finally
  717.       get moved into the next group.
  718.       */
  719.       if (MayGoDown)
  720.       { return(DoGame(g, t->LT_Succ, MayChangeColors,
  721.               MayGoUpperHalf, 0));
  722.       }
  723.     }
  724.  
  725.   /*
  726.       Nothing helps. We cannot pair the remaining members of this group.
  727.       Give up!
  728.   */
  729.   return(0);
  730. }
  731.  
  732.  
  733.  
  734.  
  735. /*
  736.     The DoGroup() function is the main function to do the Swiss pairing.
  737.     It pairs the member of one group of players (Usually the players with
  738.     the same number of points.) and calls itself for the next group, if
  739.     possible.
  740.  
  741.     Inputs: g        -    the group to pair
  742.  
  743.     Results: 1 = Success, finish!
  744.          0 = The group could not be paired, so the higher groups need
  745.          to be rearranged.
  746.  
  747.  
  748.     The algorithm depends on the rules from the "Turnierleiterhandbuch des
  749.     Deutschen Schachbundes" (The official german chess federation's guide
  750.     to managing chess-tournaments). But this rules aren't quite clear and
  751.     especially not definite. For example i don't see what should happen,
  752.     if some players with the same number of points are leading, but have
  753.     already been paired against each other.
  754.  
  755.     The pairing begins with collecting the players in groups of members
  756.     with the same number of points. These groups are used to build the new
  757.     internal ranking list. Players of one group are ordered by the previous
  758.     list and by the official lists in the first round. DoGroup() gets called
  759.     for the highest group, if successfull for the next group and so on.
  760.  
  761.     But how to pair the group-members? We begin with splitting the members
  762.     in an upper and a lower half. (The lower has possibly one member more,
  763.     if the number of group-members is odd.) Both halfs get splitted again,
  764.     depending on their last color. Example: (The players to the left had
  765.     white in the last round.)
  766.  
  767.         Upper Half:   1   3
  768.                   2
  769.         Lower Half:   4   5
  770.                   7   6
  771.  
  772.     DoGame() gets called to find an opponent for player 1. We try player 5
  773.     and next player 6. DoGame() calls itself for an opponent of player 2,
  774.     if successfull. If it is successfull again, it calls itself for the third
  775.     time, to find an opponent for player 3. When 3 games have been found,
  776.     this group is done and we move the remaining player into the next group
  777.     and call DoGroup() for it.
  778.  
  779.     But possibly it isn't possible to pair the players in that way. In that
  780.     case we need to allow some things that we don't like very much: Pairing
  781.     opponents of the same color (1-4), pairing opponents from the upper half
  782.     (1-3) or dropping players which will get moved into the next group
  783.     then. The variables MayChangeColors, MayGoUpperHalf and MayGoDown tell
  784.     us, what is allowed.
  785.  
  786.     A game is possible, if
  787.     a) The same players haven't already been paired
  788.     b) No opponent has the same color for the third time (However, it
  789.        is allowed to have four times black in 5 rounds! Not my choice,
  790.        i follow the guide mentioned above.)
  791.     c) the remaining players may be paired and a) and b) holds still
  792.        for them
  793. */
  794. static int DoGroup(struct Group *g)
  795.  
  796. { struct Player *t;
  797.   int result;
  798.   int MayChangeColors, MayGoUpperHalf, MayGoDown;
  799.  
  800. #ifdef DEBUG_PAIRINGS
  801. #ifdef AZTEC_C
  802. #define strchr index
  803. #endif
  804.  
  805.   /*
  806.       Print the list of group members.
  807.   */
  808.   { extern struct Group *FirstGroup;
  809.     struct Group *myg;
  810.     struct Player *myt;
  811.     char *myptr;
  812.  
  813.     for (myg = FirstGroup;  myg != NULL;  myg = myg->Next)
  814.     { printf("(");
  815.       for(myt = myg->First;  myt != NULL;  myt = myt->LT_Succ)
  816.       { if (myt != myg->First)
  817.     { printf(",");
  818.     }
  819.     myptr = strchr(myt->Name, '(')+1;
  820.     while (*myptr != ')')
  821.     { printf("%c", *(myptr++));
  822.     }
  823.       }
  824.       printf(")");
  825.     }
  826.     printf("\n");
  827.   }
  828. #endif    /*  DEBUG_PAIRINGS   */
  829.  
  830.   /*
  831.       Looking for a nonempty group (Not sure, if this might happen, but
  832.       does no harm!
  833.   */
  834.   while(g != NULL  &&  g->NumMembers == 0)
  835.   { g = g->Next;
  836.   }
  837.   if (g == NULL)
  838.   { return(1);
  839.   }
  840.  
  841.  
  842.   /*
  843.       Remove the TNFLAGSF_NOTDOWN flag from all group members. (They might
  844.       have received it sooner.)
  845.   */
  846.   for(t = g->First;  t != NULL;  t = t->LT_Succ)
  847.   { t->Flags &= ~TNFLAGSF_NOTDOWN;
  848.   }
  849.  
  850.   /*
  851.       We begin with allowing NOTHING and slowly a little bit more...
  852.       (I would be glad, if any body knew a faster solution!)
  853.   */
  854.   MayGoDown = 0;
  855.   do
  856.   { MayGoUpperHalf = 0;
  857.     do
  858.     { MayChangeColors = 0;
  859.       do
  860.       { switch (DoGame(g, g->First, MayChangeColors, MayGoUpperHalf,
  861.                MayGoDown))
  862.     { case 1:
  863.         /*
  864.         Success!
  865.         */
  866.         return(1);
  867.       case -1:
  868.         /*
  869.         The following groups cannot be paired! Senseless to pair this
  870.         group. Undo all pairings and change the group.
  871.         */
  872.         for (t = g->First;  t != NULL;  t = t->LT_Succ)
  873.         { t->Opponent = NULL;
  874.         }
  875.         g->NumAllocMembers = 0;
  876.         goto changegroup;
  877.     }
  878.       }
  879.       while (++MayChangeColors <= g->NumMembers/2);
  880.     }
  881.     while (++MayGoUpperHalf <= g->NumMembers/4);
  882.   }
  883.   while (++MayGoDown <= g->NumMembers % 2);
  884.  
  885. changegroup:
  886.   /*
  887.       It isn't possible, to pair this group. The last possibility is to
  888.       move one player into the next and retry. (DoGame() has already done
  889.       this, if there is only one member.)
  890.   */
  891.   if (g->Next != NULL  &&  g->NumMembers != 1)
  892.   { t = g->Last;
  893.     MyRemove(g, t);
  894.     MyEnqueue(g->Next, t);
  895.     result = DoGroup(g);
  896.     if (result)
  897.     { return(TRUE);
  898.     }
  899.     MyRemove(g->Next, t);
  900.     MyAddTail(g, t);
  901.   }
  902.  
  903.   /*
  904.       Doesn't help! We have failed!
  905.   */
  906.   return(FALSE);
  907. }
  908.  
  909.  
  910.  
  911.  
  912. /*
  913.     The DoSwissPairing() function gets called instead of
  914.     DoSwissPairingFirst() for round 2 and later.
  915.  
  916.     Inputs: user - TRUE, if the user may set games
  917.  
  918.     Result: TRUE, if succesfull, FALSE otherwise
  919. */
  920. #ifdef DEBUG_PAIRINGS
  921. struct Group *FirstGroup;
  922. #endif
  923. static int DoSwissPairing(int user)
  924.  
  925. { struct Player *t, *thelp, **tptr;
  926.   struct Group *g, **gptr;
  927.   void *PKey = NULL;
  928.   short gpoints;
  929.   int BoardNr;
  930.   int i, result;
  931. #ifndef DEBUG_PAIRINGS
  932.   struct Group *FirstGroup;
  933. #endif
  934.  
  935.   FirstGroup = NULL;
  936.  
  937.   /*
  938.       First create the new ranking list
  939.   */
  940.   t = RankingFirst;
  941.   RankingFirst = NULL;
  942.   while (t != NULL)
  943.   { for (tptr = &RankingFirst;  *tptr != NULL;
  944.      tptr = &((*tptr)->RankNext))
  945.     { if (t->Points > (*tptr)->Points)
  946.       { break;
  947.       }
  948.     }
  949.     thelp = t->RankNext;
  950.     t->RankNext = *tptr;
  951.     *tptr = t;
  952.     t = thelp;
  953.   }
  954.  
  955.  
  956.   /*
  957.       Allow the user to make settings.
  958.   */
  959.   if ((BoardNr = GetSettings(user))  ==  -1)
  960.   { return(FALSE);
  961.   }
  962.  
  963.  
  964.   /*
  965.       Create the groups of players with the same number of points
  966.   */
  967.   gpoints = -1;   /*
  968.               Force initialization of g and gpoints in the following
  969.               loop.
  970.           */
  971.   gptr = &FirstGroup;
  972.   i = 0;
  973.   for (t = RankingFirst;  t != NULL;  t = t->RankNext)
  974.   { t->Nr = i++;
  975.     if (t->Flags & TNFLAGSF_WITHDRAWN  ||  t->Opponent != NULL  ||
  976.     t->GFlags & GMFLAGSF_NOFIGHT)
  977.     { continue;
  978.     }
  979.     if (gpoints != t->Points)   /*  Create new group    */
  980.     { if ((g = GetMem(&PKey, sizeof(*g)))  ==  NULL)
  981.       { PutMemList(&PKey);
  982.     return(FALSE);
  983.       }
  984.       *gptr = g;
  985.       gptr = &(g->Next);
  986.       gpoints = t->Points;
  987.     }
  988.  
  989.     MyAddTail(g, t);
  990.   }
  991.  
  992.  
  993. #ifdef AMIGA
  994.   set(App, MUIA_Application_Sleep, TRUE);
  995. #endif
  996.   result = DoGroup(FirstGroup);
  997. #ifdef AMIGA
  998.   set(App, MUIA_Application_Sleep, FALSE);
  999. #endif
  1000.  
  1001.   if (!result)
  1002.   { ShowError((char *) GetChaosString(MSG_NO_PAIRING));
  1003.     PutMemList(&PKey);
  1004.     return(FALSE);
  1005.   }
  1006.  
  1007.   /*
  1008.       Select colors
  1009.   */
  1010.   for (t = RankingFirst;  t != NULL;  t = t->RankNext)
  1011.   { if (t->Flags & TNFLAGSF_WITHDRAWN)
  1012.     { continue;
  1013.     }
  1014.     if (t->Opponent == NULL)
  1015.     { t->GFlags = GMFLAGSF_POINTFORFREE|GMFLAGSF_NOFIGHT;
  1016.     }
  1017.     else
  1018.     { /*
  1019.       Select colors
  1020.       */
  1021.       thelp = t->Opponent;
  1022.       if ((t->GFlags & GMFLAGSF_WHITE) == 0  &&
  1023.       (thelp->GFlags & GMFLAGSF_WHITE) == 0)
  1024.       { if (t->HowMuchWhiteLast < thelp->HowMuchWhiteLast  ||
  1025.         (t->HowMuchWhiteLast == thelp->HowMuchWhiteLast  &&
  1026.          (t->HowMuchWhite < thelp->HowMuchWhite  ||
  1027.           (t->HowMuchWhite == thelp->HowMuchWhite  &&
  1028.            t->Nr > thelp->Nr))))
  1029.     { thelp = t;
  1030.     }
  1031.     thelp->GFlags = GMFLAGSF_WHITE;
  1032.       }
  1033.     }
  1034.   }
  1035.   return(TRUE);
  1036. }
  1037.  
  1038.  
  1039.  
  1040.  
  1041. /*
  1042.     DoRoundRobin() does the Round Robin pairings. Here all rounds are paired
  1043.     at once.
  1044.  
  1045.     Inputs: mode - 0 for FIDE system, TNMODEF_SHIFT_SYSTEM for shift system
  1046.  
  1047.     Result: TRUE, if successfull, FALSE otherwise
  1048. */
  1049. static int DoRoundRobin(int mode)
  1050.  
  1051. { struct Player *t, **ttab, *tg;
  1052.   struct Group g;
  1053.   int i, j, k, l;
  1054.   int NumGames;
  1055.   int GamesMissing = 0;
  1056.   short flag, BoardNr;
  1057.   void *PMem = NULL, *GMem = NULL;
  1058.  
  1059.   /*
  1060.       Allocate a table of player numbers
  1061.   */
  1062.   if ((ttab = GetMem(&PMem, sizeof(*ttab)*NumPlayers))  ==  NULL)
  1063.   { return(FALSE);
  1064.   }
  1065.  
  1066.   /*
  1067.       Give any player a different number
  1068.   */
  1069.   g.First = g.Last = NULL;
  1070.   g.NumMembers = 0;
  1071.   for (t = (struct Player *) PlayerList.lh_Head;
  1072.        t->Tn_Node.ln_Succ != NULL;
  1073.        t = (struct Player *) t->Tn_Node.ln_Succ)
  1074.   { MyAddTail(&g, t);
  1075.   }
  1076.   while (g.NumMembers > 0)
  1077.   { i = RangeRand(g.NumMembers);
  1078.     for (t = g.First;  i > 0;  i--, t = t->LT_Succ)
  1079.     {
  1080.     }
  1081.     t->Nr = g.NumMembers;
  1082.     MyRemove(&g, t);
  1083.     ttab[g.NumMembers] = t;
  1084.   }
  1085.   NumGames = (NumPlayers+1)/2;
  1086.  
  1087.   if ((mode & TNMODEF_SHIFT_SYSTEM)  ==  0)
  1088.   { /*
  1089.     Here comes the FIDE-system.
  1090.  
  1091.     Assume n=NumPlayers (n=NumPlayers+1 for an odd number of players)
  1092.     The FIDE system wants the following pairings for round 1:
  1093.     1:n, 2:n-1, 3:n-2, 4:n-3 and so on. (n as opponent means free round,
  1094.     if the number of players is odd.)
  1095.     */
  1096.     for (i = 0;  i < NumGames; i++)
  1097.     { t = ttab[i];
  1098.       if(i == 0  &&  ((NumPlayers%2) != 0))    /*  Spielfrei   */
  1099.       { t->Opponent = NULL;
  1100.     t->BoardNr = i;
  1101.     t->GFlags = GMFLAGSF_POINTFORFREE|GMFLAGSF_NOFIGHT;
  1102.       }
  1103.       else
  1104.       { tg = ttab[NumGames*2-i-1];
  1105.  
  1106.     t->Opponent = tg;
  1107.     tg->Opponent = t;
  1108.     t->BoardNr = tg->BoardNr = i+1;
  1109.     t->GFlags = GMFLAGSF_WHITE;
  1110.     tg->GFlags = 0;
  1111.     GamesMissing++;
  1112.       }
  1113.     }
  1114.  
  1115.     if (!NewGames(&GMem, 0))
  1116.     { goto Error;
  1117.     }
  1118.  
  1119.     /*
  1120.     The following rounds are determined by a simple algorithm:
  1121.     (See Ernst Schubart, Helmut Noettger: "Turnierleiterhandbuch des
  1122.     Deutschen Schachbundes" (The official german chess federation's
  1123.     guide to managing chess-tournaments), p.64
  1124.  
  1125.     - In the odd rounds the players 1, 2, 3 and so on have player n as
  1126.       opponent (or have a free round, if the number of players is even.
  1127.       In the even rounds n plays against k+1, k+2, k+3 and so on, where
  1128.       k=n/2.
  1129.     - All other participants play against the player whose number is the
  1130.       number of their last opponent, incremented by 1. Player n is left
  1131.       out, player 1 comes after n-1.
  1132.     - If the number of players is even, the players 1, 2, ..., k have
  1133.       white, when playing against opponent n. The other players have
  1134.       black in that case.
  1135.     - In all other games the player with the lower number has the white
  1136.       pieces, if the sum of the two player-numbers is odd. Otherwise he
  1137.       gets the black pieces.
  1138.     */
  1139.     for (j = 1;  j < NumGames*2-1;  j++)
  1140.     { /*
  1141.       First get an opponent for player n.
  1142.       */
  1143.       k = (((j%2) == 0) ? 0 : NumGames) + j/2 + 1;
  1144.       t = ttab[k-1];
  1145.       if ((NumPlayers%2) == 0) /*  No point for free    */
  1146.       { tg = ttab[NumPlayers-1];
  1147.     t->BoardNr = tg->BoardNr = BoardNr = 0;
  1148.     t->GFlags = (t->Nr <= NumGames) ? GMFLAGSF_WHITE : 0;
  1149.     tg->GFlags = GMFLAGSF_WHITE - t->GFlags;
  1150.     tg->Opponent = t;
  1151.     GamesMissing++;
  1152.       }
  1153.       else
  1154.       { tg = NULL;
  1155.     t->BoardNr = BoardNr = -1;
  1156.     t->GFlags = GMFLAGSF_POINTFORFREE|GMFLAGSF_NOFIGHT;
  1157.       }
  1158.       t->Opponent = tg;
  1159.  
  1160.       /*
  1161.       Get the other games
  1162.       */
  1163.       for (i = 1;  i < NumGames;  i++)
  1164.       { if (++k == NumGames*2)
  1165.     { k = 1;
  1166.     }
  1167.     t = ttab[k-1];
  1168.     if ((tg = GameAddress(t, j)->Opponent) == NULL  ||
  1169.         tg->Nr == NumGames*2)
  1170.     { l = t->Nr;
  1171.     }
  1172.     else
  1173.     { l = tg->Nr;
  1174.     }
  1175.     if (++l == NumGames*2)
  1176.     { l = 1;
  1177.     }
  1178.     tg = ttab[l-1];
  1179.     flag = (((t->Nr+tg->Nr) % 2) != 0) ? GMFLAGSF_WHITE : 0;
  1180.     if (t->Nr > tg->Nr)
  1181.     { flag = GMFLAGSF_WHITE-flag;
  1182.     }
  1183.     t->GFlags = flag;
  1184.     tg->GFlags = GMFLAGSF_WHITE-flag;
  1185.     t->BoardNr = tg->BoardNr = ++BoardNr;
  1186.     t->Opponent = tg;
  1187.     tg->Opponent = t;
  1188.     GamesMissing++;
  1189.       }
  1190.  
  1191.       if (!NewGames(&GMem, 0))
  1192.       { goto Error;
  1193.       }
  1194.     }
  1195.   }
  1196.   else
  1197.   { /*
  1198.     Here comes the shift system. Its algorithm is rather easy, if you
  1199.     have seen it in practice.
  1200.  
  1201.     The boards are placed reverted on the table and all players sit down
  1202.     for the first round in the following order:
  1203.                 1 : k
  1204.                 2 : k+1
  1205.                 3 : k+2
  1206.                   .
  1207.                   .
  1208.                   .
  1209.               k-1 : n
  1210.     (We assume again, that n is the number of players is even and
  1211.     k = n/2. If this isn't true, we add a virtual player n. Playing
  1212.     against n means having a free game.)
  1213.  
  1214.     After each round the players 1, 2, 3, ..., n-1 are shifted clockwise.
  1215.     All boards remain unchanged, except for the board of player n, who
  1216.     may keep his place, but has to revert his board.
  1217.  
  1218.     Below the array ttab is used to simulate the table. (That's probably
  1219.     why it's got his name...)
  1220.     */
  1221.     int lastflag;
  1222.  
  1223.     for (i = 0;  i < NumGames*2-1;  i++)
  1224.     { for (j = 0, flag = GMFLAGSF_WHITE;  j < NumGames; j++)
  1225.       { t = ttab[j];
  1226.     if (j+NumGames < NumPlayers)
  1227.     { tg = ttab[j+NumGames];
  1228.       t->GFlags = flag;
  1229.       flag = GMFLAGSF_WHITE-flag;
  1230.       tg->GFlags = flag;
  1231.       tg->Opponent = t;
  1232.       tg->BoardNr = j+1;
  1233.     }
  1234.     else
  1235.     { tg = NULL;
  1236.     }
  1237.     t->Opponent = tg;
  1238.     t->BoardNr = j+1;
  1239.       }
  1240.       if (i == 0)
  1241.       { lastflag = flag;
  1242.       }
  1243.       else if (NumPlayers == NumGames*2)
  1244.       { t = ttab[NumGames-1];
  1245.     tg = ttab[NumGames*2-1];
  1246.     t->GFlags = lastflag;
  1247.     lastflag = GMFLAGSF_WHITE-lastflag;
  1248.     tg->GFlags = lastflag;
  1249.       }
  1250.     if (!NewGames(&GMem, 0))
  1251.     { goto Error;
  1252.     }
  1253.  
  1254.     /*
  1255.     Shift all players except for n.
  1256.     */
  1257.     t = ttab[0];
  1258.     for (j = 0;  j < NumGames-1;  j++)
  1259.     { ttab[j] = ttab[j+1];
  1260.     }
  1261.     ttab[NumGames-1] = ttab[NumGames*2-2];
  1262.     for (j = NumGames*2-3;  j >= NumGames;  j--)
  1263.     { ttab[j+1] = ttab[j];
  1264.     }
  1265.     ttab[NumGames] = t;
  1266.     }
  1267.   }
  1268.  
  1269.   PutMem(ttab);
  1270.   MoveMemList(&GMem, &TrnMem);
  1271.   NumGamesMissing = GamesMissing;
  1272.   return(TRUE);
  1273.  
  1274. Error:
  1275.   for (t = (struct Player *) PlayerList.lh_Head;
  1276.        t->Tn_Node.ln_Succ != NULL;
  1277.        t = (struct Player *) t->Tn_Node.ln_Succ)
  1278.   { t->First_Game = NULL;
  1279.   }
  1280.   PutMemList(&GMem);
  1281.   PutMem(ttab);
  1282.   NumRounds = 0;
  1283.   return(FALSE);
  1284. }
  1285.  
  1286.  
  1287.  
  1288.  
  1289. /*
  1290.     This function selects the boards, where the players should play.
  1291. */
  1292. static void SelectBoards(void)
  1293.  
  1294. { struct Player *p;
  1295.   int BoardNr;
  1296.  
  1297.   for (p = RankingFirst;  p != NULL;  p = p->RankNext)
  1298.   { p->BoardNr = -1;
  1299.   }
  1300.  
  1301.   for (p = RankingFirst, BoardNr = 0;  p != NULL;  p = p->RankNext)
  1302.   { if (p->BoardNr < 0  &&  p->Opponent != NULL)
  1303.     { p->BoardNr = p->Opponent->BoardNr = BoardNr++;
  1304.     }
  1305.   }
  1306. }
  1307.  
  1308.  
  1309.  
  1310.  
  1311.  
  1312. /*
  1313.     DoPairings() is the function that gets called from the menu.
  1314.  
  1315.     Input:  mode - tournament mode (TNMODEF_SWISS_PAIRING or
  1316.            TNMODEF_ROUND_ROBIN with or without TNMODEF_SHIFT_SYSTEM)
  1317.         save - TRUE, if the user should be asked to save the tournament
  1318.         user - TRUE, if the user may set games (ignored for Round
  1319.            Robin tournaments)
  1320.  
  1321.     Result: TRUE, if successfull, FALSE otherwise
  1322. */
  1323. int DoPairings(int mode, int save, int user)
  1324.  
  1325. { struct Player *t, *rankingfirst;
  1326.   char *name;
  1327.   char trnfilename[TRNFILENAME_LEN+1];
  1328.   char ending[20];
  1329.   int len, endlen, oldNumRounds = NumRounds;
  1330.  
  1331.   /*
  1332.       Copy the ranking pointers. This allows to undo the pairing calls, if
  1333.       something goes wrong. Additionally the Opponent and GFlags fields
  1334.       get initialized.
  1335.   */
  1336.   rankingfirst = RankingFirst;
  1337.   for (t = (struct Player *) PlayerList.lh_Head;
  1338.        t->Tn_Node.ln_Succ != NULL;
  1339.        t = (struct Player *) t->Tn_Node.ln_Succ)
  1340.   { t->Helpptr = t->RankNext;
  1341.     t->Opponent = NULL;
  1342.     t->GFlags = 0;
  1343.   }
  1344.  
  1345.   if (mode & TNMODEF_SWISS_PAIRING)
  1346.   { if (!((NumRounds == 0)  ?  DoSwissPairingFirst(user)  :
  1347.                    DoSwissPairing(user)))
  1348.     { goto Error;
  1349.     }
  1350.     SelectBoards();
  1351.     NewGames(&TrnMem, 2);
  1352.   }
  1353.   else
  1354.   { if (!DoRoundRobin(mode))
  1355.     { goto Error;
  1356.     }
  1357.   }
  1358.   TrnMode |= mode;
  1359.   IsSaved = FALSE;
  1360.  
  1361.  
  1362.   /*
  1363.       Offer the user to save data. If the convention "name.roundnumber.cdat"
  1364.       was kept until now, we keep this.
  1365.   */
  1366.   if (save)
  1367.   { strcpy(trnfilename, TrnFileName);
  1368.     sprintf(ending, ".%d.cdat", oldNumRounds);
  1369.     endlen = strlen(ending);
  1370.     len = strlen(trnfilename);
  1371.     if (len >= endlen  &&
  1372.     Stricmp((STRPTR) trnfilename+(len-endlen), (STRPTR) ending)  ==  0)
  1373.     { sprintf(trnfilename+(len-endlen), ".%d.cdat", NumRounds);
  1374.     }
  1375.     name = FileRequest(trnfilename, NULL, NULL, TRUE);
  1376.     if (name  !=  NULL  &&  *name != '\0')
  1377.     { return(SaveTournament(name));
  1378.     }
  1379.   }
  1380.   return(TRUE);;
  1381.  
  1382. Error:
  1383.   /*
  1384.       Get the old ranking list
  1385.   */
  1386.   RankingFirst = rankingfirst;
  1387.   for (t = (struct Player *) PlayerList.lh_Head;
  1388.        t->Tn_Node.ln_Succ != NULL;
  1389.        t = (struct Player *) t->Tn_Node.ln_Succ)
  1390.   { t->RankNext = t->Helpptr;
  1391.   }
  1392.   return(FALSE);
  1393. }
  1394.