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

List:       kde-commits
Subject:    playground/games/backgammon
From:       Dmitry Vladimirovich Chernov <diman4ik.chernov () gmail ! com>
Date:       2013-11-03 16:55:48
Message-ID: E1Vd0xo-0002Rf-HR () scm ! kde ! org
[Download RAW message or body]

SVN commit 1368464 by dmitryvchernov:

added clip parsing tests

 M  +31 -5     games/backgammon/fibs/backgammon_fibs.cpp  
 M  +99 -0     games/backgammon/fibs/clip.cpp  
 M  +2 -0      games/backgammon/fibs/clip.h  
 M  +10 -1     test/CMakeLists.txt  
 A             test/test_clip.cpp   [License: GPL (v2+)]
 A             test/test_clip.h   [License: GPL (v2+)]


--- trunk/playground/games/backgammon/games/backgammon/fibs/backgammon_fibs.cpp \
#1368463:1368464 @@ -994,6 +994,32 @@
 
 void BackgammonFibs::readBoardStyle3( const QString& line )
 {
+	QVector<int> field(28);
+	QVector<int> dice(2);
+	
+	Clip::parseBoardStyle3( line, field, dice, m_yourTurn, m_needReverse );
+	
+	setBoard( field );
+	
+	Rolls your_roll( dice[0], dice[1] );
+    Rolls opponent_roll( dice[2], dice[3] );
+	
+	if( !m_watchMode )
+    {
+        if( m_yourTurn )
+		{
+			if( your_roll.first != 0 )
+            {
+                setDice( your_roll );
+                switchToState( Move1 );
+            }
+            else
+            {
+                emit( engineMessage( "Click to roll" ) );
+                switchToState( Roll );
+            }
+		}
+	}
     /*
     board:You:GammonBot:    // 0,1,2
     1:      // match length 3
@@ -1017,7 +1043,7 @@
     2:      // Number of pieces you can move 49
     0:0:0   // Unused                        50, 51, 52
     */
-    QStringList parts = line.split(':');
+    /*QStringList parts = line.split(':');
     
     if( parts.size() < 45 )
         return;
@@ -1048,11 +1074,11 @@
         playerO = &getOpponent();
         
         kDebug() << "playerX is you";
-        
+        setBoard( cells );
         m_needReverse = true;
     }
     
-    QVector<int> cells;
+    QVector<int> cells(28);
     
     int bar1 = parts[6].toInt();
     int bar2 = parts[31].toInt();
@@ -1096,7 +1122,7 @@
     }
     
     m_yourTurn = false;
-    
+    setBoard( cells );
     if( parts[32].toInt() > 0 )
     {
         if( &getYou() == playerO )
@@ -1181,7 +1207,7 @@
     Q_UNUSED( bar );
     Q_UNUSED( my_bar_count );
     Q_UNUSED( opponent_bar_count );
-    Q_UNUSED( can_move );
+    Q_UNUSED( can_move );*/
 }
 
 int BackgammonFibs::reverseIndex( int index )
--- trunk/playground/games/backgammon/games/backgammon/fibs/clip.cpp #1368463:1368464
@@ -24,7 +24,9 @@
 #include <QFile>
 #include <QStringList>
 
+#include "../../board.h"
 
+
 void Clip::initPatterns()
 {
     QString pattern;
@@ -440,6 +442,103 @@
     }
 }
 
