[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    branches/work/kreversi_rewrite
From:       Dmitry Suzdalev <dimsuz () gmail ! com>
Date:       2006-09-04 11:43:04
Message-ID: 1157370184.399056.13119.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 580747 by dimsuz:

Add the ability to show legal moves


 M  +5 -4      kreversichip.cpp  
 M  +13 -0     kreversigame.cpp  
 M  +4 -0      kreversigame.h  
 M  +55 -8     kreversiscene.cpp  
 M  +10 -0     kreversiscene.h  
 M  +1 -0      kreversiui.rc  
 M  +3 -0      mainwindow.cpp  


--- branches/work/kreversi_rewrite/kreversichip.cpp #580746:580747
@@ -29,10 +29,11 @@
 {
     if(show)
     {
-        QPixmap pix = pixmap();
+        QPixmap origPix = pixmap();
+        QPixmap pix = origPix;
         QPainter p(&pix);
-        p.setBrush(Qt::black);
-        p.drawEllipse( 3*pix.width()/8, 3*pix.height()/8, pix.width()/4, \
pix.height()/4); +        p.fillRect( 1, 1, pix.width()-2, pix.height()-2, Qt::gray \
); +        p.drawPixmap(0,0, origPix);
         p.end();
         setPixmap(pix);
     }
@@ -45,7 +46,7 @@
 KReversiChipFrameSet::KReversiChipFrameSet( const QPixmap& allFrames, int frameSize \
)  {
     // we skip x,y = (0,0) case, because allFrames has a transparent frame as the 
-    // the first one. Just for symmetry I guess (so the pix remains square)
+    // the first one. It has it just for symmetry I guess (so the whole big pix \
remains square)  for(int y=0; y < allFrames.height(); y += frameSize )
         for(int x=0; x < allFrames.width(); x += frameSize )
         {
--- branches/work/kreversi_rewrite/kreversigame.cpp #580746:580747
@@ -451,6 +451,19 @@
     return m_changedChips.first();
 }
 
+MoveList KReversiGame::possibleMoves() const
+{
+    MoveList l;
+    for(int row=0; row < 8; ++row)
+        for(int col=0; col<8; ++col)
+        {
+            KReversiMove move(m_curPlayer, row, col);
+            if( isMovePossible( move ) )
+                l.append(move);
+        }
+    return l;
+}
+
 int KReversiGame::playerScore( ChipColor player ) const
 {
     return m_board->playerScore( player );
--- branches/work/kreversi_rewrite/kreversigame.h #580746:580747
@@ -107,6 +107,10 @@
      *  were turned by that move
      */
     MoveList changedChips() const { return m_changedChips; }
+    /**
+     *  @return a list of possible moves for current player
+     */
+    MoveList possibleMoves() const;
 signals:
     void boardChanged();
     void moveFinished();
--- branches/work/kreversi_rewrite/kreversiscene.cpp #580746:580747
@@ -15,7 +15,7 @@
 
 KReversiScene::KReversiScene( KReversiGame* game , const QPixmap& chipsPixmap )
     : m_hintChip(0), m_lastMoveChip(0), m_showingHint(false), m_demoMode(false), 
-    m_showLastMove(false)
+    m_showLastMove(false), m_showPossibleMoves(false)
 {
     setBackgroundBrush( Qt::lightGray );
 
@@ -49,9 +49,28 @@
 void KReversiScene::setShowLastMove( bool show )
 {
     m_showLastMove = show;
-    displayLastAndPossibleMoves();
+    if(show)
+        displayLastAndPossibleMoves();
+    else
+    {
+        if(m_lastMoveChip)
+            m_lastMoveChip->showLastMoveMarker(false);
+    }
 }
 
+void KReversiScene::setShowLegalMoves( bool show )
+{
+    m_showPossibleMoves = show;
+    if(show)
+        displayLastAndPossibleMoves();
+    else
+    {
+        // NOTE: or delete?
+        foreach( QGraphicsRectItem* rect, m_possibleMovesItems )
+            rect->hide();
+    }
+}
+
 bool KReversiScene::isBusy() const
 {
     return m_animTimer->isActive();
@@ -208,14 +227,35 @@
         if(m_lastMoveChip)
             m_lastMoveChip->showLastMoveMarker(true);
     }
-    else
+
+    // ==== Show Possible Moves ====
+    if( m_showPossibleMoves && !m_game->isComputersTurn() )
     {
-        if(m_lastMoveChip)
-            m_lastMoveChip->showLastMoveMarker(false);
+        MoveList l = m_game->possibleMoves();
+        kDebug() << "number of rects: " << m_possibleMovesItems.count() << endl;
+        // if m_possibleMovesItems contains less rects then there are items in l
+        // lets fill it with additional rects.
+        // Else we'll just reuse rects that we already have.
+        // NOTE: maybe make m_possibleMovesItems a QVector and simply do resize()?
+        if( m_possibleMovesItems.count() < l.count() )
+        {
+            int numtoadd = l.count() - m_possibleMovesItems.count();
+            kDebug() << "growing num possible moves by: " << numtoadd << endl;
+            for( int i=0; i<numtoadd; ++i)
+            {
+                QGraphicsRectItem *item = new QGraphicsRectItem( 0, 0, CHIP_SIZE-1, \
CHIP_SIZE-1, 0, this ); +                item->setBrush( Qt::darkGreen );
+                m_possibleMovesItems.append( item );
+            }
+        }
+
+        // now let's setup rects to appropriate positions
+        for(int i=0; i<l.size(); ++i )
+        {
+            m_possibleMovesItems[i]->setPos( cellTopLeft( l.at(i).row, l.at(i).col ) \
); +            m_possibleMovesItems[i]->show();
+        }
     }
-
-    // ==== Show Possible Moves ====
-    // TODO
 }
 
 void KReversiScene::slotHint()
@@ -309,6 +349,13 @@
     
     //kDebug() << "Cell (" << row << "," << col << ") clicked." << endl;
 
+    // hide shown legal moves
+    if( m_showPossibleMoves )
+    {
+        foreach( QGraphicsRectItem* rect, m_possibleMovesItems )
+            rect->hide();
+    }
+
     m_game->makePlayerMove( row, col, false );
 }
 
--- branches/work/kreversi_rewrite/kreversiscene.h #580746:580747
@@ -54,6 +54,10 @@
      */
     void setShowLastMove( bool show );
     /**
+     *  This will make scene visually mark squares with possible moves
+     */
+    void setShowLegalMoves( bool show );
+    /**
      *  Shows hint for player
      */
     void slotHint();
@@ -144,5 +148,11 @@
      *  If true, then last made turn will be shown to the player
      */
     bool m_showLastMove;
+    /**
+     *  If true, then all possible moves will be shown to the player
+     */
+    bool m_showPossibleMoves;
+    // FIXME dimsuz: document
+    QList<QGraphicsRectItem*> m_possibleMovesItems;
 };
 #endif
--- branches/work/kreversi_rewrite/kreversiui.rc #580746:580747
@@ -23,5 +23,6 @@
   <Action name="demo" />
   <Separator />
   <Action name="show_last_move" />
+  <Action name="show_legal_moves" />
 </ToolBar>
 </kpartgui>
--- branches/work/kreversi_rewrite/mainwindow.cpp #580746:580747
@@ -68,6 +68,9 @@
     KToggleAction *showLast = new KToggleAction(KIcon("lastmoves"), i18n("Show last \
                move"), actionCollection(), "show_last_move");
     connect( showLast, SIGNAL(triggered(bool)), m_scene, SLOT(setShowLastMove(bool)) \
);  
+    KToggleAction *showLegal = new KToggleAction(KIcon("legalmoves"), i18n("Show \
legal moves"), actionCollection(), "show_legal_moves" ); +    connect( showLegal, \
SIGNAL(triggered(bool)), m_scene, SLOT(setShowLegalMoves(bool)) ); +
     addAction(newGameAct);
     addAction(quitAct);
     addAction(m_undoAct);


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic