Tick-tack-toe (Jumping into C++) The exercise is from Alex Allain's book "Jumping into C++".
From Chapter 10 Practical Problem 4:


  
  Write a small tic-tac-toe program that allows two players to play
  tic-tac-toe competitively. Your program should check to see if either
  player has won, or if the board is filled completely (with the game
  ending in a tie). Bonus: can you make your program detect if the game
  cannot be won by either side before the entire grid is filled?
  


Here's my code. My main goal was a functioning tick-tack-toe game with little concern on the user experience (e.g.: the coordinates a player enters start from 0, or (0,0) coordinate is in the upper left corner no lower left as one would intuitively think). As for coding style I wanted the code to be as DRY as possible, with small, atomic, easy to understand functions doing most of the work.

#include &lt;iostream&gt;

using namespace std;

bool checkWin(int board[][3], int player) {
    // horizontal
    for (int i = 0; i &lt; 3; i++) {
        if (board[i][0] == player &amp;&amp; board[i][1] == player &amp;&amp; board[i][2] == player) return true;
    }

    // vertical
    for (int i = 0; i &lt; 3; i++) {
        if (board[0][i] == player &amp;&amp; board[1][i] == player &amp;&amp; board[2][i] == player) return true;
    }

    // diagonal
    if ((board[0][0] == player &amp;&amp; board[1][1] == player &amp;&amp; board[2][2] == player) ||
        (board[0][2] == player &amp;&amp; board[1][1] == player &amp;&amp; board[2][0] == player)) 
        return true;

    return false;
}

// TODO implement function if the game is a tie before the last move

bool fillPlace(int board[][3], int x, int y, int value) {
    if (x &gt; 2 || x &lt; 0) {
        cout &lt;&lt; "Invalid x coordinate. Try again.\n";
        return false;
    } else if (y &gt; 2 || y &lt; 0) {
        cout &lt;&lt; "Invalid y coordinate. Try again.\n";
        return false;
    } else if (board[x][y] != 0) {
        cout &lt;&lt; "Place is already filled. Try again.\n";
        return false;
    } else {
        board[x][y] = value;
        return true;
    }
}

void initBoard(int board[][3], int size) {
    for (int i = 0; i &lt; size; i++) {
        for (int j = 0; j &lt; 3; j++) {
            board[i][j] = 0;
        }
    }
}

void printBoard(int board[][3], int size) {
    cout &lt;&lt; "-----" &lt;&lt; endl;
    for (int i = 0; i &lt; size; i++) {
        for (int j = 0; j &lt; 3; j++) {
            cout &lt;&lt; board[i][j] &lt;&lt; " ";
        }
        cout &lt;&lt; endl;
    }
    cout &lt;&lt; "-----" &lt;&lt; endl;
}

bool isBoardFilled(int board[][3]) {
    bool isFilled = true;
    for (int i = 0; i &lt; 3; i++) {
        for (int j = 0; j &lt; 3; j++) {
            if (board[i][j] == 0) isFilled = false;
        }
    }
    return isFilled;
}

int main() {

    int board[3][3];
    initBoard(board, 3);
    printBoard(board, 3);

    int playerOnesTurn = true;
    while (true) {
        int x, y;

        if (playerOnesTurn) {            
            cout &lt;&lt; "Player 1's turn" &lt;&lt; endl;
            do {
                cout &lt;&lt; "Enter x coordinate: "; cin &gt;&gt; x;
                cout &lt;&lt; "Enter y coordinate: "; cin &gt;&gt; y;
            } while (!fillPlace(board, x, y, 1));
        } else {
            cout &lt;&lt; "Player 2's turn" &lt;&lt; endl;
            do {
                cout &lt;&lt; "Enter x coordinate: "; cin &gt;&gt; x;
                cout &lt;&lt; "Enter y coordinate: "; cin &gt;&gt; y;
            } while (!fillPlace(board, x, y, 2));
        }
        playerOnesTurn = !playerOnesTurn;

        if (isBoardFilled(board)) {
            cout &lt;&lt; "The game is a tie.\n";
            break;
        }

        printBoard(board, 3);
        if (checkWin(board, 1)) {
            cout &lt;&lt; "Player 1 has won\n";
            break;
        } else if (checkWin(board, 2)) {
            cout &lt;&lt; "Player 2 has won\n";
            break;
        }
    }

    return 0;
}
Avoid using namespace std

This can cause name collisions because it adds every name in the std namespace to the global namespace. For a small program like this one it's unlikely that you'll run into any problems (then again, maybe not) but it's best to get into the habit of using the std:: prefix on names in the std namespace.

