camino-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Camino-devel] camino/src unittests.pro common/Board.cpp commo...


From: Philippe Fremy
Subject: [Camino-devel] camino/src unittests.pro common/Board.cpp commo...
Date: Mon, 03 Mar 2003 17:13:49 -0500

CVSROOT:        /cvsroot/camino
Module name:    camino
Changes by:     Philippe Fremy <address@hidden> 03/03/03 17:13:49

Modified files:
        src            : unittests.pro 
        src/common     : Board.cpp Board.h TestBoard.cpp TestBoard.h 
                         Tile.cpp Tile.h const.h 

Log message:
        many methods and test to check move correctness on board

Patches:
Index: camino/src/common/Board.cpp
diff -u camino/src/common/Board.cpp:1.6 camino/src/common/Board.cpp:1.7
--- camino/src/common/Board.cpp:1.6     Sun Mar  2 18:30:12 2003
+++ camino/src/common/Board.cpp Mon Mar  3 17:13:49 2003
@@ -2,7 +2,7 @@
 **
 ** Camino
 **
-** Version : $Id: Board.cpp,v 1.6 2003/03/02 23:30:12 pfremy Exp $
+** Version : $Id: Board.cpp,v 1.7 2003/03/03 22:13:49 pfremy Exp $
 **
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 16/01/2003
@@ -29,6 +29,8 @@
 #include "Board.h"
 #include "Tile.h"
 
+int Board::neihgbourRow[8] = { -1, -1, -1,  0, 1, 1,  1,  0 };
+int Board::neihgbourCol[8] = { -1,  0,  1,  1, 1, 0, -1, -1 };
 
 /** add comments here */
 Board::Board()
