From 66eec913c97bbdd30e62ca9bcdb4d4a534b15d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Teuli=C3=A8re?= Date: Sun, 19 Feb 2012 12:26:44 +0100 Subject: [PATCH] Added several assertions --- game/board.cpp | 16 ++++++++++++++++ game/round.cpp | 15 +++++++++++++++ game/round.h | 2 +- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/game/board.cpp b/game/board.cpp index 33211f8..bb724f5 100644 --- a/game/board.cpp +++ b/game/board.cpp @@ -159,12 +159,18 @@ void Board::addRound(const Dictionary &iDic, const Round &iRound) { if (isVacant(row, col + i)) { + ASSERT(iRound.isPlayedFromRack(i), "Invalid round (1)"); t = iRound.getTile(i); m_tilesRow[row][col + i] = t; m_jokerRow[row][col + i] = iRound.isJoker(i); m_tilesCol[col + i][row] = t; m_jokerCol[col + i][row] = iRound.isJoker(i); } + else + { + ASSERT(!iRound.isPlayedFromRack(i), "Invalid round (2)"); + ASSERT(t == m_tilesRow[row][col + i].toUpper(), "Invalid round (3)"); + } } } else @@ -174,12 +180,18 @@ void Board::addRound(const Dictionary &iDic, const Round &iRound) { if (isVacant(row + i, col)) { + ASSERT(iRound.isPlayedFromRack(i), "Invalid round (1)"); t = iRound.getTile(i); m_tilesRow[row + i][col] = t; m_jokerRow[row + i][col] = iRound.isJoker(i); m_tilesCol[col][row + i] = t; m_jokerCol[col][row + i] = iRound.isJoker(i); } + else + { + ASSERT(!iRound.isPlayedFromRack(i), "Invalid round (2)"); + ASSERT(t == m_tilesRow[row + i][col].toUpper(), "Invalid round (3)"); + } } } buildCross(iDic); @@ -206,6 +218,8 @@ void Board::removeRound(const Dictionary &iDic, const Round &iRound) "Invalid round removal"); if (iRound.isPlayedFromRack(i)) { + ASSERT(iRound.isJoker(i) == m_jokerRow[row][col + i], + "Invalid round removal"); m_tilesRow[row][col + i] = Tile(); m_jokerRow[row][col + i] = false; m_crossRow[row][col + i].setAny(); @@ -223,6 +237,8 @@ void Board::removeRound(const Dictionary &iDic, const Round &iRound) "Invalid round removal"); if (iRound.isPlayedFromRack(i)) { + ASSERT(iRound.isJoker(i) == m_jokerRow[row + i][col], + "Invalid round removal"); m_tilesRow[row + i][col] = Tile(); m_jokerRow[row + i][col] = false; m_crossRow[row + i][col].setAny(); diff --git a/game/round.cpp b/game/round.cpp index 3d1e437..763f327 100644 --- a/game/round.cpp +++ b/game/round.cpp @@ -26,6 +26,7 @@ #include "tile.h" #include "round.h" #include "encoding.h" +#include "debug.h" INIT_LOGGER(game, Round); @@ -45,32 +46,44 @@ void Round::setWord(const vector &iTiles) } +void Round::setTile(unsigned int iIndex, const Tile &iTile) +{ + ASSERT(iIndex < m_word.size(), "Invalid index"); + m_word[iIndex] = iTile; +} + + void Round::setFromRack(unsigned int iIndex) { + ASSERT(iIndex < m_word.size(), "Invalid index"); m_rackOrigin[iIndex] = true; } void Round::setFromBoard(unsigned int iIndex) { + ASSERT(iIndex < m_word.size(), "Invalid index"); m_rackOrigin[iIndex] = false; } bool Round::isJoker(unsigned int iIndex) const { + ASSERT(iIndex < m_word.size(), "Invalid index"); return m_word[iIndex].isJoker(); } const Tile& Round::getTile(unsigned int iIndex) const { + ASSERT(iIndex < m_word.size(), "Invalid index"); return m_word[iIndex]; } bool Round::isPlayedFromRack(unsigned int iIndex) const { + ASSERT(iIndex < m_word.size(), "Invalid index"); return m_rackOrigin[iIndex]; } @@ -96,6 +109,8 @@ void Round::addRightFromRack(const Tile &iTile, bool iJoker) void Round::removeRight() { + ASSERT(!m_word.empty() && !m_rackOrigin.empty(), + "Trying to remove tiles that were never added"); m_word.pop_back(); m_rackOrigin.pop_back(); } diff --git a/game/round.h b/game/round.h index b4735f4..337cb4a 100644 --- a/game/round.h +++ b/game/round.h @@ -55,7 +55,7 @@ public: *************************/ void setPoints(int iPoints) { m_points = iPoints; } void setBonus(bool iBonus) { m_bonus = iBonus; } - void setTile(unsigned int iIndex, const Tile &iTile) { m_word[iIndex] = iTile; } + void setTile(unsigned int iIndex, const Tile &iTile); void setWord(const vector &iTiles); void setFromRack(unsigned int iIndex); void setFromBoard(unsigned int iIndex);