< Previous | Contents | Next >
This next function receives a board and the human’s piece. It returns the square number for where the player wants to move. The function asks the player for the square number to which he wants to move until the response is a legal move. Then the function returns the move.
int humanMove(const vector<char>& board, char human)
{
int move = askNumber("Where will you move?", (board.size() - 1)); while (!isLegal(move, board))
{
cout << "\nThat square is already occupied, foolish human.\n"; move = askNumber("Where will you move?", (board.size() - 1));
}
cout << "Fine.. .\n";
return move;
}
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.