< Previous | Contents | Next >

The isLegal() Function

This function receives a board and a move. It returns true if the move is a legal one on the board or false if the move is not legal. A legal move is represented by the number of an empty square.

Introducing the Tic-Tac-Toe Game 213


inline bool isLegal(int move, const vector<char>& board)

{

return (board[move] == EMPTY);

}

Again, notice that the vector that represents the board is passed through a constant reference. This means that the vector is passed efficiently; it is not copied. It also means that the vector is safeguarded against any change.

You can see that I inlined isLegal(). Modern compilers are quite good at optimizing on their own; however, since this function is just one line, its a good candidate for inlining.