author | cniehaus <cniehaus> | 2003-08-12 19:06:14 (UTC) |
---|---|---|
committer | cniehaus <cniehaus> | 2003-08-12 19:06:14 (UTC) |
commit | db952faa29fa56602b061ef4faff61cc57f71243 (patch) (unidiff) | |
tree | 9b4d36895e0e9a07ff6c66e17c1bb9e0bf9daca0 | |
parent | 713725e85c03c3bbbc0358301ed84241c6d0dd5b (diff) | |
download | opie-db952faa29fa56602b061ef4faff61cc57f71243.zip opie-db952faa29fa56602b061ef4faff61cc57f71243.tar.gz opie-db952faa29fa56602b061ef4faff61cc57f71243.tar.bz2 |
ok, now OYatzee checks for what the user got (Full House and such)
-rw-r--r-- | noncore/games/oyatzee/oyatzee.cpp | 149 | ||||
-rw-r--r-- | noncore/games/oyatzee/oyatzee.h | 19 |
2 files changed, 165 insertions, 3 deletions
diff --git a/noncore/games/oyatzee/oyatzee.cpp b/noncore/games/oyatzee/oyatzee.cpp index 85d9616..5c4d1f7 100644 --- a/noncore/games/oyatzee/oyatzee.cpp +++ b/noncore/games/oyatzee/oyatzee.cpp | |||
@@ -28,95 +28,239 @@ | |||
28 | #include <qregexp.h> | 28 | #include <qregexp.h> |
29 | 29 | ||
30 | #include <stdlib.h> | 30 | #include <stdlib.h> |
31 | #include <unistd.h> | 31 | #include <unistd.h> |
32 | #include <pwd.h> | 32 | #include <pwd.h> |
33 | #include <sys/types.h> | 33 | #include <sys/types.h> |
34 | 34 | ||
35 | OYatzee::OYatzee( QWidget *parent , const char *name, WFlags fl ) : QMainWindow( parent , name , fl ) | 35 | OYatzee::OYatzee( QWidget *parent , const char *name, WFlags fl ) : QMainWindow( parent , name , fl ) |
36 | { | 36 | { |
37 | QWidget *thing = new QWidget( this ); | 37 | QWidget *thing = new QWidget( this ); |
38 | setCentralWidget( thing ); | 38 | setCentralWidget( thing ); |
39 | 39 | ||
40 | QVBoxLayout *vbox = new QVBoxLayout( thing ); | 40 | QVBoxLayout *vbox = new QVBoxLayout( thing ); |
41 | 41 | ||
42 | sb = new Scoreboard( thing , "sb" ); | 42 | sb = new Scoreboard( thing , "sb" ); |
43 | dw = new DiceWidget( thing , "dw" ); | 43 | dw = new DiceWidget( thing , "dw" ); |
44 | 44 | ||
45 | vbox->addWidget( sb ); | 45 | vbox->addWidget( sb ); |
46 | vbox->addWidget( dw ); | 46 | vbox->addWidget( dw ); |
47 | 47 | ||
48 | setPlayerNumber( 2 ); | 48 | setPlayerNumber( 2 ); |
49 | setRoundsNumber( 1 ); | 49 | setRoundsNumber( 1 ); |
50 | 50 | ||
51 | connect( dw->rollButton, SIGNAL( clicked() ), this , SLOT( slotRollDices() ) ); | 51 | connect( dw->rollButton, SIGNAL( clicked() ), this , SLOT( slotRollDices() ) ); |
52 | } | 52 | } |
53 | 53 | ||
54 | OYatzee::~OYatzee() | 54 | OYatzee::~OYatzee() |
55 | { | 55 | { |
56 | } | 56 | } |
57 | 57 | ||
58 | void OYatzee::detectPosibilities() | 58 | void OYatzee::detectPosibilities() |
59 | { | 59 | { |
60 | posibilities.clear(); | ||
61 | qDebug( "running detectPosibilities()" ); | ||
62 | |||
60 | Dice *d = dw->diceList.first(); | 63 | Dice *d = dw->diceList.first(); |
61 | 64 | ||
62 | QValueList<int> numbers; | 65 | QValueListInt numbers; |
63 | 66 | ||
64 | for ( ; d != 0 ; d = dw->diceList.next() ) | 67 | for ( ; d != 0 ; d = dw->diceList.next() ) |
65 | { | 68 | { |
66 | numbers.append( d->Value ); | 69 | numbers.append( d->Value ); |
67 | } | 70 | } |
71 | |||
72 | //the 6 numbers | ||
73 | QValueListInt::Iterator it; | ||
74 | |||
75 | for ( int i = 1 ; i < 7 ; ++i ) // check for 1-->6 | ||
76 | { | ||
77 | bool cont = false; | ||
78 | it = numbers.begin(); | ||
79 | for ( ; it != numbers.end() ; ++it ) | ||
80 | { | ||
81 | if ( cont ) | ||
82 | continue; | ||
83 | |||
84 | if ( numbers.find( i ) != numbers.end() ) | ||
85 | { | ||
86 | posibilities.append( i ); | ||
87 | cont = true; | ||
88 | } | ||
89 | } | ||
90 | } | ||
91 | |||
92 | |||
93 | //3er, 4er, Yatzee | ||
94 | it = numbers.begin(); | ||
95 | int count; | ||
96 | int temp; | ||
97 | int countFH = 0; //for the full-house-check | ||
98 | |||
99 | for ( int i = 1 ; i < 7 ; ++i ) // check for 1-->6 at least 3 times | ||
100 | { | ||
101 | count = 0; | ||
102 | temp = 0; | ||
103 | it = numbers.begin(); | ||
104 | for ( ; it != numbers.end() ; ++it ) | ||
105 | { | ||
106 | if ( *it == i ) | ||
107 | { | ||
108 | count++; | ||
109 | temp++; | ||
110 | } | ||
111 | if ( temp == 2 ) | ||
112 | countFH = temp; | ||
113 | } | ||
114 | |||
115 | if ( count >= 3 ) | ||
116 | { | ||
117 | posibilities.append( 7 ); | ||
118 | |||
119 | //now we check if it is a full house | ||
120 | if ( count == 3 && countFH == 2 ) //aka Full House | ||
121 | posibilities.append( 9 ); | ||
122 | } | ||
123 | if ( count >= 4 ) | ||
124 | posibilities.append( 8 ); | ||
125 | if ( count == 5 ) //Yatzee | ||
126 | posibilities.append( 12 ); | ||
127 | } | ||
128 | |||
129 | //S-Straight | ||
130 | if ( numbers.find( 3 ) != numbers.end() && numbers.find( 4 ) != numbers.end() ) | ||
131 | { | ||
132 | bool isLong = false; | ||
133 | bool isShort = true; | ||
134 | //L-Straight | ||
135 | if ( numbers.find( 2 ) != numbers.end() && numbers.find( 5 ) != numbers.end() ) | ||
136 | { | ||
137 | isShort = true; | ||
138 | |||
139 | //12345 or 23456 | ||
140 | if ( numbers.find( 1 ) != numbers.end() || numbers.find( 6) != numbers.end() ) | ||
141 | isLong = true; | ||
142 | } | ||
143 | //1234 | ||
144 | if ( numbers.find( 1 ) != numbers.end() && numbers.find( 2 ) != numbers.end() ) | ||
145 | isShort = true; | ||
146 | //3456 | ||
147 | if ( numbers.find( 5 ) != numbers.end() && numbers.find( 6 ) != numbers.end() ) | ||
148 | isShort = true; | ||
149 | |||
150 | if ( isShort ) | ||
151 | posibilities.append( 10 ); | ||
152 | if ( isLong ) | ||
153 | posibilities.append( 11 ); | ||
154 | } | ||
155 | |||
156 | posibilities.append( 13 ); //Chance, well, this is allways possible | ||
157 | |||
158 | displayPossibilites(); | ||
159 | } | ||
160 | |||
161 | void OYatzee::displayPossibilites() | ||
162 | { | ||
163 | qDebug( "running displayPossibilites(), %d item", posibilities.count() ); | ||
164 | |||
165 | for ( QValueListInt::Iterator it = posibilities.begin() ; it != posibilities.end(); ++it ) | ||
166 | { | ||
167 | qDebug( QString::number( *it ) ); | ||
168 | switch ( *it ) | ||
169 | { | ||
170 | case Ones: | ||
171 | qDebug( "1er" ); | ||
172 | break; | ||
173 | case Twos: | ||
174 | qDebug( "2er" ); | ||
175 | break; | ||
176 | case Threes: | ||
177 | qDebug( "3er" ); | ||
178 | break; | ||
179 | case Fours: | ||
180 | qDebug( "4er" ); | ||
181 | break; | ||
182 | case Fives: | ||
183 | qDebug( "5er" ); | ||
184 | break; | ||
185 | case Sixes: | ||
186 | qDebug( "6er" ); | ||
187 | break; | ||
188 | case ThreeOfAKind: | ||
189 | qDebug( "3oaK" ); | ||
190 | break; | ||
191 | case FourOfAKind: | ||
192 | qDebug( "4oaK" ); | ||
193 | break; | ||
194 | case FullHouse: | ||
195 | qDebug( "Full House" ); | ||
196 | break; | ||
197 | case SStraight: | ||
198 | qDebug( "Short S" ); | ||
199 | break; | ||
200 | case LStraight: | ||
201 | qDebug( "Long S" ); | ||
202 | break; | ||
203 | case Yatzee: | ||
204 | qDebug( "Yatzee!" ); | ||
205 | break; | ||
206 | case Chance: | ||
207 | qDebug( "Chance" ); | ||
208 | break; | ||
209 | } | ||
210 | } | ||
68 | } | 211 | } |
69 | 212 | ||
70 | void OYatzee::setPlayerNumber( const int num ) | 213 | void OYatzee::setPlayerNumber( const int num ) |
71 | { | 214 | { |
72 | numOfPlayers = num; | 215 | numOfPlayers = num; |
73 | } | 216 | } |
74 | 217 | ||
75 | void OYatzee::setRoundsNumber( const int num ) | 218 | void OYatzee::setRoundsNumber( const int num ) |
76 | { | 219 | { |
77 | numOfRounds = num; | 220 | numOfRounds = num; |
78 | } | 221 | } |
79 | 222 | ||
80 | void OYatzee::slotStartGame() | 223 | void OYatzee::slotStartGame() |
81 | { | 224 | { |
82 | } | 225 | } |
83 | 226 | ||
84 | void OYatzee::slotRollDices() | 227 | void OYatzee::slotRollDices() |
85 | { | 228 | { |
86 | Dice *d = dw->diceList.first(); | 229 | Dice *d = dw->diceList.first(); |
87 | 230 | ||
88 | for ( ; d != 0 ; d = dw->diceList.next() ) | 231 | for ( ; d != 0 ; d = dw->diceList.next() ) |
89 | { | 232 | { |
90 | d->roll(); | 233 | if ( !d->isSelected ) |
234 | d->roll(); | ||
91 | } | 235 | } |
92 | 236 | ||
93 | detectPosibilities(); | 237 | detectPosibilities(); |
94 | } | 238 | } |
95 | 239 | ||
96 | /* | 240 | /* |
97 | * Scoreboard | 241 | * Scoreboard |
98 | */ | 242 | */ |
99 | Scoreboard::Scoreboard( QWidget *parent, const char *name ) : QWidget( parent , name ) | 243 | Scoreboard::Scoreboard( QWidget *parent, const char *name ) : QWidget( parent , name ) |
100 | { | 244 | { |
101 | } | 245 | } |
102 | 246 | ||
103 | void Scoreboard::paintEvent( QPaintEvent * ) | 247 | void Scoreboard::paintEvent( QPaintEvent * ) |
104 | { | 248 | { |
105 | QPainter p; | 249 | QPainter p; |
106 | p.begin( this ); | 250 | p.begin( this ); |
107 | 251 | ||
108 | p.drawRect( 0,0, this->width() , this->height() ); | 252 | p.drawRect( 0,0, this->width() , this->height() ); |
109 | } | 253 | } |
110 | 254 | ||
111 | /* | 255 | /* |
112 | * Dice | 256 | * Dice |
113 | */ | 257 | */ |
114 | Dice::Dice( QWidget *parent , const char *name ) : QFrame( parent , name ) | 258 | Dice::Dice( QWidget *parent , const char *name ) : QFrame( parent , name ) |
115 | { | 259 | { |
116 | QTime t = QTime::currentTime(); // set random seed | 260 | QTime t = QTime::currentTime(); // set random seed |
117 | srand(t.hour()*12+t.minute()*60+t.second()*60); | 261 | srand(t.hour()*12+t.minute()*60+t.second()*60); |
118 | 262 | ||
119 | connect( this , SIGNAL( selected() ), this , SLOT( slotSelected() ) ); | 263 | connect( this , SIGNAL( selected() ), this , SLOT( slotSelected() ) ); |
120 | } | 264 | } |
121 | 265 | ||
122 | void Dice::slotSelected() | 266 | void Dice::slotSelected() |
@@ -174,47 +318,46 @@ void Dice::paintNumber( QPainter *p ) | |||
174 | break; | 318 | break; |
175 | case 4: | 319 | case 4: |
176 | p->drawText( 10,10,"4"); | 320 | p->drawText( 10,10,"4"); |
177 | break; | 321 | break; |
178 | case 5: | 322 | case 5: |
179 | p->drawText( 10,10,"5"); | 323 | p->drawText( 10,10,"5"); |
180 | break; | 324 | break; |
181 | case 6: | 325 | case 6: |
182 | p->drawText( 10,10,"6"); | 326 | p->drawText( 10,10,"6"); |
183 | break; | 327 | break; |
184 | } | 328 | } |
185 | } | 329 | } |
186 | 330 | ||
187 | /* | 331 | /* |
188 | * DiceWidget | 332 | * DiceWidget |
189 | */ | 333 | */ |
190 | DiceWidget::DiceWidget( QWidget *parent , const char *name ) : QWidget( parent , name ) | 334 | DiceWidget::DiceWidget( QWidget *parent , const char *name ) : QWidget( parent , name ) |
191 | { | 335 | { |
192 | rollButton = new QPushButton( tr( "Roll" ) , this ) ; | 336 | rollButton = new QPushButton( tr( "Roll" ) , this ) ; |
193 | 337 | ||
194 | for ( int i = 0 ; i < 5 ; i++ ) | 338 | for ( int i = 0 ; i < 5 ; i++ ) |
195 | { | 339 | { |
196 | //appending the 5 dices of the game | 340 | //appending the 5 dices of the game |
197 | diceList.append( new Dice( this, "wuerfel" ) ); | 341 | diceList.append( new Dice( this, "wuerfel" ) ); |
198 | } | 342 | } |
199 | 343 | ||
200 | QHBoxLayout *hbox = new QHBoxLayout( this ); | 344 | QHBoxLayout *hbox = new QHBoxLayout( this ); |
201 | 345 | ||
202 | Dice *d = diceList.first(); | 346 | Dice *d = diceList.first(); |
203 | 347 | ||
204 | for ( ; d != 0 ; d = diceList.next() ) | 348 | for ( ; d != 0 ; d = diceList.next() ) |
205 | { | 349 | { |
206 | d->roll(); | ||
207 | hbox->addWidget( d ); | 350 | hbox->addWidget( d ); |
208 | } | 351 | } |
209 | 352 | ||
210 | hbox->addWidget( rollButton ); | 353 | hbox->addWidget( rollButton ); |
211 | } | 354 | } |
212 | 355 | ||
213 | /* | 356 | /* |
214 | * Player | 357 | * Player |
215 | */ | 358 | */ |
216 | Player::Player( QString name ) | 359 | Player::Player( QString name ) |
217 | { | 360 | { |
218 | playerName = name; | 361 | playerName = name; |
219 | } | 362 | } |
220 | 363 | ||
diff --git a/noncore/games/oyatzee/oyatzee.h b/noncore/games/oyatzee/oyatzee.h index 01ab36d..65a18fc 100644 --- a/noncore/games/oyatzee/oyatzee.h +++ b/noncore/games/oyatzee/oyatzee.h | |||
@@ -1,72 +1,91 @@ | |||
1 | #ifndef WORDGAME_H | 1 | #ifndef WORDGAME_H |
2 | #define WORDGAME_H | 2 | #define WORDGAME_H |
3 | 3 | ||
4 | #include <qmainwindow.h> | 4 | #include <qmainwindow.h> |
5 | #include <qlabel.h> | 5 | #include <qlabel.h> |
6 | #include <qlist.h> | 6 | #include <qlist.h> |
7 | 7 | ||
8 | #include <stdlib.h> // rand() function | 8 | #include <stdlib.h> // rand() function |
9 | #include <qdatetime.h> // seed for rand() | 9 | #include <qdatetime.h> // seed for rand() |
10 | 10 | ||
11 | class Dice; | 11 | class Dice; |
12 | class Game; | 12 | class Game; |
13 | class Scoreboard; | 13 | class Scoreboard; |
14 | class DiceWidget; | 14 | class DiceWidget; |
15 | 15 | ||
16 | typedef QList<Dice> dicesList; | 16 | typedef QList<Dice> dicesList; |
17 | typedef QValueList<int> QValueListInt; | ||
17 | 18 | ||
18 | class OYatzee : public QMainWindow { | 19 | class OYatzee : public QMainWindow { |
19 | Q_OBJECT | 20 | Q_OBJECT |
20 | public: | 21 | public: |
21 | OYatzee( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 ); | 22 | OYatzee( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 ); |
22 | ~OYatzee(); | 23 | ~OYatzee(); |
23 | 24 | ||
24 | Game *g; | 25 | Game *g; |
25 | DiceWidget *dw; | 26 | DiceWidget *dw; |
26 | Scoreboard *sb; | 27 | Scoreboard *sb; |
27 | 28 | ||
29 | QValueListInt posibilities; | ||
28 | 30 | ||
29 | void setPlayerNumber( const int num ); | 31 | void setPlayerNumber( const int num ); |
30 | void setRoundsNumber( const int num ); | 32 | void setRoundsNumber( const int num ); |
33 | |||
34 | enum { | ||
35 | Ones=1, | ||
36 | Twos = 2, | ||
37 | Threes = 3, | ||
38 | Fours = 4, | ||
39 | Fives = 5, | ||
40 | Sixes = 6, | ||
41 | ThreeOfAKind = 7, //12444 | ||
42 | FourOfAKind = 8, //14444 | ||
43 | FullHouse = 9, //22555 | ||
44 | SStraight = 10, //13456 | ||
45 | LStraight = 11, //12345 | ||
46 | Yatzee = 12, //55555 | ||
47 | Chance = 13}; | ||
31 | 48 | ||
32 | public slots: | 49 | public slots: |
33 | void slotStartGame(); | 50 | void slotStartGame(); |
34 | void slotRollDices(); | 51 | void slotRollDices(); |
35 | 52 | ||
36 | private: | 53 | private: |
37 | int numOfPlayers; | 54 | int numOfPlayers; |
38 | int numOfRounds; | 55 | int numOfRounds; |
39 | 56 | ||
40 | void detectPosibilities(); | 57 | void detectPosibilities(); |
58 | void displayPossibilites(); | ||
59 | |||
41 | }; | 60 | }; |
42 | 61 | ||
43 | class Dice : public QFrame | 62 | class Dice : public QFrame |
44 | { | 63 | { |
45 | Q_OBJECT | 64 | Q_OBJECT |
46 | public: | 65 | public: |
47 | Dice( QWidget* parent = 0, const char* name = 0 ); | 66 | Dice( QWidget* parent = 0, const char* name = 0 ); |
48 | 67 | ||
49 | int Value; | 68 | int Value; |
50 | bool isSelected; | 69 | bool isSelected; |
51 | 70 | ||
52 | int hasValue(); | 71 | int hasValue(); |
53 | void roll(); | 72 | void roll(); |
54 | virtual void mousePressEvent( QMouseEvent* ); | 73 | virtual void mousePressEvent( QMouseEvent* ); |
55 | 74 | ||
56 | private slots: | 75 | private slots: |
57 | void slotSelected(); | 76 | void slotSelected(); |
58 | 77 | ||
59 | signals: | 78 | signals: |
60 | void selected(); | 79 | void selected(); |
61 | 80 | ||
62 | protected: | 81 | protected: |
63 | void paintEvent( QPaintEvent *e ); | 82 | void paintEvent( QPaintEvent *e ); |
64 | void paintNumber( QPainter *p ); | 83 | void paintNumber( QPainter *p ); |
65 | }; | 84 | }; |
66 | 85 | ||
67 | class DiceWidget : public QWidget | 86 | class DiceWidget : public QWidget |
68 | { | 87 | { |
69 | Q_OBJECT | 88 | Q_OBJECT |
70 | public: | 89 | public: |
71 | DiceWidget( QWidget *parent = 0, const char* name = 0 ); | 90 | DiceWidget( QWidget *parent = 0, const char* name = 0 ); |
72 | 91 | ||