Alternatively, you can introduce using declarations like using std::cout; to add specific names to the global namespace.

Avoid std::endl in favor of \n

std::endl flushes the stream, which can cause an unnecessary loss in performance. In printBoard() you use std::endl three times but you can easily replace some of those uses with \n.

Use a class for the Tic Tac Toe board

Instead of using a 3x3 array it's better to create a class to hold the state of the Tic Tac Toe board, with functions to set or get the value of a place on the board, determine whether or not the board is filled, etc. This allows you to hide implementation details which may change later on -- for example, if your compiler supports C++11 or will do so in the future you may want to change your internal representation of the board to use std::array. If you have a class for the Tic Tac Toe board the external interface would not change, only the code that implements the functionality.

Here's what that class might look like:

class Board {
    int board[3][3];
public:
    typedef int value_type;

    // More modern alternative to typedef:
    //using value_type = int;

    Board() {
        for (std::size_t i = 0; i &lt; 3; i++) {
            for (std::size_t j = 0; j &lt; 3; j++) {
                board[i][j] = 0;
            }
        }
    }

    // Gets the value of a board place
    int place(std::size_t x, std::size_t y) const {
        // Might want to check coordinates and throw an exception (e.g. std::out_of_range) if coordinate(s) are invalid
        return board[x][y];
    }

    // Sets the value of a board place
    // It would probably be better to throw an exception (e.g. std::out_of_range) instead of printing to std::cout
    bool place(std::size_t x, std::size_t y, int value) {
        if (x &gt; 2 || x &lt; 0) {
            cout &lt;&lt; "Invalid x coordinate. Try again.\n";
            return false;
        } else if (y &gt; 2 || y &lt; 0) {
            cout &lt;&lt; "Invalid y coordinate. Try again.\n";
            return false;
        } else if (board[x][y] != 0) {
            cout &lt;&lt; "Place is already filled. Try again.\n";
            return false;
        } else {
            board[x][y] = value;
            return true;
        }
    }

    bool is_filled() const {
        for (int i = 0; i &lt; 3; i++) {
            for (int j = 0; j &lt; 3; j++) {
                // Return immediately once we find a non-filled place
                if (board[i][j] == 0) return false;
            }
        }

        // All places were found to be filled
        return true;
    }
};


(Note the use of std::size_t for the coordinate type and the typedef for the int value type, which mimic practices from the standard library.)

You would no longer need initBoard() since the Board constructor performs the initialization, and some of your other functions would become member functions of the class. In some cases you can improve the performance of these functions -- e.g. you can keep a count of how many places are filled by incrementing the count each time a new place is filled by Board::place(), and then Board::is_filled() simply has to check if that count equals 9 rather than checking every place in the board (the performance gain is negligible for such a tiny board but the principle is important).

Overload operator&lt;&lt; for printing the board

printBoard() only prints to std::cout. You can overload operator&lt;&lt; in order to insert the board into any std::ostream. Using the above Board class the code would look like this:

std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, Board board) {
    os &lt;&lt; "-----\n";

    for (int i = 0; i &lt; 3; i++) {
        for (int j = 0; j &lt; 3; j++) {
            os &lt;&lt; board.place(i, j) &lt;&lt; " ";
        }

        os &lt;&lt; '\n';
    }

    os &lt;&lt; "-----" &lt;&lt; std::endl;

    return os;
}


