http://www.koders.com/cpp/fidDABD2F414F09B30C488D8FB2403AE87609AAA748.aspx

http://www.koders.com/cpp/fidDABD2F414F09B30C488D8FB2403AE87609AAA748.aspx
All LanguagesActionScript AdaASPASP.NETAssemblerC C#C++CobolColdFusionDelphiEiffelErlang FortranJavaJavaScript JSPLisp LuaMathematica MatlabObjectiveC PerlPHPPrologPythonRuby SchemeSmalltalk SQLTclVB VB.NET All LicensesAcademic Free License Apache License, Version 2.0Apache Software LicenseApple Public Source LicenseBSD License Common Public License Eclipse Public License v1.0Globus Toolkit Public License GNU General Public License v1 v2 v3 GNU Lesser General Public LicenseIBM Public License Intel Open Source License Microsoft Community LicenseMicrosoft Permissive License Microsoft Reference License Microsoft Visual Studio SDK LicenseMIT Derived License Mozilla Public License Version 1.0 Mozilla Public License Version 1.1Netscape Public License, Version 1.0 Netscape Public License, Version 1.1 Open Software LicensePHP License Python Software Foundation LicenseSleepycat Software Product License Sun Public LicenseW3C Software License zlib/libpng LicenseZope Public License

 






[ Show Code]
...kalahari\qkalahari\QKalaha\
   .kdbgrc.kala
   ...grc.QKalaPreference.cpp
   clientsocket.cpp
   clientsocket.h
   dlgnetwork.cpp
   dlgnetwork.h
   kala.pro
   kala.xpm
   kalaserver.cpp
   kalaserver.h
   Main.cpp
   QIniFile.cpp
   qinifile.h
   QKala.cpp
   QKala.h
   QKala.ini
   QKalaBox.cpp
   QKalaBox.h
   QKalaColor.h
   QKalahari.cpp
   QKalahari.h
   QKalaPreference.cpp
   QKalaPreference.h
   QKalaSingleField.cpp
   QKalaSingleField.h
   serversocket.cpp
   serversocket.h
   socket.cpp
   socket.h
   widgetip.cpp
   widgetip.h

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
#include "QKala.h"

QKala::QKala( QWidget* parent, const char* name, WFlags flags ): QWidget( parent, name, flags )
	{
	_usKalaNumberFields = 0;
	_myServer = 0;
	
	initKala();
	}
	
QKala::~QKala()
	{
	int iFieldNr;
	
	for ( iFieldNr = 0; iFieldNr < ( _usKalaNumberFields * 2 + 2 ); ++iFieldNr )
		{
		delete _sfFields[iFieldNr];
		}
	if ( 0 != _myServer ) delete _myServer;
	}
	