+static int reverseIndex( int index )
+{
+    return qAbs( 23 - index );
+}
+
+bool Clip::parseBoardStyle3( const QString& line, QVector<int>& cells, QVector<int>& \
dice, bool& yourTurn, bool& needReverse ) +{
+	/*
+    board:You:GammonBot:    // 0,1,2
+    1:      // match length 3
+    0:0:    // current scores 4, 5
+    0:      // bar 6
+    -2: 0: 0: 0: 0: 5: 1: 2: 0: 0: 0: -5: 5: 0: 0: 0: -3: 0: -5: 0: 0: 1: 0: 1: // \
board 7 - 30 +    0:      // bar 31
+    -1:     // X's turn 32
+    0:0:    // Player's roll 33, 34
+    0:0:    // Opponent's roll 35, 36
+    1:      // Doubling cube 37
+    1:1:    // Both may double 38, 39
+    0:      // You didn't double 40
+    1:      // You are O  41 
+    -1:     // You move from 24 to 1  42
+    0:25:   // Your home and bar index 43, 44
+    0:      // Your home count 45
+    0:      // Your bar count  46
+    0:      // Opponent's home count 47
+    0:      // Opponent's bar count  48
+    2:      // Number of pieces you can move 49
+    0:0:0   // Unused                        50, 51, 52
+    */
+    QStringList parts = line.split(':');
+    
+    if( parts.size() < 45 )
+        return false;
+    
+    QString player1Name = parts[1];
+    QString player2Name = parts[2];
+    
+    int match_length = parts[3].toInt();
+    
+    int player_score = parts[4].toInt();
+    int opponent_score = parts[5].toInt();
+    
+	needReverse = ( parts[42].toInt() > 0 ); // Direction : -1 	If you play from \
position 24 to position 1. 1 	If you play from position 1 to position 24 +
+	bool youPlayerX = ( parts[41].toInt() < 0 ); // -1 	If you are X. 1 	If you are O.
+    
+    for( int i = 7; i < 31; ++i )
+    {
+        int nextCell = parts[ i ].toInt();
+        
+        if( needReverse )
+            cells[ reverseIndex(i - 7) ] = youPlayerX? nextCell : - nextCell; // You \
are always negative from 24 to 1 in kbackgammon representation +        else
+            cells[ i - 7 ] = youPlayerX? nextCell : - nextCell;
+    }
+    
+    bool XsTurn = ( parts[32].toInt() < 0 );
+	
+    yourTurn = ( youPlayerX ) ? XsTurn : !XsTurn;
+    
+    dice.push_back( parts[33].toInt() );
+	dice.push_back( parts[34].toInt() );
+	dice.push_back( parts[35].toInt() );
+	dice.push_back( parts[36].toInt() );
+    
+    int doubling_cube = parts[37].toInt();
+    int may_double_you = parts[38].toInt();
+    int may_double_opponent = parts[39].toInt();
+    int was_doubled = parts[40].toInt();
+    
+    int my_home_count = parts[45].toInt();
+    int opponent_home_count = parts[46].toInt();
+    
+    int my_bar_count = parts[47].toInt();
+    int opponent_bar_count = parts[48].toInt();
+	
+	cells[ Board::NEGATIVE_BAR ] = -qAbs( my_bar_count );
+	cells[ Board::POSITIVE_BAR ] = -qAbs( opponent_bar_count );
+    
+    cells[ Board::NEGATIVE_POOL ] = -qAbs( my_home_count );
+    cells[ Board::POSITIVE_POOL ] = qAbs( opponent_home_count );        
+    
+    int can_move = parts[49].toInt();
+	
+    Q_UNUSED( match_length );
+    Q_UNUSED( doubling_cube );
+    Q_UNUSED( may_double_you );
+    Q_UNUSED( may_double_opponent );
+    Q_UNUSED( was_doubled );
+    Q_UNUSED( my_bar_count );
+    Q_UNUSED( opponent_bar_count );
+    Q_UNUSED( can_move );
+	
+	return true;
+}
+
 QRegExp Clip::str_patternBanner;
 QRegExp Clip::str_patternYourLogin;
 QRegExp Clip::str_patternServerTime;
--- trunk/playground/games/backgammon/games/backgammon/fibs/clip.h #1368463:1368464
@@ -122,6 +122,8 @@
     static void getAll( QString& line_given, QRegExp end_pattern, QVector<QString>& \
vec );  static void skipAll( QString& line_given, QRegExp end_pattern );
     
+	static bool parseBoardStyle3( const QString& text, QVector<int>& field, \
QVector<int>& dice, bool& yourTurn, bool& needReverse ); +    
     static QRegExp str_patternWelcome;
     static QRegExp str_patternOwnInfo;
     static QRegExp str_patternBanner;
--- trunk/playground/games/backgammon/test/CMakeLists.txt #1368463:1368464
@@ -23,4 +23,13 @@
 QT4_AUTOMOC( ${test_backgammonruleset_SRCS} )
 kde4_add_executable( test_backgammonruleset ${test_backgammonruleset_SRCS} )
 target_link_libraries(test_backgammonruleset  ${QT_QTCORE_LIBRARY} \
                ${QT_QTTEST_LIBRARY} ${KDE4_KIO_LIBS} )
-ADD_TEST( NAME BackgammonRulesetTest COMMAND test_backgammonruleset )
\ No newline at end of file
+ADD_TEST( NAME BackgammonRulesetTest COMMAND test_backgammonruleset )
+
+
+SET( test_clip_SRCS  	test_clip.cpp
+									../games/backgammon/fibs/clip.cpp )
+
+QT4_AUTOMOC( ${test_clip_SRCS} )
+kde4_add_executable( test_clip ${test_clip_SRCS} )
+target_link_libraries(test_clip  ${QT_QTCORE_LIBRARY} ${QT_QTTEST_LIBRARY} \
${KDE4_KIO_LIBS} ) +ADD_TEST( NAME ClipTest COMMAND test_clip )
\ No newline at end of file


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

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