summaryrefslogtreecommitdiff
path: root/noncore/games/minesweep/minefield.cpp
authorerik <erik>2007-01-22 23:01:41 (UTC)
committer erik <erik>2007-01-22 23:01:41 (UTC)
commitadcf6075db477909dd8170a74862a6ef91a5127f (patch) (unidiff)
treeda0a1e35c5d392271bce29e84d3af5c30a864f56 /noncore/games/minesweep/minefield.cpp
parent9b4871054d01a47b4c546952a0948553413840d6 (diff)
downloadopie-adcf6075db477909dd8170a74862a6ef91a5127f.zip
opie-adcf6075db477909dd8170a74862a6ef91a5127f.tar.gz
opie-adcf6075db477909dd8170a74862a6ef91a5127f.tar.bz2
Each file in this commit had a problem where a function might return
a null value for a pointer and that null value was not checked.
Diffstat (limited to 'noncore/games/minesweep/minefield.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/minesweep/minefield.cpp4
1 files changed, 3 insertions, 1 deletions
diff --git a/noncore/games/minesweep/minefield.cpp b/noncore/games/minesweep/minefield.cpp
index 72c05b0..1987ea5 100644
--- a/noncore/games/minesweep/minefield.cpp
+++ b/noncore/games/minesweep/minefield.cpp
@@ -515,233 +515,235 @@ void MineField::held()
515 updateMine( currRow, currCol ); 515 updateMine( currRow, currCol );
516 ignoreClick = TRUE; 516 ignoreClick = TRUE;
517} 517}
518 518
519 519
520 520
521 521
522void MineField::keyPressEvent( QKeyEvent* e ) 522void MineField::keyPressEvent( QKeyEvent* e )
523{ 523{
524#if defined(Q_WS_QWS) || defined(_WS_QWS_) 524#if defined(Q_WS_QWS) || defined(_WS_QWS_)
525 flagAction = ( e->key() == Key_Up ) ? FlagOn : NoAction; 525 flagAction = ( e->key() == Key_Up ) ? FlagOn : NoAction;
526#else 526#else
527 flagAction = ( ( e->state() & ShiftButton ) == ShiftButton ) ? FlagOn : NoAction; 527 flagAction = ( ( e->state() & ShiftButton ) == ShiftButton ) ? FlagOn : NoAction;
528#endif 528#endif
529} 529}
530 530
531void MineField::keyReleaseEvent( QKeyEvent* ) 531void MineField::keyReleaseEvent( QKeyEvent* )
532{ 532{
533 flagAction = NoAction; 533 flagAction = NoAction;
534} 534}
535 535
536int MineField::getHint( int row, int col ) 536int MineField::getHint( int row, int col )
537{ 537{
538 int hint = 0; 538 int hint = 0;
539 for ( int c = col-1; c <= col+1; c++ ) 539 for ( int c = col-1; c <= col+1; c++ )
540 for ( int r = row-1; r <= row+1; r++ ) { 540 for ( int r = row-1; r <= row+1; r++ ) {
541 Mine* m = mine( r, c ); 541 Mine* m = mine( r, c );
542 if ( m && m->isMined() ) 542 if ( m && m->isMined() )
543 hint++; 543 hint++;
544 } 544 }
545 545
546 return hint; 546 return hint;
547} 547}
548 548
549void MineField::setHint( int row, int col ) 549void MineField::setHint( int row, int col )
550{ 550{
551 Mine *m = mine( row, col ); 551 Mine *m = mine( row, col );
552 if ( !m ) 552 if ( !m )
553 return; 553 return;
554 554
555 int hint = getHint( row, col ); 555 int hint = getHint( row, col );
556 556
557 if ( !hint ) { 557 if ( !hint ) {
558 for ( int c = col-1; c <= col+1; c++ ) 558 for ( int c = col-1; c <= col+1; c++ )
559 for ( int r = row-1; r <= row+1; r++ ) { 559 for ( int r = row-1; r <= row+1; r++ ) {
560 Mine* m = mine( r, c ); 560 Mine* m = mine( r, c );
561 if ( m && m->state() == Mine::Hidden ) { 561 if ( m && m->state() == Mine::Hidden ) {
562 m->activate( TRUE ); 562 m->activate( TRUE );
563 nonminecount--; 563 nonminecount--;
564 setHint( r, c ); 564 setHint( r, c );
565 updateCell( r, c ); 565 updateCell( r, c );
566 } 566 }
567 } 567 }
568 } 568 }
569 569
570 m->setHint( hint ); 570 m->setHint( hint );
571 updateCell( row, col ); 571 updateCell( row, col );
572} 572}
573 573
574/* 574/*
575 Only place mines after first click, since it is pointless to 575 Only place mines after first click, since it is pointless to
576 kill the player before the game has started. 576 kill the player before the game has started.
577*/ 577*/
578 578
579void MineField::cellClicked( int row, int col ) 579void MineField::cellClicked( int row, int col )
580{ 580{
581 if ( state() == GameOver ) 581 if ( state() == GameOver )
582 return; 582 return;
583 if ( state() == Waiting ) { 583 if ( state() == Waiting ) {
584 Mine* m = mine( row, col ); 584 Mine* m = mine( row, col );
585 if ( !m ) 585 if ( !m )
586 return; 586 return;
587 m->setState( Mine::Empty ); 587 m->setState( Mine::Empty );
588 nonminecount--; 588 nonminecount--;
589 placeMines(); 589 placeMines();
590 setState( Playing ); 590 setState( Playing );
591 emit gameStarted(); 591 emit gameStarted();
592 updateMine( row, col ); 592 updateMine( row, col );
593 } else { // state() == Playing 593 } else { // state() == Playing
594 holdTimer->stop(); 594 holdTimer->stop();
595 if ( ignoreClick ) 595 if ( ignoreClick )
596 ignoreClick = FALSE; 596 ignoreClick = FALSE;
597 else 597 else
598 updateMine( row, col ); 598 updateMine( row, col );
599 } 599 }
600} 600}
601 601
602void MineField::updateMine( int row, int col ) 602void MineField::updateMine( int row, int col )
603{ 603{
604 Mine* m = mine( row, col ); 604 Mine* m = mine( row, col );
605 if ( !m ) 605 if ( !m )
606 return; 606 return;
607 607
608 bool wasFlagged = m->state() == Mine::Flagged; 608 bool wasFlagged = m->state() == Mine::Flagged;
609 bool wasEmpty = m->state() == Mine::Empty; 609 bool wasEmpty = m->state() == Mine::Empty;
610 610
611 m->activate( flagAction == NoAction ); 611 m->activate( flagAction == NoAction );
612 612
613 if ( m->state() == Mine::Exploded ) { 613 if ( m->state() == Mine::Exploded ) {
614 emit gameOver( FALSE ); 614 emit gameOver( FALSE );
615 setState( GameOver ); 615 setState( GameOver );
616 return; 616 return;
617 } else if ( m->state() == Mine::Empty ) { 617 } else if ( m->state() == Mine::Empty ) {
618 setHint( row, col ); 618 setHint( row, col );
619 if ( !wasEmpty ) 619 if ( !wasEmpty )
620 nonminecount--; 620 nonminecount--;
621 } 621 }
622 622
623 if ( flagAction != NoAction ) { 623 if ( flagAction != NoAction ) {
624 if ( m->state() == Mine::Flagged ) { 624 if ( m->state() == Mine::Flagged ) {
625 if (mineguess > 0) { 625 if (mineguess > 0) {
626 --mineguess; 626 --mineguess;
627 emit mineCount( mineguess ); 627 emit mineCount( mineguess );
628 if ( m->isMined() ) 628 if ( m->isMined() )
629 --minecount; 629 --minecount;
630 } else { 630 } else {
631 m->setState(Mine::Hidden); 631 m->setState(Mine::Hidden);
632 } 632 }
633 } else if ( wasFlagged ) { 633 } else if ( wasFlagged ) {
634 ++mineguess; 634 ++mineguess;
635 emit mineCount( mineguess ); 635 emit mineCount( mineguess );
636 if ( m->isMined() ) 636 if ( m->isMined() )
637 ++minecount; 637 ++minecount;
638 } 638 }
639 } 639 }
640 640
641 updateCell( row, col ); 641 updateCell( row, col );
642 642
643 if ( !minecount && !mineguess || !nonminecount ) { 643 if ( !minecount && !mineguess || !nonminecount ) {
644 emit gameOver( TRUE ); 644 emit gameOver( TRUE );
645 setState( GameOver ); 645 setState( GameOver );
646 } 646 }
647} 647}
648 648
649void MineField::showMines() 649void MineField::showMines()
650{ 650{
651 for ( int c = 0; c < numCols; c++ ) 651 for ( int c = 0; c < numCols; c++ )
652 for ( int r = 0; r < numRows; r++ ) { 652 for ( int r = 0; r < numRows; r++ ) {
653 Mine* m = mine( r, c ); 653 Mine* m = mine( r, c );
654 if ( !m ) 654 if ( !m )
655 continue; 655 continue;
656 if ( m->isMined() && m->state() == Mine::Hidden ) 656 if ( m->isMined() && m->state() == Mine::Hidden )
657 m->setState( Mine::Mined ); 657 m->setState( Mine::Mined );
658 if ( !m->isMined() && m->state() == Mine::Flagged ) 658 if ( !m->isMined() && m->state() == Mine::Flagged )
659 m->setState( Mine::Wrong ); 659 m->setState( Mine::Wrong );
660 660
661 updateCell( r, c ); 661 updateCell( r, c );
662 } 662 }
663} 663}
664 664
665void MineField::paletteChange( const QPalette &o ) 665void MineField::paletteChange( const QPalette &o )
666{ 666{
667 Mine::paletteChange(); 667 Mine::paletteChange();
668 QScrollView::paletteChange( o ); 668 QScrollView::paletteChange( o );
669} 669}
670 670
671void MineField::writeConfig(Config& cfg) const 671void MineField::writeConfig(Config& cfg) const
672{ 672{
673 cfg.setGroup("Field"); 673 cfg.setGroup("Field");
674 cfg.writeEntry("Level",lev); 674 cfg.writeEntry("Level",lev);
675 QString grid=""; 675 QString grid="";
676 if ( stat == Playing ) { 676 if ( stat == Playing ) {
677 for ( int x = 0; x < numCols; x++ ) 677 for ( int x = 0; x < numCols; x++ )
678 for ( int y = 0; y < numRows; y++ ) { 678 for ( int y = 0; y < numRows; y++ ) {
679 char code='A'+(x*17+y*101)%21; // Reduce the urge to cheat 679 char code='A'+(x*17+y*101)%21; // Reduce the urge to cheat
680 const Mine* m = mine( y, x ); 680 const Mine* m = mine( y, x );
681 int st = (int)m->state(); if ( m->isMined() ) st+=5; 681 int st = (int)m->state(); if ( m->isMined() ) st+=5;
682 grid += code + st; 682 grid += code + st;
683 } 683 }
684 } 684 }
685 cfg.writeEntry("Grid",grid); 685 cfg.writeEntry("Grid",grid);
686} 686}
687 687
688void MineField::readConfig(Config& cfg) 688void MineField::readConfig(Config& cfg)
689{ 689{
690 cfg.setGroup("Field"); 690 cfg.setGroup("Field");
691 lev = cfg.readNumEntry("Level",1); 691 lev = cfg.readNumEntry("Level",1);
692 setup(lev); 692 setup(lev);
693 flagAction = NoAction; 693 flagAction = NoAction;
694 ignoreClick = FALSE; 694 ignoreClick = FALSE;
695 currRow = currCol = 0; 695 currRow = currCol = 0;
696 QString grid = cfg.readEntry("Grid"); 696 QString grid = cfg.readEntry("Grid");
697 int x; 697 int x;
698 if ( !grid.isEmpty() ) { 698 if ( !grid.isEmpty() ) {
699 int i=0; 699 int i=0;
700 minecount=0; 700 minecount=0;
701 mineguess=0; 701 mineguess=0;
702 for ( x = 0; x < numCols; x++ ) { 702 for ( x = 0; x < numCols; x++ ) {
703 for ( int y = 0; y < numRows; y++ ) { 703 for ( int y = 0; y < numRows; y++ ) {
704 char code='A'+(x*17+y*101)%21; // Reduce the urge to cheat 704 char code='A'+(x*17+y*101)%21; // Reduce the urge to cheat
705 int st = (char)(QChar)grid[i++]-code; 705 int st = (char)(QChar)grid[i++]-code;
706 Mine* m = mine( y, x ); 706 Mine* m = mine( y, x );
707 if (!m)
708 continue;
707 if ( st >= 5 ) { 709 if ( st >= 5 ) {
708 st-=5; 710 st-=5;
709 m->setMined(TRUE); 711 m->setMined(TRUE);
710 minecount++; 712 minecount++;
711 mineguess++; 713 mineguess++;
712 } 714 }
713 m->setState((Mine::MineState)st); 715 m->setState((Mine::MineState)st);
714 switch ( m->state() ) { 716 switch ( m->state() ) {
715 case Mine::Flagged: 717 case Mine::Flagged:
716 if (m->isMined()) 718 if (m->isMined())
717 minecount--; 719 minecount--;
718 mineguess--; 720 mineguess--;
719 break; 721 break;
720 case Mine::Empty: 722 case Mine::Empty:
721 --nonminecount; 723 --nonminecount;
722 break; 724 break;
723 default: 725 default:
724 break; 726 break;
725 } 727 }
726 } 728 }
727 } 729 }
728 for ( x = 0; x < numCols; x++ ) { 730 for ( x = 0; x < numCols; x++ ) {
729 for ( int y = 0; y < numRows; y++ ) { 731 for ( int y = 0; y < numRows; y++ ) {
730 Mine* m = mine( y, x ); 732 Mine* m = mine( y, x );
731 if ( m->state() == Mine::Empty ) 733 if ( m && m->state() == Mine::Empty )
732 m->setHint(getHint(y,x)); 734 m->setHint(getHint(y,x));
733 } 735 }
734 } 736 }
735 } 737 }
736 setState( Playing ); 738 setState( Playing );
737 emit mineCount( mineguess ); 739 emit mineCount( mineguess );
738} 740}
739 741
740QSize MineField::sizeHint() const 742QSize MineField::sizeHint() const
741{ 743{
742 if ( qApp->desktop()->width() >= 240 ) 744 if ( qApp->desktop()->width() >= 240 )
743 return QSize(200,200); 745 return QSize(200,200);
744 else 746 else
745 return QSize(160, 160); 747 return QSize(160, 160);
746} 748}
747 749