source: trunk/src/ScoreDice.cpp @ 134

Last change on this file since 134 was 119, checked in by guyru, 5 years ago

added yahtzee joker support to new scoring class

File size: 5.7 KB
Line 
1
2/***************************************************************************
3 *   Copyright (C) 2006-2007 by Guy Rutenberg   *
4 *   guyrutenberg@gmail.com   *
5 *                                                                         *
6 *   This program is free software; you can redistribute it and/or modify  *
7 *   it under the terms of the GNU General Public License as published by  *
8 *   the Free Software Foundation; either version 2 of the License, or     *
9 *   (at your option) any later version.                                   *
10 *                                                                         *
11 *   This program is distributed in the hope that it will be useful,       *
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14 *   GNU General Public License for more details.                          *
15 *                                                                         *
16 *   You should have received a copy of the GNU General Public License     *
17 *   along with this program; if not, write to the                         *
18 *   Free Software Foundation, Inc.,                                       *
19 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20 ***************************************************************************/
21 
22#include "ScoreDice.h"
23
24/**
25 * \brief constructor
26 *
27 * This constructor also sets the values of the dice.
28 */
29ScoreDice::ScoreDice(short int dice[5])
30{
31        SetDice(dice);
32}
33
34/**
35 * \brief default constructor
36 * \note if you use this constructor you must call ScoreDice::SetDice before
37 * using the class.
38 * \see ScoreDice::SetDice
39 */
40ScoreDice::ScoreDice()
41{
42        //nothing to do here
43}
44/**
45 * \brief Sets the dice values
46 * \param dice An array of 5 short ints containing the values of the dice.
47 */
48void ScoreDice::SetDice(short int dice[5])
49{
50        for (int i=0; i<5; i++)
51                m_dice[i] = dice[i];
52       
53        //fill the dice hash
54        for (int i=0; i<6; i++)
55                m_dicehash[i] = 0;
56        for (int i=0; i<5; i++)
57                m_dicehash[dice[i]-1] += 1;     
58       
59        m_yahtzee_joker = false;
60}
61
62/**
63 * Tells the scoring class wheter or not we have a Yahtzee Joker.
64 * \param is_yahtzee_joker [bool] true if there is a Yahtzee Joker.
65 * \note checking whether we got a yahtzee joker is done by an outside function,
66 * and it needs the dice set before it can operate.
67 * \see MainFrame::YahtzeeJoker()
68 */
69void ScoreDice::SetYahtzeeJoker(bool is_yahtzee_joker)
70{
71        m_yahtzee_joker = is_yahtzee_joker;
72}
73
74/**
75 * \brief Gets the value of a dice.
76 * \param number the index of the dice wanted.
77 * \return the value of the dice. 0 if wrong input.
78 */
79short int ScoreDice::GetDice(short int number)
80{
81        if (number<=5 && number >=1)
82                return m_dice[number-1];
83        return 0;
84}
85/**
86 * \return the score for the aces box.
87 */
88short int ScoreDice::Aces() const
89{
90        return m_dicehash[0];
91}
92
93/**
94 * \return the score for the twos box.
95 */
96short int ScoreDice::Twos() const
97{
98        return 2*m_dicehash[1];
99}
100
101
102/**
103 * \return the score for the threes box.
104 */
105short int ScoreDice::Threes() const
106{
107        return 3*m_dicehash[2];
108}
109
110/**
111 * \return the score for the fours box.
112 */
113short int ScoreDice::Fours() const
114{
115        return 4*m_dicehash[3];
116}
117
118/**
119 *
120 * \return the score for the fives box.
121 */
122short int ScoreDice::Fives() const
123{
124        return 5*m_dicehash[4];
125}
126
127/**
128 *
129 * \return the score for the sixes box.
130 */
131short int ScoreDice::Sixes() const
132{
133        return 6*m_dicehash[5];
134}
135
136/**
137 *
138 * \return the score for the "Three of A Kind" box.
139 */
140short int ScoreDice::ThreeOfAKind() const
141{
142        bool three = false;
143        short int temp = 0;
144       
145        for (int i=0; i<6; i++)
146                if (m_dicehash[i] >= 3)
147                        three = true;
148        if (three)
149                return Chance();
150        return 0;
151}
152
153/**
154 *
155 * \return the score for the "Four of A Kind" box.
156 */
157short int ScoreDice::FourOfAKind() const
158{
159        bool four = false;
160        short int temp = 0;
161       
162        for (int i=0; i<6; i++)
163                if (m_dicehash[i] >= 4)
164                        four = true;
165        if (four)
166                return Chance();
167        return 0;
168}
169
170/**
171 *
172 * \return the score for the "Full House" box.
173 */
174short int ScoreDice::FullHouse() const
175{
176        bool two = false;
177        bool three = false;
178
179        for (int i=0; i<6; i++)
180                if (m_dicehash[i] == 2)
181                        two = true;
182        for (int i=0; i<6; i++)
183                if (m_dicehash[i] == 3)
184                        three = true;
185        if ((two && three) || m_yahtzee_joker)
186                return 25;
187        return 0;
188}
189
190/**
191 *
192 * \return the score for the small sequence box.
193 */
194short int ScoreDice::SmallSequence() const
195{
196        if ( (m_dicehash[0]>=1 && m_dicehash[1]>=1 && m_dicehash[2]>=1 &&
197                m_dicehash[3]>=1) || (m_dicehash[1]>=1 && m_dicehash[2]>=1 &&
198                m_dicehash[3]>=1 && m_dicehash[4]>=1) || (m_dicehash[2]>=1 &&
199                m_dicehash[3]>=1 && m_dicehash[4]>=1 && m_dicehash[5]>=1))
200                return 30;
201        else if (m_yahtzee_joker)
202                return 30;
203        return 0;
204}
205
206/**
207 *
208 * \return the score for the large sequence box.
209 */
210short int ScoreDice::LargeSequence() const
211{
212        if ( (m_dicehash[0]==1 && m_dicehash[1]==1 && m_dicehash[2]==1 &&
213                m_dicehash[3]==1 && m_dicehash[4]==1) || (m_dicehash[1]==1 &&
214                m_dicehash[2]==1 && m_dicehash[3]==1 && m_dicehash[4]==1 &&
215                m_dicehash[5]==1))
216                return 40;
217        else if (m_yahtzee_joker)
218                return 40;
219        return 0;
220}
221/**
222 *
223 * \return the score for the Yahtzee box.
224 */
225short int ScoreDice::Yahtzee() const
226{
227        if (IsYahtzee())
228                return 50;
229        return 0;
230}
231
232
233/**
234 *
235 * \return the score for the chance box.
236 */
237short int ScoreDice::Chance() const
238{
239        short int temp = 0;
240        for(int i = 0; i<5; i++)
241                temp += m_dice[i];
242       
243        return temp;
244}
245
246/**
247 * \brief determines wheter the current dice hold a Yahtzee or not.
248 * \return true or false depending on whether there is a Yahtzee or not.
249 */
250bool ScoreDice::IsYahtzee() const
251{
252        if ((m_dice[0]==m_dice[1]) && (m_dice[1]==m_dice[2]) && \
253                        (m_dice[1]==m_dice[3]) && (m_dice[1]==m_dice[4]))
254                return true;
255        return false;
256}
Note: See TracBrowser for help on using the repository browser.