(Note the replacement of some of the std::endl uses with '\n`.)

To print the board to std::cout you'd simply use std::cout &lt;&lt; board;, and you can use the same function to print the board to a different std::ostream such as a std::fstream (for writing to a file).

Make your code DRYer

In some cases you can make your code DRYer. For example, the code in the if and else sections is very similar here:

if (playerOnesTurn) {            
    cout &lt;&lt; "Player 1's turn" &lt;&lt; endl;
    do {
        cout &lt;&lt; "Enter x coordinate: "; cin &gt;&gt; x;
        cout &lt;&lt; "Enter y coordinate: "; cin &gt;&gt; y;
    } while (!fillPlace(board, x, y, 1));
 } else {
    cout &lt;&lt; "Player 2's turn" &lt;&lt; endl;
    do {
        cout &lt;&lt; "Enter x coordinate: "; cin &gt;&gt; x;
        cout &lt;&lt; "Enter y coordinate: "; cin &gt;&gt; y;
    } while (!fillPlace(board, x, y, 2));
 }


You can put that in a function like this:

// Capture Board by reference so we modify the original Board, not a copy
void player_move(Board&amp; board, int player) {
    cout &lt;&lt; "Player " &lt;&lt; player &lt;&lt; "'s turn" &lt;&lt; endl;

    std::size_t x, y;

    do {
        cout &lt;&lt; "Enter x coordinate: "; cin &gt;&gt; x;
        cout &lt;&lt; "Enter y coordinate: "; cin &gt;&gt; y;
    } while (!board.place(x, y, player));
}




Here's a demo program with some of the changes I suggested:

#include &lt;iostream&gt;
#include &lt;ostream&gt;

using namespace std;

class Board {
    int board[3][3];
public:
    typedef int value_type;

    // More modern alternative to typedef:
    //using value_type = int;

    Board() {
        for (std::size_t i = 0; i &lt; 3; i++) {
            for (std::size_t j = 0; j &lt; 3; j++) {
                board[i][j] = 0;
            }
        }
    }

    // Gets the value of a board place
    int place(std::size_t x, std::size_t y) const {
        // Might want to check coordinates and throw an exception (e.g. std::out_of_range) if coordinate(s) are invalid
        return board[x][y];
    }

    // Sets the value of a board place
    // It would probably be better to throw an exception (e.g. std::out_of_range) instead of printing to std::cout
    bool place(std::size_t x, std::size_t y, int value) {
        if (x &gt; 2 || x &lt; 0) {
            cout &lt;&lt; "Invalid x coordinate. Try again.\n";
            return false;
        } else if (y &gt; 2 || y &lt; 0) {
            cout &lt;&lt; "Invalid y coordinate. Try again.\n";
            return false;
        } else if (board[x][y] != 0) {
            cout &lt;&lt; "Place is already filled. Try again.\n";
            return false;
        } else {
            board[x][y] = value;
            return true;
        }
    }

    bool is_filled() const {
        for (int i = 0; i &lt; 3; i++) {
            for (int j = 0; j &lt; 3; j++) {
                // Return immediately once we find a non-filled place
                if (board[i][j] == 0) return false;
            }
        }

        // All places were found to be filled
        return true;
    }
};

bool checkWin(Board&amp; board, int player) {
    // horizontal
    for (int i = 0; i &lt; 3; i++) {
        if (board.place(i, 0) == player &amp;&amp; board.place(i, 1) == player &amp;&amp; board.place(i, 2) == player) return true;
    }

    // vertical
    for (int i = 0; i &lt; 3; i++) {
        if (board.place(0, i) == player &amp;&amp; board.place(1, i) == player &amp;&amp; board.place(2, i) == player) return true;
    }

    // diagonal
    if ((board.place(0, 0) == player &amp;&amp; board.place(1, 1) == player &amp;&amp; board.place(2, 2) == player) ||
        (board.place(0, 2) == player &amp;&amp; board.place(1, 1) == player &amp;&amp; board.place(2, 0) == player))
        return true;

    return false;
}

// TODO implement function if the game is a tie before the last move

std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, Board board) {
    os &lt;&lt; "-----\n";

    for (int i = 0; i &lt; 3; i++) {
        for (int j = 0; j &lt; 3; j++) {
            os &lt;&lt; board.place(i, j) &lt;&lt; " ";
        }

        os &lt;&lt; '\n';
    }

    os &lt;&lt; "-----" &lt;&lt; std::endl;

    return os;
}

// Capture Board by reference so we modify the original Board, not a copy
void player_move(Board&amp; board, int player) {
    cout &lt;&lt; "Player " &lt;&lt; player &lt;&lt; "'s turn" &lt;&lt; endl;

    std::size_t x, y;

    do {
        cout &lt;&lt; "Enter x coordinate: "; cin &gt;&gt; x;
        cout &lt;&lt; "Enter y coordinate: "; cin &gt;&gt; y;
    } while (!board.place(x, y, player));
}

int main() {
    Board board;
    std::cout &lt;&lt; board;

    // should be a bool, not an int
    // alternatively, just use an int which is 1 when it's player 1's turn and 2 when player 2's turn
    bool playerOnesTurn = true;

    while (true) {
        player_move(board, playerOnesTurn ? 1 : 2);

        playerOnesTurn = !playerOnesTurn;

        if (board.is_filled()) {
            cout &lt;&lt; "The game is a tie.\n";
            break;
        }

        std::cout &lt;&lt; board;

        if (checkWin(board, 1)) {
            cout &lt;&lt; "Player 1 has won\n";
            break;
        } else if (checkWin(board, 2)) {
            cout &lt;&lt; "Player 2 has won\n";
            break;
        }
    }

    return 0;
}


I left a few other minor tips in the comments and I didn't make all the changes I suggested (e.g. I did not remove using namespace std).