< Previous | Contents | Next >

The displayBoard() Function

This function displays the board passed to it. Because each element in the board is either a space, an ’X’, or an ’O’, the function can display each one. I use a few other characters on my keyboard to draw a decent-looking Tic-Tac-Toe board.

void displayBoard(const vector<char>& board)

{

cout << "\n\t" << board[0] << " | " << board[1] << " | " << board[2]; cout << "\n\t" << "———————";

cout << "\n\t" << board[3] << " | " << board[4] << " | " << board[5]; cout << "\n\t" << "———————";

Introducing the Tic-Tac-Toe Game 211



cout << "\n\t" << board[6] << " | " << board[7] << " | " << board[8]; cout << "\n\n";

}

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 changes. Since I plan to simply display the board and not change it in this function, this is perfect.