void QKala::initKala()
	{
	QString 	name;
	int 		iField 		= 0;

	this->setPalette( widget_Palette );
	
	if ( 0 != _myServer )
		{
		if ( ( 0 == QKalaSingleField::NetMode() ) || ( 0 == QKalaSingleField::Connected() ) )
			{
			delete _myServer;
			_myServer = 0;
			}
		}
	
//deleting old fields if there are any		
	if ( _usKalaNumberFields != 0 )
		{
		for ( iField = 0 ; iField < ( _usKalaNumberFields * 2 + 2 ) ; ++iField )
			{
			delete _sfFields[ iField ];
			}
		}
	
//Setting up fields	
	for ( iField = 0 ; iField < ( QKalaSingleField::NumberFields() * 2 + 2 ) ; ++iField )
		{
		name = "SingleField";
		name += QString::number( iField );
		
		_sfFields[ iField ] = new QKalaSingleField( iField, this, name );
		}
		
		_usKalaNumberFields = QKalaSingleField::NumberFields();
//Connecting slots

	//signalInitFields
	for ( iField = 0; iField < ( QKalaSingleField::NumberFields() * 2 + 2 ); ++iField )
		{
		QObject::connect(	this, 								SIGNAL( signalInitFields()),
					_sfFields[iField],						SLOT( slotInitField()) );
		}
//Attention: Take last field ! Otherwise score is wrong displayed because rest of fields can change after signal!
	QObject::connect(	_sfFields[QKalaSingleField::NumberFields() * 2 + 1], 			SIGNAL( signalFieldInitialized() ),
				this,							   		SLOT( slotFieldInitialized() ) );
				
	//signalGameStausMessage
	for ( iField = 0; iField < ( QKalaSingleField::NumberFields() * 2 + 2 ); ++iField )
		{
		QObject::connect(	_sfFields[iField],						SIGNAL( signalSFGameStatusMessage( const QString &, int )),
					this,								SIGNAL( signalKalaGameStatusMessage( const QString &, int )) );
		}
	//signalGameErrorMessage
	for ( iField = 0; iField < ( QKalaSingleField::NumberFields() * 2 + 2 ); ++iField )
		{
		QObject::connect(	_sfFields[iField],						SIGNAL( signalSFGameErrorMessage( const QString & )),
					this,								SIGNAL( signalKalaGameErrorMessage( const QString & )) );
		}

	//signalDraw
	for ( iField = 0 ; iField < ( QKalaSingleField::NumberFields() * 2 + 1 ); ++iField )
		{                                             		
		QObject::connect( 	_sfFields[iField ],     					SIGNAL( signalDraw( unsigned short )),
					_sfFields[iField + 1],						SLOT( slotDraw( unsigned short )));
		}
		QObject::connect(	_sfFields[QKalaSingleField::NumberFields() * 2 + 1],		SIGNAL( signalDraw( unsigned short )),
				 	_sfFields[0], 							SLOT( slotDraw( unsigned short )));
	//signalAdd
	for ( iField = 0 ; iField < QKalaSingleField::NumberFields(); ++iField )
		{
		QObject::connect( 	_sfFields[iField],	 					SIGNAL( signalAdd( unsigned short, unsigned short )),
				 	_sfFields[QKalaSingleField::NumberFields() * 2 + 1],		SLOT( slotAdd( unsigned short, unsigned short )));
	    	QObject::connect( 	_sfFields[iField], 						SIGNAL( signalAdd( unsigned short, unsigned short )),
				 	_sfFields[QKalaSingleField::NumberFields()],			SLOT( slotAdd( unsigned short, unsigned short )));
		QObject::connect( 	_sfFields[iField + ( QKalaSingleField::NumberFields() + 1 )],	SIGNAL( signalAdd( unsigned short, unsigned short )),
				 	_sfFields[QKalaSingleField::NumberFields() * 2 + 1],		SLOT( slotAdd( unsigned short, unsigned short )));
	    	QObject::connect( 	_sfFields[iField + ( QKalaSingleField::NumberFields() + 1 )],	SIGNAL( signalAdd( unsigned short, unsigned short )),
				 	_sfFields[QKalaSingleField::NumberFields()],			SLOT( slotAdd( unsigned short, unsigned short )));		
		}
		
	//signalLastBall	
	for ( iField = 0 ; iField < QKalaSingleField::NumberFields(); ++iField )
		{
		QObject::connect( 	_sfFields[iField],				 		SIGNAL( signalLastBall( unsigned short )),
				 	_sfFields[( QKalaSingleField::NumberFields() * 2 ) - iField], 	SLOT( slotLastBall( unsigned short )));
		}
	for ( iField = ( QKalaSingleField::NumberFields() + 1 ) ; iField < ( QKalaSingleField::NumberFields() * 2 + 1 ); ++iField )
		{
		QObject::connect( 	_sfFields[iField], 						SIGNAL( signalLastBall( unsigned short )),
				 	_sfFields[( QKalaSingleField::NumberFields() * 2 ) - iField], 	SLOT( slotLastBall( unsigned short )));
		}	

	//signalActive
	for ( iField = 0 ; iField < ( QKalaSingleField::NumberFields() * 2 + 2 ); ++iField )	
		{
     	for ( int i = 0; i < ( QKalaSingleField::NumberFields() * 2 + 2 ); ++i )
     		{
			QObject::connect( 	_sfFields[iField], 					SIGNAL( signalActiv() ),
			 			_sfFields[i], 						SLOT( slotActiv() ));
			}
		}
		                                                  		
	//signalDrawEnd
	for ( iField = 0 ; iField < ( QKalaSingleField::NumberFields() * 2 + 2 ); ++iField )
		{
		QObject::connect( 	_sfFields[iField], 						SIGNAL( signalDrawEnd() ),
				 	this, 								SLOT( slotDrawEnd() ) );
		}

	emit signalInitFields();
	setSize();
	emit signalRuleChanged( QKalaSingleField::Rules() );
	
	if ( QKalaSingleField::NetMode() ) initNetMode();
	}
	
	
