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

List:       kde-commits
Subject:    kdereview/knights/src
From:       Miha Čančula <miha.cancula () gmail ! com>
Date:       2010-10-12 20:56:19
Message-ID: 20101012205619.697F7AC895 () svn ! kde ! org
[Download RAW message or body]

SVN commit 1185294 by mihac:

FICS protocol recognises opponent's castling

 M  +20 -0     core/move.cpp  
 M  +7 -0      core/move.h  
 M  +60 -19    proto/ficsprotocol.cpp  
 M  +2 -0      proto/ficsprotocol.h  


--- trunk/kdereview/knights/src/core/move.cpp #1185293:1185294
@@ -55,6 +55,26 @@
 
 }
 
+Move Move::castling(Move::CastlingSide side, Color color)
+{
+    Move m;
+    const int rank = (color == White) ? 1 : 8;
+    const int rookFile = (side == QueenSide) ? 1 : 8;
+    const int kingDestinationFile = (side == QueenSide) ? 3 : 7;
+    const int rookDestinationFile = (side == QueenSide) ? 4 : 6;
+
+    m.setFrom( 5, rank );
+    m.setTo( kingDestinationFile, rank );
+
+    m.setFlag( Castle, true );
+    Move rookMove;
+    rookMove.setFrom( rookFile, rank );
+    rookMove.setTo( rookDestinationFile, rank );
+    m.setAdditionalMoves(QList<Move>() << rookMove);
+
+    return m;
+}
+
 Move::Move()
         : d ( new MovePrivate )
 {
--- trunk/kdereview/knights/src/core/move.h #1185293:1185294
@@ -50,8 +50,15 @@
         Check = 0x10,
         CheckMate = 0x20,
     };
+    enum CastlingSide
+    {
+        QueenSide,
+        KingSide
+    };
     Q_DECLARE_FLAGS ( Flags, MoveFlag )
 
+    static Move castling(CastlingSide side, Color color);
+
     Move ( Pos from, Pos to, Flags flags = None );
     Move ( QString string );
     Move();
--- trunk/kdereview/knights/src/proto/ficsprotocol.cpp #1185293:1185294
@@ -51,6 +51,11 @@
 const QString FicsProtocol::pieces = QLatin1String( "PRNBQKprnbqk" );
 const QString FicsProtocol::coordinate = QLatin1String( "[abdcdefgh][12345678]" );
 const QString FicsProtocol::remainingTime = QLatin1String( "\\d+ \\d+ \\d+ \\d+ \\d+ \
(\\d+) (\\d+) \\d+" ); +const QString FicsProtocol::movePattern = \
QString(QLatin1String("(none|o-o|o-o-o|[%2]\\/%3\\-%4(=[%5])?)")) +        .arg ( \
pieces ) +        .arg ( coordinate )
+        .arg ( coordinate )
+        .arg ( pieces );
 
 const QRegExp FicsProtocol::seekRegExp ( QString ( QLatin1String( "%1 %2 seeking %3 \
                %4 %5\\(\"play %6\" to respond\\)" ) )
         .arg ( namePattern )
@@ -70,13 +75,18 @@
         .arg ( argsPattern )
                                          );
 
-const QRegExp FicsProtocol::moveRegExp ( QString ( QLatin1String( "<12>.*%1 \
[%2]\\/(%3)\\-(%4)(=[%5])?" )) +const QRegExp FicsProtocol::moveRegExp ( QString ( \
                QLatin1String( "<12>.*%1 %2" ))
         .arg ( remainingTime )
+        .arg ( movePattern )
+                               );
+
+const QRegExp FicsProtocol::moveStringExp ( QString ( \
                QLatin1String("[%1]\\/(%2)\\-(%3)(=[%4])?"))
         .arg ( pieces )
         .arg ( coordinate )
         .arg ( coordinate )
         .arg ( pieces )
                                        );
+
 const QRegExp FicsProtocol::challengeRegExp ( QString ( QLatin1String( "Challenge: \
                %1 %2 %3 %4 %5 %6" ))
         .arg ( namePattern )
         .arg ( ratingPattern )
@@ -94,9 +104,6 @@
         .arg ( timePattern )
                                            );
 
-const QRegExp FicsProtocol::gameInfoExp ( QString ( QLatin1String ( "<12>.*%1 none \
                \\(0:00\\) none" ))
-        .arg ( remainingTime ));
-
 FicsProtocol::FicsProtocol ( QObject* parent ) : Protocol ( parent )
 {
     kDebug() << Timeout << endl;
@@ -258,9 +265,22 @@
             }
             // TODO: Check for incorrect logins
             else if ( line.contains ( "Starting FICS session" ) ) {
+                kDebug() << line;
                 m_stage = SeekStage;
                 setupOptions();
                 openGameDialog();
+                QString name = QLatin1String(line);
+                name.remove(0, name.indexOf(QLatin1String("session as ")) + 11);
+                if (name.contains(QLatin1String("(U)")))
+                {
+                    name.truncate(name.indexOf(QLatin1String("(U)")));
+                }
+                else
+                {
+                    name.truncate(name.indexOf(QLatin1Char(' ')));
+                }
+                kDebug() << name;
+                setPlayerName(name);
             } else if ( line.contains ( "Invalid password" ) ) {
                 forcePrompt = true;
             }
@@ -309,23 +329,16 @@
             }
             break;
         case PlayStage:
