author | cniehaus <cniehaus> | 2002-04-14 12:17:36 (UTC) |
---|---|---|
committer | cniehaus <cniehaus> | 2002-04-14 12:17:36 (UTC) |
commit | 9ac41db7292a4aa58d38b1672d1ee855d53a8dd9 (patch) (unidiff) | |
tree | f34a61b66efc29a7fdf28ef28b21ecf7b627ff07 | |
parent | cd9b12a72d2b91da4a369bf7ddf859207caa5c90 (diff) | |
download | opie-9ac41db7292a4aa58d38b1672d1ee855d53a8dd9.zip opie-9ac41db7292a4aa58d38b1672d1ee855d53a8dd9.tar.gz opie-9ac41db7292a4aa58d38b1672d1ee855d53a8dd9.tar.bz2 |
i18n-stuff. There are still lots of string left!
-rw-r--r-- | noncore/games/tictac/tictac.cpp | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/noncore/games/tictac/tictac.cpp b/noncore/games/tictac/tictac.cpp index e887b00..20cc942 100644 --- a/noncore/games/tictac/tictac.cpp +++ b/noncore/games/tictac/tictac.cpp | |||
@@ -127,251 +127,251 @@ void TicTacGameBoard::newGame() | |||
127 | // This slot is activated when a TicTacButton emits the signal "clicked()", | 127 | // This slot is activated when a TicTacButton emits the signal "clicked()", |
128 | // i.e. the user has clicked on a TicTacButton. | 128 | // i.e. the user has clicked on a TicTacButton. |
129 | // | 129 | // |
130 | 130 | ||
131 | void TicTacGameBoard::buttonClicked() | 131 | void TicTacGameBoard::buttonClicked() |
132 | { | 132 | { |
133 | if ( st != HumansTurn ) // not ready | 133 | if ( st != HumansTurn ) // not ready |
134 | return; | 134 | return; |
135 | int i = buttons->findRef( (TicTacButton*)sender() ); | 135 | int i = buttons->findRef( (TicTacButton*)sender() ); |
136 | TicTacButton *b = buttons->at(i); // get piece that was pressed | 136 | TicTacButton *b = buttons->at(i); // get piece that was pressed |
137 | if ( b->type() == TicTacButton::Blank ) {// empty piece? | 137 | if ( b->type() == TicTacButton::Blank ) {// empty piece? |
138 | btArray->at(i) = TicTacButton::Circle; | 138 | btArray->at(i) = TicTacButton::Circle; |
139 | updateButtons(); | 139 | updateButtons(); |
140 | if ( checkBoard( btArray ) == 0 )// not a winning move? | 140 | if ( checkBoard( btArray ) == 0 )// not a winning move? |
141 | computerMove(); | 141 | computerMove(); |
142 | int s = checkBoard( btArray ); | 142 | int s = checkBoard( btArray ); |
143 | if ( s ) { // any winners yet? | 143 | if ( s ) { // any winners yet? |
144 | st = s == TicTacButton::Circle ? HumanWon : ComputerWon; | 144 | st = s == TicTacButton::Circle ? HumanWon : ComputerWon; |
145 | emit finished(); | 145 | emit finished(); |
146 | } | 146 | } |
147 | } | 147 | } |
148 | } | 148 | } |
149 | 149 | ||
150 | 150 | ||
151 | // -------------------------------------------------------------------------- | 151 | // -------------------------------------------------------------------------- |
152 | // TicTacGameBoard::updateButtons() | 152 | // TicTacGameBoard::updateButtons() |
153 | // | 153 | // |
154 | // Updates all buttons that have changed state | 154 | // Updates all buttons that have changed state |
155 | // | 155 | // |
156 | 156 | ||
157 | void TicTacGameBoard::updateButtons() | 157 | void TicTacGameBoard::updateButtons() |
158 | { | 158 | { |
159 | for ( int i=0; i<nBoard*nBoard; i++ ) { | 159 | for ( int i=0; i<nBoard*nBoard; i++ ) { |
160 | if ( buttons->at(i)->type() != btArray->at(i) ) | 160 | if ( buttons->at(i)->type() != btArray->at(i) ) |
161 | buttons->at(i)->setType( (TicTacButton::Type)btArray->at(i) ); | 161 | buttons->at(i)->setType( (TicTacButton::Type)btArray->at(i) ); |
162 | buttons->at(i)->setEnabled( buttons->at(i)->type() == | 162 | buttons->at(i)->setEnabled( buttons->at(i)->type() == |
163 | TicTacButton::Blank ); | 163 | TicTacButton::Blank ); |
164 | } | 164 | } |
165 | } | 165 | } |
166 | 166 | ||
167 | 167 | ||
168 | // -------------------------------------------------------------------------- | 168 | // -------------------------------------------------------------------------- |
169 | // TicTacGameBoard::checkBoard() | 169 | // TicTacGameBoard::checkBoard() |
170 | // | 170 | // |
171 | // Checks if one of the players won the game, works for any board size. | 171 | // Checks if one of the players won the game, works for any board size. |
172 | // | 172 | // |
173 | // Returns: | 173 | // Returns: |
174 | // - TicTacButton::Cross if the player with X buttons won | 174 | // - TicTacButton::Cross if the player with X buttons won |
175 | // - TicTacButton::Circle if the player with O buttons won | 175 | // - TicTacButton::Circle if the player with O buttons won |
176 | // - Zero (0) if there is no winner yet | 176 | // - Zero (0) if there is no winner yet |
177 | // | 177 | // |
178 | 178 | ||
179 | int TicTacGameBoard::checkBoard( TicTacArray *a ) | 179 | int TicTacGameBoard::checkBoard( TicTacArray *a ) |
180 | { | 180 | { |
181 | int t = 0; | 181 | int t = 0; |
182 | int row, col; | 182 | int row, col; |
183 | bool won = FALSE; | 183 | bool won = FALSE; |
184 | for ( row=0; row<nBoard && !won; row++ ) {// check horizontal | 184 | for ( row=0; row<nBoard && !won; row++ ) {// check horizontal |
185 | t = a->at(row*nBoard); | 185 | t = a->at(row*nBoard); |
186 | if ( t == TicTacButton::Blank ) | 186 | if ( t == TicTacButton::Blank ) |
187 | continue; | 187 | continue; |
188 | col = 1; | 188 | col = 1; |
189 | while ( col<nBoard && a->at(row*nBoard+col) == t ) | 189 | while ( col<nBoard && a->at(row*nBoard+col) == t ) |
190 | col++; | 190 | col++; |
191 | if ( col == nBoard ) | 191 | if ( col == nBoard ) |
192 | won = TRUE; | 192 | won = TRUE; |
193 | } | 193 | } |
194 | for ( col=0; col<nBoard && !won; col++ ) {// check vertical | 194 | for ( col=0; col<nBoard && !won; col++ ) {// check vertical |
195 | t = a->at(col); | 195 | t = a->at(col); |
196 | if ( t == TicTacButton::Blank ) | 196 | if ( t == TicTacButton::Blank ) |
197 | continue; | 197 | continue; |
198 | row = 1; | 198 | row = 1; |
199 | while ( row<nBoard && a->at(row*nBoard+col) == t ) | 199 | while ( row<nBoard && a->at(row*nBoard+col) == t ) |
200 | row++; | 200 | row++; |
201 | if ( row == nBoard ) | 201 | if ( row == nBoard ) |
202 | won = TRUE; | 202 | won = TRUE; |
203 | } | 203 | } |
204 | if ( !won ) { // check diagonal top left | 204 | if ( !won ) { // check diagonal top left |
205 | t = a->at(0); // to bottom right | 205 | t = a->at(0); // to bottom right |
206 | if ( t != TicTacButton::Blank ) { | 206 | if ( t != TicTacButton::Blank ) { |
207 | int i = 1; | 207 | int i = 1; |
208 | while ( i<nBoard && a->at(i*nBoard+i) == t ) | 208 | while ( i<nBoard && a->at(i*nBoard+i) == t ) |
209 | i++; | 209 | i++; |
210 | if ( i == nBoard ) | 210 | if ( i == nBoard ) |
211 | won = TRUE; | 211 | won = TRUE; |
212 | } | 212 | } |
213 | } | 213 | } |
214 | if ( !won ) { // check diagonal bottom left | 214 | if ( !won ) { // check diagonal bottom left |
215 | int j = nBoard-1; // to top right | 215 | int j = nBoard-1; // to top right |
216 | int i = 0; | 216 | int i = 0; |
217 | t = a->at(i+j*nBoard); | 217 | t = a->at(i+j*nBoard); |
218 | if ( t != TicTacButton::Blank ) { | 218 | if ( t != TicTacButton::Blank ) { |
219 | i++; j--; | 219 | i++; j--; |
220 | while ( i<nBoard && a->at(i+j*nBoard) == t ) { | 220 | while ( i<nBoard && a->at(i+j*nBoard) == t ) { |
221 | i++; j--; | 221 | i++; j--; |
222 | } | 222 | } |
223 | if ( i == nBoard ) | 223 | if ( i == nBoard ) |
224 | won = TRUE; | 224 | won = TRUE; |
225 | } | 225 | } |
226 | } | 226 | } |
227 | if ( !won ) // no winner | 227 | if ( !won ) // no winner |
228 | t = 0; | 228 | t = 0; |
229 | return t; | 229 | return t; |
230 | } | 230 | } |
231 | 231 | ||
232 | 232 | ||
233 | // -------------------------------------------------------------------------- | 233 | // -------------------------------------------------------------------------- |
234 | // TicTacGameBoard::computerMove() | 234 | // TicTacGameBoard::computerMove() |
235 | // | 235 | // |
236 | // Puts a piece on the game board. Very, very simple. | 236 | // Puts a piece on the game board. Very, very simple. |
237 | // | 237 | // |
238 | 238 | ||
239 | void TicTacGameBoard::computerMove() | 239 | void TicTacGameBoard::computerMove() |
240 | { | 240 | { |
241 | int numButtons = nBoard*nBoard; | 241 | int numButtons = nBoard*nBoard; |
242 | int *altv = new int[numButtons]; // buttons alternatives | 242 | int *altv = new int[numButtons]; // buttons alternatives |
243 | int altc = 0; | 243 | int altc = 0; |
244 | int stopHuman = -1; | 244 | int stopHuman = -1; |
245 | TicTacArray a = btArray->copy(); | 245 | TicTacArray a = btArray->copy(); |
246 | int i; | 246 | int i; |
247 | for ( i=0; i<numButtons; i++ ) { // try all positions | 247 | for ( i=0; i<numButtons; i++ ) { // try all positions |
248 | if ( a[i] != TicTacButton::Blank )// already a piece there | 248 | if ( a[i] != TicTacButton::Blank )// already a piece there |
249 | continue; | 249 | continue; |
250 | a[i] = TicTacButton::Cross; // test if computer wins | 250 | a[i] = TicTacButton::Cross; // test if computer wins |
251 | if ( checkBoard(&a) == a[i] ) { // computer will win | 251 | if ( checkBoard(&a) == a[i] ) { // computer will win |
252 | st = ComputerWon; | 252 | st = ComputerWon; |
253 | stopHuman = -1; | 253 | stopHuman = -1; |
254 | break; | 254 | break; |
255 | } | 255 | } |
256 | a[i] = TicTacButton::Circle; // test if human wins | 256 | a[i] = TicTacButton::Circle; // test if human wins |
257 | if ( checkBoard(&a) == a[i] ) { // oops... | 257 | if ( checkBoard(&a) == a[i] ) { // oops... |
258 | stopHuman = i; // remember position | 258 | stopHuman = i; // remember position |
259 | a[i] = TicTacButton::Blank; // restore button | 259 | a[i] = TicTacButton::Blank; // restore button |
260 | continue; // computer still might win | 260 | continue; // computer still might win |
261 | } | 261 | } |
262 | a[i] = TicTacButton::Blank; // restore button | 262 | a[i] = TicTacButton::Blank; // restore button |
263 | altv[altc++] = i; // remember alternative | 263 | altv[altc++] = i; // remember alternative |
264 | } | 264 | } |
265 | if ( stopHuman >= 0 ) // must stop human from winning | 265 | if ( stopHuman >= 0 ) // must stop human from winning |
266 | a[stopHuman] = TicTacButton::Cross; | 266 | a[stopHuman] = TicTacButton::Cross; |
267 | else if ( i == numButtons ) { // tried all alternatives | 267 | else if ( i == numButtons ) { // tried all alternatives |
268 | if ( altc > 0 ) // set random piece | 268 | if ( altc > 0 ) // set random piece |
269 | a[altv[rand()%(altc--)]] = TicTacButton::Cross; | 269 | a[altv[rand()%(altc--)]] = TicTacButton::Cross; |
270 | if ( altc == 0 ) { // no more blanks | 270 | if ( altc == 0 ) { // no more blanks |
271 | st = NobodyWon; | 271 | st = NobodyWon; |
272 | emit finished(); | 272 | emit finished(); |
273 | } | 273 | } |
274 | } | 274 | } |
275 | *btArray = a; // update model | 275 | *btArray = a; // update model |
276 | updateButtons(); // update buttons | 276 | updateButtons(); // update buttons |
277 | delete[] altv; | 277 | delete[] altv; |
278 | } | 278 | } |
279 | 279 | ||
280 | 280 | ||
281 | //*************************************************************************** | 281 | //*************************************************************************** |
282 | //* TicTacToe member functions | 282 | //* TicTacToe member functions |
283 | //*************************************************************************** | 283 | //*************************************************************************** |
284 | 284 | ||
285 | // -------------------------------------------------------------------------- | 285 | // -------------------------------------------------------------------------- |
286 | // Creates a game widget with a game board and two push buttons, and connects | 286 | // Creates a game widget with a game board and two push buttons, and connects |
287 | // signals of child widgets to slots. | 287 | // signals of child widgets to slots. |
288 | // | 288 | // |
289 | 289 | ||
290 | TicTacToe::TicTacToe( int boardSize, QWidget *parent, const char *name ) | 290 | TicTacToe::TicTacToe( int boardSize, QWidget *parent, const char *name ) |
291 | : QWidget( parent, name ) | 291 | : QWidget( parent, name ) |
292 | { | 292 | { |
293 | QVBoxLayout * l = new QVBoxLayout( this, 6 ); | 293 | QVBoxLayout * l = new QVBoxLayout( this, 6 ); |
294 | 294 | ||
295 | // Create a message label | 295 | // Create a message label |
296 | 296 | ||
297 | message = new QLabel( this ); | 297 | message = new QLabel( this ); |
298 | message->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); | 298 | message->setFrameStyle( QFrame::WinPanel | QFrame::Sunken ); |
299 | message->setAlignment( AlignCenter ); | 299 | message->setAlignment( AlignCenter ); |
300 | l->addWidget( message ); | 300 | l->addWidget( message ); |
301 | 301 | ||
302 | // Create the game board and connect the signal finished() to this | 302 | // Create the game board and connect the signal finished() to this |
303 | // gameOver() slot | 303 | // gameOver() slot |
304 | 304 | ||
305 | board = new TicTacGameBoard( boardSize, this ); | 305 | board = new TicTacGameBoard( boardSize, this ); |
306 | connect( board, SIGNAL(finished()), SLOT(gameOver()) ); | 306 | connect( board, SIGNAL(finished()), SLOT(gameOver()) ); |
307 | l->addWidget( board ); | 307 | l->addWidget( board ); |
308 | 308 | ||
309 | // Create a horizontal frame line | 309 | // Create a horizontal frame line |
310 | 310 | ||
311 | QFrame *line = new QFrame( this ); | 311 | QFrame *line = new QFrame( this ); |
312 | line->setFrameStyle( QFrame::HLine | QFrame::Sunken ); | 312 | line->setFrameStyle( QFrame::HLine | QFrame::Sunken ); |
313 | l->addWidget( line ); | 313 | l->addWidget( line ); |
314 | 314 | ||
315 | // Create the combo box for deciding who should start, and | 315 | // Create the combo box for deciding who should start, and |
316 | // connect its clicked() signals to the buttonClicked() slot | 316 | // connect its clicked() signals to the buttonClicked() slot |
317 | 317 | ||
318 | whoStarts = new QComboBox( this ); | 318 | whoStarts = new QComboBox( this ); |
319 | whoStarts->insertItem( "Computer starts" ); | 319 | whoStarts->insertItem( tr( "Computer starts" ) ); |
320 | whoStarts->insertItem( "Human starts" ); | 320 | whoStarts->insertItem( tr( "Human starts" ) ); |
321 | l->addWidget( whoStarts ); | 321 | l->addWidget( whoStarts ); |
322 | 322 | ||
323 | // Create the push buttons and connect their clicked() signals | 323 | // Create the push buttons and connect their clicked() signals |
324 | // to this right slots. | 324 | // to this right slots. |
325 | 325 | ||
326 | newGame = new QPushButton( "Play!", this ); | 326 | newGame = new QPushButton( tr( "Play!" ), this ); |
327 | connect( newGame, SIGNAL(clicked()), SLOT(newGameClicked()) ); | 327 | connect( newGame, SIGNAL(clicked()), SLOT(newGameClicked()) ); |
328 | quit = new QPushButton( "Quit", this ); | 328 | quit = new QPushButton( tr( "Quit" ), this ); |
329 | connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); | 329 | connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); |
330 | QHBoxLayout * b = new QHBoxLayout; | 330 | QHBoxLayout * b = new QHBoxLayout; |
331 | l->addLayout( b ); | 331 | l->addLayout( b ); |
332 | b->addWidget( newGame ); | 332 | b->addWidget( newGame ); |
333 | b->addWidget( quit ); | 333 | b->addWidget( quit ); |
334 | showMaximized(); | 334 | showMaximized(); |
335 | newState(); | 335 | newState(); |
336 | } | 336 | } |
337 | 337 | ||
338 | 338 | ||
339 | // -------------------------------------------------------------------------- | 339 | // -------------------------------------------------------------------------- |
340 | // TicTacToe::newGameClicked() - SLOT | 340 | // TicTacToe::newGameClicked() - SLOT |
341 | // | 341 | // |
342 | // This slot is activated when the new game button is clicked. | 342 | // This slot is activated when the new game button is clicked. |
343 | // | 343 | // |
344 | 344 | ||
345 | void TicTacToe::newGameClicked() | 345 | void TicTacToe::newGameClicked() |
346 | { | 346 | { |
347 | board->computerStarts( whoStarts->currentItem() == 0 ); | 347 | board->computerStarts( whoStarts->currentItem() == 0 ); |
348 | board->newGame(); | 348 | board->newGame(); |
349 | newState(); | 349 | newState(); |
350 | } | 350 | } |
351 | 351 | ||
352 | 352 | ||
353 | // -------------------------------------------------------------------------- | 353 | // -------------------------------------------------------------------------- |
354 | // TicTacToe::gameOver() - SLOT | 354 | // TicTacToe::gameOver() - SLOT |
355 | // | 355 | // |
356 | // This slot is activated when the TicTacGameBoard emits the signal | 356 | // This slot is activated when the TicTacGameBoard emits the signal |
357 | // "finished()", i.e. when a player has won or when it is a draw. | 357 | // "finished()", i.e. when a player has won or when it is a draw. |
358 | // | 358 | // |
359 | 359 | ||
360 | void TicTacToe::gameOver() | 360 | void TicTacToe::gameOver() |
361 | { | 361 | { |
362 | newState(); // update text box | 362 | newState(); // update text box |
363 | } | 363 | } |
364 | 364 | ||
365 | 365 | ||
366 | // -------------------------------------------------------------------------- | 366 | // -------------------------------------------------------------------------- |
367 | // Updates the message to reflect a new state. | 367 | // Updates the message to reflect a new state. |
368 | // | 368 | // |
369 | 369 | ||
370 | void TicTacToe::newState() | 370 | void TicTacToe::newState() |
371 | { | 371 | { |
372 | static const char *msg[] = { // TicTacGameBoard::State texts | 372 | static const char *msg[] = { // TicTacGameBoard::State texts |
373 | "Click Play to start", "Make your move", | 373 | "Click Play to start", "Make your move", |
374 | "You won!", "Computer won!", "It's a draw" }; | 374 | "You won!", "Computer won!", "It's a draw" }; |
375 | message->setText( msg[board->state()] ); | 375 | message->setText( msg[board->state()] ); |
376 | return; | 376 | return; |
377 | } | 377 | } |