void QKala::initNetMode()
	{
	int iField = 0;

	if ( 0 == _myServer )
		{
		_myServer = new KalaServer(  QKalaSingleField::ServerPort() );
		
		QObject::connect( _myServer, 	SIGNAL( signalSocketErrorMessage( const QString & ) ),
					  this,		SIGNAL( signalKalaGameErrorMessage( const QString & ) ) );
	
		QObject::connect( _myServer, 	SIGNAL( signalSocketStatusMessage( const QString & )),
					  this,		SIGNAL( signalKalaSocketStatusMessage( const QString & ) ) );
			
		QObject::connect( this, 	SIGNAL( signalKalaConnect( const QString &, const int ) ),
				  _myServer,	SLOT( slotConnect( const QString &, const int ) ) );
    	
		QObject::connect( this, 	SIGNAL( signalKalaDisconnect() ),
				  _myServer,	SLOT( slotDisconnect() ) );
			
		QObject::connect( _myServer, 	SIGNAL( signalConnected( bool ) ),
					  this,		SIGNAL( signalKalaConnected( bool ) ) );
				
		QObject::connect( _myServer, 	SIGNAL( signalConnected( bool ) ),
					  this,		SLOT( slotConnected( bool ) ) );
	
		QObject::connect( this, 	SIGNAL( signalKalaToSocketMessage( const QString & ) ),
				  _myServer,	SLOT( slotWrite( const QString & ) ) );
			
		QObject::connect( _myServer, 	SIGNAL( signalInitNetGame( const QString & ) ),
					  this,		SIGNAL( signalKalaNetInit( const QString & ) ) );
		
		}	


	for ( iField = 0; iField < ( QKalaSingleField::NumberFields() ); ++iField )
		{
		QObject::connect( 	_sfFields[iField], 	SIGNAL( signalStartDraw( const QString & ) ),
				 	_myServer, 		SLOT( slotWrite( const QString & ) ) );
		}
			  		
	for ( iField = ( QKalaSingleField::NumberFields() + 1 ) ; iField < ( QKalaSingleField::NumberFields() * 2 + 1 ); ++iField )
		{
		QObject::connect( 	_myServer, 		SIGNAL( signalDrawStart( unsigned short ) ),
				 	_sfFields[iField], 	SLOT( slotStartDraw( unsigned short ) ) );
		}			
	}

	
void QKala::setSize()
	{
	int 		iField 		= 0;
	int 		iHeigth 	= 0;
	int 		iBigWidth	= 0;
	int 		iLittleWidth 	= 0;

//Geometry	

	iHeigth		= _sfFields[QKalaSingleField::NumberFields()]->height();
	iBigWidth	= _sfFields[QKalaSingleField::NumberFields()]->width();
	iLittleWidth	= _sfFields[QKalaSingleField::NumberFields() - 1]->width();
		          	
	this->setFixedWidth( ( 2 * iBigWidth ) + ( QKalaSingleField::NumberFields() * iLittleWidth ) );
	this->setFixedHeight( iHeigth );
	//Big Fields
	_sfFields[2 * QKalaSingleField::NumberFields() + 1]->setGeometry( 0, 0, iBigWidth, iHeigth );
	_sfFields[QKalaSingleField::NumberFields()]->setGeometry( this->width() - iBigWidth, 0, iBigWidth, iHeigth );
	//Small Fields
	for ( iField = 0 ; iField < ( QKalaSingleField::NumberFields() ); ++iField )
		{
		_sfFields[iField]->setGeometry( iBigWidth + ( iField * iLittleWidth ), iHeigth/2, iLittleWidth, iHeigth/2 );
		_sfFields[( QKalaSingleField::NumberFields() * 2 ) - iField]->setGeometry( iBigWidth + ( iField * iLittleWidth ), 0, iLittleWidth, iHeigth/2 );
		}	
	
	for ( iField = 0 ; iField < ( QKalaSingleField::NumberFields() * 2 + 2 ); ++iField )
		{
		_sfFields[iField]->show();
		}
	}

