summaryrefslogtreecommitdiff
path: root/noncore/games/oyatzee/oyatzee.cpp
blob: 5c4d1f72fc583636b4b6649e37eedff3485dfa5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "oyatzee.h"

#include <qpe/applnk.h>
#include <qpe/global.h>
#include <qpe/filemanager.h>
#include <qpe/resource.h>
#include <qpe/config.h>

#include <qapplication.h>
#include <qmessagebox.h>
#include <qcombobox.h>
#include <qdatetime.h>
#include <qfileinfo.h>
#include <qfile.h>
#include <qdir.h>
#include <qiconset.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qtextstream.h>
#include <qtimer.h>
#include <qpe/qpetoolbar.h>
#include <qtoolbutton.h>
#include <qvbox.h>
#include <qwidgetstack.h>
#include <qpainter.h>
#include <qlayout.h>
#include <qregexp.h>

#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>

OYatzee::OYatzee( QWidget *parent , const char *name, WFlags fl ) : QMainWindow( parent , name , fl )
{
	QWidget *thing = new QWidget( this );
	setCentralWidget( thing );
	
	QVBoxLayout *vbox = new QVBoxLayout( thing );

	sb = new Scoreboard( thing , "sb" );
	dw = new DiceWidget( thing , "dw" );
	
	vbox->addWidget( sb );
	vbox->addWidget( dw );

	setPlayerNumber( 2 );
	setRoundsNumber( 1 );
	
	connect( dw->rollButton, SIGNAL( clicked() ), this , SLOT( slotRollDices() ) );
}

OYatzee::~OYatzee()
{
}

void OYatzee::detectPosibilities()
{
	posibilities.clear();
	qDebug( "running detectPosibilities()" );
	
	Dice *d = dw->diceList.first();

	QValueListInt numbers;
	
	for ( ; d != 0 ; d = dw->diceList.next() )
	{
		numbers.append( d->Value );
	}
	
	//the 6 numbers
	QValueListInt::Iterator it;

	for ( int i = 1 ; i < 7 ; ++i ) // check for 1-->6
	{
		bool cont = false;
		it = numbers.begin();
		for ( ; it != numbers.end() ; ++it )
		{
			if ( cont )
				continue;
		
			if ( numbers.find( i ) != numbers.end() )
			{
				posibilities.append( i );
				cont = true;
			}
		}
	}
		
	
	//3er, 4er, Yatzee
	it = numbers.begin();
	int count;
	int temp;
	int countFH = 0; //for the full-house-check

	for ( int i = 1 ; i < 7 ; ++i ) // check for 1-->6 at least 3 times
	{
		count = 0;
		temp = 0;
		it = numbers.begin();
		for ( ; it != numbers.end() ; ++it )
		{
			if ( *it == i )
			{
				count++;
				temp++;
			}
			if ( temp == 2 )
				countFH = temp;
		}
		
		if ( count >= 3 )
		{
			posibilities.append( 7 );

			//now we check if it is a full house
			if ( count == 3 && countFH == 2 ) //aka Full House
				posibilities.append( 9 );
		}
		if ( count >= 4 )
			posibilities.append( 8 );
		if ( count == 5 )                     //Yatzee
			posibilities.append( 12 );
	}
	
	//S-Straight
	if ( numbers.find( 3 ) != numbers.end() && numbers.find( 4 ) != numbers.end() )
	{
		bool isLong = false;
		bool isShort = true;
		//L-Straight
		if ( numbers.find( 2 ) != numbers.end() && numbers.find( 5 ) != numbers.end() )
		{
			isShort = true;
		
			//12345 or 23456
			if ( numbers.find( 1 ) != numbers.end() || numbers.find( 6) != numbers.end() )
				isLong = true;
		}
		//1234
		if ( numbers.find( 1 ) != numbers.end() && numbers.find( 2 ) != numbers.end() )
			isShort = true;
		//3456
		if ( numbers.find( 5 ) != numbers.end() && numbers.find( 6 ) != numbers.end() )
			isShort = true;
		
		if ( isShort )
			posibilities.append( 10 );
		if ( isLong )
			posibilities.append( 11 );
	}	
	
	posibilities.append( 13 );        //Chance, well, this is allways possible

	displayPossibilites();
}

