File indexing completed on 2024-04-14 03:59:45

0001 /***************************************************************************
0002  *   Copyright (C) 2005, 2008 by Albert Astals Cid <aacid@kde.org>         *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  ***************************************************************************/
0009 
0010 #include "player.h"
0011 
0012 player::player()
0013 {
0014     initScores();
0015 }
0016 
0017 void player::setName(const QString &name)
0018 {
0019     m_name = name;
0020 }
0021 
0022 QString player::name() const
0023 {
0024     return m_name;
0025 }
0026 
0027 bool player::isHuman() const
0028 {
0029     return m_human;
0030 }
0031 
0032 void player::setHuman(bool human)
0033 {
0034     m_human = human;
0035 }
0036 
0037 void player::initScores()
0038 {
0039     for (int i = 0; i < 13; i++) m_scores[i] = -1;
0040 }
0041 
0042 int player::score(int index) const
0043 {
0044     return m_scores[index];
0045 }
0046 
0047 void player::setScore(int index, int value)
0048 {
0049     m_scores[index] = value;
0050 }
0051 
0052 int player::bonus() const
0053 {
0054     if (scoreRange(0, 5) > 62) return 35;
0055     return -1;
0056 }
0057 
0058 int player::upperTotal() const
0059 {
0060     return scoreRange(0, 5);
0061 }
0062 
0063 int player::upperTotalWithBonus() const
0064 {
0065     int score = qMax(bonus(), 0);
0066     score += upperTotal();
0067     return score;
0068 }
0069 
0070 int player::lowerTotal() const
0071 {
0072     return scoreRange(6, 12);
0073 }
0074 
0075 int player::grandTotal() const
0076 {
0077     int up, down;
0078     up = upperTotalWithBonus();
0079     down = lowerTotal();
0080     
0081     if (up >= 0)
0082     {
0083         if (down >= 0) return up + down;
0084         else return up;
0085     }
0086     else
0087     {
0088         if (down >= 0) return down;
0089         else return -1;
0090     }
0091 }
0092 
0093 bool player::allScores() const
0094 {
0095     int i = 0;
0096     bool all = true;
0097     while (all && i < 13)
0098     {
0099         all = m_scores[i] != -1;
0100         i++;
0101     }
0102     return all;
0103 }
0104 
0105 
0106 bool player::noScores() const
0107 {
0108     bool none = true;
0109     for(int i = 0; none && i < 13; ++i) none = m_scores[i] == -1;
0110     return none;
0111 }
0112 
0113 int player::scoreRange(int begin, int end) const
0114 {
0115     int score = 0;
0116     bool any = false;
0117     for (int i = begin; i <= end; i++)
0118     {
0119         if (m_scores[i] >= 0)
0120         {
0121             score += m_scores[i];
0122             any = true;
0123         }
0124     }
0125     if (any) return score;
0126     else return -1;
0127 }