[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-05-19 12:21:29
Message-ID: 20130519122129.0F999AC85D () svn ! kde ! org
[Download RAW message or body]

SVN commit 1355454 by dmitryvchernov:

many changes and many bugs introduced

 M  +5 -0      backgammonscene.cpp  
 M  +14 -4     games/backgammon/backgammon_ruleset.cpp  
 M  +15 -5     games/backgammon/backgammongame.cpp  
 M  +7 -6      games/backgammon/backgammongame.h  
 M  +27 -3     test/test_backgammon_ruleset.cpp  


--- trunk/playground/games/backgammon/backgammonscene.cpp #1355453:1355454
@@ -667,6 +667,11 @@
 
 void BackgammonScene::showProposedMoves( QVector<int> indexes, Checker::Color color )
 {
+	kDebug() << "proposed moves : ";
+	
+	for( int i = 0; i < indexes.size(); i++ )
+		kDebug() << indexes[i];
+	
 	if(m_showLegalMoves)
 	{
 		foreach( int index, indexes )
--- trunk/playground/games/backgammon/games/backgammon/backgammon_ruleset.cpp #1355453:1355454
@@ -153,12 +153,11 @@
 	}		
 	
 	if( cell >= BackgammonGame::thrown_ind )
+	{
 		if( !canThrow( field, dir ) )
 			ret = false;
+	}
 		
-	if( !qAbs( field[cell] <= 6 ) )
-	  ret = false;
-	
 	return ret;	
 }
 
@@ -195,7 +194,7 @@
 		{
 			ret = -1 + die;
 		}
-		else if( ret >= BackgammonGame::thrown_ind )
+		else if( ret >= BackgammonGame::bar_ind )
 		{
 			ret = BackgammonGame::thrown_ind_positive;
 		}
@@ -245,6 +244,8 @@
 		{
 			ret.push_back(index);
 		
+			if( index < BackgammonGame::thrown_ind )
+			{
 			index = getIndexForDie( index, dice[1], dir );
 			if( canPut( field, dir, index ) )
 			{
@@ -252,6 +253,7 @@
 				ret.push_back(index);
 			}
 		}
+		}
 		
 		if( dice[0] != dice[1] )
 		{
@@ -261,6 +263,8 @@
 		  {
 			  ret.push_back(index);
 		  
+			  if( index < BackgammonGame::thrown_ind )
+			  {
 			  if( !sum_put ) 
 			  {
 				  index = getIndexForDie( index, dice[0], dir );
@@ -270,6 +274,7 @@
 		  }
 		}
 	}
+	}
 	else
 	{
 		int index = src;
@@ -282,6 +287,9 @@
 				ret.push_back(index);
 			else
 				break;
+			
+			if( index >= BackgammonGame::thrown_ind )
+				break;
 		}
 	}
 	
@@ -295,6 +303,7 @@
 		int summ = 0;
 		for( int i = 18; i < 24; ++i )
 		{
+			if( canTake( field, dir, i ) )
 			summ += field[i];
 		}
 		
@@ -307,6 +316,7 @@
 		int summ = 0;
 		for( int i = 0; i < 6; ++i )
 		{
+			if( canTake( field, dir, i ) )
 			summ += field[i];
 		}
 		
--- trunk/playground/games/backgammon/games/backgammon/backgammongame.cpp #1355453:1355454
@@ -561,17 +561,25 @@
         m_field[ind]++;
 }
 
+void BackgammonGame::adjustIndexesToBoard( QVector<int>& indexes, const  IPlayer& player )
+{
+	for( int i = 0; i < indexes.size(); ++i )
+	{
+		indexes[i] = adjustIndexToBoard( indexes[i], player );
+	}
+}
+
 int BackgammonGame::adjustIndexToBoard( int src, const  IPlayer& player )
 {
-    if( src >= bar_ind )
+    /*if( src >= bar_ind )
     {
         src = Board::bar( player.color() );
     }
 
-    else if( src >= thrown_ind )
+    /*else if( src >= thrown_ind )
     {
-        src =  Board::home( Checker::getAnotherColor( player.color() ) );
-    }
+        src =  Board::home( player.color() );
+    }*/
     return src;
 }
 