void OYatzee::displayPossibilites()
{
	qDebug( "running displayPossibilites(), %d item", posibilities.count() );
	
	for ( QValueListInt::Iterator it = posibilities.begin() ; it != posibilities.end(); ++it )
	{
		qDebug( QString::number( *it ) );
		switch ( *it )
		{
			case Ones:
				qDebug( "1er" );
				break;
			case Twos:
				qDebug( "2er" );
				break;
			case Threes:
				qDebug( "3er" );
				break;
			case Fours:
				qDebug( "4er" );
				break;
			case Fives:
				qDebug( "5er" );
				break;
			case Sixes:
				qDebug( "6er" );
				break;
			case ThreeOfAKind:
				qDebug( "3oaK" );
				break;
			case FourOfAKind:
				qDebug( "4oaK" );
				break;
			case FullHouse:
				qDebug( "Full House" );
				break;
			case SStraight:
				qDebug( "Short S" );
				break;
			case LStraight:
				qDebug( "Long S" );
				break;
			case Yatzee:
				qDebug( "Yatzee!" );
				break;
			case Chance:
				qDebug( "Chance" );
				break;
		}
	}
}

void OYatzee::setPlayerNumber( const int num )
{
	numOfPlayers = num;
}

void OYatzee::setRoundsNumber( const int num )
{
	numOfRounds = num;
}

void OYatzee::slotStartGame()
{
}

void OYatzee::slotRollDices()
{
	Dice *d = dw->diceList.first();
	
	for ( ; d != 0 ; d = dw->diceList.next() )
	{
		if ( !d->isSelected )
			d->roll();
	}
	
	detectPosibilities();
}

/*
 * Scoreboard
 */
Scoreboard::Scoreboard( QWidget *parent, const char *name ) : QWidget( parent , name )
{
}

void Scoreboard::paintEvent( QPaintEvent * )
{
	QPainter p;
	p.begin( this );

	p.drawRect( 0,0, this->width() , this->height() );
}

/*
 * Dice
 */
Dice::Dice( QWidget *parent , const char *name ) : QFrame( parent , name )
{
	QTime t = QTime::currentTime();   // set random seed
	srand(t.hour()*12+t.minute()*60+t.second()*60);

	connect( this , SIGNAL( selected() ), this , SLOT( slotSelected() ) );
}

void Dice::slotSelected()
{
	if ( isSelected )
		isSelected = false;
	else isSelected = true;

	update();
}

int Dice::hasValue()
{
	return Value;
}

void Dice::roll()
{
	Value = rand()%6;
	Value += 1;

	update();
}

void Dice::mousePressEvent( QMouseEvent* /*e*/ )
{
	    emit selected();
}

void Dice::paintEvent( QPaintEvent * )
{
	QPainter p;
	p.begin( this );

	p.drawRect( 0,0, this->width() , this->height() );

	if ( isSelected )
		p.drawRect( 20,20, 10,10 );

	paintNumber( &p );
}

void Dice::paintNumber( QPainter *p )
{
	switch ( Value )
	{
		case 1:
			p->drawText( 10,10,"1");
			break;
		case 2:
			p->drawText( 10,10,"2");
			break;
		case 3:
			p->drawText( 10,10,"3");
			break;
		case 4:
			p->drawText( 10,10,"4");
			break;
		case 5:
			p->drawText( 10,10,"5");
			break;
		case 6:
			p->drawText( 10,10,"6");
			break;
	}
}

/*
 * DiceWidget
 */
DiceWidget::DiceWidget( QWidget *parent , const char *name ) : QWidget(  parent , name )
{
	rollButton = new QPushButton( tr( "Roll" ) , this ) ;
	
	for ( int i = 0 ; i < 5 ; i++ )
	{
		//appending the 5 dices of the game
		diceList.append( new Dice( this, "wuerfel" ) );
	}
	
	QHBoxLayout *hbox = new QHBoxLayout( this );

	Dice *d = diceList.first();
	
	for ( ; d != 0 ; d = diceList.next() )
	{
		hbox->addWidget( d );
	}

	hbox->addWidget( rollButton );
}

/*
 * Player
 */
Player::Player( QString name )
{
	playerName = name;
}