Tic Tac Toe Board

Discussion in 'C++' started by tomakated, Dec 8, 2004.

  1. tomakated

    tomakated Guest

    How who I go about printing a Tic Tac Toe Board that is 3 x 3<?.
     
    tomakated, Dec 8, 2004
    #1
    1. Advertisements

  2. tomakated

    Thrillhouse Guest

    How is the board stored? If it's a two-dimensional array then it's as
    simple as nested for loops.
     
    Thrillhouse, Dec 8, 2004
    #2
    1. Advertisements

  3. tomakated

    Jon Bell Guest

    How about this?

    cout << " | | \n"
    << "---+---+---\n"
    << " | | \n"
    << "---+---+---\n"
    << " | | \n";
     
    Jon Bell, Dec 8, 2004
    #3
  4. To generalize this a bit, if Tic Tac Toe boards are represented as instances of
    vector< vector<char> >, you can do something like this:

    #include <iostream>
    #include <vector>
    #include <boost/assign.hpp>
    #include <boost/iostreams/format_lite.hpp>

    int main()
    {
    using namespace std;
    using namespace boost::io;
    using namespace boost::assign;

    typedef vector<char> row;
    typedef vector<row> board;

    board b =
    list_of( list_of('X')(' ')('O') )
    ( list_of('O')('O')('X') )
    ( list_of('X')(' ')('X') );

    std::cout << punctuate<row>(" ", " | ", " ")
    << punctuate< vector<_> >("", "\n---+---+---\n", "")
    << b;

    }

    Output (view with fixed-width font):

    X | | O
    ---+---+---
    O | O | X
    ---+---+---
    X | | X

    Here I'm using the new Boost Assign library to initialize the board, and my
    Format Lite library to format it. (See
    http://home.comcast.net/~jturkanis/format_lite/)

    Of course, this might be using a sledge hammer to kill a mosquito -- or, to use
    a phrase I am trying to popularize -- using a daisy-cutter to cut a daisy.

    Jonathan
     
    Jonathan Turkanis, Dec 8, 2004
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.