@@ -681,7 +689,9 @@
 	
 	if( cellMatchesPlayer( player, src ) )
 	{
-		emit( proposedMoves( getProposedMoves( src, player ), player.color() ) );
+		QVector<int> indexes = getProposedMoves( src, player );
+		adjustIndexesToBoard( indexes, player );
+		emit( proposedMoves( indexes, player.color() ) );
 		emit( selectChecker( adjustIndexToBoard( src, player ) ) );
 	}
 }
--- trunk/playground/games/backgammon/games/backgammon/backgammongame.h #1355453:1355454
@@ -60,12 +60,12 @@
     
     virtual void undo();    
 	
-	static const int bar_ind = 26;
-    static const int thrown_ind = 24;
-	static const int thrown_ind_positive = 24;
-	static const int thrown_ind_negative = 25;
-	static const int bar_ind_positive = 26;
-	static const int bar_ind_negative = 27;
+	static const int bar_ind = 24;
+    static const int thrown_ind = 26;
+	static const int thrown_ind_positive = 27;
+	static const int thrown_ind_negative = 26;
+	static const int bar_ind_positive = 24;
+	static const int bar_ind_negative = 25;
 
 signals:
     /**
@@ -232,6 +232,7 @@
     
     QVector<int> getProposedMoves( int cell, const IPlayer& player );
     
+	void adjustIndexesToBoard( QVector<int>& indexes, const IPlayer& player  );
     int adjustIndexToBoard( int src, const IPlayer& player  );
     
     /*
--- trunk/playground/games/backgammon/test/test_backgammon_ruleset.cpp #1355453:1355454
@@ -102,11 +102,11 @@
 	
 	{
 		const QVector<int> field = 
-		{ 1, 0, 0, -1, 0, 4, 0, 3, 0, 1, 0, -4, 4, 0, 0, 0, -2, -1, -5, 2, -1, 0, 0, 0, 0, 0, -1, 0 };
+		{ 1, 0, 0, -1, 0, 4, 0, 3, 0, 1, 0, -4, 4, 0, 0, 0, -2, -1, -5, 2, -1, 0, 0, 0, -1, 0, 0, 0 };
 
 		{
 			const QVector<int> dice = { 6, 4 };
-			QVector<int> moves = BackgammonRuleset::getMoves( field, IPlayer::positive, dice, 26 );
+			QVector<int> moves = BackgammonRuleset::getMoves( field, IPlayer::positive, dice, 24 );
 			
 			QVERIFY( moves.size() == 2 );
 		}
@@ -122,12 +122,25 @@
 			const QVector<int> dice = { 6, 6 };
 			QVector<int> moves = BackgammonRuleset::getMoves( field, IPlayer::positive, dice, 18 );
 			
-			QVERIFY( moves.size() == 2 );
+			QVERIFY( moves.size() == 1 );
 			
 			QVERIFY( moves[0] == BackgammonGame::thrown_ind_positive );
 		}		
 	}
+	
+	{
+		const QVector<int> field =
+		{ 2, -2, 5, 4, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4, -3, -2, -4, 0, 0, 0, 0 };
+		
+		const QVector<int> dice = { 6, 6 };
+		
+		QVector<int> moves = BackgammonRuleset::getMoves( field, IPlayer::negative, dice, 5 );
+		
+		QVERIFY( moves.size() == 1 );
+			
+		QVERIFY( moves[0] == BackgammonGame::thrown_ind_negative );
 }
+}
 
 void TestBackgammonRuleset::testGotMoves()
 {
@@ -152,7 +165,18 @@
 		
 		QVERIFY( res );		
 	}
+	
+	{
+		const QVector<int> field =
+		{ 2, -2, 5, 4, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4, -3, -2, -4, 0, 0, 0, 0 };
+		
+		const QVector<int> dice = { 6, 6 };
+		
+		bool res = BackgammonRuleset::gotMoves( field, IPlayer::negative, dice );
+		
+		QVERIFY( res );	
 }
+}
 
 void TestBackgammonRuleset::testCanMove()
 {
[prev in list] [next in list] [prev in thread] [next in thread] 

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