@@ -44,9 +46,9 @@
 
 void Board::clear()
 {
-       for( uint i = 0; i < nbHorTiles; i++ ) {
-               for( uint j = 0; j < nbVerTiles; j++ ) {
-                       delete _tiles[i][j];
+       for( int i = 0; i < nbRow; i++ ) {
+               for( int j = 0; j < nbCol; j++ ) {
+                       if (_tiles[i][j]) delete _tiles[i][j];
                        _tiles[i][j] = 0;
                }
        }
@@ -54,11 +56,9 @@
 
 void Board::init()
 {
-       for( uint i = 0; i < nbHorTiles; i++ ) {
-               for( uint j = 0; j < nbVerTiles; j++ ) {
-                       Tile * tile = new Tile();
-                       tile->setPosition( i, j );
-                       _tiles[i][j] = tile;
+       for( int i = 0; i < nbRow; i++ ) {
+               for( int j = 0; j < nbCol; j++ ) {
+                       _tiles[i][j] = 0L;
                }
        }
 }
@@ -74,7 +74,7 @@
        Tile * ret = 0;
 
        if( ( row >= 0 ) && ( col >= 0 ) && 
-               ( row < nbHorTiles ) && ( col < nbVerTiles ) ) {
+               ( row < nbRow ) && ( col < nbCol ) ) {
                ret = _tiles[row][col];
        }
 
@@ -85,7 +85,12 @@
 {
        bool ret = true;
 
-       if( isPossible( row, col, tile ) ) {
+       if (tile == 0L) {
+               qDebug("Board::setTile - null tiled passed for %dx%d", row, col 
);
+               return false;
+       }
+
+       if( isMovePossible( row, col, tile ) ) {
                tile->setRotation( computeRotation( row, col, tile ) );
                _tiles[row][col] = tile;
                _moves.push( tile );
@@ -96,46 +101,43 @@
        return ret;
 }
 
-bool Board::isPossible( int row, int col, Tile  * /* tile */ )
+bool Board::isMovePossible( int row, int col, Tile  *  tile )
 {
-       bool ret = true;
+       if( ( row < 0 ) || ( col < 0 )) return false;
+       if (( row >= nbRow ) || ( col >= nbCol ) ) return false;
 
-       if( ( row >= 0 ) && ( col >= 0 ) 
-               && ( row < nbHorTiles ) && ( col < nbVerTiles ) ) {
-               ret = hasNeighbour( row, col );
+       if (tile->getType() == Tile::TILE_UNKNOWN) {
+               qDebug("Board::setTile - TILE_UNKNOWN passed for %dx%d", row, 
col );
+               return false;
+       }
+
+       // already a tile on it
+       if ( getTile( row, col ) ) return false;
+
+       // theorically more complicated, since only two corners can be
+       // set to roads, not 4.
+       
+       // only roads are allowed in corners
+       if ( ( row == 0 || row == nbRow -1) 
+                && ( col == 0 || col == nbCol -1) ) {
+               if (tile->isBeginningRoad() == false) return false;
        } else {
-               ret = false;
+               // check neighbour if not a corner tile
+               if (hasNeighbour( row, col ) == false) return false;
        }
 
-       return ret;
+       return true;
 }
 
 bool Board::hasNeighbour( int row, int col )
 {
        bool ret = false;
+       int i;
 
-       if( row > 0 ) {
-               if( col > 0 ) {
-                       ret = ret || ( _tiles[row-1][col-1]->getType() != 
Tile::TILE_UNKNOWN );
-               }
-               ret = ret || ( _tiles[row-1][col]->getType() != 
Tile::TILE_UNKNOWN );
-               if( col < nbVerTiles-1 ) {
-                       ret = ret || ( _tiles[row-1][col+1]->getType() != 
Tile::TILE_UNKNOWN );
-               }
-       }
-       if( col > 0 ) {
-               ret = ret || ( _tiles[row][col-1]->getType() != 
Tile::TILE_UNKNOWN );
-       }
-       if( col < nbVerTiles-1 ) {
-               ret = ret || ( _tiles[row][col+1]->getType() != 
Tile::TILE_UNKNOWN );
-       }
-       if( row < nbHorTiles-1 ) {
-               if( col > 0 ) {
-                       ret = ret || ( _tiles[row+1][col-1]->getType() != 
Tile::TILE_UNKNOWN );
-               }
-               ret = ret || ( _tiles[row+1][col]->getType() != 
Tile::TILE_UNKNOWN );
-               if( col < nbVerTiles-1 ) {
-                       ret = ret || ( _tiles[row+1][col+1]->getType() != 
Tile::TILE_UNKNOWN );
+       for( i=0; i<8; i++) {
+               if ( getTile( row + neihgbourRow[i], col + neihgbourCol[i] ) ) {
+                       ret = true;
+                       break;
                }
        }
 
@@ -162,4 +164,13 @@
        _moves.remove();
 
        return ret;
+}
+
+bool Board::isMovePossible( Tile  * tile )
+{
+       if (! tile) {
+               qDebug("Board::isMovePossible( tile ) - null tile passed as 
arg" );
+               return false;
+       }
+       return isMovePossible( tile->getRow(), tile->getCol(), tile );
 }
Index: camino/src/common/Board.h
diff -u camino/src/common/Board.h:1.5 camino/src/common/Board.h:1.6
--- camino/src/common/Board.h:1.5       Sun Mar  2 18:30:12 2003
+++ camino/src/common/Board.h   Mon Mar  3 17:13:49 2003
@@ -5,7 +5,7 @@
 ** Board.h
 ** Handles the game's board
 **
-** Version : $Id: Board.h,v 1.5 2003/03/02 23:30:12 pfremy Exp $
+** Version : $Id: Board.h,v 1.6 2003/03/03 22:13:49 pfremy Exp $
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 16/01/2003
 ** Copyright: Pascal Audoux, Philippe Fremy 2003
@@ -27,6 +27,9 @@
 #ifndef BOARD_H
 #define BOARD_H
 
+#ifdef DEBUG
+class TestBoard;
+#endif
 
 // generic include files
 // include files for QT
@@ -65,7 +68,8 @@
 
        bool setTile( int row, int col, Tile * tile );
 
-       bool isPossible( int row, int col, Tile  * tile );
+       bool isMovePossible( int row, int col, Tile  * tile );
+       bool isMovePossible( Tile  * tile );
 
        int computeRotation( int row, int col, Tile * tile );
 
@@ -75,8 +79,15 @@
 protected:
        bool hasNeighbour( int row, int col );
 
-       Tile * _tiles[ nbHorTiles ][ nbVerTiles ];
+       Tile * _tiles[ nbRow ][ nbCol ];
        QPtrStack<Tile> _moves;
+
+       static int neihgbourRow[8];
+       static int neihgbourCol[8];
+
+#ifdef DEBUG
+friend class TestBoard;
+#endif
 };
 
 #endif // BOARD_H
Index: camino/src/common/TestBoard.cpp
diff -u camino/src/common/TestBoard.cpp:1.1 camino/src/common/TestBoard.cpp:1.2
--- camino/src/common/TestBoard.cpp:1.1 Thu Jan 16 17:45:43 2003
+++ camino/src/common/TestBoard.cpp     Mon Mar  3 17:13:49 2003
@@ -2,7 +2,7 @@
 **
 ** Camino
 **
-** Version : $Id: TestBoard.cpp,v 1.1 2003/01/16 22:45:43 Audoux Exp $
+** Version : $Id: TestBoard.cpp,v 1.2 2003/03/03 22:13:49 pfremy Exp $
 **
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 16/01/2003
@@ -23,36 +23,105 @@
 
 #include "TestBoard.h"
 #include "Board.h"
+#include "common/Tile.h"
+#include "common/PhilAsserts.h"
 
 // register the suite so that it is run in the runner
 CPPUNIT_TEST_SUITE_REGISTRATION( TestBoard );
 
+using namespace Camino;
+
 void TestBoard::setUp()
 {
-       // add initialisation for every tests
+       b.init();
+       get();
 }
 
 void TestBoard::tearDown()
 {
-       // add finilisation for every test
+       b.init();
+//     delete t;
+}
+
+Tile * TestBoard::get()
+{
+       t = new Tile( Tile::TILE_ROAD_CROSS);
+       return t;
+}
+
+void TestBoard::testInit()
+{
+       b.clear();
+       b.init();
+       b.clear();
+       b.init();
+
+       checkEquals( b.getTile( 0, 0), 0L );
+       checkEquals( b.getTile( 1, 1), 0L );
+       checkEquals( b.getTile( 100, 100), 0L );
+
+       b.setTile( 0,0, t );
+       checkEquals( b.getTile( 0, 0), t );
+       b.setTile( 1,1, t );
+       checkEquals( b.getTile( 1, 1), t );
+}
+
+void TestBoard::testMoveWithinLimits()
+{
+       checkEquals( b.isMovePossible( -1, -1, t ), false );
+       checkEquals( b.isMovePossible( nbRow, 0, t ), false );
+       checkEquals( b.isMovePossible( 0, -1, t ), false );
+       checkEquals( b.isMovePossible( nbRow, nbCol, t ), false );
+
+       checkEquals( b.isMovePossible( 0, 0, t ), true  );
+}
+
+void TestBoard::testMoveOnExistingTile()
+{
+       b.setTile( 0, 0, t);
+       checkAssert( b.isMovePossible( 1, 1, t ) );
+       b.setTile( 1, 1, get() );
+       checkNotAssert( b.isMovePossible( 1, 1, t ) );
 }
 
-void TestTemplate::testConstructor()
+void TestBoard::testOnlyRoadInCorners()
 {
-       // do something
+       Tile * t2 = new Tile( Tile::TILE_RIVER_STRAIGHT );
+       checkAssert( b.isMovePossible( 0, 0, t ) );
+       checkAssert( b.isMovePossible( 0, nbCol-1, t ) );
+       checkAssert( b.isMovePossible( nbRow-1, 0, t ) );
+       checkAssert( b.isMovePossible( nbRow-1, nbCol-1, t ) );
+
+       checkNotAssert( b.isMovePossible( 0, 0, t2 ) );
+       checkNotAssert( b.isMovePossible( 0, nbCol-1, t2 ) );
+       checkNotAssert( b.isMovePossible( nbRow-1, 0, t2 ) );
+       checkNotAssert( b.isMovePossible( nbRow-1, nbCol-1, t2 ) );
+}
 
+void TestBoard::testHasNeighbour()
+{
+       checkNotAssert( b.hasNeighbour( 1, 1 ) );
+       checkNotAssert( b.hasNeighbour( 0, 0 ) );
+       checkNotAssert( b.hasNeighbour( nbRow, nbCol ) );
+
+       b.setTile( 0, 0, get() );
+       b.setTile( 1, 1, get() );
+       checkAssert( b.hasNeighbour( 0, 0 ) );
+       checkAssert( b.hasNeighbour( 1, 0 ) );
+       checkAssert( b.hasNeighbour( 0, 1 ) );
+       checkAssert( b.hasNeighbour( 2, 2 ) );
+       checkNotAssert( b.hasNeighbour( 3, 3 ) );
 }
 
-/*
-void TestTemplate::testHop()
+void TestBoard::testMoveWithNeighbour()
 {
-       // do something
-       CPPUNIT_ASSERT( /* some assertion */ 1 );
-       CPPUNIT_FAIL( "The exception was not raised!" );
-       CPPUNIT_ASSERT_EQUAL( 1, 1 );
-       CPPUNIT_ASSERT_MESSAGE( "This is a custom message" , /* some_assertion 
*/ 1 );
-       CPPUNIT_ASSERT_EQUAL( (char *) "aslfdjalsfj", (char *) "hop" );
-       CPPUNIT_ASSERT_EQUAL_MESSAGE( "This is a custom message for a failed 
equality!", 3, 2 );
+       checkAssert( b.setTile( 0, 0, get() ) );
+       checkAssert( b.setTile( 1, 1, get() ) );
+       checkAssert( b.setTile( 2, 2, get() ) );
+       checkNotAssert( b.setTile( 4, 4, get() ) );
+       checkAssert( b.setTile( 2, 3, get() ) );
+       checkAssert( b.setTile( 2, 4, get() ) );
+       checkAssert( b.setTile( 3, 4, get() ) );
+       checkAssert( b.setTile( 4, 4, get() ) );
 }
-*/
 
Index: camino/src/common/TestBoard.h
diff -u camino/src/common/TestBoard.h:1.1 camino/src/common/TestBoard.h:1.2
--- camino/src/common/TestBoard.h:1.1   Thu Jan 16 17:45:43 2003
+++ camino/src/common/TestBoard.h       Mon Mar  3 17:13:49 2003
@@ -5,7 +5,7 @@
 ** TestBoard.h
 ** test for the Board class
 **
-** Version : $Id: TestBoard.h,v 1.1 2003/01/16 22:45:43 Audoux Exp $
+** Version : $Id: TestBoard.h,v 1.2 2003/03/03 22:13:49 pfremy Exp $
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 16/01/2003
 **
@@ -29,7 +29,11 @@
 #define TEST_BOARD_H
 
 #include "cppunit/TestCase.h"
-#include "cppunit/extensions/helpermacros.h"
+#include "cppunit/extensions/HelperMacros.h"
+
+#include "common/Board.h"
+
+class Tile;
 
 class TestBoard  : public CppUnit::TestCase
 {
@@ -45,16 +49,29 @@
        // declare the test suite
        CPPUNIT_TEST_SUITE( TestBoard );
 
-       CPPUNIT_TEST( testConstructor );
+       CPPUNIT_TEST( testInit );
+       CPPUNIT_TEST( testMoveWithinLimits );
+       CPPUNIT_TEST( testMoveOnExistingTile );
+       CPPUNIT_TEST( testOnlyRoadInCorners );
+       CPPUNIT_TEST( testHasNeighbour );
+       CPPUNIT_TEST( testMoveWithNeighbour );
 
        CPPUNIT_TEST_SUITE_END();
 
 
        // declare the test functions
 
-       void testConstructor();
+       void testInit();
+       void testMoveWithinLimits();
+       void testMoveOnExistingTile();
+       void testHasNeighbour();
+       void testOnlyRoadInCorners();
+       void testMoveWithNeighbour();
 
 protected:
+       Tile * get();
+       Tile * t;
+       Board b;
        // declare some members
 };
 
Index: camino/src/common/Tile.cpp
diff -u camino/src/common/Tile.cpp:1.6 camino/src/common/Tile.cpp:1.7
--- camino/src/common/Tile.cpp:1.6      Mon Mar  3 15:15:15 2003
+++ camino/src/common/Tile.cpp  Mon Mar  3 17:13:49 2003
@@ -2,7 +2,7 @@
 **
 ** Camino
 **
-** Version : $Id: Tile.cpp,v 1.6 2003/03/03 20:15:15 Audoux Exp $
+** Version : $Id: Tile.cpp,v 1.7 2003/03/03 22:13:49 pfremy Exp $
 **
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 14/01/2003
@@ -115,6 +115,19 @@
        _isRotationKnown = false;
 }
 
+bool Tile::isBeginningRoad()
+{
+       if (_type == TILE_ROAD_STRAIGHT
+               || _type == TILE_ROAD_T
+               || _type == TILE_ROAD_TURN
+               || _type == TILE_ROAD_CROSS ) {
+               return true;
+       }
+
+       return false;
+
+}
+
 void Tile::rotate()
 {
        if( _isRotationKnown ) {
@@ -124,7 +137,3 @@
                }
        }
 }
-
-
-
-
Index: camino/src/common/Tile.h
diff -u camino/src/common/Tile.h:1.7 camino/src/common/Tile.h:1.8
--- camino/src/common/Tile.h:1.7        Mon Mar  3 15:15:15 2003
+++ camino/src/common/Tile.h    Mon Mar  3 17:13:49 2003
@@ -5,7 +5,7 @@
 ** Tile.h
 ** Manage a tile on the board game
 **
-** Version : $Id: Tile.h,v 1.7 2003/03/03 20:15:15 Audoux Exp $
+** Version : $Id: Tile.h,v 1.8 2003/03/03 22:13:49 pfremy Exp $
 ** Author(s) : Philippe Fremy, Pascal Audoux
 ** Creation : 16/01/2003
 ** Copyright: Pascal Audoux, Philippe Fremy 2003
@@ -97,6 +97,8 @@
        virtual void setRow( int row ) { _row = row; }
 
        virtual void setCol( int col ) { _col = col; }
+
+       bool isBeginningRoad();
 
 protected:
        int _row, _col;
Index: camino/src/common/const.h
diff -u camino/src/common/const.h:1.2 camino/src/common/const.h:1.3
--- camino/src/common/const.h:1.2       Fri Feb 28 07:40:54 2003
+++ camino/src/common/const.h   Mon Mar  3 17:13:49 2003
@@ -5,8 +5,8 @@
 namespace Camino {
 
 // allow for caminoto boar size
-const int nbHorTiles = 14;
-const int nbVerTiles = 10;
+const int nbRow = 14;
+const int nbCol = 10;
 
 const int nbTiles4players = 5;
 const int nbTiles2players = 6;
Index: camino/src/unittests.pro
diff -u camino/src/unittests.pro:1.9 camino/src/unittests.pro:1.10
--- camino/src/unittests.pro:1.9        Tue Jan 28 16:53:12 2003
+++ camino/src/unittests.pro    Mon Mar  3 17:13:48 2003
@@ -19,12 +19,12 @@
 }
 
 # server
-HEADERS += server/TestCaminoServer.h
-SOURCES += server/TestCaminoServer.cpp
+#HEADERS += server/TestCaminoServer.h
+#SOURCES += server/TestCaminoServer.cpp
 
 # common
-HEADERS += common/TestMsgDecoder.h 
-SOURCES += common/TestMsgDecoder.cpp 
+HEADERS += common/TestBoard.h 
+SOURCES += common/TestBoard.cpp 
 
 HEADERS += common/PhilAsserts.h common/PhilTestRunner.h
 SOURCES += common/PhilAsserts.cpp common/PhilTestRunner.cpp




reply via email to

[Prev in Thread] Current Thread [Next in Thread]