summaryrefslogtreecommitdiff
authorharlekin <harlekin>2003-03-24 18:09:38 (UTC)
committer harlekin <harlekin>2003-03-24 18:09:38 (UTC)
commitc23656023651523d37406b43fd031ace4e2de89f (patch) (unidiff)
treefd57e9985256064e57c3f93722ba8282f7e3be41
parent58f8a4fe595182abb4372ab4f990e39cf67ef08a (diff)
downloadopie-c23656023651523d37406b43fd031ace4e2de89f.zip
opie-c23656023651523d37406b43fd031ace4e2de89f.tar.gz
opie-c23656023651523d37406b43fd031ace4e2de89f.tar.bz2
pine icon now in go dir
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
@@ -1,464 +1,464 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved. 2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of Qtopia Environment. 4** This file is part of Qtopia Environment.
5** 5**
6** This file may be distributed and/or modified under the terms of the 6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software 7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the 8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file. 9** packaging of this file.
10** 10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20 20
21#include "gowidget.h" 21#include "gowidget.h"
22 22
23#include <qpe/config.h> 23#include <qpe/config.h>
24#include <qpe/resource.h> 24#include <qpe/resource.h>
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
147GoWidget::~GoWidget() 147GoWidget::~GoWidget()
148{ 148{
149 writeConfig(); 149 writeConfig();
150} 150}
151 151
152void GoWidget::writeConfig() 152void GoWidget::writeConfig()
153{ 153{
154 Config cfg("Go"); 154 Config cfg("Go");
155 cfg.setGroup("Game"); 155 cfg.setGroup("Game");
156 cfg.writeEntry("TwoPlayer", twoplayer); 156 cfg.writeEntry("TwoPlayer", twoplayer);
157 cfg.writeEntry("CurrentPlayer", currentPlayer); 157 cfg.writeEntry("CurrentPlayer", currentPlayer);
158 cfg.writeEntry("NPassed", nPassed); 158 cfg.writeEntry("NPassed", nPassed);
159 QString b; 159 QString b;
160 for (int i=0; i<19; i++) 160 for (int i=0; i<19; i++)
161 for (int j=0; j<19; j++) 161 for (int j=0; j<19; j++)
162 b += board[i][j] == BLACK ? 'B' : board[i][j] == WHITE ? 'W' : '.'; 162 b += board[i][j] == BLACK ? 'B' : board[i][j] == WHITE ? 'W' : '.';
163 cfg.writeEntry("Board", b); 163 cfg.writeEntry("Board", b);
164 cfg.writeEntry("LastX", lastX); 164 cfg.writeEntry("LastX", lastX);
165 cfg.writeEntry("LastY", lastY); 165 cfg.writeEntry("LastY", lastY);
166 extern int blackPrisoners, whitePrisoners; 166 extern int blackPrisoners, whitePrisoners;
167 cfg.writeEntry("BlackPrisoners", blackPrisoners); 167 cfg.writeEntry("BlackPrisoners", blackPrisoners);
168 cfg.writeEntry("WhitePrisoners", whitePrisoners); 168 cfg.writeEntry("WhitePrisoners", whitePrisoners);
169} 169}
170 170
171void GoWidget::readConfig() 171void GoWidget::readConfig()
172{ 172{
173 init(); 173 init();
174 Config cfg("Go"); 174 Config cfg("Go");
175 cfg.setGroup("Game"); 175 cfg.setGroup("Game");
176 twoplayer = cfg.readBoolEntry("TwoPlayer"); 176 twoplayer = cfg.readBoolEntry("TwoPlayer");
177 currentPlayer = (bVal)cfg.readNumEntry("CurrentPlayer",1); 177 currentPlayer = (bVal)cfg.readNumEntry("CurrentPlayer",1);
178 nPassed = cfg.readNumEntry("NPassed",0); 178 nPassed = cfg.readNumEntry("NPassed",0);
179 QString b = cfg.readEntry("Board"); 179 QString b = cfg.readEntry("Board");
180 if ( b.length() == 19*19 ) 180 if ( b.length() == 19*19 )
181 for (int i=0; i<19; i++) 181 for (int i=0; i<19; i++)
182 for (int j=0; j<19; j++) { 182 for (int j=0; j<19; j++) {
183 QChar ch = b[j+19*i]; 183 QChar ch = b[j+19*i];
184 if ( ch != '.' ) 184 if ( ch != '.' )
185 GoPlaceStone( ch == 'B' ? BLACK : WHITE, i, j ); 185 GoPlaceStone( ch == 'B' ? BLACK : WHITE, i, j );
186 } 186 }
187 lastX = cfg.readNumEntry("LastX"); 187 lastX = cfg.readNumEntry("LastX");
188 lastY = cfg.readNumEntry("LastY"); 188 lastY = cfg.readNumEntry("LastY");
189 extern int blackPrisoners, whitePrisoners; 189 extern int blackPrisoners, whitePrisoners;
190 blackPrisoners = cfg.readNumEntry("BlackPrisoners",0); 190 blackPrisoners = cfg.readNumEntry("BlackPrisoners",0);
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;
224 gameActive = TRUE; 224 gameActive = TRUE;
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;
286 } 286 }
287 //printf( "you do (%d,%d)\n", x, y ); 287 //printf( "you do (%d,%d)\n", x, y );
288 nPassed = 0; 288 nPassed = 0;
289 if ( twoplayer ) 289 if ( twoplayer )
290 currentPlayer = (currentPlayer==WHITE) ? BLACK : WHITE; 290 currentPlayer = (currentPlayer==WHITE) ? BLACK : WHITE;
291 else 291 else
292 doComputerMove(); 292 doComputerMove();
293 293
294 emit showTurn( currentPlayer == WHITE ? *whiteStone : *blackStone ); 294 emit showTurn( currentPlayer == WHITE ? *whiteStone : *blackStone );
295 295
296} 296}
297 297
298void GoWidget::pass() 298void GoWidget::pass()
299{ 299{
300 if ( !gameActive ) 300 if ( !gameActive )
301 return; 301 return;
302 nPassed++; 302 nPassed++;
303 if ( nPassed >= 2 ) 303 if ( nPassed >= 2 )
304 endGame(); 304 endGame();
305 else if ( !twoplayer ) 305 else if ( !twoplayer )
306 doComputerMove(); 306 doComputerMove();
307} 307}
308 308
309void GoWidget::resign() 309void GoWidget::resign()
310{ 310{
311 if ( gameActive ) 311 if ( gameActive )
312 endGame(); 312 endGame();
313} 313}
314 314
315 315
316void GoWidget::newGame() 316void GoWidget::newGame()
317{ 317{
318 init(); 318 init();
319 update(); 319 update();
320} 320}
321 321
322 322
323void GoWidget::endGame() 323void GoWidget::endGame()
324{ 324{
325 gameActive = FALSE; 325 gameActive = FALSE;
326 326
327 int w,b; 327 int w,b;
328 CountUp( &w, &b); 328 CountUp( &w, &b);
329 QString s = tr("White %1, Black %2. ").arg(w).arg(b); 329 QString s = tr("White %1, Black %2. ").arg(w).arg(b);
330 if ( w > b ) 330 if ( w > b )
331 s += tr("White wins."); 331 s += tr("White wins.");
332 else if ( w < b ) 332 else if ( w < b )
333 s += tr("Black wins."); 333 s += tr("Black wins.");
334 else 334 else
335 s += tr("A draw."); 335 s += tr("A draw.");
336 emit showScore( s ); 336 emit showScore( s );
337} 337}
338 338
339void GoWidget::doComputerMove() 339void GoWidget::doComputerMove()
340{ 340{
341 int ox = lastX; 341 int ox = lastX;
342 int oy = lastY; 342 int oy = lastY;
343 lastX = lastY = -1; 343 lastX = lastY = -1;
344 emit showTurn( *blackStone ); 344 emit showTurn( *blackStone );
345 refresh( ox, oy); 345 refresh( ox, oy);
346 qApp->processEvents(); 346 qApp->processEvents();
347 short int x,y; 347 short int x,y;
348 if ( genMove( computer_color, &x, &y ) ) { 348 if ( genMove( computer_color, &x, &y ) ) {
349 lastX = x; 349 lastX = x;
350 lastY = y; 350 lastY = y;
351 //printf( "I do (%d,%d)\n", x, y ); 351 //printf( "I do (%d,%d)\n", x, y );
352 GoPlaceStone(computer_color,x,y); 352 GoPlaceStone(computer_color,x,y);
353 nPassed = 0; 353 nPassed = 0;
354 } else { 354 } else {
355 emit showScore( tr("I pass") ); 355 emit showScore( tr("I pass") );
356 nPassed++; 356 nPassed++;
357 if ( nPassed >= 2 ) 357 if ( nPassed >= 2 )
358 endGame(); 358 endGame();
359 } 359 }
360} 360}
361 361
362void GoWidget::mousePressEvent( QMouseEvent *me ) 362void GoWidget::mousePressEvent( QMouseEvent *me )
363{ 363{
364 if ( !gameActive ) 364 if ( !gameActive )
365 return; 365 return;
366 int x = x2board(me->x()); 366 int x = x2board(me->x());
367 int y = y2board(me->y()); 367 int y = y2board(me->y());
368 showStone(x,y,currentPlayer); 368 showStone(x,y,currentPlayer);
369} 369}
370 370
371void GoWidget::mouseMoveEvent( QMouseEvent *me ) 371void GoWidget::mouseMoveEvent( QMouseEvent *me )
372{ 372{
373 if ( !gameActive ) 373 if ( !gameActive )
374 return; 374 return;
375 int x = x2board(me->x()); 375 int x = x2board(me->x());
376 int y = y2board(me->y()); 376 int y = y2board(me->y());
377 if ( x != newX || y != newY ) 377 if ( x != newX || y != newY )
378 showStone(x,y,currentPlayer); 378 showStone(x,y,currentPlayer);
379} 379}
380 380
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{
426 QString s = tr( "Prisoners: black %1, white %2" ).arg(blackcnt).arg(whitecnt); 426 QString s = tr( "Prisoners: black %1, white %2" ).arg(blackcnt).arg(whitecnt);
427 emit showScore( s ); 427 emit showScore( s );
428} 428}
429 429
430void GoWidget::setTwoplayer( bool b ) 430void GoWidget::setTwoplayer( bool b )
431{ 431{
432 twoplayer = b; 432 twoplayer = b;
433} 433}
434 434
435void GoWidget::setHandicap( int h ) 435void GoWidget::setHandicap( int h )
436{ 436{
437 current_handicap = h; 437 current_handicap = h;
438} 438}
439 439
440 440
441extern "C" { 441extern "C" {
442 442
443 voidremovestone(short x, short y) 443 voidremovestone(short x, short y)
444{ 444{
445 GoWidget::self->removeStone(x,y); 445 GoWidget::self->removeStone(x,y);
446} 446}
447 447
448 voidplacestone (enum bVal c, short x, short y ) 448 voidplacestone (enum bVal c, short x, short y )
449{ 449{
450 GoWidget::self->placeStone(c,x,y); 450 GoWidget::self->placeStone(c,x,y);
451} 451}
452 452
453 voidintrMoveReport(enum bVal c ,char *coord ,char *reason ) 453 voidintrMoveReport(enum bVal c ,char *coord ,char *reason )
454{ 454{
455 qDebug( "intrMoveReport colour %d, %s %s", c, coord, reason ); 455 qDebug( "intrMoveReport colour %d, %s %s", c, coord, reason );
456} 456}
457 457
458 voidintrPrisonerReport( short blackcnt, short whitecnt ) 458 voidintrPrisonerReport( short blackcnt, short whitecnt )
459{ 459{
460 GoWidget::self->reportPrisoners(blackcnt,whitecnt); 460 GoWidget::self->reportPrisoners(blackcnt,whitecnt);
461} 461}
462 462
463} 463}
464 464