Исключения лучше использовать для исключительных случаев, хотя здесь они будут встречаться довольно часто (по крайней мере, 4 раза за внешний цикл ...)
Это может быть легко рефакторинг:
typedef std::vector< std::vector<int> > board_type;
namespace {
bool check(board_type const& board, int val, int w1, int h1, int w2, int h2)
{
if (w1 < 0 || w2 < 0) { return false; }
if (w1 >= board.size() || w2 >= board.size()) { return false; }
if (h1 < 0 || h2 < 0) { return false; }
if (h1 >= board[w1].size() || h2 >= board[w2].size()) { return false; }
return board[w1][h1] == val && board[w2][h2] == val;
}
} // anonymous namespace
bool connect3::checkIfPositionIsBaseCase(Position aPosition) const {
vector< vector< int > > thisP = aPosition.getBoard();
bool encounteredZero = false;
for( int w = 0; w < thisP.size(); w++ ) {
for( int h = 0; h < thisP.at(w).size(); h++ ){
int val = thisP[w][h];
if (val == 0) { encounteredZero = true; continue; }
// Check in all directions with a clock-wise rotation
if (check(thisP, val, w-1, h-1, w-2, h-2)) { return true; }
if (check(thisP, val, w , h-1, w , h-2)) { return true; }
if (check(thisP, val, w+1, h-1, w+2, h-2)) { return true; }
if (check(thisP, val, w+1, h , w+2, h )) { return true; }
if (check(thisP, val, w+1, h+1, w+2, h+2)) { return true; }
if (check(thisP, val, w , h+1, w , h+2)) { return true; }
if (check(thisP, val, w-1, h+1, w-2, h+2)) { return true; }
if (check(thisP, val, w-1, h , w-2, h )) { return true; }
}
}
return !encounteredZero;
}
И там не будет никаких исключений :) Мне также легче проверить, что проверки правильные и исчерпывающие ...