-            if ( moveRegExp.indexIn ( QLatin1String( line ) ) > -1 ) {
-                emit timeChanged ( White, QTime().addSecs ( moveRegExp.cap ( 1 \
                ).toInt() ) );
-                emit timeChanged ( Black, QTime().addSecs ( moveRegExp.cap ( 1 \
                ).toInt() ) );
-                Move m;
-                m.setFrom ( moveRegExp.cap ( 3 ) );
-                m.setTo ( moveRegExp.cap ( 4 ) );
-                if ( moveRegExp.numCaptures() > 4 )
+            if ( moveRegExp.indexIn ( QLatin1String( line ) ) > -1 )
                 {
-                    m.setFlag( Move::Promote, true );
-                    QChar typeChar = moveRegExp.cap( 5 ).mid( 1,1 )[0];
-                    m.setPromotedType(Piece::typeFromChar(typeChar));
-                }
-                emit pieceMoved ( m );
-            } else if ( gameInfoExp.indexIn( QLatin1String( line ) ) > -1 )
+                const int whiteTimeLimit = moveRegExp.cap(1).toInt();
+                const int blackTimeLimit = moveRegExp.cap(2).toInt();
+                const QString moveString = moveRegExp.cap(3);
+
+                kDebug() << moveString;
+
+                if (moveString == QLatin1String("none"))
             {
-                const int whiteTimeLimit = gameInfoExp.cap(1).toInt();
-                const int blackTimeLimit = gameInfoExp.cap(2).toInt();
                 if ( playerColor() == White )
                 {
                     setAttribute( QLatin1String("playerTimeLimit"), QTime().addSecs( \
whiteTimeLimit ) ); @@ -337,7 +350,35 @@
                     setAttribute( QLatin1String("oppTimeLimit"), QTime().addSecs( \
whiteTimeLimit ) );  }
                 emit initSuccesful();
+                    break;
             }
+
+                Move m;
+                if (moveString == QLatin1String("o-o"))
+                {
+                    // Short (king's rook) castling
+                    m = Move::castling ( Move::KingSide, \
oppositeColor(playerColor()) ); +                }
+                else if (moveString == QLatin1String("o-o-o"))
+                {
+                    // Long (Queen's rock) castling
+                    m = Move::castling ( Move::QueenSide, \
oppositeColor(playerColor()) ); +                }
+                else if ( moveStringExp.indexIn ( moveString ) > -1 )
+                {
+                    m.setFrom ( moveStringExp.cap ( 1 ) );
+                    m.setTo ( moveStringExp.cap ( 2 ) );
+                    if ( moveStringExp.numCaptures() > 2 )
+                    {
+                        m.setFlag( Move::Promote, true );
+                        QChar typeChar = moveRegExp.cap( 3 ).mid( 1,1 )[0];
+                        m.setPromotedType(Piece::typeFromChar(typeChar));
+                    }
+                }
+                emit timeChanged ( White, QTime().addSecs ( whiteTimeLimit ) );
+                emit timeChanged ( Black, QTime().addSecs ( blackTimeLimit ) );
+                emit pieceMoved ( m );
+            }
             else if ( line.contains ( "lost contact or quit" ) ) {
                 emit gameOver ( NoColor );
             }
--- trunk/kdereview/knights/src/proto/ficsprotocol.h #1185293:1185294
@@ -77,8 +77,10 @@
     static const QString pieces;
     static const QString coordinate;
     static const QString remainingTime;
+    static const QString movePattern;
 
     static const QRegExp moveRegExp;
+    static const QRegExp moveStringExp;
     static const QRegExp seekRegExp;
     static const QRegExp soughtRegExp;
     static const QRegExp challengeRegExp;


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

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