-rw-r--r-- | noncore/games/go/gowidget.cpp | 34 |
1 files changed, 13 insertions, 21 deletions
diff --git a/noncore/games/go/gowidget.cpp b/noncore/games/go/gowidget.cpp index ec03c1d..bc5d788 100644 --- a/noncore/games/go/gowidget.cpp +++ b/noncore/games/go/gowidget.cpp | |||
@@ -1,461 +1,453 @@ | |||
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 | /* OPIE */ | 23 | /* OPIE */ |
24 | #include <opie2/odebug.h> | 24 | #include <opie2/odebug.h> |
25 | #include <opie2/oresource.h> | ||
25 | #include <qpe/config.h> | 26 | #include <qpe/config.h> |
26 | #include <qpe/resource.h> | ||
27 | using namespace Opie::Core; | 27 | using namespace Opie::Core; |
28 | 28 | ||
29 | /* QT */ | 29 | /* QT */ |
30 | #include <qpainter.h> | 30 | #include <qpainter.h> |
31 | #include <qtoolbar.h> | 31 | #include <qtoolbar.h> |
32 | #include <qmenubar.h> | 32 | #include <qmenubar.h> |
33 | #include <qaction.h> | 33 | #include <qaction.h> |
34 | #include <qapplication.h> //processEvents() | 34 | #include <qapplication.h> //processEvents() |
35 | #include <qlabel.h> | 35 | #include <qlabel.h> |
36 | 36 | ||
37 | static const enum bVal computer_color = BLACK; | 37 | static const enum bVal computer_color = BLACK; |
38 | 38 | ||
39 | static int current_handicap = 1; | 39 | static int current_handicap = 1; |
40 | 40 | ||
41 | static QBrush *goBrush; | 41 | static QBrush *goBrush; |
42 | //static QImage *newBlackStone; | ||
43 | //static QImage *blackStone; | ||
44 | //static QImage *whiteStone; | ||
45 | static QPixmap *newBlackStone; | 42 | static QPixmap *newBlackStone; |
46 | static QPixmap *blackStone; | 43 | static QPixmap *blackStone; |
47 | static QPixmap *whiteStone; | 44 | static QPixmap *whiteStone; |
48 | 45 | ||
49 | static bool smallStones = FALSE; | 46 | static bool smallStones = FALSE; |
50 | 47 | ||
51 | GoMainWidget::GoMainWidget( QWidget *parent, const char* name, WFlags fl) : | 48 | GoMainWidget::GoMainWidget( QWidget *parent, const char* name, WFlags fl) : |
52 | QMainWindow( parent, name, fl ) | 49 | QMainWindow( parent, name, fl ) |
53 | { | 50 | { |
51 | setCaption( tr( "Go" ) ); | ||
54 | setToolBarsMovable( FALSE ); | 52 | setToolBarsMovable( FALSE ); |
55 | GoWidget *go = new GoWidget(this); | 53 | GoWidget *go = new GoWidget(this); |
56 | 54 | ||
57 | setCentralWidget(go); | 55 | setCentralWidget(go); |
58 | toolbar = new QToolBar(this); | 56 | toolbar = new QToolBar(this); |
59 | toolbar->setHorizontalStretchable( TRUE ); | 57 | toolbar->setHorizontalStretchable( TRUE ); |
60 | addToolBar(toolbar); | 58 | addToolBar(toolbar); |
61 | 59 | ||
62 | QMenuBar *mb = new QMenuBar( toolbar ); | 60 | QMenuBar *mb = new QMenuBar( toolbar ); |
63 | mb->setMargin(0); | 61 | mb->setMargin(0); |
64 | QPopupMenu *file = new QPopupMenu( this ); | 62 | QPopupMenu *file = new QPopupMenu( this ); |
65 | 63 | ||
66 | QAction *a = new QAction( tr( "New Game" ), QString::null, 0, this, 0 ); | 64 | QAction *a = new QAction( tr( "New Game" ), QString::null, 0, this, 0 ); |
67 | connect( a, SIGNAL( activated() ), go, SLOT( newGame() ) ); | 65 | connect( a, SIGNAL( activated() ), go, SLOT( newGame() ) ); |
68 | a->addTo( file ); | 66 | a->addTo( file ); |
69 | 67 | ||
70 | a = new QAction( tr( "Pass" ), Resource::loadPixmap( "pass" ), QString::null, 0, this, 0 ); | 68 | a = new QAction( tr( "Pass" ), Opie::Core::OResource::loadPixmap( "pass", Opie::Core::OResource::SmallIcon ), |
69 | QString::null, 0, this, 0 ); | ||
71 | connect( a, SIGNAL( activated() ), go, SLOT( pass() ) ); | 70 | connect( a, SIGNAL( activated() ), go, SLOT( pass() ) ); |
72 | a->addTo( file ); | 71 | a->addTo( file ); |
73 | a->addTo( toolbar ); | 72 | a->addTo( toolbar ); |
74 | 73 | ||
75 | 74 | ||
76 | a = new QAction( tr( "Resign" ), Resource::loadPixmap( "reset" ), QString::null, 0, this, 0 ); | 75 | a = new QAction( tr( "Resign" ), Opie::Core::OResource::loadPixmap( "reset", Opie::Core::OResource::SmallIcon ), |
76 | QString::null, 0, this, 0 ); | ||
77 | connect( a, SIGNAL( activated() ), go, SLOT( resign() ) ); | 77 | connect( a, SIGNAL( activated() ), go, SLOT( resign() ) ); |
78 | a->addTo( file ); | 78 | a->addTo( file ); |
79 | 79 | ||
80 | a = new QAction( tr( "Two player option" ), QString::null, 0, this, 0 ); | 80 | a = new QAction( tr( "Two player option" ), QString::null, 0, this, 0 ); |
81 | a->setToggleAction( TRUE ); | 81 | a->setToggleAction( TRUE ); |
82 | connect( a, SIGNAL( toggled(bool) ), go, SLOT( setTwoplayer(bool) ) ); | 82 | connect( a, SIGNAL( toggled(bool) ), go, SLOT( setTwoplayer(bool) ) ); |
83 | a->addTo( file ); | 83 | a->addTo( file ); |
84 | 84 | ||
85 | mb->insertItem( tr( "Game" ), file ); | 85 | mb->insertItem( tr( "Game" ), file ); |
86 | 86 | ||
87 | QLabel *turnLabel = new QLabel( toolbar ); | 87 | QLabel *turnLabel = new QLabel( toolbar ); |
88 | turnLabel->setBackgroundMode( PaletteButton ); | 88 | turnLabel->setBackgroundMode( PaletteButton ); |
89 | connect( go, SIGNAL(showTurn(const QPixmap&)), | 89 | connect( go, SIGNAL(showTurn(const QPixmap&)), |
90 | turnLabel, SLOT(setPixmap(const QPixmap&)) ); | 90 | turnLabel, SLOT(setPixmap(const QPixmap&)) ); |
91 | 91 | ||
92 | 92 | ||
93 | QLabel * scoreLabel = new QLabel( toolbar ); | 93 | QLabel * scoreLabel = new QLabel( toolbar ); |
94 | scoreLabel->setBackgroundMode( PaletteButton ); | 94 | scoreLabel->setBackgroundMode( PaletteButton ); |
95 | connect( go, SIGNAL(showScore(const QString&)), | 95 | connect( go, SIGNAL(showScore(const QString&)), |
96 | scoreLabel, SLOT(setText(const QString&)) ); | 96 | scoreLabel, SLOT(setText(const QString&)) ); |
97 | 97 | ||
98 | toolbar->setStretchableWidget( scoreLabel ); | 98 | toolbar->setStretchableWidget( scoreLabel ); |
99 | 99 | ||
100 | go->readConfig(); | 100 | go->readConfig(); |
101 | } | 101 | } |
102 | 102 | ||
103 | void GoMainWidget::resizeEvent( QResizeEvent * ) | 103 | void GoMainWidget::resizeEvent( QResizeEvent * ) |
104 | { | 104 | { |
105 | //### this won't work because of the text label... | 105 | //### this won't work because of the text label... |
106 | /* | 106 | /* |
107 | if ( width() > height() ) | 107 | if ( width() > height() ) |
108 | moveToolBar( toolbar, Left ); | 108 | moveToolBar( toolbar, Left ); |
109 | else | 109 | else |
110 | moveToolBar( toolbar, Top ); | 110 | moveToolBar( toolbar, Top ); |
111 | */ | 111 | */ |
112 | } | 112 | } |
113 | 113 | ||
114 | GoWidget *GoWidget::self = 0; | 114 | GoWidget *GoWidget::self = 0; |
115 | 115 | ||
116 | GoWidget::GoWidget( QWidget *parent, const char* name) : | 116 | GoWidget::GoWidget( QWidget *parent, const char* name) : |
117 | QWidget( parent, name ) | 117 | QWidget( parent, name ) |
118 | { | 118 | { |
119 | if ( self ) | 119 | if ( self ) |
120 | fatal( "Only one Go widget allowed" ); | 120 | fatal( "Only one Go widget allowed" ); |
121 | self = this; | 121 | self = this; |
122 | twoplayer = FALSE; | 122 | twoplayer = FALSE; |
123 | 123 | ||
124 | 124 | ||
125 | d = bx = by = 1; | 125 | d = bx = by = 1; |
126 | 126 | ||
127 | QPixmap pix = Resource::loadPixmap( "go/pine" ); | 127 | QPixmap pix = Opie::Core::OResource::loadPixmap( "go/pine" ); |
128 | goBrush = new QBrush( black, pix ); | 128 | goBrush = new QBrush( black, pix ); |
129 | /* | 129 | blackStone = new QPixmap(Opie::Core::OResource::loadPixmap( "Go-black" )); |
130 | QString fn = Resource::findPixmap("Go-black"); | 130 | whiteStone = new QPixmap(Opie::Core::OResource::loadPixmap( "Go-white" )); |
131 | blackStone = new QImage( fn ); | 131 | newBlackStone = new QPixmap(Opie::Core::OResource::loadPixmap( "Go-black-highlight" )); |
132 | fn = Resource::findPixmap("Go-black-highlight"); | ||
133 | newBlackStone = new QImage( fn ); | ||
134 | fn = Resource::findPixmap("Go-white"); | ||
135 | whiteStone = new QImage( fn ); | ||
136 | */ | ||
137 | blackStone = new QPixmap(Resource::loadPixmap( "Go-black" )); | ||
138 | whiteStone = new QPixmap(Resource::loadPixmap( "Go-white" )); | ||
139 | newBlackStone = new QPixmap(Resource::loadPixmap( "Go-black-highlight" )); | ||
140 | 132 | ||
141 | init(); | 133 | init(); |
142 | } | 134 | } |
143 | 135 | ||
144 | GoWidget::~GoWidget() | 136 | GoWidget::~GoWidget() |
145 | { | 137 | { |
146 | writeConfig(); | 138 | writeConfig(); |
147 | } | 139 | } |
148 | 140 | ||
149 | void GoWidget::writeConfig() | 141 | void GoWidget::writeConfig() |
150 | { | 142 | { |
151 | Config cfg("Go"); | 143 | Config cfg("Go"); |
152 | cfg.setGroup("Game"); | 144 | cfg.setGroup("Game"); |
153 | cfg.writeEntry("TwoPlayer", twoplayer); | 145 | cfg.writeEntry("TwoPlayer", twoplayer); |
154 | cfg.writeEntry("CurrentPlayer", currentPlayer); | 146 | cfg.writeEntry("CurrentPlayer", currentPlayer); |
155 | cfg.writeEntry("NPassed", nPassed); | 147 | cfg.writeEntry("NPassed", nPassed); |
156 | QString b; | 148 | QString b; |
157 | for (int i=0; i<19; i++) | 149 | for (int i=0; i<19; i++) |
158 | for (int j=0; j<19; j++) | 150 | for (int j=0; j<19; j++) |
159 | b += board[i][j] == BLACK ? 'B' : board[i][j] == WHITE ? 'W' : '.'; | 151 | b += board[i][j] == BLACK ? 'B' : board[i][j] == WHITE ? 'W' : '.'; |
160 | cfg.writeEntry("Board", b); | 152 | cfg.writeEntry("Board", b); |
161 | cfg.writeEntry("LastX", lastX); | 153 | cfg.writeEntry("LastX", lastX); |
162 | cfg.writeEntry("LastY", lastY); | 154 | cfg.writeEntry("LastY", lastY); |
163 | extern int blackPrisoners, whitePrisoners; | 155 | extern int blackPrisoners, whitePrisoners; |
164 | cfg.writeEntry("BlackPrisoners", blackPrisoners); | 156 | cfg.writeEntry("BlackPrisoners", blackPrisoners); |
165 | cfg.writeEntry("WhitePrisoners", whitePrisoners); | 157 | cfg.writeEntry("WhitePrisoners", whitePrisoners); |
166 | } | 158 | } |
167 | 159 | ||
168 | void GoWidget::readConfig() | 160 | void GoWidget::readConfig() |
169 | { | 161 | { |
170 | init(); | 162 | init(); |
171 | Config cfg("Go"); | 163 | Config cfg("Go"); |
172 | cfg.setGroup("Game"); | 164 | cfg.setGroup("Game"); |
173 | twoplayer = cfg.readBoolEntry("TwoPlayer"); | 165 | twoplayer = cfg.readBoolEntry("TwoPlayer"); |
174 | currentPlayer = (bVal)cfg.readNumEntry("CurrentPlayer",1); | 166 | currentPlayer = (bVal)cfg.readNumEntry("CurrentPlayer",1); |
175 | nPassed = cfg.readNumEntry("NPassed",0); | 167 | nPassed = cfg.readNumEntry("NPassed",0); |
176 | QString b = cfg.readEntry("Board"); | 168 | QString b = cfg.readEntry("Board"); |
177 | if ( b.length() == 19*19 ) | 169 | if ( b.length() == 19*19 ) |
178 | for (int i=0; i<19; i++) | 170 | for (int i=0; i<19; i++) |
179 | for (int j=0; j<19; j++) { | 171 | for (int j=0; j<19; j++) { |
180 | QChar ch = b[j+19*i]; | 172 | QChar ch = b[j+19*i]; |
181 | if ( ch != '.' ) | 173 | if ( ch != '.' ) |
182 | GoPlaceStone( ch == 'B' ? BLACK : WHITE, i, j ); | 174 | GoPlaceStone( ch == 'B' ? BLACK : WHITE, i, j ); |
183 | } | 175 | } |
184 | lastX = cfg.readNumEntry("LastX"); | 176 | lastX = cfg.readNumEntry("LastX"); |
185 | lastY = cfg.readNumEntry("LastY"); | 177 | lastY = cfg.readNumEntry("LastY"); |
186 | extern int blackPrisoners, whitePrisoners; | 178 | extern int blackPrisoners, whitePrisoners; |
187 | blackPrisoners = cfg.readNumEntry("BlackPrisoners",0); | 179 | blackPrisoners = cfg.readNumEntry("BlackPrisoners",0); |
188 | whitePrisoners = cfg.readNumEntry("WhitePrisoners",0); | 180 | whitePrisoners = cfg.readNumEntry("WhitePrisoners",0); |
189 | reportPrisoners(blackPrisoners,whitePrisoners); | 181 | reportPrisoners(blackPrisoners,whitePrisoners); |
190 | emit showTurn( currentPlayer == WHITE ? *whiteStone : *blackStone ); | 182 | emit showTurn( currentPlayer == WHITE ? *whiteStone : *blackStone ); |
191 | } | 183 | } |
192 | 184 | ||
193 | void GoWidget::resizeEvent( QResizeEvent * ) | 185 | void GoWidget::resizeEvent( QResizeEvent * ) |
194 | { | 186 | { |
195 | d = QMIN(width(),height())/19; | 187 | d = QMIN(width(),height())/19; |
196 | // int r = (d/2-1); | 188 | // int r = (d/2-1); |
197 | bx = (width() - 18*d)/2 ; | 189 | bx = (width() - 18*d)/2 ; |
198 | by = (height() - 18*d)/2 ; | 190 | by = (height() - 18*d)/2 ; |
199 | 191 | ||
200 | if ( d < 10 && !smallStones ) { | 192 | if ( d < 10 && !smallStones ) { |
201 | blackStone->convertFromImage( blackStone->convertToImage().smoothScale(8,8) ); | 193 | blackStone->convertFromImage( blackStone->convertToImage().smoothScale(8,8) ); |
202 | whiteStone->convertFromImage( whiteStone->convertToImage().smoothScale(8,8) ); | 194 | whiteStone->convertFromImage( whiteStone->convertToImage().smoothScale(8,8) ); |
203 | newBlackStone->convertFromImage( newBlackStone->convertToImage().smoothScale(8,8) ); | 195 | newBlackStone->convertFromImage( newBlackStone->convertToImage().smoothScale(8,8) ); |
204 | 196 | ||
205 | smallStones = TRUE; | 197 | smallStones = TRUE; |
206 | } else if ( d >= 10 && smallStones ) { | 198 | } else if ( d >= 10 && smallStones ) { |
207 | blackStone = new QPixmap(Resource::loadPixmap( "Go-black" )); | 199 | blackStone = new QPixmap(Opie::Core::OResource::loadPixmap( "Go-black" )); |
208 | whiteStone = new QPixmap(Resource::loadPixmap( "Go-white" )); | 200 | whiteStone = new QPixmap(Opie::Core::OResource::loadPixmap( "Go-white" )); |
209 | newBlackStone = new QPixmap(Resource::loadPixmap( "Go-black-highlight" )); | 201 | newBlackStone = new QPixmap(Opie::Core::OResource::loadPixmap( "Go-black-highlight" )); |
210 | smallStones = FALSE; | 202 | smallStones = FALSE; |
211 | } | 203 | } |
212 | } | 204 | } |
213 | 205 | ||
214 | void GoWidget::init() | 206 | void GoWidget::init() |
215 | { | 207 | { |
216 | lastX = lastY = newX = newY = -1; | 208 | lastX = lastY = newX = newY = -1; |
217 | nPassed = 0; | 209 | nPassed = 0; |
218 | for ( int i = 0; i < 19; i++ ) | 210 | for ( int i = 0; i < 19; i++ ) |
219 | for ( int j = 0; j < 19; j++ ) | 211 | for ( int j = 0; j < 19; j++ ) |
220 | board[i][j]=-1; | 212 | board[i][j]=-1; |
221 | gameActive = TRUE; | 213 | gameActive = TRUE; |
222 | goRestart(current_handicap); | 214 | goRestart(current_handicap); |
223 | 215 | ||
224 | if ( twoplayer ) { | 216 | if ( twoplayer ) { |
225 | currentPlayer = BLACK; | 217 | currentPlayer = BLACK; |
226 | } else { | 218 | } else { |
227 | doComputerMove(); | 219 | doComputerMove(); |
228 | currentPlayer = WHITE; | 220 | currentPlayer = WHITE; |
229 | } | 221 | } |
230 | emit showTurn( currentPlayer == WHITE ? *whiteStone : *blackStone ); | 222 | emit showTurn( currentPlayer == WHITE ? *whiteStone : *blackStone ); |
231 | } | 223 | } |
232 | 224 | ||
233 | void GoWidget::paintEvent( QPaintEvent *e ) | 225 | void GoWidget::paintEvent( QPaintEvent *e ) |
234 | { | 226 | { |
235 | int i,j; | 227 | int i,j; |
236 | 228 | ||
237 | int r = whiteStone->width()/2; | 229 | int r = whiteStone->width()/2; |
238 | 230 | ||
239 | QPainter p(this); | 231 | QPainter p(this); |
240 | p.fillRect( bx - d/2, by - d/2, 19*d, 19*d, *goBrush ); | 232 | p.fillRect( bx - d/2, by - d/2, 19*d, 19*d, *goBrush ); |
241 | 233 | ||
242 | int xMin = QMAX( x2board(e->rect().left()), 0 ); | 234 | int xMin = QMAX( x2board(e->rect().left()), 0 ); |
243 | int xMax = QMIN( x2board(e->rect().right()), 18 ); | 235 | int xMax = QMIN( x2board(e->rect().right()), 18 ); |
244 | int yMin = QMAX( y2board(e->rect().top()), 0 ); | 236 | int yMin = QMAX( y2board(e->rect().top()), 0 ); |
245 | int yMax = QMIN( y2board(e->rect().bottom()), 18 ); | 237 | int yMax = QMIN( y2board(e->rect().bottom()), 18 ); |
246 | 238 | ||
247 | QColor pine( 255, 186, 89 ); | 239 | QColor pine( 255, 186, 89 ); |
248 | p.setPen( pine.dark() ); | 240 | p.setPen( pine.dark() ); |
249 | 241 | ||
250 | for ( i = xMin; i < xMax+1 ; i ++ ) { | 242 | for ( i = xMin; i < xMax+1 ; i ++ ) { |
251 | p.drawLine( bx+i*d, by, bx+i*d, by+18*d ); | 243 | p.drawLine( bx+i*d, by, bx+i*d, by+18*d ); |
252 | } | 244 | } |
253 | for ( j = yMin; j < yMax+1 ; j ++ ) { | 245 | for ( j = yMin; j < yMax+1 ; j ++ ) { |
254 | p.drawLine( bx, by+j*d, bx+18*d, by+j*d); | 246 | p.drawLine( bx, by+j*d, bx+18*d, by+j*d); |
255 | } | 247 | } |
256 | 248 | ||
257 | // dots are at (3,3), (3,9), (3,15) and so on | 249 | // dots are at (3,3), (3,9), (3,15) and so on |
258 | p.setBrush( black ); | 250 | p.setBrush( black ); |
259 | for ( i = 3; i < xMax+1; i+=6 ) | 251 | for ( i = 3; i < xMax+1; i+=6 ) |
260 | for ( j = 3; j < yMax+1; j+=6 ) | 252 | for ( j = 3; j < yMax+1; j+=6 ) |
261 | p.drawEllipse( bx+i*d-2, by+j*d-2, 5, 5 ); | 253 | p.drawEllipse( bx+i*d-2, by+j*d-2, 5, 5 ); |
262 | 254 | ||
263 | 255 | ||
264 | for ( i = xMin; i < xMax+1; i++ ) | 256 | for ( i = xMin; i < xMax+1; i++ ) |
265 | for ( j = yMin; j < yMax+1; j++ ) { | 257 | for ( j = yMin; j < yMax+1; j++ ) { |
266 | if ( board[i][j] == WHITE || | 258 | if ( board[i][j] == WHITE || |
267 | currentPlayer==WHITE && newX == i && newY == j ) | 259 | currentPlayer==WHITE && newX == i && newY == j ) |
268 | p.drawPixmap( bx+i*d - r, by+j*d - r, *whiteStone ); | 260 | p.drawPixmap( bx+i*d - r, by+j*d - r, *whiteStone ); |
269 | else if ( i == lastX && j == lastY ) | 261 | else if ( i == lastX && j == lastY ) |
270 | p.drawPixmap( bx+i*d - r, by+j*d - r, *newBlackStone ); | 262 | p.drawPixmap( bx+i*d - r, by+j*d - r, *newBlackStone ); |
271 | else if ( board[i][j] == BLACK || | 263 | else if ( board[i][j] == BLACK || |
272 | currentPlayer==BLACK && newX == i && newY == j) | 264 | currentPlayer==BLACK && newX == i && newY == j) |
273 | p.drawPixmap( bx+i*d - r, by+j*d - r, *blackStone ); | 265 | p.drawPixmap( bx+i*d - r, by+j*d - r, *blackStone ); |
274 | } | 266 | } |
275 | } | 267 | } |
276 | 268 | ||
277 | void GoWidget::doMove( int x, int y ) | 269 | void GoWidget::doMove( int x, int y ) |
278 | { | 270 | { |
279 | 271 | ||
280 | if ( !GoPlaceStone( currentPlayer, x, y ) ) { | 272 | if ( !GoPlaceStone( currentPlayer, x, y ) ) { |
281 | //printf( "Illegal move (%d,%d)\n", x, y ); | 273 | //printf( "Illegal move (%d,%d)\n", x, y ); |
282 | return; | 274 | return; |
283 | } | 275 | } |
284 | //printf( "you do (%d,%d)\n", x, y ); | 276 | //printf( "you do (%d,%d)\n", x, y ); |
285 | nPassed = 0; | 277 | nPassed = 0; |
286 | if ( twoplayer ) | 278 | if ( twoplayer ) |
287 | currentPlayer = (currentPlayer==WHITE) ? BLACK : WHITE; | 279 | currentPlayer = (currentPlayer==WHITE) ? BLACK : WHITE; |
288 | else | 280 | else |
289 | doComputerMove(); | 281 | doComputerMove(); |
290 | 282 | ||
291 | emit showTurn( currentPlayer == WHITE ? *whiteStone : *blackStone ); | 283 | emit showTurn( currentPlayer == WHITE ? *whiteStone : *blackStone ); |
292 | 284 | ||
293 | } | 285 | } |
294 | 286 | ||
295 | void GoWidget::pass() | 287 | void GoWidget::pass() |
296 | { | 288 | { |
297 | if ( !gameActive ) | 289 | if ( !gameActive ) |
298 | return; | 290 | return; |
299 | nPassed++; | 291 | nPassed++; |
300 | if ( nPassed >= 2 ) | 292 | if ( nPassed >= 2 ) |
301 | endGame(); | 293 | endGame(); |
302 | else if ( !twoplayer ) | 294 | else if ( !twoplayer ) |
303 | doComputerMove(); | 295 | doComputerMove(); |
304 | } | 296 | } |
305 | 297 | ||
306 | void GoWidget::resign() | 298 | void GoWidget::resign() |
307 | { | 299 | { |
308 | if ( gameActive ) | 300 | if ( gameActive ) |
309 | endGame(); | 301 | endGame(); |
310 | } | 302 | } |
311 | 303 | ||
312 | 304 | ||
313 | void GoWidget::newGame() | 305 | void GoWidget::newGame() |
314 | { | 306 | { |
315 | init(); | 307 | init(); |
316 | update(); | 308 | update(); |
317 | } | 309 | } |
318 | 310 | ||
319 | 311 | ||
320 | void GoWidget::endGame() | 312 | void GoWidget::endGame() |
321 | { | 313 | { |
322 | gameActive = FALSE; | 314 | gameActive = FALSE; |
323 | 315 | ||
324 | int w,b; | 316 | int w,b; |
325 | CountUp( &w, &b); | 317 | CountUp( &w, &b); |
326 | QString s = tr("White %1, Black %2. ").arg(w).arg(b); | 318 | QString s = tr("White %1, Black %2. ").arg(w).arg(b); |
327 | if ( w > b ) | 319 | if ( w > b ) |
328 | s += tr("White wins."); | 320 | s += tr("White wins."); |
329 | else if ( w < b ) | 321 | else if ( w < b ) |
330 | s += tr("Black wins."); | 322 | s += tr("Black wins."); |
331 | else | 323 | else |
332 | s += tr("A draw."); | 324 | s += tr("A draw."); |
333 | emit showScore( s ); | 325 | emit showScore( s ); |
334 | } | 326 | } |
335 | 327 | ||
336 | void GoWidget::doComputerMove() | 328 | void GoWidget::doComputerMove() |
337 | { | 329 | { |
338 | int ox = lastX; | 330 | int ox = lastX; |
339 | int oy = lastY; | 331 | int oy = lastY; |
340 | lastX = lastY = -1; | 332 | lastX = lastY = -1; |
341 | emit showTurn( *blackStone ); | 333 | emit showTurn( *blackStone ); |
342 | refresh( ox, oy); | 334 | refresh( ox, oy); |
343 | qApp->processEvents(); | 335 | qApp->processEvents(); |
344 | short int x,y; | 336 | short int x,y; |
345 | if ( genMove( computer_color, &x, &y ) ) { | 337 | if ( genMove( computer_color, &x, &y ) ) { |
346 | lastX = x; | 338 | lastX = x; |
347 | lastY = y; | 339 | lastY = y; |
348 | //printf( "I do (%d,%d)\n", x, y ); | 340 | //printf( "I do (%d,%d)\n", x, y ); |
349 | GoPlaceStone(computer_color,x,y); | 341 | GoPlaceStone(computer_color,x,y); |
350 | nPassed = 0; | 342 | nPassed = 0; |
351 | } else { | 343 | } else { |
352 | emit showScore( tr("I pass") ); | 344 | emit showScore( tr("I pass") ); |
353 | nPassed++; | 345 | nPassed++; |
354 | if ( nPassed >= 2 ) | 346 | if ( nPassed >= 2 ) |
355 | endGame(); | 347 | endGame(); |
356 | } | 348 | } |
357 | } | 349 | } |
358 | 350 | ||
359 | void GoWidget::mousePressEvent( QMouseEvent *me ) | 351 | void GoWidget::mousePressEvent( QMouseEvent *me ) |
360 | { | 352 | { |
361 | if ( !gameActive ) | 353 | if ( !gameActive ) |
362 | return; | 354 | return; |
363 | int x = x2board(me->x()); | 355 | int x = x2board(me->x()); |
364 | int y = y2board(me->y()); | 356 | int y = y2board(me->y()); |
365 | showStone(x,y,currentPlayer); | 357 | showStone(x,y,currentPlayer); |
366 | } | 358 | } |
367 | 359 | ||
368 | void GoWidget::mouseMoveEvent( QMouseEvent *me ) | 360 | void GoWidget::mouseMoveEvent( QMouseEvent *me ) |
369 | { | 361 | { |
370 | if ( !gameActive ) | 362 | if ( !gameActive ) |
371 | return; | 363 | return; |
372 | int x = x2board(me->x()); | 364 | int x = x2board(me->x()); |
373 | int y = y2board(me->y()); | 365 | int y = y2board(me->y()); |
374 | if ( x != newX || y != newY ) | 366 | if ( x != newX || y != newY ) |
375 | showStone(x,y,currentPlayer); | 367 | showStone(x,y,currentPlayer); |
376 | } | 368 | } |
377 | 369 | ||
378 | void GoWidget::showStone( int x, int y, enum bVal c ) | 370 | void GoWidget::showStone( int x, int y, enum bVal c ) |
379 | { | 371 | { |
380 | 372 | ||
381 | if ( newX > -1 ) { | 373 | if ( newX > -1 ) { |
382 | refresh( newX, newY ); | 374 | refresh( newX, newY ); |
383 | newY = newX = -1; | 375 | newY = newX = -1; |
384 | } | 376 | } |
385 | if ( x < 0 || x > 18 || y < 0 || y > 18 ) { | 377 | if ( x < 0 || x > 18 || y < 0 || y > 18 ) { |
386 | newX = newY = -1; | 378 | newX = newY = -1; |
387 | return; | 379 | return; |
388 | } | 380 | } |
389 | if ( board[x][y] == -1 && !Suicide( c, x, y ) ) { | 381 | if ( board[x][y] == -1 && !Suicide( c, x, y ) ) { |
390 | newX = x; | 382 | newX = x; |
391 | newY = y; | 383 | newY = y; |
392 | refresh(x,y); | 384 | refresh(x,y); |
393 | } | 385 | } |
394 | 386 | ||
395 | } | 387 | } |
396 | 388 | ||
397 | void GoWidget::mouseReleaseEvent( QMouseEvent * ) | 389 | void GoWidget::mouseReleaseEvent( QMouseEvent * ) |
398 | { | 390 | { |
399 | if ( gameActive && newX > -1 ) | 391 | if ( gameActive && newX > -1 ) |
400 | doMove( newX, newY ); | 392 | doMove( newX, newY ); |
401 | newX = newY = -1; | 393 | newX = newY = -1; |
402 | } | 394 | } |
403 | 395 | ||
404 | void GoWidget::refresh( int x, int y ) | 396 | void GoWidget::refresh( int x, int y ) |
405 | { | 397 | { |
406 | update( bx+d*x-d/2-1, by+d*y-d/2-1, d+2, d+2 ); | 398 | update( bx+d*x-d/2-1, by+d*y-d/2-1, d+2, d+2 ); |
407 | } | 399 | } |
408 | 400 | ||
409 | void GoWidget::removeStone(short x, short y) | 401 | void GoWidget::removeStone(short x, short y) |
410 | { | 402 | { |
411 | board[x][y]=-1; | 403 | board[x][y]=-1; |
412 | refresh( x, y ); | 404 | refresh( x, y ); |
413 | } | 405 | } |
414 | 406 | ||
415 | void GoWidget::placeStone (enum bVal c, short x, short y ) | 407 | void GoWidget::placeStone (enum bVal c, short x, short y ) |
416 | { | 408 | { |
417 | board[x][y]=c; | 409 | board[x][y]=c; |
418 | refresh( x, y ); | 410 | refresh( x, y ); |
419 | } | 411 | } |
420 | 412 | ||
421 | void GoWidget::reportPrisoners( int blackcnt, int whitecnt ) | 413 | void GoWidget::reportPrisoners( int blackcnt, int whitecnt ) |
422 | { | 414 | { |
423 | QString s = tr( "Prisoners: black %1, white %2" ).arg(blackcnt).arg(whitecnt); | 415 | QString s = tr( "Prisoners: black %1, white %2" ).arg(blackcnt).arg(whitecnt); |
424 | emit showScore( s ); | 416 | emit showScore( s ); |
425 | } | 417 | } |
426 | 418 | ||
427 | void GoWidget::setTwoplayer( bool b ) | 419 | void GoWidget::setTwoplayer( bool b ) |
428 | { | 420 | { |
429 | twoplayer = b; | 421 | twoplayer = b; |
430 | } | 422 | } |
431 | 423 | ||
432 | void GoWidget::setHandicap( int h ) | 424 | void GoWidget::setHandicap( int h ) |
433 | { | 425 | { |
434 | current_handicap = h; | 426 | current_handicap = h; |
435 | } | 427 | } |
436 | 428 | ||
437 | 429 | ||
438 | extern "C" { | 430 | extern "C" { |
439 | 431 | ||
440 | voidremovestone(short x, short y) | 432 | voidremovestone(short x, short y) |
441 | { | 433 | { |
442 | GoWidget::self->removeStone(x,y); | 434 | GoWidget::self->removeStone(x,y); |
443 | } | 435 | } |
444 | 436 | ||
445 | voidplacestone (enum bVal c, short x, short y ) | 437 | voidplacestone (enum bVal c, short x, short y ) |
446 | { | 438 | { |
447 | GoWidget::self->placeStone(c,x,y); | 439 | GoWidget::self->placeStone(c,x,y); |
448 | } | 440 | } |
449 | 441 | ||
450 | voidintrMoveReport(enum bVal c ,char *coord ,char *reason ) | 442 | voidintrMoveReport(enum bVal c ,char *coord ,char *reason ) |
451 | { | 443 | { |
452 | odebug << "intrMoveReport colour " << c << ", " << coord << " " << reason << "" << oendl; | 444 | odebug << "intrMoveReport colour " << c << ", " << coord << " " << reason << "" << oendl; |
453 | } | 445 | } |
454 | 446 | ||
455 | voidintrPrisonerReport( short blackcnt, short whitecnt ) | 447 | voidintrPrisonerReport( short blackcnt, short whitecnt ) |
456 | { | 448 | { |
457 | GoWidget::self->reportPrisoners(blackcnt,whitecnt); | 449 | GoWidget::self->reportPrisoners(blackcnt,whitecnt); |
458 | } | 450 | } |
459 | 451 | ||
460 | } | 452 | } |
461 | 453 | ||