001    ///////////////////////////////////////////////////////////////////////////////
002    // Copyright (c) 2006, Frank S. Nestel, All Rights Reserved.
003    //
004    // This library is free software; you can redistribute it and/or
005    // modify it under the terms of the GNU Lesser General Public
006    // License as published by the Free Software Foundation; either
007    // version 2.1 of the License, or (at your option) any later version.
008    //
009    // This library is distributed in the hope that it will be useful,
010    // but WITHOUT ANY WARRANTY; without even the implied warranty of
011    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012    // GNU General Public License for more details.
013    //
014    // You should have received a copy of the GNU Lesser General Public
015    // License along with this program; if not, write to the Free Software
016    // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
017    ///////////////////////////////////////////////////////////////////////////////
018    
019    package de.spieleck.app.turn.scoring;
020    
021    import de.spieleck.app.turn.PlayerGameScore;
022    import de.spieleck.app.turn.PlayerScore;
023    
024    /**
025     * A ScoringMode for chess results.
026     * <br />
027     * Only knows 1:0, 0.5:0.5, 0:1 as results.
028     *
029     * <p><a href="$URL: https://svn.sourceforge.net/svnroot/jtourney/src/de/spieleck/app/turn/scoring/Chess.java $">$URL: https://svn.sourceforge.net/svnroot/jtourney/src/de/spieleck/app/turn/scoring/Chess.java $</a></p>
030     *
031     * @author Frank S. Nestel
032     * @author $Author: nestefan $
033     * @version $Revision: 2 $ $Date: 2006-03-20 14:33:27 +0100 (Mo, 20 Mrz 2006) $ $Author: nestefan $
034     */
035    public class Chess
036        extends BaseScoring
037    {
038        public PlayerGameScore parseRawScore(String line)
039        {
040            String l = line.trim();
041            if ( "0.5".equals(line) || ".5".equals(line) )
042                return new ChessScore(1);
043            else if ( "1".equals(line) || "1.".equals(line) || "1.0".equals(line) )
044                return new ChessScore(2);
045            else if ( "0".equals(line) || "0.".equals(line) || "0.0".equals(line) )
046                return new ChessScore(0);
047            return null;
048        }
049    
050        public String checkScore(PlayerGameScore[] pgs)
051        {
052            if ( pgs.length == 2 )
053            {
054                ChessScore c0 = (ChessScore) pgs[0];
055                ChessScore c1 = (ChessScore) pgs[1];
056                if ( c0.d + c1.d == 2 )
057                    return null;
058                return "Sum of scores is "+(c0.d + c1.d);
059            }
060            else if ( pgs.length == 1 )
061            {
062                ChessScore c0 = (ChessScore) pgs[0];
063                if ( c0.d == 1 )
064                    return null;
065                return "Single score is "+c0.d;
066            }
067            return "Chess scoring cannot be applied to "+pgs.length+"players";
068        }
069    
070        public PlayerGameScore[] adjustScore(PlayerGameScore[] pgs)
071        {
072            return pgs;
073        }
074    
075        public PlayerGameScore zeroScore()
076        {
077            return new ChessScore(0);
078        }
079    
080        public PlayerScore addPlayers(PlayerScore ps, PlayerScore pgs)
081        {
082            return new ChessScore(((ChessScore)ps).d + ((ChessScore)pgs).d);
083        }
084    
085        public static class ChessScore
086            implements PlayerGameScore, PlayerScore
087        {
088            private int d;
089    
090            public ChessScore(int d)
091            {
092                this.d = d;
093            }
094    
095            public int compareTo(Object o)
096            {
097                if ( ! ( o instanceof ChessScore ) ) 
098                    return -1;
099                return this.d - ((ChessScore)o).d;
100            }
101    
102            public String toString()
103            {
104                return Integer.toString(d/2) + ( (d & 1) != 0 ? ".5" : ".0" );
105            }
106        }
107    
108        public String toString()
109        {
110            return "Chess";
111        }
112    }