summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/go/gowidget.cpp44
1 files changed, 22 insertions, 22 deletions
diff --git a/noncore/games/go/gowidget.cpp b/noncore/games/go/gowidget.cpp
index 8567b30..1d38c9f 100644
--- a/noncore/games/go/gowidget.cpp
+++ b/noncore/games/go/gowidget.cpp
@@ -25,122 +25,122 @@
25 25
26#include <qpainter.h> 26#include <qpainter.h>
27#include <qpixmap.h> 27#include <qpixmap.h>
28#include <qpe/qpetoolbar.h> 28#include <qpe/qpetoolbar.h>
29#include <qpe/qpemenubar.h> 29#include <qpe/qpemenubar.h>
30#include <qpopupmenu.h> 30#include <qpopupmenu.h>
31#include <qaction.h> 31#include <qaction.h>
32#include <qapplication.h> //processEvents() 32#include <qapplication.h> //processEvents()
33#include <qlabel.h> 33#include <qlabel.h>
34 34
35//#include <stdio.h> 35//#include <stdio.h>
36 36
37#include "amigo.h" 37#include "amigo.h"
38#include "goplayutils.h" 38#include "goplayutils.h"
39 39
40static const enum bVal computer_color = BLACK; 40static const enum bVal computer_color = BLACK;
41 41
42static int current_handicap = 1; 42static int current_handicap = 1;
43 43
44static QBrush *goBrush; 44static QBrush *goBrush;
45//static QImage *newBlackStone; 45//static QImage *newBlackStone;
46//static QImage *blackStone; 46//static QImage *blackStone;
47//static QImage *whiteStone; 47//static QImage *whiteStone;
48static QPixmap *newBlackStone; 48static QPixmap *newBlackStone;
49static QPixmap *blackStone; 49static QPixmap *blackStone;
50static QPixmap *whiteStone; 50static QPixmap *whiteStone;
51 51
52static bool smallStones = FALSE; 52static bool smallStones = FALSE;
53 53
54GoMainWidget::GoMainWidget( QWidget *parent, const char* name) : 54GoMainWidget::GoMainWidget( QWidget *parent, const char* name) :
55 QMainWindow( parent, name ) 55 QMainWindow( parent, name )
56{ 56{
57 setToolBarsMovable( FALSE ); 57 setToolBarsMovable( FALSE );
58 GoWidget *go = new GoWidget(this); 58 GoWidget *go = new GoWidget(this);
59 59
60 setCentralWidget(go); 60 setCentralWidget(go);
61 toolbar = new QPEToolBar(this); 61 toolbar = new QPEToolBar(this);
62 toolbar->setHorizontalStretchable( TRUE ); 62 toolbar->setHorizontalStretchable( TRUE );
63 addToolBar(toolbar); 63 addToolBar(toolbar);
64 64
65 QPEMenuBar *mb = new QPEMenuBar( toolbar ); 65 QPEMenuBar *mb = new QPEMenuBar( toolbar );
66 mb->setMargin(0); 66 mb->setMargin(0);
67 QPopupMenu *file = new QPopupMenu( this ); 67 QPopupMenu *file = new QPopupMenu( this );
68 68
69 QAction *a = new QAction( tr( "New Game" ), QString::null, 0, this, 0 ); 69 QAction *a = new QAction( tr( "New Game" ), QString::null, 0, this, 0 );
70 connect( a, SIGNAL( activated() ), go, SLOT( newGame() ) ); 70 connect( a, SIGNAL( activated() ), go, SLOT( newGame() ) );
71 a->addTo( file ); 71 a->addTo( file );
72 72
73 a = new QAction( tr( "Pass" ), Resource::loadPixmap( "pass" ), QString::null, 0, this, 0 ); 73 a = new QAction( tr( "Pass" ), Resource::loadPixmap( "pass" ), QString::null, 0, this, 0 );
74 connect( a, SIGNAL( activated() ), go, SLOT( pass() ) ); 74 connect( a, SIGNAL( activated() ), go, SLOT( pass() ) );
75 a->addTo( file ); 75 a->addTo( file );
76 a->addTo( toolbar ); 76 a->addTo( toolbar );
77 77
78 78
79 a = new QAction( tr( "Resign" ), Resource::loadPixmap( "reset" ), QString::null, 0, this, 0 ); 79 a = new QAction( tr( "Resign" ), Resource::loadPixmap( "reset" ), QString::null, 0, this, 0 );
80 connect( a, SIGNAL( activated() ), go, SLOT( resign() ) ); 80 connect( a, SIGNAL( activated() ), go, SLOT( resign() ) );
81 a->addTo( file ); 81 a->addTo( file );
82 82
83 a = new QAction( tr( "Two player option" ), QString::null, 0, this, 0 ); 83 a = new QAction( tr( "Two player option" ), QString::null, 0, this, 0 );
84 a->setToggleAction( TRUE ); 84 a->setToggleAction( TRUE );
85 connect( a, SIGNAL( toggled(bool) ), go, SLOT( setTwoplayer(bool) ) ); 85 connect( a, SIGNAL( toggled(bool) ), go, SLOT( setTwoplayer(bool) ) );
86 a->addTo( file ); 86 a->addTo( file );
87 87
88 mb->insertItem( tr( "Game" ), file ); 88 mb->insertItem( tr( "Game" ), file );
89 89
90 QLabel *turnLabel = new QLabel( toolbar ); 90 QLabel *turnLabel = new QLabel( toolbar );
91 turnLabel->setBackgroundMode( PaletteButton ); 91 turnLabel->setBackgroundMode( PaletteButton );
92 connect( go, SIGNAL(showTurn(const QPixmap&)), 92 connect( go, SIGNAL(showTurn(const QPixmap&)),
93 turnLabel, SLOT(setPixmap(const QPixmap&)) ); 93 turnLabel, SLOT(setPixmap(const QPixmap&)) );
94 94
95 95
96 QLabel * scoreLabel = new QLabel( toolbar ); 96 QLabel * scoreLabel = new QLabel( toolbar );
97 scoreLabel->setBackgroundMode( PaletteButton ); 97 scoreLabel->setBackgroundMode( PaletteButton );
98 connect( go, SIGNAL(showScore(const QString&)), 98 connect( go, SIGNAL(showScore(const QString&)),
99 scoreLabel, SLOT(setText(const QString&)) ); 99 scoreLabel, SLOT(setText(const QString&)) );
100 100
101 toolbar->setStretchableWidget( scoreLabel ); 101 toolbar->setStretchableWidget( scoreLabel );
102 102
103 go->readConfig(); 103 go->readConfig();
104} 104}
105 105
106void GoMainWidget::resizeEvent( QResizeEvent * ) 106void GoMainWidget::resizeEvent( QResizeEvent * )
107{ 107{
108 //### this won't work because of the text label... 108 //### this won't work because of the text label...
109 /* 109 /*
110 if ( width() > height() ) 110 if ( width() > height() )
111 moveToolBar( toolbar, Left ); 111 moveToolBar( toolbar, Left );
112 else 112 else
113 moveToolBar( toolbar, Top ); 113 moveToolBar( toolbar, Top );
114 */ 114 */
115} 115}
116 116
117GoWidget *GoWidget::self = 0; 117GoWidget *GoWidget::self = 0;
118 118
119GoWidget::GoWidget( QWidget *parent, const char* name) : 119GoWidget::GoWidget( QWidget *parent, const char* name) :
120 QWidget( parent, name ) 120 QWidget( parent, name )
121{ 121{
122 if ( self ) 122 if ( self )
123 fatal( "Only one Go widget allowed" ); 123 fatal( "Only one Go widget allowed" );
124 self = this; 124 self = this;
125 twoplayer = FALSE; 125 twoplayer = FALSE;
126 126
127 127
128 d = bx = by = 1; 128 d = bx = by = 1;
129 129
130 QPixmap pix = Resource::loadPixmap( "pine" ); 130 QPixmap pix = Resource::loadPixmap( "go/pine" );
131 goBrush = new QBrush( black, pix ); 131 goBrush = new QBrush( black, pix );
132 /* 132 /*
133 QString fn = Resource::findPixmap("Go-black"); 133 QString fn = Resource::findPixmap("Go-black");
134 blackStone = new QImage( fn ); 134 blackStone = new QImage( fn );
135 fn = Resource::findPixmap("Go-black-highlight"); 135 fn = Resource::findPixmap("Go-black-highlight");
136 newBlackStone = new QImage( fn ); 136 newBlackStone = new QImage( fn );
137 fn = Resource::findPixmap("Go-white"); 137 fn = Resource::findPixmap("Go-white");
138 whiteStone = new QImage( fn ); 138 whiteStone = new QImage( fn );
139 */ 139 */
140 blackStone = new QPixmap(Resource::loadPixmap( "Go-black" )); 140 blackStone = new QPixmap(Resource::loadPixmap( "Go-black" ));
141 whiteStone = new QPixmap(Resource::loadPixmap( "Go-white" )); 141 whiteStone = new QPixmap(Resource::loadPixmap( "Go-white" ));
142 newBlackStone = new QPixmap(Resource::loadPixmap( "Go-black-highlight" )); 142 newBlackStone = new QPixmap(Resource::loadPixmap( "Go-black-highlight" ));
143 143
144 init(); 144 init();
145} 145}
146 146
@@ -191,33 +191,33 @@ void GoWidget::readConfig()
191 whitePrisoners = cfg.readNumEntry("WhitePrisoners",0); 191 whitePrisoners = cfg.readNumEntry("WhitePrisoners",0);
192 reportPrisoners(blackPrisoners,whitePrisoners); 192 reportPrisoners(blackPrisoners,whitePrisoners);
193 emit showTurn( currentPlayer == WHITE ? *whiteStone : *blackStone ); 193 emit showTurn( currentPlayer == WHITE ? *whiteStone : *blackStone );
194} 194}
195 195
196void GoWidget::resizeEvent( QResizeEvent * ) 196void GoWidget::resizeEvent( QResizeEvent * )
197{ 197{
198 d = QMIN(width(),height())/19; 198 d = QMIN(width(),height())/19;
199 // int r = (d/2-1); 199 // int r = (d/2-1);
200 bx = (width() - 18*d)/2 ; 200 bx = (width() - 18*d)/2 ;
201 by = (height() - 18*d)/2 ; 201 by = (height() - 18*d)/2 ;
202 202
203 if ( d < 10 && !smallStones ) { 203 if ( d < 10 && !smallStones ) {
204 blackStone->convertFromImage( blackStone->convertToImage().smoothScale(8,8) ); 204 blackStone->convertFromImage( blackStone->convertToImage().smoothScale(8,8) );
205 whiteStone->convertFromImage( whiteStone->convertToImage().smoothScale(8,8) ); 205 whiteStone->convertFromImage( whiteStone->convertToImage().smoothScale(8,8) );
206 newBlackStone->convertFromImage( newBlackStone->convertToImage().smoothScale(8,8) ); 206 newBlackStone->convertFromImage( newBlackStone->convertToImage().smoothScale(8,8) );
207 207
208 smallStones = TRUE; 208 smallStones = TRUE;
209 } else if ( d >= 10 && smallStones ) { 209 } else if ( d >= 10 && smallStones ) {
210 blackStone = new QPixmap(Resource::loadPixmap( "Go-black" )); 210 blackStone = new QPixmap(Resource::loadPixmap( "Go-black" ));
211 whiteStone = new QPixmap(Resource::loadPixmap( "Go-white" )); 211 whiteStone = new QPixmap(Resource::loadPixmap( "Go-white" ));
212 newBlackStone = new QPixmap(Resource::loadPixmap( "Go-black-highlight" )); 212 newBlackStone = new QPixmap(Resource::loadPixmap( "Go-black-highlight" ));
213 smallStones = FALSE; 213 smallStones = FALSE;
214 } 214 }
215} 215}
216 216
217void GoWidget::init() 217void GoWidget::init()
218{ 218{
219 lastX = lastY = newX = newY = -1; 219 lastX = lastY = newX = newY = -1;
220 nPassed = 0; 220 nPassed = 0;
221 for ( int i = 0; i < 19; i++ ) 221 for ( int i = 0; i < 19; i++ )
222 for ( int j = 0; j < 19; j++ ) 222 for ( int j = 0; j < 19; j++ )
223 board[i][j]=-1; 223 board[i][j]=-1;
@@ -225,61 +225,61 @@ void GoWidget::init()
225 goRestart(current_handicap); 225 goRestart(current_handicap);
226 226
227 if ( twoplayer ) { 227 if ( twoplayer ) {
228 currentPlayer = BLACK; 228 currentPlayer = BLACK;
229 } else { 229 } else {
230 doComputerMove(); 230 doComputerMove();
231 currentPlayer = WHITE; 231 currentPlayer = WHITE;
232 } 232 }
233 emit showTurn( currentPlayer == WHITE ? *whiteStone : *blackStone ); 233 emit showTurn( currentPlayer == WHITE ? *whiteStone : *blackStone );
234} 234}
235 235
236void GoWidget::paintEvent( QPaintEvent *e ) 236void GoWidget::paintEvent( QPaintEvent *e )
237{ 237{
238 int i,j; 238 int i,j;
239 239
240 int r = whiteStone->width()/2; 240 int r = whiteStone->width()/2;
241 241
242 QPainter p(this); 242 QPainter p(this);
243 p.fillRect( bx - d/2, by - d/2, 19*d, 19*d, *goBrush ); 243 p.fillRect( bx - d/2, by - d/2, 19*d, 19*d, *goBrush );
244 244
245 int xMin = QMAX( x2board(e->rect().left()), 0 ); 245 int xMin = QMAX( x2board(e->rect().left()), 0 );
246 int xMax = QMIN( x2board(e->rect().right()), 18 ); 246 int xMax = QMIN( x2board(e->rect().right()), 18 );
247 int yMin = QMAX( y2board(e->rect().top()), 0 ); 247 int yMin = QMAX( y2board(e->rect().top()), 0 );
248 int yMax = QMIN( y2board(e->rect().bottom()), 18 ); 248 int yMax = QMIN( y2board(e->rect().bottom()), 18 );
249 249
250 QColor pine( 255, 186, 89 ); 250 QColor pine( 255, 186, 89 );
251 p.setPen( pine.dark() ); 251 p.setPen( pine.dark() );
252 252
253 for ( i = xMin; i < xMax+1 ; i ++ ) { 253 for ( i = xMin; i < xMax+1 ; i ++ ) {
254 p.drawLine( bx+i*d, by, bx+i*d, by+18*d ); 254 p.drawLine( bx+i*d, by, bx+i*d, by+18*d );
255 } 255 }
256 for ( j = yMin; j < yMax+1 ; j ++ ) { 256 for ( j = yMin; j < yMax+1 ; j ++ ) {
257 p.drawLine( bx, by+j*d, bx+18*d, by+j*d); 257 p.drawLine( bx, by+j*d, bx+18*d, by+j*d);
258 } 258 }
259 259
260 // dots are at (3,3), (3,9), (3,15) and so on 260 // dots are at (3,3), (3,9), (3,15) and so on
261 p.setBrush( black ); 261 p.setBrush( black );
262 for ( i = 3; i < xMax+1; i+=6 ) 262 for ( i = 3; i < xMax+1; i+=6 )
263 for ( j = 3; j < yMax+1; j+=6 ) 263 for ( j = 3; j < yMax+1; j+=6 )
264 p.drawEllipse( bx+i*d-2, by+j*d-2, 5, 5 ); 264 p.drawEllipse( bx+i*d-2, by+j*d-2, 5, 5 );
265 265
266 266
267 for ( i = xMin; i < xMax+1; i++ ) 267 for ( i = xMin; i < xMax+1; i++ )
268 for ( j = yMin; j < yMax+1; j++ ) { 268 for ( j = yMin; j < yMax+1; j++ ) {
269 if ( board[i][j] == WHITE || 269 if ( board[i][j] == WHITE ||
270 currentPlayer==WHITE && newX == i && newY == j ) 270 currentPlayer==WHITE && newX == i && newY == j )
271 p.drawPixmap( bx+i*d - r, by+j*d - r, *whiteStone ); 271 p.drawPixmap( bx+i*d - r, by+j*d - r, *whiteStone );
272 else if ( i == lastX && j == lastY ) 272 else if ( i == lastX && j == lastY )
273 p.drawPixmap( bx+i*d - r, by+j*d - r, *newBlackStone ); 273 p.drawPixmap( bx+i*d - r, by+j*d - r, *newBlackStone );
274 else if ( board[i][j] == BLACK || 274 else if ( board[i][j] == BLACK ||
275 currentPlayer==BLACK && newX == i && newY == j) 275 currentPlayer==BLACK && newX == i && newY == j)
276 p.drawPixmap( bx+i*d - r, by+j*d - r, *blackStone ); 276 p.drawPixmap( bx+i*d - r, by+j*d - r, *blackStone );
277 } 277 }
278} 278}
279 279
280void GoWidget::doMove( int x, int y ) 280void GoWidget::doMove( int x, int y )
281{ 281{
282 282
283 if ( !GoPlaceStone( currentPlayer, x, y ) ) { 283 if ( !GoPlaceStone( currentPlayer, x, y ) ) {
284 //printf( "Illegal move (%d,%d)\n", x, y ); 284 //printf( "Illegal move (%d,%d)\n", x, y );
285 return; 285 return;
@@ -381,45 +381,45 @@ void GoWidget::mouseMoveEvent( QMouseEvent *me )
381void GoWidget::showStone( int x, int y, enum bVal c ) 381void GoWidget::showStone( int x, int y, enum bVal c )
382{ 382{
383 383
384 if ( newX > -1 ) { 384 if ( newX > -1 ) {
385 refresh( newX, newY ); 385 refresh( newX, newY );
386 newY = newX = -1; 386 newY = newX = -1;
387 } 387 }
388 if ( x < 0 || x > 18 || y < 0 || y > 18 ) { 388 if ( x < 0 || x > 18 || y < 0 || y > 18 ) {
389 newX = newY = -1; 389 newX = newY = -1;
390 return; 390 return;
391 } 391 }
392 if ( board[x][y] == -1 && !Suicide( c, x, y ) ) { 392 if ( board[x][y] == -1 && !Suicide( c, x, y ) ) {
393 newX = x; 393 newX = x;
394 newY = y; 394 newY = y;
395 refresh(x,y); 395 refresh(x,y);
396 } 396 }
397 397
398} 398}
399 399
400void GoWidget::mouseReleaseEvent( QMouseEvent * ) 400void GoWidget::mouseReleaseEvent( QMouseEvent * )
401{ 401{
402 if ( gameActive && newX > -1 ) 402 if ( gameActive && newX > -1 )
403 doMove( newX, newY ); 403 doMove( newX, newY );
404 newX = newY = -1; 404 newX = newY = -1;
405} 405}
406 406
407void GoWidget::refresh( int x, int y ) 407void GoWidget::refresh( int x, int y )
408{ 408{
409 update( bx+d*x-d/2-1, by+d*y-d/2-1, d+2, d+2 ); 409 update( bx+d*x-d/2-1, by+d*y-d/2-1, d+2, d+2 );
410} 410}
411 411
412void GoWidget::removeStone(short x, short y) 412void GoWidget::removeStone(short x, short y)
413{ 413{
414 board[x][y]=-1; 414 board[x][y]=-1;
415 refresh( x, y ); 415 refresh( x, y );
416} 416}
417 417
418void GoWidget::placeStone (enum bVal c, short x, short y ) 418void GoWidget::placeStone (enum bVal c, short x, short y )
419{ 419{
420 board[x][y]=c; 420 board[x][y]=c;
421 refresh( x, y ); 421 refresh( x, y );
422} 422}
423 423
424void GoWidget::reportPrisoners( int blackcnt, int whitecnt ) 424void GoWidget::reportPrisoners( int blackcnt, int whitecnt )
425{ 425{