//emits a signal for statusbar when turn changes
void QKala::setTurn()
	{
	QString qsTurn = "Turn: ";
	
	if ( 1 == QKalaSingleField::WhosTurn() )	qsTurn += "Player 1";
	else if ( 0 == QKalaSingleField::WhosTurn() )	qsTurn += "Player 2";
	
	emit signalTurnChanged( qsTurn );
	}

//gets the score of the Players and emits a signal for the statusbar	
void QKala::setScore()
	{
	QString 			qsMessage;
	unsigned short usCounter	= 0;
	_usScorePlayerOne 		= 0;		
	_usScorePlayerTwo 		= 0;
//simple rules		
	if ( 1 == QKalaSingleField::Rules() )
		{
		// getting score of players
		for ( usCounter = 0; usCounter < ( QKalaSingleField::NumberFields() + 1 ); ++usCounter )
			{
			_usScorePlayerOne += _sfFields[usCounter]->NumberBalls();
			}
		for ( usCounter = ( QKalaSingleField::NumberFields() + 1 ); usCounter < ( QKalaSingleField::NumberFields() * 2 + 2 ); ++usCounter )
			{
			_usScorePlayerTwo += _sfFields[usCounter]->NumberBalls();
			}
		}
//advanced rules		
	else if ( 0 == QKalaSingleField::Rules() )
		{
		_usScorePlayerOne += _sfFields[QKalaSingleField::NumberFields()]->NumberBalls();
		_usScorePlayerTwo += _sfFields[QKalaSingleField::NumberFields() * 2 + 1]->NumberBalls();		
		}
	else debug( "Error: number of rules can only be 1 or 0 !" );

	qsMessage = "Score: ";
	qsMessage += QString::number( ( int ) _usScorePlayerOne );
	qsMessage += " : ";
	qsMessage += QString::number( ( int ) _usScorePlayerTwo );
	
	emit signalScoreChanged( qsMessage );
	}
//Slots

//is called when on draw ends to test end of game, init setScore, and change the turn of the players
void QKala::slotDrawEnd()
	{
	unsigned short usPlayerOne 	= 0;
	unsigned short usPlayerTwo 	= 0;
	unsigned short usCounter	= 0;	

	setScore();
	setTurn();

	// if one of the two rows is emty -> End of game!	
	for ( usCounter = 0; usCounter < QKalaSingleField::NumberFields(); ++usCounter )
		{
		usPlayerOne += _sfFields[usCounter]->NumberBalls();
		usPlayerTwo += _sfFields[usCounter + ( QKalaSingleField::NumberFields() + 1 )]->NumberBalls();
		}

	if ( ( 0 == usPlayerOne ) || ( 0 == usPlayerTwo ) )
		{
		emit signalKalaGameStatusMessage( "End of game", 12000 );
		emit signalEndOfGame( _usScorePlayerOne, _usScorePlayerTwo );
		}	
	}

void QKala::slotFieldInitialized()
	{
	setTurn();
	setScore();
	}
	
void QKala::slotConnected( bool Connected )
	{
	if ( Connected ) QKalaSingleField::setConnection( true );
	else QKalaSingleField::setConnection( false );
	}
	

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us

你可能感兴趣的:(http://www.koders.com/cpp/fidDABD2F414F09B30C488D8FB2403AE87609AAA748.aspx)