WITilePointer BoardModel::getTile(const vector2du32 &position) const { if (validCoords(position)) { return _tiles[position.x][position.y]; } LOG_ERROR("Error getting tile in coords " << position.x << "," << position.y); return WITilePointer(); }
void Game1v1Test::TestGame1v1PutDownAndRemovePiece() { #ifdef DEBUG_PRINT std::cout << __FUNCTION__ << std::endl; #endif // game1v1 must be reset before starting (otherwise it might fail) this->Reset(); Board extraBoard(m_board); Player extraPlayer1(m_player1); Player extraPlayer2(m_player2); std::vector<Coordinate> validCoords(PIECE_MAX_SQUARES); for (int8_t i = e_numberOfPieces - 1 ; i >= e_minimumPieceIndex ; i--) { // both m_player1 and m_player2 should have all the pieces available anyway const std::list<PieceConfiguration> &pieceConfList = m_player1.m_pieces[i].GetPrecalculatedConfs(); std::list<PieceConfiguration>::const_iterator pieceConfIt; for (pieceConfIt = pieceConfList.begin(); pieceConfIt != pieceConfList.end(); pieceConfIt++) { // sets&iterators to retrieve nk points STLCoordinateSet_t nkPointsSetPlayer1; STLCoordinateSet_t nkPointsSetPlayer2; STLCoordinateSet_t nkPointsSetExtraPlayer1; STLCoordinateSet_t nkPointsSetExtraPlayer2; STLCoordinateSet_t::const_iterator nkIt; STLCoordinateSet_t::const_iterator extraNKIt; // put down both pieces on the real game1v1 board using the game1v1 method Game1v1::PutDownPiece( m_board, m_player1.GetStartingCoordinate(), *pieceConfIt, m_player1, m_player2); TestGame1v1GetAllNucleationPoints(Game1v1::e_Game1v1Player1); TestGame1v1GetAllNucleationPoints(Game1v1::e_Game1v1Player2); Game1v1::PutDownPiece( m_board, m_player2.GetStartingCoordinate(), *pieceConfIt, m_player2, m_player1); TestGame1v1GetAllNucleationPoints(Game1v1::e_Game1v1Player1); TestGame1v1GetAllNucleationPoints(Game1v1::e_Game1v1Player2); // put down the very same pieces on the extraBoard using the extra players // using directly m_pieceSquares attribute PieceConfigurationContainer_t::const_iterator it; for (it = pieceConfIt->m_pieceSquares.begin(); it != pieceConfIt->m_pieceSquares.end(); it++) { Coordinate player1Coord( m_player1.GetStartingCoordinate().m_row + it->m_row, m_player1.GetStartingCoordinate().m_col + it->m_col); Coordinate player2Coord( m_player2.GetStartingCoordinate().m_row + it->m_row, m_player2.GetStartingCoordinate().m_col + it->m_col); assert(extraBoard.IsCoordEmpty(player1Coord)); assert(extraBoard.IsCoordEmpty(player2Coord)); extraBoard.SetPlayerInCoord(player1Coord, extraPlayer1); extraBoard.SetPlayerInCoord(player2Coord, extraPlayer2); } // for (it = a_pieceConf.m_pieceSquares.begin(); // recalculate all the nk points for both extra players on the board // using the "compute" method (it's slow but it's been well tested) Coordinate thisCoord(0, 0); for (thisCoord.m_row = 0 ; thisCoord.m_row < extraBoard.GetNRows(); thisCoord.m_row++) { for (thisCoord.m_col = 0 ; thisCoord.m_col < extraBoard.GetNColumns(); thisCoord.m_col++) { // "extra" player1 if (rules::IsNucleationPointCompute(extraBoard, extraPlayer1, thisCoord)) { extraPlayer1.SetNucleationPoint(thisCoord); } else { extraPlayer1.UnsetNucleationPoint(thisCoord); } // "extra" player2 if (rules::IsNucleationPointCompute(extraBoard, extraPlayer2, thisCoord)) { extraPlayer2.SetNucleationPoint(thisCoord); } else { extraPlayer2.UnsetNucleationPoint(thisCoord); } assert (m_board.IsCoordEmpty(thisCoord) == extraBoard.IsCoordEmpty(thisCoord)); assert (m_board.IsPlayerInCoord(thisCoord, m_player1) == extraBoard.IsPlayerInCoord(thisCoord, extraPlayer1)); assert (m_board.IsPlayerInCoord(thisCoord, m_player2) == extraBoard.IsPlayerInCoord(thisCoord, extraPlayer2)); } } // GetAllNucleationPoints method must retrieve the same nucleation points m_player1.GetAllNucleationPoints(nkPointsSetPlayer1); m_player2.GetAllNucleationPoints(nkPointsSetPlayer2); extraPlayer1.GetAllNucleationPoints(nkPointsSetExtraPlayer1); extraPlayer2.GetAllNucleationPoints(nkPointsSetExtraPlayer2); assert(nkPointsSetPlayer1.size() == nkPointsSetExtraPlayer1.size()); assert(nkPointsSetPlayer2.size() == nkPointsSetExtraPlayer2.size()); nkIt= nkPointsSetPlayer1.begin(); extraNKIt = nkPointsSetExtraPlayer1.begin(); while (nkIt != nkPointsSetPlayer1.end() && extraNKIt != nkPointsSetExtraPlayer1.end()) { assert (*nkIt == *extraNKIt); assert (m_player1.IsNucleationPoint(*nkIt) == extraPlayer1.IsNucleationPoint(*extraNKIt)); nkIt++; extraNKIt++; } assert (nkIt == nkPointsSetPlayer1.end() && extraNKIt == nkPointsSetExtraPlayer1.end()); nkIt= nkPointsSetPlayer2.begin(); extraNKIt = nkPointsSetExtraPlayer2.begin(); while (nkIt != nkPointsSetPlayer2.end() && extraNKIt != nkPointsSetExtraPlayer2.end()) { assert (*nkIt == *extraNKIt); assert (m_player2.IsNucleationPoint(*nkIt) == extraPlayer2.IsNucleationPoint(*extraNKIt)); nkIt++; extraNKIt++; } assert (nkIt == nkPointsSetPlayer2.end() && extraNKIt == nkPointsSetExtraPlayer2.end()); // remove pieces and do it again Game1v1::RemovePiece( m_board, m_player1.GetStartingCoordinate(), *pieceConfIt, m_player1, m_player2); TestGame1v1GetAllNucleationPoints(Game1v1::e_Game1v1Player2); TestGame1v1GetAllNucleationPoints(Game1v1::e_Game1v1Player1); Game1v1::RemovePiece( m_board, m_player2.GetStartingCoordinate(), *pieceConfIt, m_player2, m_player1); TestGame1v1GetAllNucleationPoints(Game1v1::e_Game1v1Player2); TestGame1v1GetAllNucleationPoints(Game1v1::e_Game1v1Player1); // remove the very same pieces from the extraBoard using the extra players // using directly m_pieceSquares attribute for (it = pieceConfIt->m_pieceSquares.begin(); it != pieceConfIt->m_pieceSquares.end(); it++) { Coordinate player1Coord( m_player1.GetStartingCoordinate().m_row + it->m_row, m_player1.GetStartingCoordinate().m_col + it->m_col); Coordinate player2Coord( m_player2.GetStartingCoordinate().m_row + it->m_row, m_player2.GetStartingCoordinate().m_col + it->m_col); assert(extraBoard.IsPlayerInCoord(player1Coord, extraPlayer1)); assert(extraBoard.IsPlayerInCoord(player2Coord, extraPlayer2)); extraBoard.BlankCoord(player1Coord); extraBoard.BlankCoord(player2Coord); } // for (it = a_pieceConf.m_pieceSquares.begin(); for (thisCoord.m_row = 0 ; thisCoord.m_row < extraBoard.GetNRows(); thisCoord.m_row++) { for (thisCoord.m_col = 0 ; thisCoord.m_col < extraBoard.GetNColumns(); thisCoord.m_col++) { // "extra" player1 if (rules::IsNucleationPointCompute(extraBoard, extraPlayer1, thisCoord)) { extraPlayer1.SetNucleationPoint(thisCoord); } else { extraPlayer1.UnsetNucleationPoint(thisCoord); } // "extra" player2 if (rules::IsNucleationPointCompute(extraBoard, extraPlayer2, thisCoord)) { extraPlayer2.SetNucleationPoint(thisCoord); } else { extraPlayer2.UnsetNucleationPoint(thisCoord); } assert (m_board.IsCoordEmpty(thisCoord) == extraBoard.IsCoordEmpty(thisCoord)); assert (m_board.IsPlayerInCoord(thisCoord, m_player1) == extraBoard.IsPlayerInCoord(thisCoord, extraPlayer1)); assert (m_board.IsPlayerInCoord(thisCoord, m_player2) == extraBoard.IsPlayerInCoord(thisCoord, extraPlayer2)); } } // GetAllNucleationPoints method must retrieve the same nucleation points nkPointsSetPlayer1.clear(); m_player1.GetAllNucleationPoints(nkPointsSetPlayer1); nkPointsSetPlayer2.clear(); m_player2.GetAllNucleationPoints(nkPointsSetPlayer2); nkPointsSetExtraPlayer1.clear(); extraPlayer1.GetAllNucleationPoints(nkPointsSetExtraPlayer1); nkPointsSetExtraPlayer2.clear(); extraPlayer2.GetAllNucleationPoints(nkPointsSetExtraPlayer2); assert(nkPointsSetPlayer1.size() == nkPointsSetExtraPlayer1.size()); assert(nkPointsSetPlayer2.size() == nkPointsSetExtraPlayer2.size()); nkIt= nkPointsSetPlayer1.begin(); extraNKIt = nkPointsSetExtraPlayer1.begin(); while (nkIt != nkPointsSetPlayer1.end() && extraNKIt != nkPointsSetExtraPlayer1.end()) { assert (*nkIt == *extraNKIt); assert (m_player1.IsNucleationPoint(*nkIt) == extraPlayer1.IsNucleationPoint(*extraNKIt)); nkIt++; extraNKIt++; } assert (nkIt == nkPointsSetPlayer1.end() && extraNKIt == nkPointsSetExtraPlayer1.end()); nkIt= nkPointsSetPlayer2.begin(); extraNKIt = nkPointsSetExtraPlayer2.begin(); while (nkIt != nkPointsSetPlayer2.end() && extraNKIt != nkPointsSetExtraPlayer2.end()) { assert (*nkIt == *extraNKIt); assert (m_player2.IsNucleationPoint(*nkIt) == extraPlayer2.IsNucleationPoint(*extraNKIt)); nkIt++; extraNKIt++; } assert (nkIt == nkPointsSetPlayer2.end() && extraNKIt == nkPointsSetExtraPlayer2.end()); } // for (pieceCoordIt = coordConfList.begin() } // for (int i = e_numberOfPieces - 1 ; i >= e_minimumPieceIndex ; i--) }
void countAndColor(BMP &TextImage, int &symbolCount, int &lineCount) { int width = TextImage.TellWidth(), height = TextImage.TellHeight(); // a vector of bool vectors std::vector< std::vector<bool> > visited(width, std::vector<bool>(height, false)); // first line is white by default bool thisLineWhite = true, lastLineWhite = true; Queue<Point> Q; int lowx, lowy, highx, highy, curx, cury, neighx, neighy, lineBottom = -1; for (int j = 0; j < height; ++j) { // if j is past lineBottom, it starts out as white line if (j > lineBottom) { thisLineWhite = true; } for (int i = 0; i < width; ++i) { // check if current pixel is black if ( !visited[i][j] && isBlack( TextImage(i,j) ) ) { // this line has a black pixel thisLineWhite = false; // we have found a new character ++symbolCount; // mark the starting values for border box and initialize newPoint lowx = highx = i; lowy = highy = j; // insert point of pixel into queue Q.push(Point(i, j)); // mark pixel as visited visited[i][j] = true; while (!Q.empty()) { // remove curPoint from queue Point curPoint = Q.pop(); // grab x and y coords curx = curPoint.x; cury = curPoint.y; // color the pixel green makeGreen(TextImage(curx,cury)); // update values of border box if (curx < lowx) lowx = curx; else if (curx > highx) highx = curx; if (cury < lowy) lowy = cury; else if (cury > highy) highy = cury; // check out the neighbors for (int x = -1; x < 2; ++x) { for (int y = -1; y < 2; ++y) { neighx = curx + x; neighy = cury + y; if ( validCoords(neighx, neighy, width, height) && !visited[neighx][neighy] && isBlack( TextImage(neighx, neighy) ) ) { // insert point into queue Q.push(Point(neighx, neighy)); // mark as visited visited[neighx][neighy] = true; } } } } // draw red box around character drawBox(TextImage, lowx - 1, lowy - 1, highx + 1, highy + 1); lineBottom = highy; } } if (lastLineWhite && !thisLineWhite) { ++lineCount; } lastLineWhite = thisLineWhite; } }